IssueService.php 34 KB

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