IssueService.php 30 KB

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