IssueService.php 34 KB

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