IssueService.php 32 KB

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