Distribute.php 2.6 KB

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