Game.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Models;
  3. use App\Services\Api\GameService;
  4. use Carbon\Carbon;
  5. use Dcat\Admin\Traits\HasDateTimeFormatter;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. class Game extends Model
  10. {
  11. use HasFactory, SoftDeletes, HasDateTimeFormatter;
  12. protected $hidden = [
  13. 'updated_at',
  14. 'deleted_at',
  15. ];
  16. public function game_types()
  17. {
  18. return $this->belongsTo(GameType::class, 'game_type_id', 'id');
  19. }
  20. //这个查不出来虚拟球员,所以没有用到
  21. public function users()
  22. {
  23. return $this->belongsToMany(User::class, 'game_users', 'game_id', 'user_id');
  24. }
  25. //关联发起人
  26. public function creator_s()
  27. {
  28. return $this->belongsTo(User::class, 'creator', 'id');
  29. }
  30. //关联比赛场地
  31. public function course()
  32. {
  33. return $this->belongsTo(Course::class, 'course_id', 'id');
  34. }
  35. public static function getGameInfo($id)
  36. {
  37. $info = self::where('games.id', $id)
  38. ->leftJoin('game_types', 'game_types.id', '=', 'games.game_type_id')
  39. ->leftJoin('courses', 'courses.id', '=', 'games.course_id')
  40. ->select('games.id as game_id', 'games.name', 'games.creator', 'begin_time',
  41. 'game_types.name as game_type', 'game_types.scale', 'courses.name as course_name', 'courses.img as course_avatar')
  42. ->first();
  43. if(empty($info)){
  44. return [];
  45. }
  46. $info = $info->toArray();
  47. $info['actual_num'] = self::getActualCount($id);
  48. $timeParse = Carbon::parse($info['begin_time']);
  49. $info['begin_time'] = $timeParse->format('Y-m-d H:i');
  50. $info['month'] = $timeParse->format('Y-m');
  51. $info['date1'] = $timeParse->format('Y-m-d');
  52. $info['date2'] = $timeParse->format('m-d');
  53. $info['time1'] = $timeParse->format('m-d H:i');
  54. $info['time2'] = $timeParse->format('H:i');
  55. $info['week1'] = $timeParse->format('w');
  56. switch ($info['week1']){
  57. case 1:
  58. $week = 'Mon';
  59. break;
  60. case 2:
  61. $week = 'Tue';
  62. break;
  63. case 3:
  64. $week = 'Wed';
  65. break;
  66. case 4:
  67. $week = 'Thu';
  68. break;
  69. case 5:
  70. $week = 'Fri';
  71. break;
  72. case 6:
  73. $week = 'Sat';
  74. break;
  75. default:
  76. $week = 'Sun';
  77. break;
  78. }
  79. $info['week2'] = $week;
  80. $info['for_human_read'] = $timeParse->diffForHumans();
  81. $info['players'] = (new GameService())->getGameUsers($id);
  82. return $info;
  83. }
  84. //获取比赛的当前实际人数
  85. public static function getActualCount($gameId)
  86. {
  87. $count = GameUser::query()
  88. ->where('game_id', $gameId)
  89. ->where('status', 1)
  90. ->whereNull('deleted_at')
  91. ->count('id');
  92. return $count;
  93. }
  94. }