Distribute.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Queue\SerializesModels;
  5. use Illuminate\Queue\InteractsWithQueue;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Support\Facades\Redis;
  9. use App\Model\Question;
  10. use App\Model\Option;
  11. use App\Model\RoomQuestion;
  12. class Distribute implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. protected $room_id; //房间ID
  16. /**
  17. * Create a new job instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct($room_id)
  22. {
  23. $this->room_id = $room_id;
  24. }
  25. /**
  26. * Execute the job.
  27. *
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. if(!$this->room_id){
  33. return 0;
  34. }
  35. //获取当前房间是否还有未结算题目
  36. $has_not_settlement_question = RoomQuestion::where("room_id", $this->room_id)->whereNull("end_at")->count();
  37. if($has_not_settlement_question){
  38. return 0;
  39. }
  40. //获取当前房间题目数
  41. $questions_id = RoomQuestion::where("room_id", $this->room_id)->pluck("question_id");
  42. if(count($questions_id) < 5){
  43. //获取题目
  44. $question = Question::inRandomOrder()->select('question_id','title')->where("is_released",1)->whereNotIn("question_id", $questions_id)->first();
  45. $options = Option::select('option_id','title')->where("question_id",$question->question_id)->get($question->question_id);
  46. //记录题目
  47. $date = date('Y-m-d H:i:s');
  48. $next_date = date('YmdHis', strtotime("{$date} +10 seconds"));
  49. RoomQuestion::insert([
  50. "room_id" => $this->room_id,
  51. "question_id" => $question->question_id,
  52. "title" => $question->title,
  53. "start_at" => $date,
  54. "created_at" => $date,
  55. "updated_at" => $date,
  56. ]);
  57. //发送题目
  58. $info['question'] = $question;
  59. $info['options'] = $options;
  60. $message = [
  61. "type" => 'question',
  62. "msg" => "获取题目成功",
  63. "info" => $info,
  64. ];
  65. Gateway::sendToGroup($this->room_id, json_encode($message));
  66. //设置下次结算固定时间
  67. $room_list = Redis::rpush("Game_distribute_{$next_date}", $this->room_id);
  68. }
  69. }
  70. }