Sport.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Sport as SportModel;
  5. use App\Services\SportClientService;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Facades\DB;
  8. use App\Models\SportEvent;
  9. use App\Models\Config;
  10. class Sport extends Command
  11. {
  12. /**
  13. * 命令名称和签名
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'sport {is_live=0}';
  18. protected $is_live = 0;
  19. protected $short_status = [
  20. 'TBD' => 0,
  21. 'NS' => 0,
  22. '1H' => 1,
  23. 'HT' => 1,
  24. '2H' => 1,
  25. 'ET' => 1,
  26. 'BT' => 1,
  27. 'P' => 1,
  28. 'SUSP' => 1,
  29. 'INT' => 1,
  30. 'FT' => 2,
  31. 'AET' => 2,
  32. 'PEN' => 2,
  33. 'PST' => 3,
  34. 'CANC' => 4,
  35. 'ABD' => 4,
  36. 'AWD' => 4,
  37. 'WO' => 4,
  38. 'LIVE' => 1,
  39. ];
  40. protected $long_status = [
  41. 'Time To Be Defined' => 0,
  42. 'Not Started' => 0,
  43. 'First Half' => 1,
  44. 'First Half, Kick Off' => 1,
  45. 'Halftime' => 1,
  46. 'Second Half' => 1,
  47. 'Second Half, 2nd Half Started' => 1,
  48. 'Extra Time' => 1,
  49. 'Break Time' => 1,
  50. 'Penalty In Progress' => 1,
  51. 'Match Suspended' => 1,
  52. 'Match Interrupted' => 1,
  53. 'Match Finished' => 2,
  54. 'Match Finished' => 2,
  55. 'Match Finished' => 2,
  56. 'Match Postponed' => 3,
  57. 'Match Cancelled' => 4,
  58. 'Match Abandoned' => 4,
  59. 'Technical Loss' => 4,
  60. 'WalkOver' => 4,
  61. 'In Progress' => 1,
  62. ];
  63. protected $fixture_status = [
  64. 'First Half' => 1,
  65. 'First Half, Kick Off' => 1,
  66. 'Halftime' => 1,
  67. 'Second Half' => 1,
  68. 'Second Half, 2nd Half Started' => 1,
  69. 'Extra Time' => 1,
  70. 'Break Time' => 1,
  71. 'Penalty In Progress' => 1,
  72. ];
  73. /**
  74. * 命令描述
  75. *
  76. * @var string
  77. */
  78. protected $description = '当天会去更新明天的赛事(23:59:00执行一次)';
  79. /**
  80. * 执行命令
  81. *
  82. * @return int
  83. */
  84. public function handle()
  85. {
  86. // $this->info('开始执行统计比赛数据任务...');
  87. $this->is_live = $this->argument('is_live');
  88. if ($this->is_live == 1) {
  89. //进行中的赛事,定时更新
  90. $this->liveFixtures();
  91. } elseif ($this->is_live == 0) {
  92. //未开始的赛事拉取
  93. $this->fixtures();
  94. } elseif ($this->is_live == 2){
  95. $this->checkLiveFixtures();
  96. //$this->updateOvertimeFixtures();
  97. } elseif ($this->is_live == 3) {
  98. $this->checkOvertimeFixtures();
  99. } else {
  100. $this->initOdds();
  101. }
  102. }
  103. //比赛开始后,超过3个小时,并且更新时间超过10分钟,还是进行中的数据,更新成已完成
  104. public function updateOvertimeFixtures()
  105. {
  106. // 比赛开始后,状态还是未开始的数据检测
  107. $end_time = date("Y-m-d H:i:s", time() - 600);
  108. SportModel::where('status', 1)->where('state', 1)->where('game_time', '<=', time() - 60*60*3)->where('updated_at', '<=', $end_time)->update(['state' =>2]);
  109. // $ids = SportModel::where('status', 1)->where('state', 1)->where('game_time', '<=', time() - 60*60*3)->pluck('data_id')->get();
  110. // print_r($ids);
  111. return true;
  112. }
  113. //到了比赛开始时间,但是状态还是未开始,检查比赛
  114. public function checkOvertimeFixtures()
  115. {
  116. //体育赛事结束前几(分钟)锁盘,90分钟结束
  117. $sport_locked = Config::where('field', 'sport_locked')->first()->val ?? 1;
  118. // 比赛开始后,状态还是未开始的数据检测
  119. $ids = SportModel::where('status', 1)->where('state', 0)->where('game_time', '<=', time())->pluck('data_id')->toArray();
  120. if ($ids) {
  121. $ids = implode('-', $ids);
  122. $data = SportClientService::fixtures(['ids' => $ids]);
  123. $this->updateOrCreateSport($data, $sport_locked, 1);
  124. }
  125. return true;
  126. }
  127. //进行中超过3分钟没有更新数据的赛事,检查比赛是否结束
  128. public function checkLiveFixtures()
  129. {
  130. //体育赛事结束前几(分钟)锁盘,90分钟结束
  131. $sport_locked = Config::where('field', 'sport_locked')->first()->val ?? 1;
  132. //1. 统一锁盘时间(比赛开始前1分钟)
  133. SportModel::where(['is_locked' => 0, 'is_roll' => 0])->where('game_time', '<=', time() + 90)->update(['is_locked' => 1]);
  134. //2. 比赛进行中,超过3分钟未更新的数据检测
  135. $end_time = date("Y-m-d H:i:s", time() - 180);
  136. $ids = SportModel::where('status', 1)->where('state', 1)->where('updated_at', '<=', $end_time)->pluck('data_id')->toArray();
  137. if ($ids) {
  138. $chunks = array_chunk($ids, 10);
  139. foreach ($chunks as $chunk) {
  140. $ids = implode('-', $chunk);
  141. $data = SportClientService::fixtures(['ids' => $ids]);
  142. $this->updateOrCreateSport($data, $sport_locked, 1);
  143. }
  144. }
  145. return true;
  146. }
  147. //更新进行中的赛事
  148. public function liveFixtures()
  149. {
  150. //体育赛事结束前几(分钟)锁盘,90分钟结束
  151. $sport_locked = Config::where('field', 'sport_locked')->first()->val ?? 1;
  152. $data = SportClientService::fixtures(['live' => 'all']);
  153. file_put_contents("fixturesLive.json", json_encode($data));
  154. $this->updateOrCreateSport($data, $sport_locked);
  155. return true;
  156. }
  157. private function updateOrCreateSport($data, $sport_locked, $is_check = 0) {
  158. $data = $data['response'];
  159. $tableData = [];
  160. $status = $this->short_status;
  161. foreach ($data as $item) {
  162. $home_statistics = !empty($item['statistics']) ? $item['statistics'][0]['statistics'] : '';
  163. $away_statistics = !empty($item['statistics']) ? $item['statistics'][1]['statistics'] : '';
  164. $sport_data = [
  165. 'data_id' => $item['fixture']['id'],
  166. 'home_team_id' => $item['teams']['home']['id'],
  167. 'home_team_en' => $item['teams']['home']['name'],
  168. 'home_team' => lang($item['teams']['home']['name']),
  169. 'home_team_logo' => $item['teams']['home']['logo'],
  170. 'guest_team_id' => $item['teams']['away']['id'],
  171. 'guest_team_en' => $item['teams']['away']['name'],
  172. 'guest_team' => lang($item['teams']['away']['name']),
  173. 'guest_team_logo' => $item['teams']['away']['logo'],
  174. 'half_score' => "{$item['score']['halftime']['home']}-{$item['score']['halftime']['away']}",
  175. 'rbt' => $item['fixture']['timestamp'],
  176. 'score' => isset($item['goals']) ? "{$item['goals']['home']}-{$item['goals']['away']}":'-',
  177. 'extra_score' => isset($item['score']['extratime']) ? "{$item['score']['extratime']['home']}-{$item['score']['extratime']['away']}" : "",//加时赛比分
  178. 'league' => lang($item['league']['name']),
  179. 'league_en' => $item['league']['name'],
  180. 'state' => $status[$item['fixture']['status']['short']],//比赛状态:0未开始1进行中2已完场3延期4取消
  181. 'game_time' => $item['fixture']['timestamp'],
  182. 'updated_at' => now(),
  183. 'home_statistics' => $home_statistics,
  184. 'away_statistics' => $away_statistics,
  185. 'is_send' => 0,
  186. ];
  187. $fixture_status = null;
  188. if (isset($item['fixture']['status']['long']) ) {
  189. $long = $item['fixture']['status']['long'];
  190. if (isset($this->fixture_status[$long])) {
  191. $fixture_status = json_encode($item['fixture']['status']);
  192. $sport_data['fixture_status'] = $fixture_status;
  193. }
  194. if (isset($this->long_status[$long])) {
  195. $sport_data['state'] = $this->long_status[$long];
  196. }
  197. }
  198. //提前锁盘(比赛进行时长,分钟)
  199. if (isset($item['fixture']['status']['elapsed'])) {
  200. $elapsed = $item['fixture']['status']['elapsed'];
  201. if ((int)$elapsed >= 90 - $sport_locked ) {
  202. $sport_data['is_locked'] = 1;
  203. }
  204. }
  205. $sport_data['score'] = $sport_data['score'] == '-' ? '' : $sport_data['score'];
  206. $sport_data['half_score'] = $sport_data['half_score'] == '-' ? '' : $sport_data['half_score'];
  207. $sport_data['extra_score'] = $sport_data['extra_score'] == '-' ? '' : $sport_data['extra_score'];
  208. //锁盘
  209. if (isset($item['fixture']['status']['blocked']) && $item['fixture']['status']['blocked']) {
  210. $sport_data['is_locked'] = 1;
  211. }
  212. //已结束
  213. if (isset($item['fixture']['status']['finished']) && $item['fixture']['status']['finished']) {
  214. $sport_data['state'] = 2;
  215. }
  216. //如果赛事取消、延期等,标记需要退款
  217. if ($sport_data['state'] > 2) {
  218. $sport_data['refund_status'] = 1;
  219. }
  220. $info = SportModel::where('data_id', $item['fixture']['id'])->first();
  221. if (!$info) {
  222. $sport_data['created_at'] = now();
  223. $sport_data['status'] = 1;
  224. $tableData[] = $sport_data;
  225. } else {
  226. SportModel::where('data_id', $item['fixture']['id'])->update($sport_data);
  227. }
  228. //比赛结束,插入比赛事件
  229. if ($sport_data['state'] == 2 && !empty($item['events'])) {
  230. foreach($item['events'] as $event) {
  231. SportEvent::create([
  232. 'data_id' => $item['fixture']['id'],
  233. 'type' => $event['type'],
  234. 'time_elapsed' => $event['time']['elapsed'],
  235. 'time' => json_encode($event['time']),
  236. 'detail' => $event['detail'],
  237. 'player' => $event['player'] ? json_encode($event['player']) : $event['player'],
  238. 'team_id' => $event['team']['id'],
  239. 'comments' => $event['comments'],
  240. 'assist' => $event['assist'] ? json_encode($event['assist']) : $event['assist'],
  241. ]);
  242. }
  243. }
  244. }
  245. if ($tableData) {
  246. SportModel::insert($tableData);
  247. }
  248. return true;
  249. }
  250. /**
  251. * 获取指定日期的所有赛事
  252. *
  253. * @return array
  254. */
  255. public function fixtures()
  256. {
  257. //根据配置拉取多少天的赛事信息
  258. $days = Config::where('field', 'sport_days')->first()->val ?? 1;
  259. for($i=0;$i<$days;$i++) {
  260. $date = Carbon::tomorrow()->addDay($i)->toDateString();
  261. $count = SportModel::where('game_time','>=', strtotime($date))->where('game_time','<=', strtotime($date." 23:59:59"))->count();
  262. if (!$count) {
  263. $data = SportClientService::fixtures(['date' => $date]);
  264. $data = $data['response'];
  265. $tableData = [];
  266. $status = $this->short_status;
  267. foreach ($data as $item) {
  268. $home_statistics = !empty($item['statistics']) ? $item['statistics'][0]['statistics'] : '';
  269. $away_statistics = !empty($item['statistics']) ? $item['statistics'][1]['statistics'] : '';
  270. $sport_data = [
  271. 'data_id' => $item['fixture']['id'],
  272. 'home_team_id' => $item['teams']['home']['id'],
  273. 'home_team_en' => $item['teams']['home']['name'],
  274. 'home_team' => lang($item['teams']['home']['name']),
  275. 'home_team_logo' => $item['teams']['home']['logo'],
  276. 'guest_team_id' => $item['teams']['away']['id'],
  277. 'guest_team_en' => $item['teams']['away']['name'],
  278. 'guest_team' => lang($item['teams']['away']['name']),
  279. 'guest_team_logo' => $item['teams']['away']['logo'],
  280. 'half_score' => "{$item['score']['halftime']['home']}-{$item['score']['halftime']['away']}",
  281. 'rbt' => $item['fixture']['timestamp'],
  282. 'score' => isset($item['score']['fulltime']) ? "{$item['score']['fulltime']['home']}-{$item['score']['fulltime']['away']}":'-',
  283. 'league' => lang($item['league']['name']),
  284. 'league_en' => $item['league']['name'],
  285. 'state' => $status[$item['fixture']['status']['short']],//比赛状态:0未开始1进行中2已完场3延期4取消
  286. 'game_time' => $item['fixture']['timestamp'],
  287. 'status' => 1,
  288. 'updated_at' => now(),
  289. 'home_statistics' => $home_statistics,
  290. 'away_statistics' => $away_statistics,
  291. 'is_send' => 0,
  292. ];
  293. $sport_data['score'] = $sport_data['score'] == '-' ? '' : $sport_data['score'];
  294. $sport_data['half_score'] = $sport_data['half_score'] == '-' ? '' : $sport_data['half_score'];
  295. if (!SportModel::where('data_id', $item['fixture']['id'])->exists()) {
  296. $sport_data['created_at'] = now();
  297. $tableData[] = $sport_data;
  298. } else {
  299. SportModel::where('data_id', $item['fixture']['id'])->update($sport_data);
  300. }
  301. //比赛结束,插入比赛事件
  302. if ($sport_data['state'] == 2 && !empty($item['events'])) {
  303. foreach($item['events'] as $event) {
  304. SportEvent::create([
  305. 'data_id' => $item['fixture']['id'],
  306. 'type' => $event['type'],
  307. 'time_elapsed' => $event['time']['elapsed'],
  308. 'time' => json_encode($event['time']),
  309. 'detail' => $event['detail'],
  310. 'player' => $event['player'] ? json_encode($event['player']) : $event['player'],
  311. 'team_id' => $event['team']['id'],
  312. 'comments' => $event['comments'],
  313. 'assist' => $event['assist'] ? json_encode($event['assist']) : $event['assist'],
  314. ]);
  315. }
  316. }
  317. }
  318. if ($tableData) {
  319. SportModel::insert($tableData);
  320. }
  321. }
  322. }
  323. return true;
  324. }
  325. public function initOdds(){
  326. $page = 1;
  327. $limit = 10;
  328. while (true) {
  329. $list = SportModel::where('odds','<>', null)->forPage($page, $limit)->get()->toArray();
  330. if (empty($list)) {
  331. break;
  332. }
  333. echo $page.PHP_EOL;
  334. foreach($list as $item) {
  335. $odds = json_decode($item['odds'], true);
  336. foreach($odds as $odd) {
  337. $odd_id = $odd['id'];
  338. $odd_name = $odd['name'];
  339. $info = DB::table('sport_odds')->where('odd_id',$odd_id)->where('odd_name_en',$odd_name)->first();
  340. if ($info && (!$info->odd_name || $odd_name != $info->odd_name_en)) {
  341. DB::table('sport_odds')->where('id', $info->id)->update([
  342. 'odd_name_en' => $odd_name,
  343. 'odd_name' => $this->getZhName($odd_name),
  344. ]);
  345. echo '更新数据:'.$odd_id.'-'.$odd_name.PHP_EOL;
  346. } elseif (!$info) {
  347. DB::table('sport_odds')->insert([
  348. 'odd_id' => $odd_id,
  349. 'odd_name_en' => $odd_name,
  350. 'odd_name' => $this->getZhName($odd_name),
  351. 'created_at' => date('Y-m-d H:i:s'),
  352. 'updated_at' => date('Y-m-d H:i:s'),
  353. ]);
  354. echo '插入数据:'.$odd_id.'-'.$odd_name.PHP_EOL;
  355. }
  356. }
  357. }
  358. $page++;
  359. }
  360. }
  361. public function getZhName ($name) {
  362. $betting_terms = [
  363. "Match Winner" => "全场胜负",
  364. "Home/Away" => "主胜/客胜",
  365. "Second Half Winner" => "下半场胜负",
  366. "Asian Handicap" => "亚洲让球盘",
  367. "Goals Over/Under" => "全场大小球",
  368. "Goals Over/Under First Half" => "上半场大小球",
  369. "Goals Over/Under - Second Half" => "下半场大小球",
  370. "HT/FT Double" => "半场+全场双猜",
  371. "Both Teams Score" => "双方均进球",
  372. "Handicap Result" => "让球结果",
  373. "Exact Score" => "精确比分",
  374. "Correct Score - First Half" => "上半场精确比分",
  375. "Correct Score - Second Half" => "下半场精确比分",
  376. "Double Chance" => "双选胜平负",
  377. "First Half Winner" => "上半场胜负",
  378. "Team To Score First" => "首支进球球队",
  379. "Team To Score Last" => "最后进球球队",
  380. "Win Both Halves" => "上下半场均获胜",
  381. "Total - Home" => "主队总进球数",
  382. "Total - Away" => "客队总进球数",
  383. "Both Teams Score - First Half" => "上半场双方均进球",
  384. "Both Teams To Score - Second Half" => "下半场双方均进球",
  385. "Odd/Even" => "总进球数奇偶",
  386. "Odd/Even - First Half" => "上半场进球奇偶",
  387. "Home Team Exact Goals Number" => "主队精确进球数",
  388. "Away Team Exact Goals Number" => "客队精确进球数",
  389. "Results/Both Teams Score" => "赛果+双方进球",
  390. "Odd/Even - Second Half" => "下半场进球奇偶",
  391. "Clean Sheet - Home" => "主队零封",
  392. "Clean Sheet - Away" => "客队零封",
  393. "Win to Nil - Home" => "主队零封获胜",
  394. "Win to Nil - Away" => "客队零封获胜",
  395. "Highest Scoring Half" => "进球更多的半场",
  396. "Handicap Result - First Half" => "上半场让球结果",
  397. "Asian Handicap First Half" => "上半场亚洲让球盘",
  398. "Double Chance - First Half" => "上半场双选胜平负",
  399. "Win To Nil" => "零封获胜",
  400. "Home Odd/Even" => "主队进球奇偶",
  401. "Away Odd/Even" => "客队进球奇偶",
  402. "To Win Either Half" => "赢得任意半场",
  403. "Result/Total Goals" => "赛果+总进球数",
  404. "First 10 min Winner" => "前10分钟胜负",
  405. "Corners Over Under" => "角球大小",
  406. "Home Team Total Goals(1st Half)" => "主队上半场总进球",
  407. "Away Team Total Goals(1st Half)" => "客队上半场总进球",
  408. "Home Team Total Goals(2nd Half)" => "主队下半场总进球",
  409. "Away Team Total Goals(2nd Half)" => "客队下半场总进球",
  410. "Draw No Bet (1st Half)" => "上半场让球平注",
  411. "European Handicap (2nd Half)" => "下半场欧洲让球",
  412. "Draw No Bet (2nd Half)" => "下半场让球平注",
  413. "Total Goals/Both Teams To Score" => "总进球+双方进球",
  414. "Home Corners Over/Under" => "主队角球大小",
  415. "Away Corners Over/Under" => "客队角球大小",
  416. "Total Corners (3 way)" => "总角球三路",
  417. "1x2 - 60 minutes" => "60分钟胜平负",
  418. "1x2 - 30 minutes" => "30分钟胜平负",
  419. "First Team to Score (3 way) 1st Half" => "上半场首支进球球队(三路)",
  420. "Total Corners (1st Half)" => "上半场总角球",
  421. "Corners. Odd/Even" => "角球奇偶",
  422. "RTG_H1" => "上半场进球数(简写)",
  423. "Cards Over/Under" => "黄牌大小",
  424. "To Qualify" => "晋级球队",
  425. "Goal Line" => "进球线",
  426. "Goal Line (1st Half)" => "上半场进球线",
  427. "Home team will score in both halves" => "主队上下半场均进球",
  428. "Away team will score in both halves" => "客队上下半场均进球",
  429. "Last Corner" => "最后角球",
  430. "How many goals will Away Team score?" => "客队进球数",
  431. "Asian Corners" => "亚洲让角球",
  432. "Match Corners" => "全场角球",
  433. "Final Score" => "最终比分",
  434. "Match Goals" => "全场进球",
  435. "Home Team Score a Goal (2nd Half)" => "主队下半场进球",
  436. "Result / Both Teams To Score" => "赛果/双方进球",
  437. "To Win 2nd Half" => "赢下半场",
  438. "Over/Under Line" => "大小球盘口",
  439. "3-Way Handicap" => "三路让球",
  440. "Away Team Goals" => "客队进球",
  441. "Both Teams To Score (2nd Half)" => "下半场双方进球",
  442. "Which team will score the 5th corner? (2 Way)" => "第5个角球归属(二路)",
  443. "Race to the 9th corner?" => "先得9个角球",
  444. "Race to the 7th corner?" => "先得7个角球",
  445. "Draw No Bet" => "让球平注",
  446. "Home Team Goals" => "主队进球",
  447. "Total Corners" => "总角球",
  448. "Fulltime Result" => "全场赛果",
  449. "Race to the 5th corner?" => "先得5个角球",
  450. "Last Team to Score (3 way)" => "最后进球球队(三路)",
  451. "Which team will score the 2nd goal?" => "第二球归属球队",
  452. "Home Team Clean Sheet" => "主队零封",
  453. "How many goals will Home Team score?" => "主队进球数",
  454. "Goals Odd/Even" => "进球奇偶",
  455. "Both Teams to Score" => "双方均进球",
  456. "Away Team Score a Goal (2nd Half)" => "客队下半场进球",
  457. "Which team will score the 4th goal?" => "第四球归属球队",
  458. "Which team will score the 7th corner? (2 Way)" => "第7个角球归属(二路)",
  459. ];
  460. if (isset($betting_terms[$name])) {
  461. return $betting_terms[$name];
  462. }
  463. return '';
  464. }
  465. }