Settlement.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 App\Model\Option;
  9. use App\Model\User;
  10. use App\Model\Room;
  11. use App\Model\RoomUser;
  12. use App\Model\RoomAnswer;
  13. use App\Model\RoomQuestion;
  14. use App\Jobs\Distribute;
  15. use GuzzleHttp\Client;
  16. use GuzzleHttp\Cookie\CookieJar;
  17. use Illuminate\Support\Facades\Redis;
  18. use GatewayWorker\Lib\Gateway;
  19. use Illuminate\Support\Facades\Log;
  20. class Settlement implements ShouldQueue
  21. {
  22. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  23. protected $room_id; //房间ID
  24. /**
  25. * Create a new job instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct($room_id)
  30. {
  31. $this->room_id = $room_id;
  32. }
  33. /**
  34. * Execute the job.
  35. *
  36. * @return void
  37. */
  38. public function handle()
  39. {
  40. //获取当前房间回答情况
  41. $question_id = RoomQuestion::where("room_id", $this->room_id)->whereNull("end_at")->value("question_id");
  42. if(!$question_id){
  43. echo "找不到题目";
  44. return 0;
  45. }
  46. //获取当前房间获取问题数
  47. $room_question_count = RoomQuestion::where("room_id", $this->room_id)->count();
  48. //注册gateway地址
  49. Gateway::$registerAddress = env("WS_REGEDIT_ADDR");
  50. //结算当前局
  51. $update_data["end_at"] = date("Y-m-d H:i:s");
  52. RoomQuestion::where([
  53. "room_id" => $this->room_id,
  54. "question_id" => $question_id,
  55. ])->update($update_data);
  56. //返回各个用户回答的问题
  57. $group_users = Gateway::getUidListByGroup($this->room_id);
  58. $anwsers = RoomAnswer::select('user_id', 'option_id', 'question_id')->where([
  59. "question_id" => $question_id,
  60. "room_id" => $this->room_id,
  61. ])->whereIn("user_id", array_values($group_users))->get();
  62. $option_id = Option::where([
  63. "question_id" => $question_id,
  64. "is_answer" => 1,
  65. ])->value("option_id");
  66. $info['anwser'] = $option_id;
  67. $info['players_answer'] = $anwsers;
  68. //发送回合结算信息
  69. $message['info'] = $info;
  70. $message['type'] = 'round_end';
  71. Gateway::sendToGroup($this->room_id, json_encode($message));
  72. if($room_question_count > 4){
  73. //当前房间结束
  74. $update_data["is_end"] = 1;
  75. $update_data["end_at"] = date("Y-m-d H:i:s");
  76. Room::where("room_id", $this->room_id)->update($update_data);
  77. //获取胜利者
  78. $players = RoomUser::select("user_id","score")->where([
  79. "room_id" => $this->room_id,
  80. ])->get();
  81. $winer = '';
  82. $winer_score = 0;
  83. $pkresult = [];
  84. foreach ($players as $k => $player) {
  85. if($player->score > $winer_score){
  86. $winer = $player->user_id;
  87. $winer_score = $player->score;
  88. }
  89. $user = User::select("user_id", "cmcc_id", "mt", "is_robot")->where("user_id", $player->user_id)->first();
  90. if($user->is_robot != 1){
  91. $mt = $user->mt;
  92. $cmcc_id = $user->cmcc_id;
  93. }
  94. $pkresult[] = [
  95. "robot_flag" => $user->is_robot,
  96. "user_id" => $user->cmcc_id,
  97. "score" => $player->score,
  98. ];
  99. }
  100. //通知平台
  101. $user_power = [];
  102. if($mt && !env("YANSHI")){
  103. //上传用户游戏信息
  104. $client = new Client([
  105. 'verify' => false,
  106. ]);
  107. $platform_upload_score_url = env("PLATFORM_UPLOAD_SCORE_URL");
  108. $req_time = date('YmdHis');
  109. $sign = md5($cmcc_id . $req_time . $mt);
  110. $params['req_param'] = [
  111. "pub_info" => [
  112. "req_src" => "1",
  113. "ver" => "1.0",
  114. "mt" => $mt,
  115. "user_id" => $cmcc_id,
  116. "req_time" => $req_time,
  117. "sign" => $sign,
  118. ],
  119. "page_info" => [
  120. // "current_page" => 1,
  121. // "page_size" => 10,
  122. ],
  123. "busi_info" => [
  124. "pk_result" => $pkresult
  125. ],
  126. ];
  127. Log::info("通知平台放请求信息: \n ". json_encode($params) ."\n");
  128. $result = $client->request('POST', $platform_upload_score_url, [
  129. 'json' => $params
  130. ]);
  131. if($result->getStatusCode() == 200){
  132. $cmcc_result = json_decode($result->getBody()->getContents());
  133. if($cmcc_result->rsp_param->pub_info->code == "0000"){
  134. $user_power = $cmcc_result->rsp_param->busi_info->user_power;
  135. }
  136. }
  137. Log::info("平台返回信息: \n ". json_encode($cmcc_result) ."\n");
  138. }
  139. //给用户发送信息
  140. foreach ($players as $k => $player) {
  141. $is_win = 0;
  142. $user = User::where("user_id", $player->user_id)->first();
  143. if($winer == $player->user_id){
  144. //更新胜利局数
  145. User::where("user_id", $player->user_id)->increment('win_count', 1);
  146. $is_win = 1;
  147. }else{
  148. User::where("user_id", $player->user_id)->increment('lose_count', 1);
  149. }
  150. //更新用户在房间的信息
  151. unset($update_data);
  152. $update_data['state'] = 2;
  153. $update_data['is_win'] = $is_win;
  154. $update_data['updated_at'] = date("Y-m-d H:i:s");
  155. RoomUser::where([
  156. "user_id" => $player->user_id,
  157. "room_id" => $this->room_id,
  158. ])->update($update_data);
  159. //用户信息
  160. $info['user_id'] = $user->user_id;
  161. $info['name'] = $user->name;
  162. $info['avatar'] = $user->avatar;
  163. $info['is_win'] = $is_win;
  164. $info['play_next'] = 0;
  165. $info['power_change'] = 0;
  166. if(!env("YANSHI")){
  167. foreach ($user_power as $k => $up) {
  168. if(isset($up->user_id) && $up->user_id == $user->cmcc_id){
  169. $info['play_next'] = $up->play_next;
  170. $info['power_change'] = $up->power_change;
  171. }
  172. }
  173. }
  174. //获取用户星力变化等信息返回
  175. $message['info'] = $info;
  176. $message['winer'] = $winer;
  177. $message['winer_score'] = $winer_score;
  178. $message['type'] = 'game_end';
  179. Gateway::sendToUid($user->user_id, json_encode($message));
  180. }
  181. }else{
  182. sleep(3);
  183. //执行发题
  184. Distribute::dispatch($this->room_id)->onQueue('distribute');
  185. }
  186. }
  187. }