IssueService.php 31 KB

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