IssueService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. // 投注情况通知 xxxx期投注统计
  208. BetService::statNotice($info->issue_no);
  209. $replyInfo = KeyboardService::findOne(['button' => '封盘开奖']);
  210. if ($replyInfo) {
  211. $text = $replyInfo->reply;
  212. $buttons = json_decode($replyInfo->buttons, true);
  213. $image = $replyInfo->image;
  214. if ($image) {
  215. $image = url($image);
  216. }
  217. // self::bettingGroupNotice($text, $buttons, $image);
  218. self::asyncBettingGroupNotice($text, $buttons, $image);
  219. }
  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. }