IssueService.php 34 KB

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