Sport.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. class Sport extends Command
  10. {
  11. /**
  12. * 命令名称和签名
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'sport';
  17. protected $short_status = [
  18. 'TBD' => 0,
  19. 'NS' => 0,
  20. '1H' => 1,
  21. 'HT' => 1,
  22. '2H' => 1,
  23. 'ET' => 1,
  24. 'BT' => 1,
  25. 'P' => 1,
  26. 'SUSP' => 1,
  27. 'INT' => 1,
  28. 'FT' => 2,
  29. 'AET' => 2,
  30. 'PEN' => 2,
  31. 'PST' => 3,
  32. 'CANC' => 4,
  33. 'ABD' => 4,
  34. 'AWD' => 4,
  35. 'WO' => 4,
  36. 'LIVE' => 1,
  37. ];
  38. /**
  39. * 命令描述
  40. *
  41. * @var string
  42. */
  43. protected $description = '当天会去更新明天的赛事(23:59:00执行一次)';
  44. /**
  45. * 执行命令
  46. *
  47. * @return int
  48. */
  49. public function handle()
  50. {
  51. $this->info('开始执行统计比赛数据任务...');
  52. $this->fixtures();
  53. $this->info('结束执行统计比赛数据任务');
  54. }
  55. /**
  56. * 获取指定日期的所有赛事
  57. *
  58. * @return array
  59. */
  60. public function fixtures()
  61. {
  62. $date = Carbon::tomorrow()->toDateString();
  63. $data = SportClientService::fixtures(['date' => $date]);
  64. // $data = SportClientService::fixtures(['live' => 'all']);
  65. // print_r($data);
  66. // file_put_contents("all_fixtures.json",json_encode($data));
  67. // die;
  68. $data = $data['response'];
  69. $tableData = [];
  70. $status = $this->short_status;
  71. foreach ($data as $item) {
  72. $home_statistics = !empty($item['statistics']) ? $item['statistics'][0]['statistics'] : '';
  73. $away_statistics = !empty($item['statistics']) ? $item['statistics'][1]['statistics'] : '';
  74. $sport_data = [
  75. 'data_id' => $item['fixture']['id'],
  76. 'home_team_id' => $item['teams']['home']['id'],
  77. 'home_team_en' => $item['teams']['home']['name'],
  78. 'home_team' => lang($item['teams']['home']['name']),
  79. 'home_team_logo' => $item['teams']['home']['logo'],
  80. 'guest_team_id' => $item['teams']['away']['id'],
  81. 'guest_team_en' => $item['teams']['away']['name'],
  82. 'guest_team' => lang($item['teams']['away']['name']),
  83. 'guest_team_logo' => $item['teams']['away']['logo'],
  84. 'half_score' => "{$item['score']['halftime']['home']}-{$item['score']['halftime']['away']}",
  85. 'rbt' => $item['fixture']['timestamp'],
  86. 'score' => isset($item['score']['fulltime']) ? "{$item['score']['fulltime']['home']}-{$item['score']['fulltime']['away']}":'-',
  87. 'league' => lang($item['league']['name']),
  88. 'league_en' => $item['league']['name'],
  89. 'state' => $status[$item['fixture']['status']['short']],//比赛状态:0未开始1进行中2已完场3延期4取消
  90. 'game_time' => $item['fixture']['timestamp'],
  91. 'status' => 1,
  92. 'updated_at' => now(),
  93. 'home_statistics' => $home_statistics,
  94. 'away_statistics' => $away_statistics,
  95. ];
  96. $sport_data['score'] = $sport_data['score'] == '-' ? '' : $sport_data['score'];
  97. $sport_data['half_score'] = $sport_data['half_score'] == '-' ? '' : $sport_data['half_score'];
  98. if (!SportModel::where('data_id', $item['fixture']['id'])->exists()) {
  99. $sport_data['created_at'] = now();
  100. $tableData[] = $sport_data;
  101. } else {
  102. SportModel::where('data_id', $item['fixture']['id'])->update($sport_data);
  103. }
  104. //比赛结束,插入比赛事件
  105. if ($sport_data['state'] == 2 && !empty($item['events'])) {
  106. foreach($item['events'] as $event) {
  107. SportEvent::create([
  108. 'data_id' => $item['fixture']['id'],
  109. 'type' => $event['type'],
  110. 'time_elapsed' => $event['time']['elapsed'],
  111. 'time' => json_encode($event['time']),
  112. 'detail' => $event['detail'],
  113. 'player' => $event['player'] ? json_encode($event['player']) : $event['player'],
  114. 'team_id' => $event['team']['id'],
  115. 'comments' => $event['comments'],
  116. 'assist' => $event['assist'] ? json_encode($event['assist']) : $event['assist'],
  117. ]);
  118. }
  119. }
  120. }
  121. if ($tableData) {
  122. SportModel::insert($tableData);
  123. }
  124. return $tableData;
  125. }
  126. public function initOdds(){
  127. $page = 1;
  128. $limit = 10;
  129. while (true) {
  130. $list = SportModel::where('odds','<>', null)->forPage($page, $limit)->get()->toArray();
  131. if (empty($list)) {
  132. break;
  133. }
  134. echo $page.PHP_EOL;
  135. foreach($list as $item) {
  136. $odds = json_decode($item['odds'], true);
  137. foreach($odds as $odd) {
  138. $odd_id = $odd['id'];
  139. $odd_name = $odd['name'];
  140. $info = DB::table('sport_odds')->where('odd_id',$odd_id)->where('odd_name_en',$odd_name)->first();
  141. if ($info && (!$info->odd_name || $odd_name != $info->odd_name_en)) {
  142. DB::table('sport_odds')->where('id', $info->id)->update([
  143. 'odd_name_en' => $odd_name,
  144. 'odd_name' => $this->getZhName($odd_name),
  145. ]);
  146. echo '更新数据:'.$odd_id.'-'.$odd_name.PHP_EOL;
  147. } elseif (!$info) {
  148. DB::table('sport_odds')->insert([
  149. 'odd_id' => $odd_id,
  150. 'odd_name_en' => $odd_name,
  151. 'odd_name' => $this->getZhName($odd_name),
  152. 'created_at' => date('Y-m-d H:i:s'),
  153. 'updated_at' => date('Y-m-d H:i:s'),
  154. ]);
  155. echo '插入数据:'.$odd_id.'-'.$odd_name.PHP_EOL;
  156. }
  157. }
  158. }
  159. $page++;
  160. }
  161. }
  162. public function getZhName ($name) {
  163. $betting_terms = [
  164. "Match Winner" => "全场胜负",
  165. "Home/Away" => "主胜/客胜",
  166. "Second Half Winner" => "下半场胜负",
  167. "Asian Handicap" => "亚洲让球盘",
  168. "Goals Over/Under" => "全场大小球",
  169. "Goals Over/Under First Half" => "上半场大小球",
  170. "Goals Over/Under - Second Half" => "下半场大小球",
  171. "HT/FT Double" => "半场+全场双猜",
  172. "Both Teams Score" => "双方均进球",
  173. "Handicap Result" => "让球结果",
  174. "Exact Score" => "精确比分",
  175. "Correct Score - First Half" => "上半场精确比分",
  176. "Correct Score - Second Half" => "下半场精确比分",
  177. "Double Chance" => "双选胜平负",
  178. "First Half Winner" => "上半场胜负",
  179. "Team To Score First" => "首支进球球队",
  180. "Team To Score Last" => "最后进球球队",
  181. "Win Both Halves" => "上下半场均获胜",
  182. "Total - Home" => "主队总进球数",
  183. "Total - Away" => "客队总进球数",
  184. "Both Teams Score - First Half" => "上半场双方均进球",
  185. "Both Teams To Score - Second Half" => "下半场双方均进球",
  186. "Odd/Even" => "总进球数奇偶",
  187. "Odd/Even - First Half" => "上半场进球奇偶",
  188. "Home Team Exact Goals Number" => "主队精确进球数",
  189. "Away Team Exact Goals Number" => "客队精确进球数",
  190. "Results/Both Teams Score" => "赛果+双方进球",
  191. "Odd/Even - Second Half" => "下半场进球奇偶",
  192. "Clean Sheet - Home" => "主队零封",
  193. "Clean Sheet - Away" => "客队零封",
  194. "Win to Nil - Home" => "主队零封获胜",
  195. "Win to Nil - Away" => "客队零封获胜",
  196. "Highest Scoring Half" => "进球更多的半场",
  197. "Handicap Result - First Half" => "上半场让球结果",
  198. "Asian Handicap First Half" => "上半场亚洲让球盘",
  199. "Double Chance - First Half" => "上半场双选胜平负",
  200. "Win To Nil" => "零封获胜",
  201. "Home Odd/Even" => "主队进球奇偶",
  202. "Away Odd/Even" => "客队进球奇偶",
  203. "To Win Either Half" => "赢得任意半场",
  204. "Result/Total Goals" => "赛果+总进球数",
  205. "First 10 min Winner" => "前10分钟胜负",
  206. "Corners Over Under" => "角球大小",
  207. "Home Team Total Goals(1st Half)" => "主队上半场总进球",
  208. "Away Team Total Goals(1st Half)" => "客队上半场总进球",
  209. "Home Team Total Goals(2nd Half)" => "主队下半场总进球",
  210. "Away Team Total Goals(2nd Half)" => "客队下半场总进球",
  211. "Draw No Bet (1st Half)" => "上半场让球平注",
  212. "European Handicap (2nd Half)" => "下半场欧洲让球",
  213. "Draw No Bet (2nd Half)" => "下半场让球平注",
  214. "Total Goals/Both Teams To Score" => "总进球+双方进球",
  215. "Home Corners Over/Under" => "主队角球大小",
  216. "Away Corners Over/Under" => "客队角球大小",
  217. "Total Corners (3 way)" => "总角球三路",
  218. "1x2 - 60 minutes" => "60分钟胜平负",
  219. "1x2 - 30 minutes" => "30分钟胜平负",
  220. "First Team to Score (3 way) 1st Half" => "上半场首支进球球队(三路)",
  221. "Total Corners (1st Half)" => "上半场总角球",
  222. "Corners. Odd/Even" => "角球奇偶",
  223. "RTG_H1" => "上半场进球数(简写)",
  224. "Cards Over/Under" => "黄牌大小",
  225. "To Qualify" => "晋级球队",
  226. "Goal Line" => "进球线",
  227. "Goal Line (1st Half)" => "上半场进球线",
  228. "Home team will score in both halves" => "主队上下半场均进球",
  229. "Away team will score in both halves" => "客队上下半场均进球",
  230. "Last Corner" => "最后角球",
  231. "How many goals will Away Team score?" => "客队进球数",
  232. "Asian Corners" => "亚洲让角球",
  233. "Match Corners" => "全场角球",
  234. "Final Score" => "最终比分",
  235. "Match Goals" => "全场进球",
  236. "Home Team Score a Goal (2nd Half)" => "主队下半场进球",
  237. "Result / Both Teams To Score" => "赛果/双方进球",
  238. "To Win 2nd Half" => "赢下半场",
  239. "Over/Under Line" => "大小球盘口",
  240. "3-Way Handicap" => "三路让球",
  241. "Away Team Goals" => "客队进球",
  242. "Both Teams To Score (2nd Half)" => "下半场双方进球",
  243. "Which team will score the 5th corner? (2 Way)" => "第5个角球归属(二路)",
  244. "Race to the 9th corner?" => "先得9个角球",
  245. "Race to the 7th corner?" => "先得7个角球",
  246. "Draw No Bet" => "让球平注",
  247. "Home Team Goals" => "主队进球",
  248. "Total Corners" => "总角球",
  249. "Fulltime Result" => "全场赛果",
  250. "Race to the 5th corner?" => "先得5个角球",
  251. "Last Team to Score (3 way)" => "最后进球球队(三路)",
  252. "Which team will score the 2nd goal?" => "第二球归属球队",
  253. "Home Team Clean Sheet" => "主队零封",
  254. "How many goals will Home Team score?" => "主队进球数",
  255. "Goals Odd/Even" => "进球奇偶",
  256. "Both Teams to Score" => "双方均进球",
  257. "Away Team Score a Goal (2nd Half)" => "客队下半场进球",
  258. "Which team will score the 4th goal?" => "第四球归属球队",
  259. "Which team will score the 7th corner? (2 Way)" => "第7个角球归属(二路)",
  260. ];
  261. if (isset($betting_terms[$name])) {
  262. return $betting_terms[$name];
  263. }
  264. return '';
  265. }
  266. }