IssueService.php 49 KB

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