IssueService.php 35 KB

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