IssueService.php 31 KB

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