IssueService.php 35 KB

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