IssueService.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  707. //更新游戏开关的切换
  708. if ($pc28Switch == 0 && $k == 0) Config::setPc28Switch();
  709. }
  710. }
  711. // sleep(5); // 等待开奖完成
  712. if ($new) {
  713. $latestIssue = $list[0]; // 最后开奖
  714. $new_issue_no = $latestIssue['lotNumber'] + 1; // 新期号
  715. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  716. // 不存在
  717. if (!$newInfo) {
  718. $res = self::submit([
  719. 'issue_no' => $new_issue_no,
  720. 'status' => self::model()::STATUS_DRAFT,
  721. 'start_time' => $startTime,
  722. 'end_time' => $endTime,
  723. ]);
  724. Prediction::prediction($new_issue_no);
  725. $id = $res['key'] ?? 0;
  726. if ($id) {
  727. self::betting($id); // 开始下注
  728. }
  729. Cache::set('new_issue_no', $new_issue_no, 10); // 缓存
  730. }
  731. }
  732. return $result;
  733. }
  734. // 获取最新的开奖数据
  735. public static function getLatestIssue2()
  736. {
  737. $url = "https://ydpc28.co/api/pc28/list";
  738. $result = file_get_contents($url);
  739. $result = json_decode($result, true);
  740. if ($result['errorCode'] != 0) {
  741. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  742. }
  743. $nextDrawInfo = $result['data']['nextDrawInfo'];
  744. $startTime = $nextDrawInfo['currentBJTime'];
  745. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  746. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  747. // }else{
  748. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  749. // }
  750. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  751. $new = true;
  752. $list = $result['data']['list'];
  753. $listKey = [];
  754. foreach ($list as $k => $v) {
  755. $listKey[$v['lotNumber']] = $v;
  756. }
  757. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  758. foreach ($oldList as $k => $v) {
  759. if (isset($listKey[$v->issue_no])) {
  760. $issue = $listKey[$v->issue_no];
  761. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  762. $winArr = array_map('intval', explode(',', $winning_numbers));
  763. // 组合
  764. $sum = array_sum($winArr);
  765. $combo = [];
  766. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  767. $combo[] = $sumOddEven;
  768. $sumSize = self::calculateSumSize($sum); // 总和大小
  769. $combo[] = $sumSize;
  770. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  771. if ($sumExtremeSize) {
  772. $combo[] = $sumExtremeSize;
  773. }
  774. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  775. if ($sumBaoZi) {
  776. $combo[] = $sumBaoZi;
  777. }
  778. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  779. if ($sumPair) {
  780. $combo[] = $sumPair;
  781. }
  782. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  783. if ($sumStraight) {
  784. $combo[] = $sumStraight;
  785. }
  786. $tail = self::getLastDigit($sum); // 总和尾数
  787. if ($tail == 0 || $tail == 9) {
  788. } else {
  789. $combo[] = '尾' . $tail; // 尾数
  790. }
  791. $combo = implode(' ', $combo);
  792. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  793. }
  794. }
  795. return $result;
  796. }
  797. // 封盘倒数
  798. public static function syncCountdownIssue()
  799. {
  800. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  801. if ($pc28Switch == 1) {
  802. $info = PcIssue::where('status', PcIssue::STATUS_BETTING)->orderBy('end_time')->first();
  803. } else {
  804. $info = self::model()::where('status', self::model()::STATUS_BETTING)->orderBy('end_time', 'asc')->first();
  805. }
  806. if ($info) {
  807. $now_date = date('Y-m-d H:i:s', time() + 60); // 提前60秒
  808. if ($info['end_time'] < $now_date) {
  809. $replyInfo = KeyboardService::findOne(['button' => '封盘倒数']);
  810. if ($replyInfo) {
  811. $text = $replyInfo->reply;
  812. $buttons = json_decode($replyInfo->buttons, true);
  813. $image = $replyInfo->image;
  814. if ($image) {
  815. $image = url($image);
  816. }
  817. if (Cache::has('issue_countdown_' . $info->issue_no)) {
  818. } else {
  819. self::asyncBettingGroupNotice($text, $buttons, $image);
  820. Cache::put('issue_countdown_' . $info->issue_no, true, 60); // 缓存50秒,防止多次发送
  821. }
  822. }
  823. }
  824. }
  825. }
  826. // 停止下注
  827. public static function syncCloseIssue()
  828. {
  829. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  830. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  831. foreach ($list as $k => $v) {
  832. if ($v['end_time'] < $now_date) {
  833. self::closeBetting($v->id);
  834. }
  835. }
  836. }
  837. // 生成开奖图片
  838. public static function lotteryImage($issue_no)
  839. {
  840. $list = self::model()::where('issue_no', '<=', $issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no', 'desc')->take(20)->get();
  841. $records = $list->toArray();
  842. foreach ($records as $k => $v) {
  843. $winning_numbers = explode(',', $v['winning_numbers']);
  844. $v['winning_numbers'] = $winning_numbers;
  845. // 组合
  846. $sum = array_sum($winning_numbers);
  847. $v['sum'] = $sum;
  848. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  849. $sumSize = self::calculateSumSize($sum); // 总和大小
  850. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  851. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  852. if (!$sumExtremeSize) {
  853. $sumExtremeSize = '-';
  854. }
  855. $v['extreme'] = $sumExtremeSize;
  856. $tail = self::getLastDigit($sum); // 总和尾数
  857. if ($tail === 0 || $tail === 9) {
  858. $tailStr = '-';
  859. } else {
  860. $tailStr = '尾' . $tail;
  861. }
  862. $v['tail'] = $tailStr;
  863. $records[$k] = $v;
  864. }
  865. $service = new LotteryImageService();
  866. $url = $service->generate($records);
  867. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  868. return $url;
  869. }
  870. // 发送开奖图片
  871. public static function sendLotteryImage($chatId, $issueNo)
  872. {
  873. $recordImage = self::lotteryImage($issueNo);
  874. self::sendMessage($chatId, '', [], url($recordImage));
  875. // dispatch(new SendTelegramMessageJob('', [], url($recordImage)));
  876. }
  877. }