IssueService.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Cao;
  4. use App\Models\CaoHistory;
  5. use App\Models\PcIssue;
  6. use App\Models\Prediction;
  7. use App\Services\BaseService;
  8. use App\Models\Issue;
  9. use App\Models\Config;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Collection;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Http;
  14. use Illuminate\Support\Facades\Log;
  15. use App\Services\GameplayRuleService;
  16. use App\Constants\GameplayRuleEnum;
  17. use App\Http\Controllers\admin\Lottery;
  18. use App\Services\KeyboardService;
  19. use App\Services\LotteryImageService;
  20. use Telegram\Bot\FileUpload\InputFile;
  21. use App\Jobs\SendTelegramMessageJob;
  22. use App\Jobs\SendTelegramGroupMessageJob;
  23. /**
  24. * 投注
  25. */
  26. class IssueService extends BaseService
  27. {
  28. const COUNTDOWN_TO_CLOSING_THE_MARKET = 60;//提前xx秒封盘
  29. const PLAYNOW_KENO_URL = 'https://www.playnow.com/services2/keno/nextdraw';
  30. const PLAYNOW_SOURCE_TIMEZONE = 'America/Vancouver';
  31. const BEIJING_TIMEZONE = 'Asia/Shanghai';
  32. const PLAYNOW_DRAW_INTERVAL_SECONDS = 210;
  33. public static function init($telegram, $data, $chatId, $firstName, $messageId): void
  34. {
  35. switch ($data) {
  36. default:
  37. //查看开奖历史图片
  38. $pattern = "/^showLotteryHistory@@\d+$/";
  39. if (preg_match($pattern, $data)) {
  40. $id = preg_replace('/^showLotteryHistory@@/', '', $data);
  41. IssueService::sendLotteryImage($chatId, $id);
  42. }
  43. break;
  44. }
  45. }
  46. /**
  47. * @description: 模型
  48. * @return {string}
  49. */
  50. public static function model(): string
  51. {
  52. return Issue::class;
  53. }
  54. /**
  55. * @description: 枚举
  56. * @return {*}
  57. */
  58. public static function enum(): string
  59. {
  60. return '';
  61. }
  62. /**
  63. * @description: 获取查询条件
  64. * @param {array} $search 查询内容
  65. * @return {array}
  66. */
  67. public static function getWhere(array $search = []): array
  68. {
  69. $where = [];
  70. if (isset($search['issue_no']) && !empty($search['issue_no'])) {
  71. $where[] = ['issue_no', '=', $search['issue_no']];
  72. }
  73. if (isset($search['id']) && !empty($search['id'])) {
  74. $where[] = ['id', '=', $search['id']];
  75. }
  76. if (isset($search['status']) && $search['status'] != '') {
  77. $where[] = ['status', '=', $search['status']];
  78. }
  79. if (isset($search['abnormal']) && !empty($search['abnormal'])) {
  80. $where[] = ['end_time', '<', date('Y-m-d H:i:s', time() - 1800)];
  81. $where[] = ['status', '!=', self::model()::STATUS_DRAW];
  82. }
  83. if (!empty($search['start_time'])) {
  84. $where[] = ['end_time', '>=', $search['start_time'].' 00:00:00'];
  85. }
  86. if (!empty($search['end_time'])) {
  87. $where[] = ['end_time', '<=', $search['end_time'].' 23:59:59'];
  88. }
  89. return $where;
  90. }
  91. /**
  92. * @description: 查询单条数据
  93. * @param array $search
  94. * @return \App\Models\Coin|null
  95. */
  96. public static function findOne(array $search): ?Issue
  97. {
  98. return self::model()::where(self::getWhere($search))->first();
  99. }
  100. /**
  101. * @description: 查询所有数据
  102. * @param array $search
  103. * @return \Illuminate\Database\Eloquent\Collection
  104. */
  105. public static function findAll(array $search = [])
  106. {
  107. return self::model()::where(self::getWhere($search))->get();
  108. }
  109. /**
  110. * @description: 分页查询
  111. * @param array $search
  112. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  113. */
  114. public static function paginate(array $search = [])
  115. {
  116. $limit = isset($search['limit']) ? $search['limit'] : 15;
  117. $paginator = self::model()::where(self::getWhere($search))->orderBy('issue_no', 'desc')->paginate($limit);
  118. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  119. }
  120. /**
  121. * @description:
  122. * @param {*} $params
  123. * @return {*}
  124. */
  125. public static function submit($params = [])
  126. {
  127. $result = false;
  128. $msg['code'] = self::NOT;
  129. $msg['msg'] = '';
  130. // 2. 判断是否是更新
  131. if (!empty($params['id'])) {
  132. // 更新
  133. $info = self::findOne(['id' => $params['id']]);
  134. if (!$info) {
  135. $msg['msg'] = '期号不存在!';
  136. } else {
  137. $result = $info->update($params);
  138. $id = $params['id'];
  139. }
  140. } else {
  141. // 创建
  142. $result = $info = self::model()::create($params);
  143. $id = $result->id;
  144. }
  145. if ($result) {
  146. $msg['code'] = self::YES;
  147. $msg['msg'] = '设置成功';
  148. $msg['key'] = $id;
  149. } else {
  150. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  151. }
  152. return $msg;
  153. }
  154. /**
  155. * @description: 开始下注
  156. * @param {*} $id
  157. * @return {*}
  158. */
  159. public static function betting($id)
  160. {
  161. $info = self::findOne(['id' => $id]);
  162. if (!$info) {
  163. return ['code' => self::NOT, 'msg' => '期号不存在'];
  164. }
  165. if (!in_array($info->status, [self::model()::STATUS_DRAFT, self::model()::STATUS_BETTING])) {
  166. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  167. }
  168. $info->status = self::model()::STATUS_BETTING;
  169. $info->save();
  170. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  171. $wanFaGuiZeTime = Cache::get("玩法规则推送时间", 0);
  172. $now = time();
  173. $wanFaGuiZeTime = intval($wanFaGuiZeTime);
  174. if ($now - $wanFaGuiZeTime > (60 * 15)) {
  175. $replyInfo = KeyboardService::findOne(['button' => '玩法规则']);
  176. if ($replyInfo) {
  177. $text = $replyInfo->reply;
  178. $buttons = json_decode($replyInfo->buttons, true);
  179. $image = $replyInfo->image;
  180. if ($image) {
  181. $image = url($image);
  182. }
  183. if (empty($buttons)) {
  184. $buttons = self::getOperateButton();
  185. }
  186. Cache::put('玩法规则推送时间', time());
  187. if ($pc28Switch == 0) self::asyncBettingGroupNotice($text, $buttons, $image);
  188. }
  189. }
  190. $replyInfo = KeyboardService::findOne(['button' => '开始下注']);
  191. if ($replyInfo) {
  192. $text = $replyInfo->reply;
  193. $buttons = json_decode($replyInfo->buttons, true);
  194. $image = $replyInfo->image;
  195. if ($image) {
  196. $image = url($image);
  197. }
  198. if ($pc28Switch == 0) self::asyncBettingGroupNotice($text, $buttons, $image);
  199. }
  200. return ['code' => self::YES, 'msg' => '开始下注'];
  201. }
  202. /**
  203. * @description: 封盘
  204. * @param {*} $id
  205. * @return {*}
  206. */
  207. public static function closeBetting($id)
  208. {
  209. $info = self::findOne(['id' => $id]);
  210. if (!$info) {
  211. return ['code' => self::NOT, 'msg' => '期号不存在'];
  212. }
  213. if ($info->status != self::model()::STATUS_BETTING) {
  214. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  215. }
  216. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  217. $info->status = self::model()::STATUS_CLOSE;
  218. $info->save();
  219. if ($pc28Switch == 0) {
  220. $replyInfo = KeyboardService::findOne(['button' => '停止下注']);
  221. if ($replyInfo) {
  222. $text = $replyInfo->reply;
  223. $buttons = json_decode($replyInfo->buttons, true);
  224. $image = $replyInfo->image;
  225. if ($image) {
  226. $image = url($image);
  227. }
  228. //停止下注的信息不发了
  229. // self::asyncBettingGroupNotice($text, $buttons, $image);
  230. }
  231. // 投注情况通知 xxxx期投注统计
  232. BetService::statNotice($info->issue_no);
  233. $replyInfo = KeyboardService::findOne(['button' => '封盘开奖']);
  234. if ($replyInfo) {
  235. $text = $replyInfo->reply;
  236. $buttons = json_decode($replyInfo->buttons, true);
  237. $image = $replyInfo->image;
  238. if ($image) {
  239. $image = url($image);
  240. }
  241. // self::bettingGroupNotice($text, $buttons, $image);
  242. self::asyncBettingGroupNotice($text, $buttons, $image);
  243. }
  244. }
  245. return ['code' => self::YES, 'msg' => '封盘成功'];
  246. }
  247. /**
  248. * @description: 开奖失败
  249. * @param {*} $id
  250. * @return {*}
  251. */
  252. public static function lotteryDrawFail($id)
  253. {
  254. $result = false;
  255. $msg['code'] = self::NOT;
  256. $msg['msg'] = '';
  257. DB::beginTransaction();
  258. try {
  259. // 更新
  260. $info = self::findOne(['id' => $id]);
  261. if (!$info) {
  262. $msg['msg'] = '期号不存在!';
  263. } else {
  264. $params['status'] = self::model()::STATUS_FAIL;
  265. $result = $info->update($params);
  266. BetService::betFail($info->issue_no);
  267. }
  268. DB::commit();
  269. return ['code' => self::YES, 'msg' => '投注已退回'];
  270. } catch (\Exception $e) {
  271. DB::rollBack();
  272. return ['code' => self::NOT, 'msg' => '投注退回失败'];
  273. }
  274. if ($result) {
  275. $msg['code'] = self::YES;
  276. $msg['msg'] = '设置成功';
  277. } else {
  278. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  279. }
  280. return $msg;
  281. }
  282. /**
  283. * @description: 开奖
  284. * @param {*} $id
  285. * @param {*} $winning_numbers 开奖号码
  286. * @param {*} $combo 开奖组合
  287. * @param {*} $recordImage 开奖图片
  288. * @return {*}
  289. */
  290. public static function lotteryDraw($id, $winning_numbers, $combo, $recordImage, $keno = null)
  291. {
  292. $info = self::findOne(['id' => $id]);
  293. if (!$info) {
  294. return ['code' => self::NOT, 'msg' => '期号不存在'];
  295. }
  296. if ($info->status == self::model()::STATUS_DRAW) {
  297. return ['code' => self::NOT, 'msg' => '期号状态不正确'];
  298. }
  299. $winArr = array_map('intval', explode(',', $winning_numbers));
  300. // 计算中奖
  301. $awards = self::award(explode(',', $winning_numbers));
  302. DB::beginTransaction();
  303. try {
  304. $info->status = self::model()::STATUS_DRAW;
  305. $info->winning_numbers = $winning_numbers;
  306. $info->combo = $combo;
  307. $kenoJson = self::encodePlayNowKeno($keno);
  308. if ($kenoJson) {
  309. $info->keno = $kenoJson;
  310. }
  311. $info->saveQuietly();
  312. Log::channel('issue')->info('官方期开奖写入源头号码', [
  313. 'issue_no' => $info->issue_no,
  314. 'keno_saved' => $kenoJson ? 'yes' : 'no',
  315. 'keno_preview' => $kenoJson ? substr($kenoJson, 0, 80) : '',
  316. ]);
  317. $size = in_array("大", $awards);
  318. $size = $size ? "大" : "小";
  319. $oddOrEven = in_array("双", $awards);
  320. $oddOrEven = $oddOrEven ? "双" : "单";
  321. Prediction::result($info->issue_no, $size, $oddOrEven, $info->winning_numbers);
  322. Cao::updateData($awards);
  323. CaoHistory::updateData($awards);
  324. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  325. $replyInfo = KeyboardService::findOne(['button' => '本期开奖']);
  326. if ($replyInfo) {
  327. $text = $replyInfo->reply;
  328. $text .= "\n";
  329. $text .= $info->issue_no . ": " . implode('+', explode(',', $winning_numbers)) . "=" . array_sum($winArr) . " " . $combo;
  330. $buttons = json_decode($replyInfo->buttons, true);
  331. $image = $replyInfo->image;
  332. if ($image) {
  333. $image = url($image);
  334. }
  335. if (empty($buttons)) {
  336. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  337. $buttons[] = [['text' => lang('✅唯一财务'), 'callback_data' => "", 'url' => "https://t.me/{$serviceAccount}"]];
  338. }
  339. // self::bettingGroupNotice($text, $buttons, $image, true);
  340. if ($pc28Switch == 0) SendTelegramGroupMessageJob::dispatch($text, $buttons, $image, true, "\n", [
  341. 'message_type' => 'draw_result',
  342. 'issue_no' => $info->issue_no,
  343. ])->afterCommit();
  344. }
  345. $recordImage = self::lotteryImage($info->issue_no);
  346. if ($recordImage) {
  347. // self::bettingGroupNotice('', [], url($recordImage));
  348. if ($pc28Switch == 0) SendTelegramGroupMessageJob::dispatch('', [], url($recordImage), false, "\n", [
  349. 'message_type' => 'draw_history_image',
  350. 'issue_no' => $info->issue_no,
  351. ])->afterCommit();
  352. }
  353. $info->image = $recordImage;
  354. $info->save();
  355. BetService::betSettled($info->issue_no, $awards);
  356. DB::commit();
  357. } catch (\Exception $e) {
  358. DB::rollBack();
  359. $message = "开奖失败:\n{$info->issue_no}\n";
  360. $message .= $e->getFile() . ':' . $e->getLine();
  361. $message .= "\n";
  362. $message .= $e->getMessage() . $winning_numbers;
  363. Log::error($message);
  364. return ['code' => self::NOT, 'msg' => '开奖失败', 'error' => $e->getMessage()];
  365. }
  366. return ['code' => self::YES, 'msg' => '开奖成功'];
  367. }
  368. // 虚拟开奖
  369. public static function fakeLotteryDraw($issue_no, $awards)
  370. {
  371. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  372. $text = "";
  373. foreach ($fake_bet_list as $k => $v) {
  374. $lastStr = self::getLastChar($v['first_name'], 1);
  375. if (in_array($v['keywords'], $awards)) {
  376. $amount = (float)$v['amount'];
  377. $odds = (float)$v['odds'];
  378. $profit = $amount * $odds;
  379. if ($profit > 880000) {
  380. $profit = 880000; // 单注最高奖金880000
  381. }
  382. $item['profit'] = $profit;
  383. $yl = $profit - $amount;
  384. if ($k + 1 <= 30) {
  385. $text .= "私聊下注 【******" . $lastStr . "】 {$yl}\n";
  386. }
  387. } else {
  388. if ($k + 1 <= 30) {
  389. $text .= "私聊下注 【******" . $lastStr . "】 -{$v['amount']}\n";
  390. }
  391. }
  392. }
  393. return $text;
  394. }
  395. /**
  396. * @description: 获取中奖的奖项
  397. * @param {*} $winning_numbers
  398. * @return {*}
  399. */
  400. public static function award($winning_numbers)
  401. {
  402. $result = [];
  403. // 组合
  404. $sum = array_sum($winning_numbers);
  405. $section = self::getSection($sum); // 总和段位
  406. $result[] = $section;
  407. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  408. $result[] = $sumOddEven;
  409. $sumSize = self::calculateSumSize($sum); // 总和大小
  410. $result[] = $sumSize;
  411. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  412. if ($sumExtremeSize) {
  413. $result[] = $sumExtremeSize;
  414. }
  415. $sumCao = $sum . '操'; // 总和数字
  416. $result[] = $sumCao;
  417. $sumCombo = $sumSize . $sumOddEven; // 总和大小单双组合
  418. $result[] = $sumCombo;
  419. $sumBaoZi = self::isBaoZi($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 豹子
  420. if ($sumBaoZi) {
  421. $result[] = $sumBaoZi;
  422. }
  423. $sumPair = self::isPair($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 对子
  424. if ($sumPair) {
  425. $result[] = $sumPair;
  426. }
  427. $sumStraight = self::isStraight($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 顺子
  428. if ($sumStraight) {
  429. $result[] = $sumStraight;
  430. }
  431. $tail = self::getLastDigit($sum); // 总和尾数
  432. if (!in_array($tail, [0, 9])) {
  433. $result[] = $tail . '尾'; // 尾数
  434. $tailOddEven = self::calculateOddEven($tail); // 尾数单双
  435. $result[] = '尾' . $tailOddEven;
  436. $tailSize = self::calculateOneSize($tail); // 尾数大小
  437. $result[] = '尾' . $tailSize;
  438. $tailCombo = '尾' . $tailSize . $tailOddEven; // 尾数大小单双组合
  439. $result[] = $tailCombo;
  440. } else {
  441. $result[] = $tail . '尾'; // 尾数
  442. }
  443. $numA = $winning_numbers[0]; // A球
  444. $result[] = $numA . 'A';
  445. $numAOddEven = self::calculateOddEven($numA); // A球单双
  446. $result[] = 'A' . $numAOddEven;
  447. $numASize = self::calculateOneSize($numA); // A球大小
  448. $result[] = 'A' . $numASize;
  449. $result[] = 'A' . $numASize . $numAOddEven; // A球大小单双组合
  450. $numB = $winning_numbers[1]; // B球
  451. $result[] = $numB . 'B';
  452. $numBOddEven = self::calculateOddEven($numB); // B球
  453. $result[] = 'B' . $numBOddEven;
  454. $numBSize = self::calculateOneSize($numB); // B球大小
  455. $result[] = 'B' . $numBSize;
  456. $result[] = 'B' . $numBSize . $numBOddEven; // B球大小单双组合
  457. $numC = $winning_numbers[2];
  458. $result[] = $numC . 'C';
  459. $numCOddEven = self::calculateOddEven($numC); // C球单双
  460. $result[] = 'C' . $numCOddEven;
  461. $numCSize = self::calculateOneSize($numC); // C球大小
  462. $result[] = 'C' . $numCSize;
  463. $result[] = 'C' . $numCSize . $numCOddEven; // C球大小单双组合
  464. return $result;
  465. }
  466. /**
  467. * @description: 算单双
  468. * @param {*} $number
  469. * @return {*}
  470. */
  471. public static function calculateOddEven($number)
  472. {
  473. if ($number & 1) {
  474. return GameplayRuleEnum::SINGLE;
  475. } else {
  476. return GameplayRuleEnum::DOUBLE;
  477. }
  478. }
  479. /**
  480. * @description: 总和大小
  481. * @param {*} $number
  482. * @return {*}
  483. */
  484. public static function calculateSumSize($number)
  485. {
  486. if ($number >= GameplayRuleEnum::SUM_BIG) {
  487. return GameplayRuleEnum::BIG;
  488. }
  489. if ($number <= GameplayRuleEnum::SUM_SMALL) {
  490. return GameplayRuleEnum::SMALL;
  491. }
  492. }
  493. /**
  494. * @description: 总和极值
  495. * @param {*} $number
  496. * @return {*}
  497. */
  498. public static function calculateSumExtremeSize($number)
  499. {
  500. $result = '';
  501. if ($number >= GameplayRuleEnum::SUM_EXTREME_BIG) {
  502. $result = GameplayRuleEnum::EXTREME_BIG;
  503. }
  504. if ($number <= GameplayRuleEnum::SUM_EXTREME_SMALL) {
  505. $result = GameplayRuleEnum::EXTREME_SMALL;
  506. }
  507. return $result;
  508. }
  509. /**
  510. * @description: 豹子
  511. * @param {int} $a
  512. * @param {int} $b
  513. * @param {int} $c
  514. * @return {*}
  515. */
  516. public static function isBaoZi(int $a, int $b, int $c)
  517. {
  518. $result = '';
  519. if ($a === $b && $b === $c) {
  520. $result = GameplayRuleEnum::BAO_ZI;
  521. }
  522. return $result;
  523. }
  524. /**
  525. * @description: 对子
  526. * @param {int} $a
  527. * @param {int} $b
  528. * @param {int} $c
  529. * @return {*}
  530. */
  531. public static function isPair($a, $b, $c)
  532. {
  533. $result = '';
  534. // 确保输入都是个位数
  535. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  536. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  537. return ''; // 或者抛出异常
  538. }
  539. if (($a == $b && $a != $c) ||
  540. ($a == $c && $a != $b) ||
  541. ($b == $c && $b != $a)) {
  542. $result = GameplayRuleEnum::PAIRS;
  543. }
  544. // 判断是否为对子情况
  545. return $result;
  546. }
  547. /**
  548. * @description: 顺子
  549. * @param {int} $a
  550. * @param {int} $b
  551. * @param {int} $c
  552. * @return {*}
  553. */
  554. public static function isStraight($a, $b, $c)
  555. {
  556. $result = '';
  557. // 确保输入都是个位数(0-9)
  558. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  559. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  560. return '';
  561. }
  562. // 去重(顺子必须三个不同数字)
  563. if ($a == $b || $a == $c || $b == $c) {
  564. return '';
  565. }
  566. // 检查是否是完全升序或完全降序的连续数字
  567. $numbers = [$a, $b, $c];
  568. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  569. list($x, $y, $z) = $numbers;
  570. // 情况1:升序连续(1,2,3)
  571. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  572. // 情况2:降序连续(3,2,1)
  573. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  574. if ($isAscending || $isDescending) {
  575. $result = GameplayRuleEnum::STRAIGHT;
  576. }
  577. return $result;
  578. }
  579. /**
  580. * 获取数字的尾数
  581. * @param int $number 输入数字
  582. * @return int 尾数
  583. */
  584. public static function getLastDigit($number)
  585. {
  586. // 确保输入是整数
  587. $number = (int)$number;
  588. // 取绝对值,处理负数情况
  589. $number = abs($number);
  590. // 取模10得到尾数
  591. return $number % 10;
  592. }
  593. /**
  594. * @description: 尾大小
  595. * @param {*} $number
  596. * @return {*}
  597. */
  598. public static function calculateOneSize($number)
  599. {
  600. if ($number >= GameplayRuleEnum::ONE_BIG) {
  601. return GameplayRuleEnum::BIG;
  602. }
  603. if ($number <= GameplayRuleEnum::ONE_SMALL) {
  604. return GameplayRuleEnum::SMALL;
  605. }
  606. }
  607. /**
  608. * @description: 获取段位
  609. * @param {*} $number
  610. * @return {*}
  611. */
  612. public static function getSection($number)
  613. {
  614. $result = '';
  615. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  616. $result = GameplayRuleEnum::ONE;
  617. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  618. $result = GameplayRuleEnum::TWO;
  619. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  620. $result = GameplayRuleEnum::THREE;
  621. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  622. $result = GameplayRuleEnum::FOUR;
  623. }
  624. return $result; // 不在任何段中
  625. }
  626. /**
  627. * @description: 近期开奖记录
  628. * @return {*}
  629. */
  630. public static function currentLotteryResults($memberId)
  631. {
  632. // $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->take(16)->get();
  633. // $text = "📅 近期开奖记录\n";
  634. // $text .= "====================\n";
  635. // if($result){
  636. // foreach($result as $k => $v){
  637. // $winArr = explode(',',$v->winning_numbers);
  638. // // 组合
  639. // $sum = array_sum($winArr);
  640. // $combo = [];
  641. // $sumOddEven = self::calculateOddEven($sum); // 总和单双
  642. // $sumSize = self::calculateSumSize($sum); // 总和大小
  643. // $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  644. // if(empty($sumExtremeSize)){
  645. // $sumExtremeSize = "-";
  646. // }
  647. // $tail = self::getLastDigit($sum); // 总和尾数
  648. // if($tail == 0){
  649. // $tail = '-'; // 尾数
  650. // }else{
  651. // $tail = '尾'.$tail; // 尾数
  652. // }
  653. // $text .= "回合:{$v->issue_no}期 \n";
  654. // $text .= "结果:".implode('+',explode(',',$v->winning_numbers))."=".array_sum(explode(',',$v->winning_numbers))." \n";
  655. // $text .= "组合:{$sumSize} {$sumOddEven} \n";
  656. // $text .= "极值:{$sumExtremeSize} \n";
  657. // $text .= "尾数:{$tail} \n";
  658. // $text .= "---------------------------\n";
  659. // }
  660. // self::telegram()->sendMessage([
  661. // 'chat_id' => $memberId,
  662. // 'text' => $text,
  663. // ]);
  664. // }else{
  665. // self::telegram()->sendMessage([
  666. // 'chat_id' => $memberId,
  667. // 'text' => "暂无开奖记录",
  668. // ]);
  669. // }
  670. $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id', 'desc')->first();
  671. if ($result) {
  672. if ($result->image) {
  673. // self::telegram()->sendPhoto([
  674. // 'chat_id' => $memberId,
  675. // 'photo' => InputFile::create(url($result->image)),
  676. // ]);
  677. return [
  678. 'chat_id' => $memberId,
  679. 'photo' => InputFile::create(url($result->image)),
  680. ];
  681. } else {
  682. // if($result->combo){
  683. // self::telegram()->sendMessage([
  684. // 'chat_id' => $memberId,
  685. // 'text' => "",
  686. // ]);
  687. // }else{
  688. // self::telegram()->sendMessage([
  689. // 'chat_id' => $memberId,
  690. // 'text' => lang("暂无开奖记录"),
  691. // ]);
  692. // }
  693. return
  694. [
  695. 'chat_id' => $memberId,
  696. 'text' => lang("暂无开奖记录"),
  697. ];
  698. }
  699. }
  700. }
  701. public static function getCombo($winArr)
  702. {
  703. // 组合
  704. $sum = array_sum($winArr);
  705. $combo = [];
  706. $sumSize = self::calculateSumSize($sum); // 总和大小
  707. $combo[] = $sumSize;
  708. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  709. $combo[] = $sumOddEven;
  710. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  711. if ($sumExtremeSize) {
  712. $combo[] = $sumExtremeSize;
  713. }
  714. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  715. if ($sumBaoZi) {
  716. $combo[] = $sumBaoZi;
  717. }
  718. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  719. if ($sumPair) {
  720. $combo[] = $sumPair;
  721. }
  722. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  723. if ($sumStraight) {
  724. $combo[] = $sumStraight;
  725. }
  726. $tail = self::getLastDigit($sum); // 总和尾数
  727. if ($tail == 0 || $tail == 9) {
  728. } else {
  729. $combo[] = '尾' . $tail; // 尾数
  730. }
  731. return implode(' ', $combo);
  732. }
  733. private static function getPlayNowConfig($key, $default)
  734. {
  735. $value = config('services.playnow.' . $key, $default);
  736. return $value === null || $value === '' ? $default : $value;
  737. }
  738. private static function getPlayNowProxyUrl(): string
  739. {
  740. $scheme = self::getPlayNowConfig('proxy.scheme', 'http');
  741. $host = self::getPlayNowConfig('proxy.host', '155.138.141.119');
  742. $port = self::getPlayNowConfig('proxy.port', '3128');
  743. $username = (string)self::getPlayNowConfig('proxy.username', 'proxyuser');
  744. $password = (string)self::getPlayNowConfig('proxy.password', '');
  745. if ($username === '' && $password === '') {
  746. return "{$scheme}://{$host}:{$port}";
  747. }
  748. if ($password === '') {
  749. return "{$scheme}://" . rawurlencode($username) . "@{$host}:{$port}";
  750. }
  751. return "{$scheme}://" . rawurlencode($username) . ':' . rawurlencode($password) . "@{$host}:{$port}";
  752. }
  753. private static function getPlayNowProxyLogContext(): array
  754. {
  755. $host = self::getPlayNowConfig('proxy.host', '155.138.141.119');
  756. $password = self::getPlayNowConfig('proxy.password', '');
  757. return [
  758. 'proxy' => $host === '' ? 'missing' : 'configured',
  759. 'proxy_auth' => $password === '' ? 'missing' : 'configured',
  760. ];
  761. }
  762. private static function fetchPlayNowKenoResult()
  763. {
  764. $url = self::getPlayNowConfig('keno_url', self::PLAYNOW_KENO_URL);
  765. $response = Http::timeout(25)
  766. ->connectTimeout(10)
  767. ->withHeaders([
  768. 'Accept' => 'application/json, text/plain, */*',
  769. 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36',
  770. ])
  771. ->withOptions([
  772. 'proxy' => self::getPlayNowProxyUrl(),
  773. 'curl' => [
  774. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  775. ],
  776. ])
  777. ->get($url);
  778. Log::channel('issue')->info('PlayNow接口响应', [
  779. 'status' => $response->status(),
  780. 'url' => $url,
  781. ] + self::getPlayNowProxyLogContext());
  782. if (!$response->successful()) {
  783. Log::channel('issue')->info('PlayNow接口请求失败', [
  784. 'status' => $response->status(),
  785. 'body' => substr($response->body(), 0, 500),
  786. ]);
  787. return null;
  788. }
  789. return $response->json();
  790. }
  791. private static function convertPlayNowDrawDateToBeijing($drawDate)
  792. {
  793. if (empty($drawDate)) {
  794. return null;
  795. }
  796. $sourceTimezone = new \DateTimeZone(self::getPlayNowConfig('timezone', self::PLAYNOW_SOURCE_TIMEZONE));
  797. $beijingTimezone = new \DateTimeZone(self::BEIJING_TIMEZONE);
  798. return (new \DateTimeImmutable($drawDate, $sourceTimezone))->setTimezone($beijingTimezone);
  799. }
  800. private static function getPlayNowDrawIntervalSeconds(array $result): int
  801. {
  802. $timeSinceDraw = isset($result['timeSinceDraw']) ? (int)$result['timeSinceDraw'] : 0;
  803. $nextDrawSeconds = isset($result['nextKenoDrawTime']) ? (int)$result['nextKenoDrawTime'] : 0;
  804. if ($timeSinceDraw > 0 && $nextDrawSeconds > 0) {
  805. return (int)round($timeSinceDraw / 1000) + $nextDrawSeconds;
  806. }
  807. return $nextDrawSeconds > 0 ? $nextDrawSeconds : self::PLAYNOW_DRAW_INTERVAL_SECONDS;
  808. }
  809. private static function calculatePlayNowPc28(array $numbers): array
  810. {
  811. $sortedNumbers = array_map('intval', $numbers);
  812. sort($sortedNumbers, SORT_NUMERIC);
  813. $sumByIndexes = function (array $indexes) use ($sortedNumbers) {
  814. $total = 0;
  815. foreach ($indexes as $index) {
  816. $total += $sortedNumbers[$index] ?? 0;
  817. }
  818. return $total % 10;
  819. };
  820. $firstNumber = $sumByIndexes([1, 4, 7, 10, 13, 16]);
  821. $secondNumber = $sumByIndexes([2, 5, 8, 11, 14, 17]);
  822. $thirdNumber = $sumByIndexes([3, 6, 9, 12, 15, 18]);
  823. return [
  824. 'numbers' => [$firstNumber, $secondNumber, $thirdNumber],
  825. 'total' => $firstNumber + $secondNumber + $thirdNumber,
  826. 'sorted_numbers' => $sortedNumbers,
  827. ];
  828. }
  829. private static function normalizePlayNowNumbers(array $numbers)
  830. {
  831. if (count($numbers) !== 20) {
  832. return null;
  833. }
  834. $normalized = [];
  835. foreach ($numbers as $number) {
  836. $value = filter_var($number, FILTER_VALIDATE_INT, [
  837. 'options' => [
  838. 'min_range' => 1,
  839. 'max_range' => 80,
  840. ],
  841. ]);
  842. if ($value === false) {
  843. return null;
  844. }
  845. $normalized[] = (int)$value;
  846. }
  847. if (count(array_unique($normalized)) !== 20) {
  848. return null;
  849. }
  850. return $normalized;
  851. }
  852. private static function encodePlayNowKeno($numbers)
  853. {
  854. if (!is_array($numbers)) {
  855. return null;
  856. }
  857. $numbers = self::normalizePlayNowNumbers($numbers);
  858. return $numbers ? json_encode(array_values($numbers)) : null;
  859. }
  860. private static function isSamePlayNowIssueNo($issueNo, int $drawNo): bool
  861. {
  862. $issueNo = trim((string)$issueNo);
  863. return ctype_digit($issueNo) && (int)$issueNo === $drawNo;
  864. }
  865. private static function normalizePlayNowDrawNo($drawNo)
  866. {
  867. $value = filter_var($drawNo, FILTER_VALIDATE_INT, [
  868. 'options' => [
  869. 'min_range' => 1,
  870. ],
  871. ]);
  872. return $value === false ? null : (int)$value;
  873. }
  874. private static function parsePlayNowDrawInfo(array $result)
  875. {
  876. if (!isset($result['draw']) || empty($result['num']) || !is_array($result['num'])) {
  877. return null;
  878. }
  879. $drawNo = self::normalizePlayNowDrawNo($result['draw']);
  880. if (!$drawNo) {
  881. return null;
  882. }
  883. $sourceNumbers = self::normalizePlayNowNumbers($result['num']);
  884. if (!$sourceNumbers) {
  885. return null;
  886. }
  887. try {
  888. $drawDateBeijing = self::convertPlayNowDrawDateToBeijing($result['drawDate'] ?? null);
  889. } catch (\Throwable $exception) {
  890. Log::channel('issue')->info('PlayNow开奖时间解析失败', [
  891. 'draw_date_raw' => $result['drawDate'] ?? '',
  892. 'error' => $exception->getMessage(),
  893. ]);
  894. return null;
  895. }
  896. $nowBeijing = new \DateTimeImmutable('now', new \DateTimeZone(self::BEIJING_TIMEZONE));
  897. $drawIntervalSeconds = self::getPlayNowDrawIntervalSeconds($result);
  898. $startDateTime = $drawDateBeijing ?: $nowBeijing;
  899. $endDateTime = $startDateTime->modify('+' . $drawIntervalSeconds . ' seconds');
  900. $pc28 = self::calculatePlayNowPc28($sourceNumbers);
  901. return [
  902. 'draw_no' => $drawNo,
  903. 'draw_date_raw' => $result['drawDate'] ?? '',
  904. 'draw_date_beijing' => $drawDateBeijing ? $drawDateBeijing->format('Y-m-d H:i:s') : '',
  905. 'next_draw_seconds' => $result['nextKenoDrawTime'] ?? '',
  906. 'draw_interval_seconds' => $drawIntervalSeconds,
  907. 'source_numbers' => $sourceNumbers,
  908. 'sorted_numbers' => $pc28['sorted_numbers'],
  909. 'pc28' => $pc28,
  910. 'winning_numbers' => implode(',', $pc28['numbers']),
  911. 'combo' => self::getCombo($pc28['numbers']),
  912. 'start_time' => $startDateTime->format('Y-m-d H:i:s'),
  913. 'end_time' => $endDateTime->format('Y-m-d H:i:s'),
  914. ];
  915. }
  916. public static function getPlayNowWinningInfo($issueNo): array
  917. {
  918. Log::channel('issue')->info('后台手动开奖获取PlayNow数据', [
  919. 'issue_no' => $issueNo,
  920. ] + self::getPlayNowProxyLogContext());
  921. try {
  922. $result = self::fetchPlayNowKenoResult();
  923. } catch (\Throwable $exception) {
  924. Log::channel('issue')->info('后台手动开奖获取PlayNow数据异常', [
  925. 'issue_no' => $issueNo,
  926. 'error' => $exception->getMessage(),
  927. 'file' => $exception->getFile(),
  928. 'line' => $exception->getLine(),
  929. ]);
  930. return ['code' => self::NOT, 'msg' => '获取开奖信息失败'];
  931. }
  932. $drawInfo = self::parsePlayNowDrawInfo($result ?: []);
  933. if (!$drawInfo) {
  934. Log::channel('issue')->info('后台手动开奖PlayNow返回数据格式异常', [
  935. 'issue_no' => $issueNo,
  936. 'result' => $result,
  937. ]);
  938. return ['code' => self::NOT, 'msg' => '获取开奖信息失败'];
  939. }
  940. Log::channel('issue')->info('后台手动开奖PlayNow计算结果', [
  941. 'issue_no' => $issueNo,
  942. 'draw' => $drawInfo['draw_no'],
  943. 'draw_date_raw' => $drawInfo['draw_date_raw'],
  944. 'draw_date_beijing' => $drawInfo['draw_date_beijing'],
  945. 'source_numbers' => implode(',', $drawInfo['source_numbers']),
  946. 'sorted_numbers' => implode(',', $drawInfo['sorted_numbers']),
  947. 'pc28_numbers' => $drawInfo['winning_numbers'],
  948. 'pc28_total' => $drawInfo['pc28']['total'],
  949. 'combo' => $drawInfo['combo'],
  950. ]);
  951. if (!self::isSamePlayNowIssueNo($issueNo, $drawInfo['draw_no'])) {
  952. Log::channel('issue')->info('后台手动开奖期号不匹配', [
  953. 'issue_no' => $issueNo,
  954. 'playnow_draw' => $drawInfo['draw_no'],
  955. ]);
  956. return ['code' => self::NOT, 'msg' => '未查询到开奖信息,请手动开奖'];
  957. }
  958. return ['code' => self::YES, 'msg' => '获取成功'] + $drawInfo;
  959. }
  960. // 获取最新的开奖数据
  961. public static function getLatestIssue()
  962. {
  963. Log::channel('issue')->info('开始获取最新期号', [
  964. 'source' => 'playnow',
  965. ] + self::getPlayNowProxyLogContext());
  966. try {
  967. $result = self::fetchPlayNowKenoResult();
  968. } catch (\Throwable $exception) {
  969. Log::channel('issue')->info('获取PlayNow最新期号异常', [
  970. 'error' => $exception->getMessage(),
  971. 'file' => $exception->getFile(),
  972. 'line' => $exception->getLine(),
  973. ]);
  974. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  975. }
  976. $drawInfo = self::parsePlayNowDrawInfo($result ?: []);
  977. if (!$drawInfo) {
  978. Log::channel('issue')->info('PlayNow返回数据格式异常', [
  979. 'result' => $result,
  980. ]);
  981. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  982. }
  983. $drawNo = $drawInfo['draw_no'];
  984. $startTime = $drawInfo['start_time'];
  985. $endTime = $drawInfo['end_time'];
  986. $pc28 = $drawInfo['pc28'];
  987. Log::channel('issue')->info('PlayNow最新开奖数据', [
  988. 'draw' => $drawNo,
  989. 'draw_date_raw' => $drawInfo['draw_date_raw'],
  990. 'draw_date_beijing' => $drawInfo['draw_date_beijing'],
  991. 'next_draw_seconds' => $drawInfo['next_draw_seconds'],
  992. 'draw_interval_seconds' => $drawInfo['draw_interval_seconds'],
  993. 'source_numbers' => implode(',', $drawInfo['source_numbers']),
  994. 'sorted_numbers' => implode(',', $drawInfo['sorted_numbers']),
  995. 'pc28_numbers' => $drawInfo['winning_numbers'],
  996. 'pc28_total' => $pc28['total'],
  997. 'next_issue_start_time' => $startTime,
  998. 'next_issue_end_time' => $endTime,
  999. ]);
  1000. $kenoJson = self::encodePlayNowKeno($drawInfo['source_numbers']);
  1001. if ($kenoJson) {
  1002. $drawIssue = self::findOne(['issue_no' => (string)$drawNo]);
  1003. if ($drawIssue && $drawIssue->status == self::model()::STATUS_DRAW && empty($drawIssue->keno)) {
  1004. $drawIssue->keno = $kenoJson;
  1005. $drawIssue->saveQuietly();
  1006. Log::channel('issue')->info('PlayNow源头号码已补写', [
  1007. 'issue_no' => $drawNo,
  1008. 'source_numbers' => implode(',', $drawInfo['source_numbers']),
  1009. 'status' => $drawIssue->status,
  1010. ]);
  1011. }
  1012. } else {
  1013. Log::channel('issue')->info('PlayNow源头号码跳过写入', [
  1014. 'issue_no' => $drawNo,
  1015. 'source_numbers_type' => gettype($drawInfo['source_numbers'] ?? null),
  1016. ]);
  1017. }
  1018. $new = true;
  1019. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  1020. foreach ($oldList as $k => $v) {
  1021. if (self::isSamePlayNowIssueNo($v->issue_no, $drawNo)) {
  1022. $winning_numbers = $drawInfo['winning_numbers'];
  1023. $winArr = $pc28['numbers'];
  1024. $combo = $drawInfo['combo'];
  1025. $key = 'lottery_numbers_' . $v->issue_no;
  1026. if (Cache::add($key, $winning_numbers, 100)) {
  1027. Log::channel('issue')->info('开奖期号: ' . $v->issue_no . ' 开奖号码: ' . $winning_numbers, [
  1028. 'source' => 'playnow',
  1029. 'draw' => $drawNo,
  1030. 'combo' => $combo,
  1031. 'pc28_total' => $pc28['total'],
  1032. ]);
  1033. self::lotteryDraw($v->id, $winning_numbers, $combo, '', $drawInfo['source_numbers']);
  1034. $new = false;
  1035. } else {
  1036. Log::channel('issue')->info('开奖期号已处理,跳过重复开奖', [
  1037. 'issue_no' => $v->issue_no,
  1038. 'winning_numbers' => $winning_numbers,
  1039. ]);
  1040. }
  1041. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  1042. //更新游戏开关的切换
  1043. if ($pc28Switch == 0) Config::setPc28Switch();
  1044. }
  1045. }
  1046. // sleep(5); // 等待开奖完成
  1047. if ($new) {
  1048. $new_issue_no = $drawNo + 1; // 新期号
  1049. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  1050. // 不存在
  1051. if (!$newInfo) {
  1052. Log::channel('issue')->info('新增期号: ' . $new_issue_no, [
  1053. 'source' => 'playnow',
  1054. 'start_time' => $startTime,
  1055. 'end_time' => $endTime,
  1056. ]);
  1057. $res = self::submit([
  1058. 'issue_no' => $new_issue_no,
  1059. 'status' => self::model()::STATUS_DRAFT,
  1060. 'start_time' => $startTime,
  1061. 'end_time' => $endTime,
  1062. ]);
  1063. Prediction::prediction($new_issue_no);
  1064. $id = $res['key'] ?? 0;
  1065. if ($id) {
  1066. self::betting($id); // 开始下注
  1067. }
  1068. Cache::set('new_issue_no', $new_issue_no, 10); // 缓存
  1069. } else {
  1070. Log::channel('issue')->info('期号已存在,无需新增', [
  1071. 'issue_no' => $new_issue_no,
  1072. 'status' => $newInfo->status,
  1073. 'start_time' => $newInfo->start_time,
  1074. 'end_time' => $newInfo->end_time,
  1075. ]);
  1076. }
  1077. }
  1078. return $result;
  1079. }
  1080. // 获取最新的开奖数据
  1081. public static function getLatestIssue2()
  1082. {
  1083. $url = "https://ydpc28.co/api/pc28/list";
  1084. $result = file_get_contents($url);
  1085. $result = json_decode($result, true);
  1086. if ($result['errorCode'] != 0) {
  1087. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  1088. }
  1089. $nextDrawInfo = $result['data']['nextDrawInfo'];
  1090. $startTime = $nextDrawInfo['currentBJTime'];
  1091. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  1092. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  1093. // }else{
  1094. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  1095. // }
  1096. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  1097. $new = true;
  1098. $list = $result['data']['list'];
  1099. $listKey = [];
  1100. foreach ($list as $k => $v) {
  1101. $listKey[$v['lotNumber']] = $v;
  1102. }
  1103. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  1104. foreach ($oldList as $k => $v) {
  1105. if (isset($listKey[$v->issue_no])) {
  1106. $issue = $listKey[$v->issue_no];
  1107. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  1108. $winArr = array_map('intval', explode(',', $winning_numbers));
  1109. // 组合
  1110. $sum = array_sum($winArr);
  1111. $combo = [];
  1112. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  1113. $combo[] = $sumOddEven;
  1114. $sumSize = self::calculateSumSize($sum); // 总和大小
  1115. $combo[] = $sumSize;
  1116. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  1117. if ($sumExtremeSize) {
  1118. $combo[] = $sumExtremeSize;
  1119. }
  1120. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  1121. if ($sumBaoZi) {
  1122. $combo[] = $sumBaoZi;
  1123. }
  1124. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  1125. if ($sumPair) {
  1126. $combo[] = $sumPair;
  1127. }
  1128. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  1129. if ($sumStraight) {
  1130. $combo[] = $sumStraight;
  1131. }
  1132. $tail = self::getLastDigit($sum); // 总和尾数
  1133. if ($tail == 0 || $tail == 9) {
  1134. } else {
  1135. $combo[] = '尾' . $tail; // 尾数
  1136. }
  1137. $combo = implode(' ', $combo);
  1138. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  1139. }
  1140. }
  1141. return $result;
  1142. }
  1143. // 封盘倒数
  1144. public static function syncCountdownIssue()
  1145. {
  1146. $info = self::model()::where('status', self::model()::STATUS_BETTING)->orderBy('end_time', 'asc')->first();
  1147. if ($info) {
  1148. $now_date = date('Y-m-d H:i:s', time() + IssueService::COUNTDOWN_TO_CLOSING_THE_MARKET);
  1149. if ($info['end_time'] < $now_date) {
  1150. $replyInfo = KeyboardService::findOne(['button' => '封盘倒数']);
  1151. if ($replyInfo) {
  1152. $text = $replyInfo->reply;
  1153. $buttons = json_decode($replyInfo->buttons, true);
  1154. $image = $replyInfo->image;
  1155. if ($image) {
  1156. $image = url($image);
  1157. }
  1158. if (Cache::has('issue_countdown_' . $info->issue_no)) {
  1159. } else {
  1160. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  1161. if ($pc28Switch == 0) {
  1162. self::asyncBettingGroupNotice($text, $buttons, $image);
  1163. Cache::put('issue_countdown_' . $info->issue_no, true, 60); // 缓存50秒,防止多次发送
  1164. }
  1165. }
  1166. }
  1167. }
  1168. }
  1169. }
  1170. // 停止下注
  1171. public static function syncCloseIssue()
  1172. {
  1173. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  1174. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  1175. foreach ($list as $k => $v) {
  1176. if ($v['end_time'] < $now_date) {
  1177. self::closeBetting($v->id);
  1178. }
  1179. }
  1180. }
  1181. // 生成开奖图片
  1182. public static function lotteryImage($issue_no)
  1183. {
  1184. $list = self::model()::where('issue_no', '<=', $issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no', 'desc')->take(20)->get();
  1185. $records = $list->toArray();
  1186. foreach ($records as $k => $v) {
  1187. $winning_numbers = explode(',', $v['winning_numbers']);
  1188. $v['winning_numbers'] = $winning_numbers;
  1189. // 组合
  1190. $sum = array_sum($winning_numbers);
  1191. $v['sum'] = $sum;
  1192. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  1193. $sumSize = self::calculateSumSize($sum); // 总和大小
  1194. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  1195. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  1196. if (!$sumExtremeSize) {
  1197. $sumExtremeSize = '-';
  1198. }
  1199. $v['extreme'] = $sumExtremeSize;
  1200. $tail = self::getLastDigit($sum); // 总和尾数
  1201. if ($tail === 0 || $tail === 9) {
  1202. $tailStr = '-';
  1203. } else {
  1204. $tailStr = '尾' . $tail;
  1205. }
  1206. $v['tail'] = $tailStr;
  1207. $records[$k] = $v;
  1208. }
  1209. $service = new LotteryImageService();
  1210. $url = $service->generate($records);
  1211. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  1212. return $url;
  1213. }
  1214. // 发送开奖图片
  1215. public static function sendLotteryImage($chatId, $issueNo)
  1216. {
  1217. $recordImage = self::lotteryImage($issueNo);
  1218. self::sendMessage($chatId, '', [], url($recordImage));
  1219. // dispatch(new SendTelegramMessageJob('', [], url($recordImage)));
  1220. }
  1221. }