IssueService.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. DB::commit();
  307. return ['code' => self::YES, 'msg' => '开奖成功'];
  308. } catch (\Exception $e) {
  309. DB::rollBack();
  310. Log::error('开奖失败: ' . $e->getMessage() . $winning_numbers);
  311. return ['code' => self::NOT, 'msg' => '开奖失败'];
  312. }
  313. }
  314. // 虚拟开奖
  315. public static function fakeLotteryDraw($issue_no, $awards)
  316. {
  317. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  318. $text = "";
  319. foreach ($fake_bet_list as $k => $v) {
  320. $lastStr = self::getLastChar($v['first_name'], 1);
  321. if (in_array($v['keywords'], $awards)) {
  322. $amount = (float)$v['amount'];
  323. $odds = (float)$v['odds'];
  324. $profit = $amount * $odds;
  325. if ($profit > 880000) {
  326. $profit = 880000; // 单注最高奖金880000
  327. }
  328. $item['profit'] = $profit;
  329. $yl = $profit - $amount;
  330. if ($k + 1 <= 30) {
  331. $text .= "私聊下注 【******" . $lastStr . "】 {$yl}\n";
  332. }
  333. } else {
  334. if ($k + 1 <= 30) {
  335. $text .= "私聊下注 【******" . $lastStr . "】 -{$v['amount']}\n";
  336. }
  337. }
  338. }
  339. return $text;
  340. }
  341. /**
  342. * @description: 获取中奖的奖项
  343. * @param {*} $winning_numbers
  344. * @return {*}
  345. */
  346. public static function award($winning_numbers)
  347. {
  348. $result = [];
  349. // 组合
  350. $sum = array_sum($winning_numbers);
  351. $section = self::getSection($sum); // 总和段位
  352. $result[] = $section;
  353. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  354. $result[] = $sumOddEven;
  355. $sumSize = self::calculateSumSize($sum); // 总和大小
  356. $result[] = $sumSize;
  357. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  358. if ($sumExtremeSize) {
  359. $result[] = $sumExtremeSize;
  360. }
  361. $sumCao = $sum . '操'; // 总和数字
  362. $result[] = $sumCao;
  363. $sumCombo = $sumSize . $sumOddEven; // 总和大小单双组合
  364. $result[] = $sumCombo;
  365. $sumBaoZi = self::isBaoZi($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 豹子
  366. if ($sumBaoZi) {
  367. $result[] = $sumBaoZi;
  368. }
  369. $sumPair = self::isPair($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 对子
  370. if ($sumPair) {
  371. $result[] = $sumPair;
  372. }
  373. $sumStraight = self::isStraight($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 顺子
  374. if ($sumStraight) {
  375. $result[] = $sumStraight;
  376. }
  377. $tail = self::getLastDigit($sum); // 总和尾数
  378. $result[] = $tail . '尾'; // 尾数
  379. $tailOddEven = self::calculateOddEven($tail); // 尾数单双
  380. $result[] = '尾' . $tailOddEven;
  381. $tailSize = self::calculateOneSize($tail); // 尾数大小
  382. $result[] = '尾' . $tailSize;
  383. $tailCombo = '尾' . $tailSize . $tailOddEven; // 尾数大小单双组合
  384. $result[] = $tailCombo;
  385. $numA = $winning_numbers[0]; // A球
  386. $result[] = $numA . 'A';
  387. $numAOddEven = self::calculateOddEven($numA); // A球单双
  388. $result[] = 'A' . $numAOddEven;
  389. $numASize = self::calculateOneSize($numA); // A球大小
  390. $result[] = 'A' . $numASize;
  391. $result[] = 'A' . $numASize . $numAOddEven; // A球大小单双组合
  392. $numB = $winning_numbers[1]; // B球
  393. $result[] = $numB . 'B';
  394. $numBOddEven = self::calculateOddEven($numB); // B球
  395. $result[] = 'B' . $numBOddEven;
  396. $numBSize = self::calculateOneSize($numB); // B球大小
  397. $result[] = 'B' . $numBSize;
  398. $result[] = 'B' . $numBSize . $numBOddEven; // B球大小单双组合
  399. $numC = $winning_numbers[2];
  400. $result[] = $numC . 'C';
  401. $numCOddEven = self::calculateOddEven($numC); // C球单双
  402. $result[] = 'C' . $numCOddEven;
  403. $numCSize = self::calculateOneSize($numC); // C球大小
  404. $result[] = 'C' . $numCSize;
  405. $result[] = 'C' . $numCSize . $numCOddEven; // C球大小单双组合
  406. return $result;
  407. }
  408. /**
  409. * @description: 算单双
  410. * @param {*} $number
  411. * @return {*}
  412. */
  413. public static function calculateOddEven($number)
  414. {
  415. if ($number & 1) {
  416. return GameplayRuleEnum::SINGLE;
  417. } else {
  418. return GameplayRuleEnum::DOUBLE;
  419. }
  420. }
  421. /**
  422. * @description: 总和大小
  423. * @param {*} $number
  424. * @return {*}
  425. */
  426. public static function calculateSumSize($number)
  427. {
  428. if ($number >= GameplayRuleEnum::SUM_BIG) {
  429. return GameplayRuleEnum::BIG;
  430. }
  431. if ($number <= GameplayRuleEnum::SUM_SMALL) {
  432. return GameplayRuleEnum::SMALL;
  433. }
  434. }
  435. /**
  436. * @description: 总和极值
  437. * @param {*} $number
  438. * @return {*}
  439. */
  440. public static function calculateSumExtremeSize($number)
  441. {
  442. $result = '';
  443. if ($number >= GameplayRuleEnum::SUM_EXTREME_BIG) {
  444. $result = GameplayRuleEnum::EXTREME_BIG;
  445. }
  446. if ($number <= GameplayRuleEnum::SUM_EXTREME_SMALL) {
  447. $result = GameplayRuleEnum::EXTREME_SMALL;
  448. }
  449. return $result;
  450. }
  451. /**
  452. * @description: 豹子
  453. * @param {int} $a
  454. * @param {int} $b
  455. * @param {int} $c
  456. * @return {*}
  457. */
  458. public static function isBaoZi(int $a, int $b, int $c)
  459. {
  460. $result = '';
  461. if ($a === $b && $b === $c) {
  462. $result = GameplayRuleEnum::BAO_ZI;
  463. }
  464. return $result;
  465. }
  466. /**
  467. * @description: 对子
  468. * @param {int} $a
  469. * @param {int} $b
  470. * @param {int} $c
  471. * @return {*}
  472. */
  473. public static function isPair($a, $b, $c)
  474. {
  475. $result = '';
  476. // 确保输入都是个位数
  477. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  478. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  479. return ''; // 或者抛出异常
  480. }
  481. if (($a == $b && $a != $c) ||
  482. ($a == $c && $a != $b) ||
  483. ($b == $c && $b != $a)) {
  484. $result = GameplayRuleEnum::PAIRS;
  485. }
  486. // 判断是否为对子情况
  487. return $result;
  488. }
  489. /**
  490. * @description: 顺子
  491. * @param {int} $a
  492. * @param {int} $b
  493. * @param {int} $c
  494. * @return {*}
  495. */
  496. public static function isStraight($a, $b, $c)
  497. {
  498. $result = '';
  499. // 确保输入都是个位数(0-9)
  500. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  501. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  502. return '';
  503. }
  504. // 去重(顺子必须三个不同数字)
  505. if ($a == $b || $a == $c || $b == $c) {
  506. return '';
  507. }
  508. // 检查是否是完全升序或完全降序的连续数字
  509. $numbers = [$a, $b, $c];
  510. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  511. list($x, $y, $z) = $numbers;
  512. // 情况1:升序连续(1,2,3)
  513. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  514. // 情况2:降序连续(3,2,1)
  515. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  516. if ($isAscending || $isDescending) {
  517. $result = GameplayRuleEnum::STRAIGHT;
  518. }
  519. return $result;
  520. }
  521. /**
  522. * 获取数字的尾数
  523. * @param int $number 输入数字
  524. * @return int 尾数
  525. */
  526. public static function getLastDigit($number)
  527. {
  528. // 确保输入是整数
  529. $number = (int)$number;
  530. // 取绝对值,处理负数情况
  531. $number = abs($number);
  532. // 取模10得到尾数
  533. return $number % 10;
  534. }
  535. /**
  536. * @description: 尾大小
  537. * @param {*} $number
  538. * @return {*}
  539. */
  540. public static function calculateOneSize($number)
  541. {
  542. if ($number >= GameplayRuleEnum::ONE_BIG) {
  543. return GameplayRuleEnum::BIG;
  544. }
  545. if ($number <= GameplayRuleEnum::ONE_SMALL) {
  546. return GameplayRuleEnum::SMALL;
  547. }
  548. }
  549. /**
  550. * @description: 获取段位
  551. * @param {*} $number
  552. * @return {*}
  553. */
  554. public static function getSection($number)
  555. {
  556. $result = '';
  557. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  558. $result = GameplayRuleEnum::ONE;
  559. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  560. $result = GameplayRuleEnum::TWO;
  561. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  562. $result = GameplayRuleEnum::THREE;
  563. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  564. $result = GameplayRuleEnum::FOUR;
  565. }
  566. return $result; // 不在任何段中
  567. }
  568. /**
  569. * @description: 近期开奖记录
  570. * @return {*}
  571. */
  572. public static function currentLotteryResults($memberId)
  573. {
  574. // $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->take(16)->get();
  575. // $text = "📅 近期开奖记录\n";
  576. // $text .= "====================\n";
  577. // if($result){
  578. // foreach($result as $k => $v){
  579. // $winArr = explode(',',$v->winning_numbers);
  580. // // 组合
  581. // $sum = array_sum($winArr);
  582. // $combo = [];
  583. // $sumOddEven = self::calculateOddEven($sum); // 总和单双
  584. // $sumSize = self::calculateSumSize($sum); // 总和大小
  585. // $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  586. // if(empty($sumExtremeSize)){
  587. // $sumExtremeSize = "-";
  588. // }
  589. // $tail = self::getLastDigit($sum); // 总和尾数
  590. // if($tail == 0){
  591. // $tail = '-'; // 尾数
  592. // }else{
  593. // $tail = '尾'.$tail; // 尾数
  594. // }
  595. // $text .= "回合:{$v->issue_no}期 \n";
  596. // $text .= "结果:".implode('+',explode(',',$v->winning_numbers))."=".array_sum(explode(',',$v->winning_numbers))." \n";
  597. // $text .= "组合:{$sumSize} {$sumOddEven} \n";
  598. // $text .= "极值:{$sumExtremeSize} \n";
  599. // $text .= "尾数:{$tail} \n";
  600. // $text .= "---------------------------\n";
  601. // }
  602. // self::telegram()->sendMessage([
  603. // 'chat_id' => $memberId,
  604. // 'text' => $text,
  605. // ]);
  606. // }else{
  607. // self::telegram()->sendMessage([
  608. // 'chat_id' => $memberId,
  609. // 'text' => "暂无开奖记录",
  610. // ]);
  611. // }
  612. $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id', 'desc')->first();
  613. if ($result) {
  614. if ($result->image) {
  615. // self::telegram()->sendPhoto([
  616. // 'chat_id' => $memberId,
  617. // 'photo' => InputFile::create(url($result->image)),
  618. // ]);
  619. return [
  620. 'chat_id' => $memberId,
  621. 'photo' => InputFile::create(url($result->image)),
  622. ];
  623. } else {
  624. // if($result->combo){
  625. // self::telegram()->sendMessage([
  626. // 'chat_id' => $memberId,
  627. // 'text' => "",
  628. // ]);
  629. // }else{
  630. // self::telegram()->sendMessage([
  631. // 'chat_id' => $memberId,
  632. // 'text' => lang("暂无开奖记录"),
  633. // ]);
  634. // }
  635. return
  636. [
  637. 'chat_id' => $memberId,
  638. 'text' => lang("暂无开奖记录"),
  639. ];
  640. }
  641. }
  642. }
  643. // 获取最新的开奖数据
  644. public static function getLatestIssue()
  645. {
  646. $url = "https://ydpc28.co/api/pc28/list";
  647. $result = file_get_contents($url);
  648. $result = json_decode($result, true);
  649. if ($result['errorCode'] != 0) {
  650. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  651. }
  652. $nextDrawInfo = $result['data']['nextDrawInfo'];
  653. $startTime = $nextDrawInfo['currentBJTime'];
  654. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  655. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  656. // }else{
  657. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  658. // }
  659. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  660. $new = true;
  661. $list = $result['data']['list'];
  662. $listKey = [];
  663. foreach ($list as $k => $v) {
  664. $listKey[$v['lotNumber']] = $v;
  665. }
  666. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  667. foreach ($oldList as $k => $v) {
  668. if (isset($listKey[$v->issue_no])) {
  669. $issue = $listKey[$v->issue_no];
  670. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  671. $winArr = array_map('intval', explode(',', $winning_numbers));
  672. // 组合
  673. $sum = array_sum($winArr);
  674. $combo = [];
  675. $sumSize = self::calculateSumSize($sum); // 总和大小
  676. $combo[] = $sumSize;
  677. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  678. $combo[] = $sumOddEven;
  679. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  680. if ($sumExtremeSize) {
  681. $combo[] = $sumExtremeSize;
  682. }
  683. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  684. if ($sumBaoZi) {
  685. $combo[] = $sumBaoZi;
  686. }
  687. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  688. if ($sumPair) {
  689. $combo[] = $sumPair;
  690. }
  691. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  692. if ($sumStraight) {
  693. $combo[] = $sumStraight;
  694. }
  695. $tail = self::getLastDigit($sum); // 总和尾数
  696. if ($tail == 0 || $tail == 9) {
  697. } else {
  698. $combo[] = '尾' . $tail; // 尾数
  699. }
  700. $key = 'lottery_numbers_' . $v->issue_no;
  701. $combo = implode(' ', $combo);
  702. if (Cache::add($key, $winning_numbers, 100)) {
  703. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  704. $new = false;
  705. }
  706. // Log::error('开奖缓存: ' .$key);
  707. // Log::error('开奖缓存结果: ' .($new ? '新开奖' : '已开奖') );
  708. }
  709. }
  710. // sleep(5); // 等待开奖完成
  711. if ($new) {
  712. $latestIssue = $list[0]; // 最后开奖
  713. $new_issue_no = $latestIssue['lotNumber'] + 1; // 新期号
  714. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  715. // 不存在
  716. if (!$newInfo) {
  717. $res = self::submit([
  718. 'issue_no' => $new_issue_no,
  719. 'status' => self::model()::STATUS_DRAFT,
  720. 'start_time' => $startTime,
  721. 'end_time' => $endTime,
  722. ]);
  723. Prediction::prediction($new_issue_no);
  724. $id = $res['key'] ?? 0;
  725. if ($id) {
  726. self::betting($id); // 开始下注
  727. }
  728. Cache::set('new_issue_no', $new_issue_no, 10); // 缓存
  729. }
  730. }
  731. return $result;
  732. }
  733. // 获取最新的开奖数据
  734. public static function getLatestIssue2()
  735. {
  736. $url = "https://ydpc28.co/api/pc28/list";
  737. $result = file_get_contents($url);
  738. $result = json_decode($result, true);
  739. if ($result['errorCode'] != 0) {
  740. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  741. }
  742. $nextDrawInfo = $result['data']['nextDrawInfo'];
  743. $startTime = $nextDrawInfo['currentBJTime'];
  744. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  745. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  746. // }else{
  747. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  748. // }
  749. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  750. $new = true;
  751. $list = $result['data']['list'];
  752. $listKey = [];
  753. foreach ($list as $k => $v) {
  754. $listKey[$v['lotNumber']] = $v;
  755. }
  756. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  757. foreach ($oldList as $k => $v) {
  758. if (isset($listKey[$v->issue_no])) {
  759. $issue = $listKey[$v->issue_no];
  760. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  761. $winArr = array_map('intval', explode(',', $winning_numbers));
  762. // 组合
  763. $sum = array_sum($winArr);
  764. $combo = [];
  765. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  766. $combo[] = $sumOddEven;
  767. $sumSize = self::calculateSumSize($sum); // 总和大小
  768. $combo[] = $sumSize;
  769. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  770. if ($sumExtremeSize) {
  771. $combo[] = $sumExtremeSize;
  772. }
  773. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  774. if ($sumBaoZi) {
  775. $combo[] = $sumBaoZi;
  776. }
  777. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  778. if ($sumPair) {
  779. $combo[] = $sumPair;
  780. }
  781. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  782. if ($sumStraight) {
  783. $combo[] = $sumStraight;
  784. }
  785. $tail = self::getLastDigit($sum); // 总和尾数
  786. if ($tail == 0 || $tail == 9) {
  787. } else {
  788. $combo[] = '尾' . $tail; // 尾数
  789. }
  790. $combo = implode(' ', $combo);
  791. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  792. }
  793. }
  794. return $result;
  795. }
  796. // 封盘倒数
  797. public static function syncCountdownIssue()
  798. {
  799. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  800. if ($pc28Switch == 1) {
  801. $info = PcIssue::where('status', PcIssue::STATUS_BETTING)->orderBy('end_time')->first();
  802. } else {
  803. $info = self::model()::where('status', self::model()::STATUS_BETTING)->orderBy('end_time', 'asc')->first();
  804. }
  805. if ($info) {
  806. $now_date = date('Y-m-d H:i:s', time() + 60); // 提前60秒
  807. if ($info['end_time'] < $now_date) {
  808. $replyInfo = KeyboardService::findOne(['button' => '封盘倒数']);
  809. if ($replyInfo) {
  810. $text = $replyInfo->reply;
  811. $buttons = json_decode($replyInfo->buttons, true);
  812. $image = $replyInfo->image;
  813. if ($image) {
  814. $image = url($image);
  815. }
  816. if (Cache::has('issue_countdown_' . $info->issue_no)) {
  817. } else {
  818. self::asyncBettingGroupNotice($text, $buttons, $image);
  819. Cache::put('issue_countdown_' . $info->issue_no, true, 60); // 缓存50秒,防止多次发送
  820. }
  821. }
  822. }
  823. }
  824. }
  825. // 停止下注
  826. public static function syncCloseIssue()
  827. {
  828. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  829. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  830. foreach ($list as $k => $v) {
  831. if ($v['end_time'] < $now_date) {
  832. self::closeBetting($v->id);
  833. }
  834. }
  835. }
  836. // 生成开奖图片
  837. public static function lotteryImage($issue_no)
  838. {
  839. $list = self::model()::where('issue_no', '<=', $issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no', 'desc')->take(20)->get();
  840. $records = $list->toArray();
  841. foreach ($records as $k => $v) {
  842. $winning_numbers = explode(',', $v['winning_numbers']);
  843. $v['winning_numbers'] = $winning_numbers;
  844. // 组合
  845. $sum = array_sum($winning_numbers);
  846. $v['sum'] = $sum;
  847. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  848. $sumSize = self::calculateSumSize($sum); // 总和大小
  849. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  850. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  851. if (!$sumExtremeSize) {
  852. $sumExtremeSize = '-';
  853. }
  854. $v['extreme'] = $sumExtremeSize;
  855. $tail = self::getLastDigit($sum); // 总和尾数
  856. if ($tail === 0 || $tail === 9) {
  857. $tailStr = '-';
  858. } else {
  859. $tailStr = '尾' . $tail;
  860. }
  861. $v['tail'] = $tailStr;
  862. $records[$k] = $v;
  863. }
  864. $service = new LotteryImageService();
  865. $url = $service->generate($records);
  866. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  867. return $url;
  868. }
  869. // 发送开奖图片
  870. public static function sendLotteryImage($chatId, $issueNo)
  871. {
  872. $recordImage = self::lotteryImage($issueNo);
  873. self::sendMessage($chatId, '', [], url($recordImage));
  874. // dispatch(new SendTelegramMessageJob('', [], url($recordImage)));
  875. }
  876. }