IssueService.php 33 KB

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