IssueService.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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)
  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. DB::beginTransaction();
  314. try {
  315. $info->status = self::model()::STATUS_DRAW;
  316. $info->winning_numbers = $winning_numbers;
  317. $info->combo = $combo;
  318. $size = in_array("大", $awards);
  319. $size = $size ? "大" : "小";
  320. $oddOrEven = in_array("双", $awards);
  321. $oddOrEven = $oddOrEven ? "双" : "单";
  322. Prediction::result($info->issue_no, $size, $oddOrEven, $info->winning_numbers);
  323. Cao::updateData($awards);
  324. CaoHistory::updateData($awards);
  325. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  326. $replyInfo = KeyboardService::findOne(['button' => '本期开奖']);
  327. if ($replyInfo) {
  328. $text = $replyInfo->reply;
  329. $text .= "\n";
  330. $text .= $info->issue_no . ": " . implode('+', explode(',', $winning_numbers)) . "=" . array_sum($winArr) . " " . $combo;
  331. $buttons = json_decode($replyInfo->buttons, true);
  332. $image = $replyInfo->image;
  333. if ($image) {
  334. $image = url($image);
  335. }
  336. if (empty($buttons)) {
  337. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  338. $buttons[] = [['text' => lang('✅唯一财务'), 'callback_data' => "", 'url' => "https://t.me/{$serviceAccount}"]];
  339. }
  340. // self::bettingGroupNotice($text, $buttons, $image, true);
  341. if ($pc28Switch == 0) SendTelegramGroupMessageJob::dispatch($text, $buttons, $image, true);
  342. }
  343. $recordImage = self::lotteryImage($info->issue_no);
  344. if ($recordImage) {
  345. // self::bettingGroupNotice('', [], url($recordImage));
  346. if ($pc28Switch == 0) SendTelegramGroupMessageJob::dispatch('', [], url($recordImage), false);
  347. }
  348. $info->image = $recordImage;
  349. $info->save();
  350. BetService::betSettled($info->issue_no, $awards);
  351. DB::commit();
  352. } catch (\Exception $e) {
  353. DB::rollBack();
  354. $message = "开奖失败:\n{$info->issue_no}\n";
  355. $message .= $e->getFile() . ':' . $e->getLine();
  356. $message .= "\n";
  357. $message .= $e->getMessage() . $winning_numbers;
  358. Log::error($message);
  359. return ['code' => self::NOT, 'msg' => '开奖失败', 'error' => $e->getMessage()];
  360. }
  361. return ['code' => self::YES, 'msg' => '开奖成功'];
  362. }
  363. // 虚拟开奖
  364. public
  365. static function fakeLotteryDraw($issue_no, $awards)
  366. {
  367. $fake_bet_list = Cache::get('fake_bet_' . $issue_no, []);
  368. $text = "";
  369. foreach ($fake_bet_list as $k => $v) {
  370. $lastStr = self::getLastChar($v['first_name'], 1);
  371. if (in_array($v['keywords'], $awards)) {
  372. $amount = (float)$v['amount'];
  373. $odds = (float)$v['odds'];
  374. $profit = $amount * $odds;
  375. if ($profit > 880000) {
  376. $profit = 880000; // 单注最高奖金880000
  377. }
  378. $item['profit'] = $profit;
  379. $yl = $profit - $amount;
  380. if ($k + 1 <= 30) {
  381. $text .= "私聊下注 【******" . $lastStr . "】 {$yl}\n";
  382. }
  383. } else {
  384. if ($k + 1 <= 30) {
  385. $text .= "私聊下注 【******" . $lastStr . "】 -{$v['amount']}\n";
  386. }
  387. }
  388. }
  389. return $text;
  390. }
  391. /**
  392. * @description: 获取中奖的奖项
  393. * @param {*} $winning_numbers
  394. * @return {*}
  395. */
  396. public
  397. static function award($winning_numbers)
  398. {
  399. $result = [];
  400. // 组合
  401. $sum = array_sum($winning_numbers);
  402. $section = self::getSection($sum); // 总和段位
  403. $result[] = $section;
  404. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  405. $result[] = $sumOddEven;
  406. $sumSize = self::calculateSumSize($sum); // 总和大小
  407. $result[] = $sumSize;
  408. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  409. if ($sumExtremeSize) {
  410. $result[] = $sumExtremeSize;
  411. }
  412. $sumCao = $sum . '操'; // 总和数字
  413. $result[] = $sumCao;
  414. $sumCombo = $sumSize . $sumOddEven; // 总和大小单双组合
  415. $result[] = $sumCombo;
  416. $sumBaoZi = self::isBaoZi($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 豹子
  417. if ($sumBaoZi) {
  418. $result[] = $sumBaoZi;
  419. }
  420. $sumPair = self::isPair($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 对子
  421. if ($sumPair) {
  422. $result[] = $sumPair;
  423. }
  424. $sumStraight = self::isStraight($winning_numbers[0], $winning_numbers[1], $winning_numbers[2]); // 顺子
  425. if ($sumStraight) {
  426. $result[] = $sumStraight;
  427. }
  428. $tail = self::getLastDigit($sum); // 总和尾数
  429. if (!in_array($tail, [0, 9])) {
  430. $result[] = $tail . '尾'; // 尾数
  431. $tailOddEven = self::calculateOddEven($tail); // 尾数单双
  432. $result[] = '尾' . $tailOddEven;
  433. $tailSize = self::calculateOneSize($tail); // 尾数大小
  434. $result[] = '尾' . $tailSize;
  435. $tailCombo = '尾' . $tailSize . $tailOddEven; // 尾数大小单双组合
  436. $result[] = $tailCombo;
  437. } else {
  438. $result[] = $tail . '尾'; // 尾数
  439. }
  440. $numA = $winning_numbers[0]; // A球
  441. $result[] = $numA . 'A';
  442. $numAOddEven = self::calculateOddEven($numA); // A球单双
  443. $result[] = 'A' . $numAOddEven;
  444. $numASize = self::calculateOneSize($numA); // A球大小
  445. $result[] = 'A' . $numASize;
  446. $result[] = 'A' . $numASize . $numAOddEven; // A球大小单双组合
  447. $numB = $winning_numbers[1]; // B球
  448. $result[] = $numB . 'B';
  449. $numBOddEven = self::calculateOddEven($numB); // B球
  450. $result[] = 'B' . $numBOddEven;
  451. $numBSize = self::calculateOneSize($numB); // B球大小
  452. $result[] = 'B' . $numBSize;
  453. $result[] = 'B' . $numBSize . $numBOddEven; // B球大小单双组合
  454. $numC = $winning_numbers[2];
  455. $result[] = $numC . 'C';
  456. $numCOddEven = self::calculateOddEven($numC); // C球单双
  457. $result[] = 'C' . $numCOddEven;
  458. $numCSize = self::calculateOneSize($numC); // C球大小
  459. $result[] = 'C' . $numCSize;
  460. $result[] = 'C' . $numCSize . $numCOddEven; // C球大小单双组合
  461. return $result;
  462. }
  463. /**
  464. * @description: 算单双
  465. * @param {*} $number
  466. * @return {*}
  467. */
  468. public
  469. static function calculateOddEven($number)
  470. {
  471. if ($number & 1) {
  472. return GameplayRuleEnum::SINGLE;
  473. } else {
  474. return GameplayRuleEnum::DOUBLE;
  475. }
  476. }
  477. /**
  478. * @description: 总和大小
  479. * @param {*} $number
  480. * @return {*}
  481. */
  482. public
  483. static function calculateSumSize($number)
  484. {
  485. if ($number >= GameplayRuleEnum::SUM_BIG) {
  486. return GameplayRuleEnum::BIG;
  487. }
  488. if ($number <= GameplayRuleEnum::SUM_SMALL) {
  489. return GameplayRuleEnum::SMALL;
  490. }
  491. }
  492. /**
  493. * @description: 总和极值
  494. * @param {*} $number
  495. * @return {*}
  496. */
  497. public
  498. 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
  517. static function isBaoZi(int $a, int $b, int $c)
  518. {
  519. $result = '';
  520. if ($a === $b && $b === $c) {
  521. $result = GameplayRuleEnum::BAO_ZI;
  522. }
  523. return $result;
  524. }
  525. /**
  526. * @description: 对子
  527. * @param {int} $a
  528. * @param {int} $b
  529. * @param {int} $c
  530. * @return {*}
  531. */
  532. public
  533. static function isPair($a, $b, $c)
  534. {
  535. $result = '';
  536. // 确保输入都是个位数
  537. if (!is_numeric($a) || !is_numeric($b) || !is_numeric($c) ||
  538. $a < 0 || $a > 9 || $b < 0 || $b > 9 || $c < 0 || $c > 9) {
  539. return ''; // 或者抛出异常
  540. }
  541. if (($a == $b && $a != $c) ||
  542. ($a == $c && $a != $b) ||
  543. ($b == $c && $b != $a)) {
  544. $result = GameplayRuleEnum::PAIRS;
  545. }
  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 isStraight($a, $b, $c)
  558. {
  559. $result = '';
  560. // 确保输入都是个位数(0-9)
  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. // 去重(顺子必须三个不同数字)
  566. if ($a == $b || $a == $c || $b == $c) {
  567. return '';
  568. }
  569. // 检查是否是完全升序或完全降序的连续数字
  570. $numbers = [$a, $b, $c];
  571. sort($numbers); // 排序后检查是否是 x, x+1, x+2
  572. list($x, $y, $z) = $numbers;
  573. // 情况1:升序连续(1,2,3)
  574. $isAscending = ($x + 1 == $y) && ($y + 1 == $z);
  575. // 情况2:降序连续(3,2,1)
  576. $isDescending = ($z + 1 == $y) && ($y + 1 == $x);
  577. if ($isAscending || $isDescending) {
  578. $result = GameplayRuleEnum::STRAIGHT;
  579. }
  580. return $result;
  581. }
  582. /**
  583. * 获取数字的尾数
  584. * @param int $number 输入数字
  585. * @return int 尾数
  586. */
  587. public
  588. static function getLastDigit($number)
  589. {
  590. // 确保输入是整数
  591. $number = (int)$number;
  592. // 取绝对值,处理负数情况
  593. $number = abs($number);
  594. // 取模10得到尾数
  595. return $number % 10;
  596. }
  597. /**
  598. * @description: 尾大小
  599. * @param {*} $number
  600. * @return {*}
  601. */
  602. public
  603. static function calculateOneSize($number)
  604. {
  605. if ($number >= GameplayRuleEnum::ONE_BIG) {
  606. return GameplayRuleEnum::BIG;
  607. }
  608. if ($number <= GameplayRuleEnum::ONE_SMALL) {
  609. return GameplayRuleEnum::SMALL;
  610. }
  611. }
  612. /**
  613. * @description: 获取段位
  614. * @param {*} $number
  615. * @return {*}
  616. */
  617. public
  618. static function getSection($number)
  619. {
  620. $result = '';
  621. if ($number >= GameplayRuleEnum::SECTION_1[0] && $number <= GameplayRuleEnum::SECTION_1[1]) {
  622. $result = GameplayRuleEnum::ONE;
  623. } elseif ($number >= GameplayRuleEnum::SECTION_2[0] && $number <= GameplayRuleEnum::SECTION_2[1]) {
  624. $result = GameplayRuleEnum::TWO;
  625. } elseif ($number >= GameplayRuleEnum::SECTION_3[0] && $number <= GameplayRuleEnum::SECTION_3[1]) {
  626. $result = GameplayRuleEnum::THREE;
  627. } elseif ($number >= GameplayRuleEnum::SECTION_4[0] && $number <= GameplayRuleEnum::SECTION_4[1]) {
  628. $result = GameplayRuleEnum::FOUR;
  629. }
  630. return $result; // 不在任何段中
  631. }
  632. /**
  633. * @description: 近期开奖记录
  634. * @return {*}
  635. */
  636. public
  637. static function currentLotteryResults($memberId)
  638. {
  639. // $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id','desc')->take(16)->get();
  640. // $text = "📅 近期开奖记录\n";
  641. // $text .= "====================\n";
  642. // if($result){
  643. // foreach($result as $k => $v){
  644. // $winArr = explode(',',$v->winning_numbers);
  645. // // 组合
  646. // $sum = array_sum($winArr);
  647. // $combo = [];
  648. // $sumOddEven = self::calculateOddEven($sum); // 总和单双
  649. // $sumSize = self::calculateSumSize($sum); // 总和大小
  650. // $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  651. // if(empty($sumExtremeSize)){
  652. // $sumExtremeSize = "-";
  653. // }
  654. // $tail = self::getLastDigit($sum); // 总和尾数
  655. // if($tail == 0){
  656. // $tail = '-'; // 尾数
  657. // }else{
  658. // $tail = '尾'.$tail; // 尾数
  659. // }
  660. // $text .= "回合:{$v->issue_no}期 \n";
  661. // $text .= "结果:".implode('+',explode(',',$v->winning_numbers))."=".array_sum(explode(',',$v->winning_numbers))." \n";
  662. // $text .= "组合:{$sumSize} {$sumOddEven} \n";
  663. // $text .= "极值:{$sumExtremeSize} \n";
  664. // $text .= "尾数:{$tail} \n";
  665. // $text .= "---------------------------\n";
  666. // }
  667. // self::telegram()->sendMessage([
  668. // 'chat_id' => $memberId,
  669. // 'text' => $text,
  670. // ]);
  671. // }else{
  672. // self::telegram()->sendMessage([
  673. // 'chat_id' => $memberId,
  674. // 'text' => "暂无开奖记录",
  675. // ]);
  676. // }
  677. $result = self::model()::where('status', self::model()::STATUS_DRAW)->orderBy('id', 'desc')->first();
  678. if ($result) {
  679. if ($result->image) {
  680. // self::telegram()->sendPhoto([
  681. // 'chat_id' => $memberId,
  682. // 'photo' => InputFile::create(url($result->image)),
  683. // ]);
  684. return [
  685. 'chat_id' => $memberId,
  686. 'photo' => InputFile::create(url($result->image)),
  687. ];
  688. } else {
  689. // if($result->combo){
  690. // self::telegram()->sendMessage([
  691. // 'chat_id' => $memberId,
  692. // 'text' => "",
  693. // ]);
  694. // }else{
  695. // self::telegram()->sendMessage([
  696. // 'chat_id' => $memberId,
  697. // 'text' => lang("暂无开奖记录"),
  698. // ]);
  699. // }
  700. return
  701. [
  702. 'chat_id' => $memberId,
  703. 'text' => lang("暂无开奖记录"),
  704. ];
  705. }
  706. }
  707. }
  708. public
  709. static function getCombo($winArr)
  710. {
  711. // 组合
  712. $sum = array_sum($winArr);
  713. $combo = [];
  714. $sumSize = self::calculateSumSize($sum); // 总和大小
  715. $combo[] = $sumSize;
  716. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  717. $combo[] = $sumOddEven;
  718. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  719. if ($sumExtremeSize) {
  720. $combo[] = $sumExtremeSize;
  721. }
  722. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  723. if ($sumBaoZi) {
  724. $combo[] = $sumBaoZi;
  725. }
  726. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  727. if ($sumPair) {
  728. $combo[] = $sumPair;
  729. }
  730. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  731. if ($sumStraight) {
  732. $combo[] = $sumStraight;
  733. }
  734. $tail = self::getLastDigit($sum); // 总和尾数
  735. if ($tail == 0 || $tail == 9) {
  736. } else {
  737. $combo[] = '尾' . $tail; // 尾数
  738. }
  739. return implode(' ', $combo);
  740. }
  741. private static function getPlayNowConfig($key, $default)
  742. {
  743. $value = config('services.playnow.' . $key, $default);
  744. return $value === null || $value === '' ? $default : $value;
  745. }
  746. private static function getPlayNowProxyUrl(): string
  747. {
  748. $scheme = self::getPlayNowConfig('proxy.scheme', 'http');
  749. $host = self::getPlayNowConfig('proxy.host', '155.138.141.119');
  750. $port = self::getPlayNowConfig('proxy.port', '3128');
  751. $username = (string)self::getPlayNowConfig('proxy.username', 'proxyuser');
  752. $password = (string)self::getPlayNowConfig('proxy.password', '');
  753. if ($username === '' && $password === '') {
  754. return "{$scheme}://{$host}:{$port}";
  755. }
  756. if ($password === '') {
  757. return "{$scheme}://" . rawurlencode($username) . "@{$host}:{$port}";
  758. }
  759. return "{$scheme}://" . rawurlencode($username) . ':' . rawurlencode($password) . "@{$host}:{$port}";
  760. }
  761. private static function getPlayNowProxyLogContext(): array
  762. {
  763. $host = self::getPlayNowConfig('proxy.host', '155.138.141.119');
  764. $password = self::getPlayNowConfig('proxy.password', '');
  765. return [
  766. 'proxy' => $host === '' ? 'missing' : 'configured',
  767. 'proxy_auth' => $password === '' ? 'missing' : 'configured',
  768. ];
  769. }
  770. private static function fetchPlayNowKenoResult()
  771. {
  772. $url = self::getPlayNowConfig('keno_url', self::PLAYNOW_KENO_URL);
  773. $response = Http::timeout(25)
  774. ->connectTimeout(10)
  775. ->withHeaders([
  776. 'Accept' => 'application/json, text/plain, */*',
  777. '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',
  778. ])
  779. ->withOptions([
  780. 'proxy' => self::getPlayNowProxyUrl(),
  781. 'curl' => [
  782. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  783. ],
  784. ])
  785. ->get($url);
  786. Log::channel('issue')->info('PlayNow接口响应', [
  787. 'status' => $response->status(),
  788. 'url' => $url,
  789. ] + self::getPlayNowProxyLogContext());
  790. if (!$response->successful()) {
  791. Log::channel('issue')->info('PlayNow接口请求失败', [
  792. 'status' => $response->status(),
  793. 'body' => substr($response->body(), 0, 500),
  794. ]);
  795. return null;
  796. }
  797. return $response->json();
  798. }
  799. private static function convertPlayNowDrawDateToBeijing($drawDate)
  800. {
  801. if (empty($drawDate)) {
  802. return null;
  803. }
  804. $sourceTimezone = new \DateTimeZone(self::getPlayNowConfig('timezone', self::PLAYNOW_SOURCE_TIMEZONE));
  805. $beijingTimezone = new \DateTimeZone(self::BEIJING_TIMEZONE);
  806. return (new \DateTimeImmutable($drawDate, $sourceTimezone))->setTimezone($beijingTimezone);
  807. }
  808. private static function getPlayNowDrawIntervalSeconds(array $result): int
  809. {
  810. $timeSinceDraw = isset($result['timeSinceDraw']) ? (int)$result['timeSinceDraw'] : 0;
  811. $nextDrawSeconds = isset($result['nextKenoDrawTime']) ? (int)$result['nextKenoDrawTime'] : 0;
  812. if ($timeSinceDraw > 0 && $nextDrawSeconds > 0) {
  813. return (int)round($timeSinceDraw / 1000) + $nextDrawSeconds;
  814. }
  815. return $nextDrawSeconds > 0 ? $nextDrawSeconds : self::PLAYNOW_DRAW_INTERVAL_SECONDS;
  816. }
  817. private static function calculatePlayNowPc28(array $numbers): array
  818. {
  819. $sortedNumbers = array_map('intval', $numbers);
  820. sort($sortedNumbers, SORT_NUMERIC);
  821. $sumByIndexes = function (array $indexes) use ($sortedNumbers) {
  822. $total = 0;
  823. foreach ($indexes as $index) {
  824. $total += $sortedNumbers[$index] ?? 0;
  825. }
  826. return $total % 10;
  827. };
  828. $firstNumber = $sumByIndexes([1, 4, 7, 10, 13, 16]);
  829. $secondNumber = $sumByIndexes([2, 5, 8, 11, 14, 17]);
  830. $thirdNumber = $sumByIndexes([3, 6, 9, 12, 15, 18]);
  831. return [
  832. 'numbers' => [$firstNumber, $secondNumber, $thirdNumber],
  833. 'total' => $firstNumber + $secondNumber + $thirdNumber,
  834. 'sorted_numbers' => $sortedNumbers,
  835. ];
  836. }
  837. private static function normalizePlayNowNumbers(array $numbers)
  838. {
  839. if (count($numbers) !== 20) {
  840. return null;
  841. }
  842. $normalized = [];
  843. foreach ($numbers as $number) {
  844. $value = filter_var($number, FILTER_VALIDATE_INT, [
  845. 'options' => [
  846. 'min_range' => 1,
  847. 'max_range' => 80,
  848. ],
  849. ]);
  850. if ($value === false) {
  851. return null;
  852. }
  853. $normalized[] = (int)$value;
  854. }
  855. if (count(array_unique($normalized)) !== 20) {
  856. return null;
  857. }
  858. return $normalized;
  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. $new = true;
  1001. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  1002. foreach ($oldList as $k => $v) {
  1003. if (self::isSamePlayNowIssueNo($v->issue_no, $drawNo)) {
  1004. $winning_numbers = $drawInfo['winning_numbers'];
  1005. $winArr = $pc28['numbers'];
  1006. $combo = $drawInfo['combo'];
  1007. $key = 'lottery_numbers_' . $v->issue_no;
  1008. if (Cache::add($key, $winning_numbers, 100)) {
  1009. Log::channel('issue')->info('开奖期号: ' . $v->issue_no . ' 开奖号码: ' . $winning_numbers, [
  1010. 'source' => 'playnow',
  1011. 'draw' => $drawNo,
  1012. 'combo' => $combo,
  1013. 'pc28_total' => $pc28['total'],
  1014. ]);
  1015. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  1016. $new = false;
  1017. } else {
  1018. Log::channel('issue')->info('开奖期号已处理,跳过重复开奖', [
  1019. 'issue_no' => $v->issue_no,
  1020. 'winning_numbers' => $winning_numbers,
  1021. ]);
  1022. }
  1023. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  1024. //更新游戏开关的切换
  1025. if ($pc28Switch == 0) Config::setPc28Switch();
  1026. }
  1027. }
  1028. // sleep(5); // 等待开奖完成
  1029. if ($new) {
  1030. $new_issue_no = $drawNo + 1; // 新期号
  1031. $newInfo = self::findOne(['issue_no' => $new_issue_no]); // 找新的期号
  1032. // 不存在
  1033. if (!$newInfo) {
  1034. Log::channel('issue')->info('新增期号: ' . $new_issue_no, [
  1035. 'source' => 'playnow',
  1036. 'start_time' => $startTime,
  1037. 'end_time' => $endTime,
  1038. ]);
  1039. $res = self::submit([
  1040. 'issue_no' => $new_issue_no,
  1041. 'status' => self::model()::STATUS_DRAFT,
  1042. 'start_time' => $startTime,
  1043. 'end_time' => $endTime,
  1044. ]);
  1045. Prediction::prediction($new_issue_no);
  1046. $id = $res['key'] ?? 0;
  1047. if ($id) {
  1048. self::betting($id); // 开始下注
  1049. }
  1050. Cache::set('new_issue_no', $new_issue_no, 10); // 缓存
  1051. } else {
  1052. Log::channel('issue')->info('期号已存在,无需新增', [
  1053. 'issue_no' => $new_issue_no,
  1054. 'status' => $newInfo->status,
  1055. 'start_time' => $newInfo->start_time,
  1056. 'end_time' => $newInfo->end_time,
  1057. ]);
  1058. }
  1059. }
  1060. return $result;
  1061. }
  1062. // 获取最新的开奖数据
  1063. public
  1064. static function getLatestIssue2()
  1065. {
  1066. $url = "https://ydpc28.co/api/pc28/list";
  1067. $result = file_get_contents($url);
  1068. $result = json_decode($result, true);
  1069. if ($result['errorCode'] != 0) {
  1070. return ['code' => self::NOT, 'msg' => '获取最新期号失败'];
  1071. }
  1072. $nextDrawInfo = $result['data']['nextDrawInfo'];
  1073. $startTime = $nextDrawInfo['currentBJTime'];
  1074. // if($nextDrawInfo['nextDrawTime'] >= date('H:i:s')) {
  1075. // $endTime = date('Y-m-d').' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  1076. // }else{
  1077. // $endTime = date('Y-m-d',strtotime('+1 day')).' '.$nextDrawInfo['nextDrawTime']; // 下一期的截止时间
  1078. // }
  1079. $endTime = date('Y-m-d H:i:s', strtotime($startTime) + 210);
  1080. $new = true;
  1081. $list = $result['data']['list'];
  1082. $listKey = [];
  1083. foreach ($list as $k => $v) {
  1084. $listKey[$v['lotNumber']] = $v;
  1085. }
  1086. $oldList = self::findAll(['status' => self::model()::STATUS_CLOSE]); // 获取所有封盘的期号
  1087. foreach ($oldList as $k => $v) {
  1088. if (isset($listKey[$v->issue_no])) {
  1089. $issue = $listKey[$v->issue_no];
  1090. $winning_numbers = implode(',', str_split((string)$issue['openCode']));
  1091. $winArr = array_map('intval', explode(',', $winning_numbers));
  1092. // 组合
  1093. $sum = array_sum($winArr);
  1094. $combo = [];
  1095. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  1096. $combo[] = $sumOddEven;
  1097. $sumSize = self::calculateSumSize($sum); // 总和大小
  1098. $combo[] = $sumSize;
  1099. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  1100. if ($sumExtremeSize) {
  1101. $combo[] = $sumExtremeSize;
  1102. }
  1103. $sumBaoZi = self::isBaoZi($winArr[0], $winArr[1], $winArr[2]); // 豹子
  1104. if ($sumBaoZi) {
  1105. $combo[] = $sumBaoZi;
  1106. }
  1107. $sumPair = self::isPair($winArr[0], $winArr[1], $winArr[2]); // 对子
  1108. if ($sumPair) {
  1109. $combo[] = $sumPair;
  1110. }
  1111. $sumStraight = self::isStraight($winArr[0], $winArr[1], $winArr[2]); // 顺子
  1112. if ($sumStraight) {
  1113. $combo[] = $sumStraight;
  1114. }
  1115. $tail = self::getLastDigit($sum); // 总和尾数
  1116. if ($tail == 0 || $tail == 9) {
  1117. } else {
  1118. $combo[] = '尾' . $tail; // 尾数
  1119. }
  1120. $combo = implode(' ', $combo);
  1121. self::lotteryDraw($v->id, $winning_numbers, $combo, '');
  1122. }
  1123. }
  1124. return $result;
  1125. }
  1126. // 封盘倒数
  1127. public
  1128. static function syncCountdownIssue()
  1129. {
  1130. $info = self::model()::where('status', self::model()::STATUS_BETTING)->orderBy('end_time', 'asc')->first();
  1131. if ($info) {
  1132. $now_date = date('Y-m-d H:i:s', time() + IssueService::COUNTDOWN_TO_CLOSING_THE_MARKET);
  1133. if ($info['end_time'] < $now_date) {
  1134. $replyInfo = KeyboardService::findOne(['button' => '封盘倒数']);
  1135. if ($replyInfo) {
  1136. $text = $replyInfo->reply;
  1137. $buttons = json_decode($replyInfo->buttons, true);
  1138. $image = $replyInfo->image;
  1139. if ($image) {
  1140. $image = url($image);
  1141. }
  1142. if (Cache::has('issue_countdown_' . $info->issue_no)) {
  1143. } else {
  1144. $pc28Switch = Config::where('field', 'pc28_switch')->first()->val;
  1145. if ($pc28Switch == 0) {
  1146. self::asyncBettingGroupNotice($text, $buttons, $image);
  1147. Cache::put('issue_countdown_' . $info->issue_no, true, 60); // 缓存50秒,防止多次发送
  1148. }
  1149. }
  1150. }
  1151. }
  1152. }
  1153. }
  1154. // 停止下注
  1155. public
  1156. static function syncCloseIssue()
  1157. {
  1158. $now_date = date('Y-m-d H:i:s', time() + 30); // 提前30秒
  1159. $list = self::findAll(['status' => self::model()::STATUS_BETTING]);
  1160. foreach ($list as $k => $v) {
  1161. if ($v['end_time'] < $now_date) {
  1162. self::closeBetting($v->id);
  1163. }
  1164. }
  1165. }
  1166. // 生成开奖图片
  1167. public
  1168. static function lotteryImage($issue_no)
  1169. {
  1170. $list = self::model()::where('issue_no', '<=', $issue_no)->where(self::getWhere(['status' => self::model()::STATUS_DRAW]))->orderBy('issue_no', 'desc')->take(20)->get();
  1171. $records = $list->toArray();
  1172. foreach ($records as $k => $v) {
  1173. $winning_numbers = explode(',', $v['winning_numbers']);
  1174. $v['winning_numbers'] = $winning_numbers;
  1175. // 组合
  1176. $sum = array_sum($winning_numbers);
  1177. $v['sum'] = $sum;
  1178. $sumOddEven = self::calculateOddEven($sum); // 总和单双
  1179. $sumSize = self::calculateSumSize($sum); // 总和大小
  1180. $v['combo'] = $sumSize . ' ' . $sumOddEven;
  1181. $sumExtremeSize = self::calculateSumExtremeSize($sum); // 总和极值
  1182. if (!$sumExtremeSize) {
  1183. $sumExtremeSize = '-';
  1184. }
  1185. $v['extreme'] = $sumExtremeSize;
  1186. $tail = self::getLastDigit($sum); // 总和尾数
  1187. if ($tail === 0 || $tail === 9) {
  1188. $tailStr = '-';
  1189. } else {
  1190. $tailStr = '尾' . $tail;
  1191. }
  1192. $v['tail'] = $tailStr;
  1193. $records[$k] = $v;
  1194. }
  1195. $service = new LotteryImageService();
  1196. $url = $service->generate($records);
  1197. self::model()::where('issue_no', $issue_no)->update(['image' => $url]);
  1198. return $url;
  1199. }
  1200. // 发送开奖图片
  1201. public
  1202. static function sendLotteryImage($chatId, $issueNo)
  1203. {
  1204. $recordImage = self::lotteryImage($issueNo);
  1205. self::sendMessage($chatId, '', [], url($recordImage));
  1206. // dispatch(new SendTelegramMessageJob('', [], url($recordImage)));
  1207. }
  1208. }