IssueService.php 31 KB

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