IssueService.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Cao;
  4. use App\Models\CaoHistory;
  5. use App\Models\PcIssue;
  6. use App\Models\Prediction;
  7. use App\Services\BaseService;
  8. use App\Models\Issue;
  9. use App\Models\Config;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Collection;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Log;
  14. use App\Services\GameplayRuleService;
  15. use App\Constants\GameplayRuleEnum;
  16. use App\Http\Controllers\admin\Lottery;
  17. use App\Services\KeyboardService;
  18. use App\Services\LotteryImageService;
  19. use Telegram\Bot\FileUpload\InputFile;
  20. use App\Jobs\SendTelegramMessageJob;
  21. use App\Jobs\SendTelegramGroupMessageJob;
  22. /**
  23. * 投注
  24. */
  25. class IssueService extends BaseService
  26. {
  27. const COUNTDOWN_TO_CLOSING_THE_MARKET = 60;//提前xx秒封盘
  28. /**
  29. * @description: 模型
  30. * @return {string}
  31. */
  32. public static function model(): string
  33. {
  34. return Issue::class;
  35. }
  36. /**
  37. * @description: 枚举
  38. * @return {*}
  39. */
  40. public static function enum(): string
  41. {
  42. return '';
  43. }
  44. /**
  45. * @description: 获取查询条件
  46. * @param {array} $search 查询内容
  47. * @return {array}
  48. */
  49. public static function getWhere(array $search = []): array
  50. {
  51. $where = [];
  52. if (isset($search['issue_no']) && !empty($search['issue_no'])) {
  53. $where[] = ['issue_no', '=', $search['issue_no']];
  54. }
  55. if (isset($search['id']) && !empty($search['id'])) {
  56. $where[] = ['id', '=', $search['id']];
  57. }
  58. if (isset($search['status']) && !empty($search['status'])) {
  59. $where[] = ['status', '=', $search['status']];
  60. }
  61. if (isset($search['abnormal']) && !empty($search['abnormal'])) {
  62. $where[] = ['end_time', '<', date('Y-m-d H:i:s', time() - 1800)];
  63. $where[] = ['status', '!=', self::model()::STATUS_DRAW];
  64. }
  65. return $where;
  66. }
  67. /**
  68. * @description: 查询单条数据
  69. * @param array $search
  70. * @return \App\Models\Coin|null
  71. */
  72. public static function findOne(array $search): ?Issue
  73. {
  74. return self::model()::where(self::getWhere($search))->first();
  75. }
  76. /**
  77. * @description: 查询所有数据
  78. * @param array $search
  79. * @return \Illuminate\Database\Eloquent\Collection
  80. */
  81. public static function findAll(array $search = [])
  82. {
  83. return self::model()::where(self::getWhere($search))->get();
  84. }
  85. /**
  86. * @description: 分页查询
  87. * @param array $search
  88. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  89. */
  90. public static function paginate(array $search = [])
  91. {
  92. $limit = isset($search['limit']) ? $search['limit'] : 15;
  93. $paginator = self::model()::where(self::getWhere($search))->orderBy('issue_no', 'desc')->paginate($limit);
  94. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  95. }
  96. /**
  97. * @description:
  98. * @param {*} $params
  99. * @return {*}
  100. */
  101. public static function submit($params = [])
  102. {
  103. $result = false;
  104. $msg['code'] = self::NOT;
  105. $msg['msg'] = '';
  106. // 2. 判断是否是更新
  107. if (!empty($params['id'])) {
  108. // 更新
  109. $info = self::findOne(['id' => $params['id']]);
  110. if (!$info) {
  111. $msg['msg'] = '期号不存在!';
  112. } else {
  113. $result = $info->update($params);
  114. $id = $params['id'];
  115. }
  116. } else {
  117. // 创建
  118. $result = $info = self::model()::create($params);
  119. $id = $result->id;
  120. }
  121. if ($result) {
  122. $msg['code'] = self::YES;
  123. $msg['msg'] = '设置成功';
  124. $msg['key'] = $id;
  125. } else {
  126. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  127. }
  128. return $msg;
  129. }
  130. /**
  131. * @description: 开始下注
  132. * @param {*} $id
  133. * @return {*}
  134. */
  135. public static function betting($id)
  136. {
  137. $info = self::findOne(['id' => $id]);
  138. if (!$info) {
  139. return ['code' => self::NOT, 'msg' => '期号不存在'];
  140. }
  141. if (!in_array($info->status, [self::model()::STATUS_DRAFT, self::model()::STATUS_BETTING])) {
  142. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  143. }
  144. $info->status = self::model()::STATUS_BETTING;
  145. $info->save();
  146. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  147. $wanFaGuiZeTime = Cache::get("玩法规则推送时间", 0);
  148. $now = time();
  149. $wanFaGuiZeTime = intval($wanFaGuiZeTime);
  150. if ($now - $wanFaGuiZeTime > (60 * 15)) {
  151. $replyInfo = KeyboardService::findOne(['button' => '玩法规则']);
  152. if ($replyInfo) {
  153. $text = $replyInfo->reply;
  154. $buttons = json_decode($replyInfo->buttons, true);
  155. $image = $replyInfo->image;
  156. if ($image) {
  157. $image = url($image);
  158. }
  159. if (empty($buttons)) {
  160. $buttons = self::getOperateButton();
  161. }
  162. Cache::put('玩法规则推送时间', time());
  163. if ($pc28Switch == 0) self::asyncBettingGroupNotice($text, $buttons, $image);
  164. }
  165. }
  166. $replyInfo = KeyboardService::findOne(['button' => '开始下注']);
  167. if ($replyInfo) {
  168. $text = $replyInfo->reply;
  169. $buttons = json_decode($replyInfo->buttons, true);
  170. $image = $replyInfo->image;
  171. if ($image) {
  172. $image = url($image);
  173. }
  174. if ($pc28Switch == 0) self::asyncBettingGroupNotice($text, $buttons, $image);
  175. }
  176. return ['code' => self::YES, 'msg' => '开始下注'];
  177. }
  178. /**
  179. * @description: 封盘
  180. * @param {*} $id
  181. * @return {*}
  182. */
  183. public static function closeBetting($id)
  184. {
  185. $info = self::findOne(['id' => $id]);
  186. if (!$info) {
  187. return ['code' => self::NOT, 'msg' => '期号不存在'];
  188. }
  189. if ($info->status != self::model()::STATUS_BETTING) {
  190. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  191. }
  192. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  193. $info->status = self::model()::STATUS_CLOSE;
  194. $info->save();
  195. if ($pc28Switch == 0) {
  196. $replyInfo = KeyboardService::findOne(['button' => '停止下注']);
  197. if ($replyInfo) {
  198. $text = $replyInfo->reply;
  199. $buttons = json_decode($replyInfo->buttons, true);
  200. $image = $replyInfo->image;
  201. if ($image) {
  202. $image = url($image);
  203. }
  204. //停止下注的信息不发了
  205. // self::asyncBettingGroupNotice($text, $buttons, $image);
  206. }
  207. // 投注情况通知 xxxx期投注统计
  208. BetService::statNotice($info->issue_no);
  209. $replyInfo = KeyboardService::findOne(['button' => '封盘开奖']);
  210. if ($replyInfo) {
  211. $text = $replyInfo->reply;
  212. $buttons = json_decode($replyInfo->buttons, true);
  213. $image = $replyInfo->image;
  214. if ($image) {
  215. $image = url($image);
  216. }
  217. // self::bettingGroupNotice($text, $buttons, $image);
  218. self::asyncBettingGroupNotice($text, $buttons, $image);
  219. }
  220. }
  221. return ['code' => self::YES, 'msg' => '封盘成功'];
  222. }
  223. /**
  224. * @description: 开奖失败
  225. * @param {*} $id
  226. * @return {*}
  227. */
  228. public static function lotteryDrawFail($id)
  229. {
  230. $result = false;
  231. $msg['code'] = self::NOT;
  232. $msg['msg'] = '';
  233. DB::beginTransaction();
  234. try {
  235. // 更新
  236. $info = self::findOne(['id' => $id]);
  237. if (!$info) {
  238. $msg['msg'] = '期号不存在!';
  239. } else {
  240. $params['status'] = self::model()::STATUS_FAIL;
  241. $result = $info->update($params);
  242. BetService::betFail($info->issue_no);
  243. }
  244. DB::commit();
  245. return ['code' => self::YES, 'msg' => '投注已退回'];
  246. } catch (\Exception $e) {
  247. DB::rollBack();
  248. return ['code' => self::NOT, 'msg' => '投注退回失败'];
  249. }
  250. if ($result) {
  251. $msg['code'] = self::YES;
  252. $msg['msg'] = '设置成功';
  253. } else {
  254. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  255. }
  256. return $msg;
  257. }
  258. /**
  259. * @description: 开奖
  260. * @param {*} $id
  261. * @param {*} $winning_numbers 开奖号码
  262. * @param {*} $combo 开奖组合
  263. * @param {*} $recordImage 开奖图片
  264. * @return {*}
  265. */
  266. public static function lotteryDraw($id, $winning_numbers, $combo, $recordImage)
  267. {
  268. $info = self::findOne(['id' => $id]);
  269. if (!$info) {
  270. return ['code' => self::NOT, 'msg' => '期号不存在'];
  271. }
  272. if ($info->status == self::model()::STATUS_DRAW) {
  273. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  274. }
  275. $winArr = array_map('intval', explode(',', $winning_numbers));
  276. // 计算中奖
  277. $awards = self::award(explode(',', $winning_numbers));
  278. DB::beginTransaction();
  279. try {
  280. $info->status = self::model()::STATUS_DRAW;
  281. $info->winning_numbers = $winning_numbers;
  282. $info->combo = $combo;
  283. $size = in_array("大", $awards);
  284. $size = $size ? "大" : "小";
  285. $oddOrEven = in_array("双", $awards);
  286. $oddOrEven = $oddOrEven ? "双" : "单";
  287. Prediction::result($info->issue_no, $size, $oddOrEven, $info->winning_numbers);
  288. Cao::updateData($awards);
  289. CaoHistory::updateData($awards);
  290. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  291. $replyInfo = KeyboardService::findOne(['button' => '本期开奖']);
  292. if ($replyInfo) {
  293. $text = $replyInfo->reply;
  294. $text .= "\n";
  295. $text .= $info->issue_no . ": " . implode('+', explode(',', $winning_numbers)) . "=" . array_sum($winArr) . " " . $combo;
  296. $buttons = json_decode($replyInfo->buttons, true);
  297. $image = $replyInfo->image;
  298. if ($image) {
  299. $image = url($image);
  300. }
  301. if (empty($buttons)) {
  302. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  303. $buttons[] = [['text' => lang('✅唯一财务'), 'callback_data' => "", 'url' => "https://t.me/{$serviceAccount}"]];
  304. }
  305. // self::bettingGroupNotice($text, $buttons, $image, true);
  306. if ($pc28Switch == 0) SendTelegramGroupMessageJob::dispatch($text, $buttons, $image, true);
  307. }
  308. $recordImage = self::lotteryImage($info->issue_no);
  309. if ($recordImage) {
  310. // self::bettingGroupNotice('', [], url($recordImage));
  311. if ($pc28Switch == 0) SendTelegramGroupMessageJob::dispatch('', [], url($recordImage), false);
  312. }
  313. $info->image = $recordImage;
  314. $info->save();
  315. BetService::betSettled($info->issue_no, $awards);
  316. DB::commit();
  317. return ['code' => self::YES, 'msg' => '开奖成功'];
  318. } catch (\Exception $e) {
  319. DB::rollBack();
  320. $message = "开奖失败:\n{$info->issue_no}\n";
  321. $message .= $e->getFile() . ':' . $e->getLine();
  322. $message .= "\n";
  323. $message .= $e->getMessage() . $winning_numbers;
  324. Log::error($message);
  325. return ['code' => self::NOT, 'msg' => '开奖失败', 'error' => $e->getMessage()];
  326. }
  327. }
  328. // 虚拟开奖
  329. public static function fakeLotteryDraw($issue_no, $awards)
  330. {
  331. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  332. $text = "";
  333. foreach ($fake_bet_list as $k => $v) {
  334. $lastStr = self::getLastChar($v['first_name'], 1);
  335. if (in_array($v['keywords'], $awards)) {
  336. $amount = (float)$v['amount'];
  337. $odds = (float)$v['odds'];
  338. $profit = $amount * $odds;
  339. if ($profit > 880000) {
  340. $profit = 880000; // 单注最高奖金880000
  341. }
  342. $item['profit'] = $profit;
  343. $yl = $profit - $amount;
  344. if ($k + 1 <= 30) {
  345. $text .= "私聊下注 【******" . $lastStr . "】 {$yl}\n";
  346. }
  347. } else {
  348. if ($k + 1 <= 30) {
  349. $text .= "私聊下注 【******" . $lastStr . "】 -{$v['amount']}\n";
  350. }
  351. }
  352. }
  353. return $text;
  354. }
  355. /**
  356. * @description: 获取中奖的奖项
  357. * @param {*} $winning_numbers
  358. * @return {*}
  359. */
  360. public static function award($winning_numbers)
  361. {
  362. $result = [];
  363. // 组合
  364. $sum = array_sum($winning_numbers);
  365. $section = self::getSection($sum); // 总和段位
  366. $result[] = $section;
  367. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  368. $result[] = $sumOddEven;
  369. $sumSize = self::calculateSumSize($sum); // 总和大小
  370. $result[] = $sumSize;
  371. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  372. if ($sumExtremeSize) {
  373. $result[] = $sumExtremeSize;
  374. }
  375. $sumCao = $sum . '操'; // 总和数字
  376. $result[] = $sumCao;
  377. $sumCombo = $sumSize . $sumOddEven; // 总和大小单双组合
  378. $result[] = $sumCombo;
  379. $sumBaoZi = self::isBaoZi($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 豹子
  380. if ($sumBaoZi) {
  381. $result[] = $sumBaoZi;
  382. }
  383. $sumPair = self::isPair($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 对子
  384. if ($sumPair) {
  385. $result[] = $sumPair;
  386. }
  387. $sumStraight = self::isStraight($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 顺子
  388. if ($sumStraight) {
  389. $result[] = $sumStraight;
  390. }
  391. $tail = self::getLastDigit($sum); // 总和尾数
  392. if (!in_array($tail, [0, 9])) {
  393. $result[] = $tail . '尾'; // 尾数
  394. $tailOddEven = self::calculateOddEven($tail); // 尾数单双
  395. $result[] = '尾' . $tailOddEven;
  396. $tailSize = self::calculateOneSize($tail); // 尾数大小
  397. $result[] = '尾' . $tailSize;
  398. $tailCombo = '尾' . $tailSize . $tailOddEven; // 尾数大小单双组合
  399. $result[] = $tailCombo;
  400. } else {
  401. $result[] = $tail . '尾'; // 尾数
  402. }
  403. $numA = $winning_numbers[0]; // A球
  404. $result[] = $numA . 'A';
  405. $numAOddEven = self::calculateOddEven($numA); // A球单双
  406. $result[] = 'A' . $numAOddEven;
  407. $numASize = self::calculateOneSize($numA); // A球大小
  408. $result[] = 'A' . $numASize;
  409. $result[] = 'A' . $numASize . $numAOddEven; // A球大小单双组合
  410. $numB = $winning_numbers[1]; // B球
  411. $result[] = $numB . 'B';
  412. $numBOddEven = self::calculateOddEven($numB); // B球
  413. $result[] = 'B' . $numBOddEven;
  414. $numBSize = self::calculateOneSize($numB); // B球大小
  415. $result[] = 'B' . $numBSize;
  416. $result[] = 'B' . $numBSize . $numBOddEven; // B球大小单双组合
  417. $numC = $winning_numbers[2];
  418. $result[] = $numC . 'C';
  419. $numCOddEven = self::calculateOddEven($numC); // C球单双
  420. $result[] = 'C' . $numCOddEven;
  421. $numCSize = self::calculateOneSize($numC); // C球大小
  422. $result[] = 'C' . $numCSize;
  423. $result[] = 'C' . $numCSize . $numCOddEven; // C球大小单双组合
  424. return $result;
  425. }
  426. /**
  427. * @description: 算单双
  428. * @param {*} $number
  429. * @return {*}
  430. */
  431. public static function calculateOddEven($number)
  432. {
  433. if ($number & 1) {
  434. return GameplayRuleEnum::SINGLE;
  435. } else {
  436. return GameplayRuleEnum::DOUBLE;
  437. }
  438. }
  439. /**
  440. * @description: 总和大小
  441. * @param {*} $number
  442. * @return {*}
  443. */
  444. public static function calculateSumSize($number)
  445. {
  446. if ($number >= GameplayRuleEnum::SUM_BIG) {
  447. return GameplayRuleEnum::BIG;
  448. }
  449. if ($number <= GameplayRuleEnum::SUM_SMALL) {
  450. return GameplayRuleEnum::SMALL;
  451. }
  452. }
  453. /**
  454. * @description: 总和极值
  455. * @param {*} $number
  456. * @return {*}
  457. */
  458. public static function calculateSumExtremeSize($number)
  459. {
  460. $result = '';
  461. if ($number >= GameplayRuleEnum::SUM_EXTREME_BIG) {
  462. $result = GameplayRuleEnum::EXTREME_BIG;
  463. }
  464. if ($number <= GameplayRuleEnum::SUM_EXTREME_SMALL) {
  465. $result = GameplayRuleEnum::EXTREME_SMALL;
  466. }
  467. return $result;
  468. }
  469. /**
  470. * @description: 豹子
  471. * @param {int} $a
  472. * @param {int} $b
  473. * @param {int} $c
  474. * @return {*}
  475. */
  476. public static function isBaoZi(int $a, int $b, int $c)
  477. {
  478. $result = '';
  479. if ($a === $b && $b === $c) {
  480. $result = GameplayRuleEnum::BAO_ZI;
  481. }
  482. return $result;
  483. }
  484. /**
  485. * @description: 对子
  486. * @param {int} $a
  487. * @param {int} $b
  488. * @param {int} $c
  489. * @return {*}
  490. */
  491. public static function isPair($a, $b, $c)
  492. {
  493. $result = '';
  494. // 确保输入都是个位数
  495. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  496. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  497. return ''; // 或者抛出异常
  498. }
  499. if (($a == $b && $a != $c) ||
  500. ($a == $c && $a != $b) ||
  501. ($b == $c && $b != $a)) {
  502. $result = GameplayRuleEnum::PAIRS;
  503. }
  504. // 判断是否为对子情况
  505. return $result;
  506. }
  507. /**
  508. * @description: 顺子
  509. * @param {int} $a
  510. * @param {int} $b
  511. * @param {int} $c
  512. * @return {*}
  513. */
  514. public static function isStraight($a, $b, $c)
  515. {
  516. $result = '';
  517. // 确保输入都是个位数(0-9)
  518. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  519. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  520. return '';
  521. }
  522. // 去重(顺子必须三个不同数字)
  523. if ($a == $b || $a == $c || $b == $c) {
  524. return '';
  525. }
  526. // 检查是否是完全升序或完全降序的连续数字
  527. $numbers = [$a, $b, $c];
  528. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  529. list($x, $y, $z) = $numbers;
  530. // 情况1:升序连续(1,2,3)
  531. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  532. // 情况2:降序连续(3,2,1)
  533. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  534. if ($isAscending || $isDescending) {
  535. $result = GameplayRuleEnum::STRAIGHT;
  536. }
  537. return $result;
  538. }
  539. /**
  540. * 获取数字的尾数
  541. * @param int $number 输入数字
  542. * @return int 尾数
  543. */
  544. public static function getLastDigit($number)
  545. {
  546. // 确保输入是整数
  547. $number = (int)$number;
  548. // 取绝对值,处理负数情况
  549. $number = abs($number);
  550. // 取模10得到尾数
  551. return $number % 10;
  552. }
  553. /**
  554. * @description: 尾大小
  555. * @param {*} $number
  556. * @return {*}
  557. */
  558. public static function calculateOneSize($number)
  559. {
  560. if ($number >= GameplayRuleEnum::ONE_BIG) {
  561. return GameplayRuleEnum::BIG;
  562. }
  563. if ($number <= GameplayRuleEnum::ONE_SMALL) {
  564. return GameplayRuleEnum::SMALL;
  565. }
  566. }
  567. /**
  568. * @description: 获取段位
  569. * @param {*} $number
  570. * @return {*}
  571. */
  572. public static function getSection($number)
  573. {
  574. $result = '';
  575. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  576. $result = GameplayRuleEnum::ONE;
  577. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  578. $result = GameplayRuleEnum::TWO;
  579. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  580. $result = GameplayRuleEnum::THREE;
  581. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  582. $result = GameplayRuleEnum::FOUR;
  583. }
  584. return $result; // 不在任何段中
  585. }
  586. /**
  587. * @description: 近期开奖记录
  588. * @return {*}
  589. */
  590. public static function currentLotteryResults($memberId)
  591. {
  592. // $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->take(16)->get();
  593. // $text = "📅 近期开奖记录\n";
  594. // $text .= "====================\n";
  595. // if($result){
  596. // foreach($result as $k => $v){
  597. // $winArr = explode(',',$v->winning_numbers);
  598. // // 组合
  599. // $sum = array_sum($winArr);
  600. // $combo = [];
  601. // $sumOddEven = self::calculateOddEven($sum); // 总和单双
  602. // $sumSize = self::calculateSumSize($sum); // 总和大小
  603. // $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  604. // if(empty($sumExtremeSize)){
  605. // $sumExtremeSize = "-";
  606. // }
  607. // $tail = self::getLastDigit($sum); // 总和尾数
  608. // if($tail == 0){
  609. // $tail = '-'; // 尾数
  610. // }else{
  611. // $tail = '尾'.$tail; // 尾数
  612. // }
  613. // $text .= "回合:{$v->issue_no}期 \n";
  614. // $text .= "结果:".implode('+',explode(',',$v->winning_numbers))."=".array_sum(explode(',',$v->winning_numbers))." \n";
  615. // $text .= "组合:{$sumSize} {$sumOddEven} \n";
  616. // $text .= "极值:{$sumExtremeSize} \n";
  617. // $text .= "尾数:{$tail} \n";
  618. // $text .= "---------------------------\n";
  619. // }
  620. // self::telegram()->sendMessage([
  621. // 'chat_id' => $memberId,
  622. // 'text' => $text,
  623. // ]);
  624. // }else{
  625. // self::telegram()->sendMessage([
  626. // 'chat_id' => $memberId,
  627. // 'text' => "暂无开奖记录",
  628. // ]);
  629. // }
  630. $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id', 'desc')->first();
  631. if ($result) {
  632. if ($result->image) {
  633. // self::telegram()->sendPhoto([
  634. // 'chat_id' => $memberId,
  635. // 'photo' => InputFile::create(url($result->image)),
  636. // ]);
  637. return [
  638. 'chat_id' => $memberId,
  639. 'photo' => InputFile::create(url($result->image)),
  640. ];
  641. } else {
  642. // if($result->combo){
  643. // self::telegram()->sendMessage([
  644. // 'chat_id' => $memberId,
  645. // 'text' => "",
  646. // ]);
  647. // }else{
  648. // self::telegram()->sendMessage([
  649. // 'chat_id' => $memberId,
  650. // 'text' => lang("暂无开奖记录"),
  651. // ]);
  652. // }
  653. return
  654. [
  655. 'chat_id' => $memberId,
  656. 'text' => lang("暂无开奖记录"),
  657. ];
  658. }
  659. }
  660. }
  661. public static function getCombo($winArr)
  662. {
  663. // 组合
  664. $sum = array_sum($winArr);
  665. $combo = [];
  666. $sumSize = self::calculateSumSize($sum); // 总和大小
  667. $combo[] = $sumSize;
  668. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  669. $combo[] = $sumOddEven;
  670. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  671. if ($sumExtremeSize) {
  672. $combo[] = $sumExtremeSize;
  673. }
  674. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  675. if ($sumBaoZi) {
  676. $combo[] = $sumBaoZi;
  677. }
  678. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  679. if ($sumPair) {
  680. $combo[] = $sumPair;
  681. }
  682. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  683. if ($sumStraight) {
  684. $combo[] = $sumStraight;
  685. }
  686. $tail = self::getLastDigit($sum); // 总和尾数
  687. if ($tail == 0 || $tail == 9) {
  688. } else {
  689. $combo[] = '尾' . $tail; // 尾数
  690. }
  691. return implode(' ', $combo);
  692. }
  693. // 获取最新的开奖数据
  694. public static function getLatestIssue()
  695. {
  696. $url = "https://ydpc28.co/api/pc28/list";
  697. $result = file_get_contents($url);
  698. $result = json_decode($result, true);
  699. if ($result['errorCode'] != 0) {
  700. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  701. }
  702. $nextDrawInfo = $result['data']['nextDrawInfo'];
  703. $startTime = $nextDrawInfo['currentBJTime'];
  704. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  705. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  706. // }else{
  707. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  708. // }
  709. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  710. $new = true;
  711. $list = $result['data']['list'];
  712. $listKey = [];
  713. foreach ($list as $k => $v) {
  714. $listKey[$v['lotNumber']] = $v;
  715. }
  716. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  717. foreach ($oldList as $k => $v) {
  718. if (isset($listKey[$v->issue_no])) {
  719. $issue = $listKey[$v->issue_no];
  720. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  721. $winArr = array_map('intval', explode(',', $winning_numbers));
  722. $combo = static::getCombo($winArr);
  723. $key = 'lottery_numbers_' . $v->issue_no;
  724. if (Cache::add($key, $winning_numbers, 100)) {
  725. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  726. $new = false;
  727. }
  728. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  729. //更新游戏开关的切换
  730. if ($pc28Switch == 0) Config::setPc28Switch();
  731. }
  732. }
  733. // sleep(5); // 等待开奖完成
  734. if ($new) {
  735. $latestIssue = $list[0]; // 最后开奖
  736. $new_issue_no = $latestIssue['lotNumber'] + 1; // 新期号
  737. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  738. // 不存在
  739. if (!$newInfo) {
  740. $res = self::submit([
  741. 'issue_no' => $new_issue_no,
  742. 'status' => self::model()::STATUS_DRAFT,
  743. 'start_time' => $startTime,
  744. 'end_time' => $endTime,
  745. ]);
  746. Prediction::prediction($new_issue_no);
  747. $id = $res['key'] ?? 0;
  748. if ($id) {
  749. self::betting($id); // 开始下注
  750. }
  751. Cache::set('new_issue_no', $new_issue_no, 10); // 缓存
  752. }
  753. }
  754. return $result['list'];
  755. }
  756. // 获取最新的开奖数据
  757. public static function getLatestIssue2()
  758. {
  759. $url = "https://ydpc28.co/api/pc28/list";
  760. $result = file_get_contents($url);
  761. $result = json_decode($result, true);
  762. if ($result['errorCode'] != 0) {
  763. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  764. }
  765. $nextDrawInfo = $result['data']['nextDrawInfo'];
  766. $startTime = $nextDrawInfo['currentBJTime'];
  767. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  768. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  769. // }else{
  770. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  771. // }
  772. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  773. $new = true;
  774. $list = $result['data']['list'];
  775. $listKey = [];
  776. foreach ($list as $k => $v) {
  777. $listKey[$v['lotNumber']] = $v;
  778. }
  779. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  780. foreach ($oldList as $k => $v) {
  781. if (isset($listKey[$v->issue_no])) {
  782. $issue = $listKey[$v->issue_no];
  783. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  784. $winArr = array_map('intval', explode(',', $winning_numbers));
  785. // 组合
  786. $sum = array_sum($winArr);
  787. $combo = [];
  788. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  789. $combo[] = $sumOddEven;
  790. $sumSize = self::calculateSumSize($sum); // 总和大小
  791. $combo[] = $sumSize;
  792. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  793. if ($sumExtremeSize) {
  794. $combo[] = $sumExtremeSize;
  795. }
  796. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  797. if ($sumBaoZi) {
  798. $combo[] = $sumBaoZi;
  799. }
  800. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  801. if ($sumPair) {
  802. $combo[] = $sumPair;
  803. }
  804. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  805. if ($sumStraight) {
  806. $combo[] = $sumStraight;
  807. }
  808. $tail = self::getLastDigit($sum); // 总和尾数
  809. if ($tail == 0 || $tail == 9) {
  810. } else {
  811. $combo[] = '尾' . $tail; // 尾数
  812. }
  813. $combo = implode(' ', $combo);
  814. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  815. }
  816. }
  817. return $result;
  818. }
  819. // 封盘倒数
  820. public static function syncCountdownIssue()
  821. {
  822. $info = self::model()::where('status', self::model()::STATUS_BETTING)->orderBy('end_time', 'asc')->first();
  823. if ($info) {
  824. $now_date = date('Y-m-d H:i:s', time() + IssueService::COUNTDOWN_TO_CLOSING_THE_MARKET);
  825. if ($info['end_time'] < $now_date) {
  826. $replyInfo = KeyboardService::findOne(['button' => '封盘倒数']);
  827. if ($replyInfo) {
  828. $text = $replyInfo->reply;
  829. $buttons = json_decode($replyInfo->buttons, true);
  830. $image = $replyInfo->image;
  831. if ($image) {
  832. $image = url($image);
  833. }
  834. if (Cache::has('issue_countdown_' . $info->issue_no)) {
  835. } else {
  836. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  837. if ($pc28Switch == 0) {
  838. self::asyncBettingGroupNotice($text, $buttons, $image);
  839. Cache::put('issue_countdown_' . $info->issue_no, true, 60); // 缓存50秒,防止多次发送
  840. }
  841. }
  842. }
  843. }
  844. }
  845. }
  846. // 停止下注
  847. public static function syncCloseIssue()
  848. {
  849. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  850. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  851. foreach ($list as $k => $v) {
  852. if ($v['end_time'] < $now_date) {
  853. self::closeBetting($v->id);
  854. }
  855. }
  856. }
  857. // 生成开奖图片
  858. public static function lotteryImage($issue_no)
  859. {
  860. $list = self::model()::where('issue_no', '<=', $issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no', 'desc')->take(20)->get();
  861. $records = $list->toArray();
  862. foreach ($records as $k => $v) {
  863. $winning_numbers = explode(',', $v['winning_numbers']);
  864. $v['winning_numbers'] = $winning_numbers;
  865. // 组合
  866. $sum = array_sum($winning_numbers);
  867. $v['sum'] = $sum;
  868. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  869. $sumSize = self::calculateSumSize($sum); // 总和大小
  870. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  871. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  872. if (!$sumExtremeSize) {
  873. $sumExtremeSize = '-';
  874. }
  875. $v['extreme'] = $sumExtremeSize;
  876. $tail = self::getLastDigit($sum); // 总和尾数
  877. if ($tail === 0 || $tail === 9) {
  878. $tailStr = '-';
  879. } else {
  880. $tailStr = '尾' . $tail;
  881. }
  882. $v['tail'] = $tailStr;
  883. $records[$k] = $v;
  884. }
  885. $service = new LotteryImageService();
  886. $url = $service->generate($records);
  887. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  888. return $url;
  889. }
  890. // 发送开奖图片
  891. public static function sendLotteryImage($chatId, $issueNo)
  892. {
  893. $recordImage = self::lotteryImage($issueNo);
  894. self::sendMessage($chatId, '', [], url($recordImage));
  895. // dispatch(new SendTelegramMessageJob('', [], url($recordImage)));
  896. }
  897. }