IssueService.php 35 KB

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