IssueService.php 35 KB

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