郑晓宇 6 rokov pred
rodič
commit
eddbf2c638

+ 98 - 0
app/Console/Commands/gamematch.php

@@ -0,0 +1,98 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use GatewayWorker\Lib\Gateway;
+use Illuminate\Support\Facades\Redis;
+
+class gamematch extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'game:match';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '启动游戏匹配';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        //注册地址
+        Gateway::$registerAddress = '127.0.0.1:1238';
+        //循环匹配
+        while (1) {
+            $data = Redis::lpop('match_list');
+            if(!$data){
+                echo "未找到匹配\n";
+                sleep(1);
+                continue;
+            }
+            $data = json_decode($data, 1);
+            $room = Redis::get($data['level']);
+            if($room){
+                Redis::del($data['level']);
+                $room = json_decode($room, 1);
+                //获取玩家ClientID
+                if($room['client_id'] == $data['client_id']){
+                    continue;
+                }
+                // $player_1_ = Gateway::getClientIdByUid($room['uid']);
+                Gateway::joinGroup($room['client_id'], $room['room_id']);
+                // $player_2 = Gateway::getClientIdByUid($data['uid']);
+                Gateway::joinGroup($data['client_id'], $room['room_id']);
+                //组合玩家信息
+                $players = [
+                    $data,
+                    $room
+                ];
+                //发送可以开始消息
+                $message = [
+                    "type" => 'match',
+                    "msg" => "匹配成功",
+                    "info" => ['room_id'=>$room['room_id'],"players"=>$players],
+                ];
+                Gateway::sendToGroup($room['room_id'], json_encode($message));
+                echo "匹配成功: {$data['client_id']} 、 {$room['client_id']}\n";
+            }else{
+                //新建一个房间
+                $room_id = $this->get_room_id();
+                $room = ['room_id'=>$room_id, "user_id"=>$data['user_id'], "avatar"=>$data['avatar'], "username"=>$data['username'],'client_id'=>$data['client_id']]; 
+                Redis::set($data['level'], json_encode($room));
+            }
+        }
+    }
+
+    public function get_room_id()
+    {
+        while (1) {
+            $room_id = 'room_' . rand(10000,99999);
+            $count = Gateway::getUidCountByGroup($room_id);
+            if(!$count){
+                return $room_id;
+            }
+        }
+    }
+
+}

+ 167 - 0
app/Home/Controllers/GameController.php

@@ -0,0 +1,167 @@
+<?php
+
+namespace App\Home\Controllers;
+
+use App\Http\Controllers\Controller;
+use Illuminate\Http\Request;
+use GatewayWorker\Lib\Gateway;
+use App\Model\Question;
+use App\Model\Option;
+use Illuminate\Support\Facades\Redis;
+
+class GameController extends Controller
+{
+
+    /**
+     * 获取问题
+     */
+    public function Question(Request $request)
+    {
+        if(!$request->session()->has('user_id')){
+            $response['code'] = 400;
+            $response['msg'] = '请先登录';
+            return response()->json($response);
+        }
+        if(!$request->session()->has('room_id')){
+            $response['code'] = 400;
+            $response['msg'] = '请先匹配对手';
+            return response()->json($response);
+        }
+        $room_id = $request->session()->get('room_id');
+        //获取当前房间问题
+        $questions = Redis::smembers($room_id);
+        $question = Question::inRandomOrder()->select('question_id','title')->where("is_released",1)->whereNotIn("question_id", $questions)->first();
+        $options = Option::select('option_id','title')->where("question_id",$question->question_id)->get($question->question_id);
+        Redis::sadd($room_id, $question->question_id);
+        //设置返回数据
+        $response['code'] = 0;
+        $response['msg'] = '获取成功';
+        $response['info']['question'] = $question;
+        $response['info']['options'] = $options;
+        $response['info']['questions_count'] = count($questions) + 1;
+        return response()->json($response);
+    }
+
+    /**
+     * 回答问题
+     */
+    public function Answer(Request $request)
+    {
+        if(!$request->session()->has('user_id')){
+            $response['code'] = 400;
+            $response['msg'] = '请先登录';
+            return response()->json($response);
+        }
+        if(!$request->session()->has('room_id')){
+            $response['code'] = 400;
+            $response['msg'] = '请先匹配对手';
+            return response()->json($response);
+        }
+
+        $message['is_end'] = 0;  //是否结束答题标志
+        $question_id = $request->input('question_id');
+        $option_id = $request->input('option_id');
+        $room_id = $request->session()->get('room_id');
+        $user_id = $request->session()->get('user_id');
+        //获取当前房间问题条数
+        $questions = Redis::smembers($room_id);
+        //问题超过或者到第4条为结束标志
+        if(count($questions) > 4){
+          $message['is_end'] = 1;
+        }
+        //检测用户断线
+        Gateway::$registerAddress = '127.0.0.1:1238';
+        //获取当前组存活用户
+        $clients = Gateway::getUidListByGroup($room_id);
+        if(count($clients) < 2){
+          $message['is_end'] = 1;
+        }
+
+        //判断答案逻辑
+        $is_true = Option::where([
+          ["question_id", $question_id],
+          ["option_id", $option_id],
+          ["is_answer", 1],
+        ])->count();
+
+        $scores = Redis::get($room_id);
+        if($scores){
+          $scores = json_decode($scores, 1);
+        }else{
+          $scores = [];
+        }
+        if(!isset($scores[$user_id])){
+          $scores[$user_id] = 0;
+        }
+
+        if($is_true){
+            $scores[$user_id] = (int) $scores[$user_id] + 1;
+        }
+        Redis::set($room_id, json_encode($scores));
+        $message['is_true'] = $is_true;
+        $message['scores'] = $scores;
+        $message['user_id'] = $user_id;
+        $message['online'] = count($clients);
+        $message['type'] = 'answer';
+        //发送消息
+        Gateway::sendToGroup($room_id, json_encode($message));
+        $response['code'] = 0;
+        $response['msg'] = '回答成功';
+
+        //用户退出房间关闭房间等逻辑 todo ...
+
+        return response()->json($response);
+    }
+
+    /**
+     * 加入房间
+     */
+    public function Join(Request $request)
+    {
+        if(!$request->session()->has('user_id')){
+            $response['code'] = 400;
+            $response['msg'] = '请先登录';
+            return response()->json($response);
+        }
+
+        $user_id = $request->session()->get('user_id');
+        $username = $request->session()->get('username');
+        $avatar = $request->session()->get('avatar');
+        $client_id = $request->input('client_id');
+        $data = json_encode(['user_id'=>$user_id,'level'=>'T1','avatar'=>$avatar,'username'=>$username,'client_id'=>$client_id]);
+        Redis::lpush('match_list',$data);
+
+        $response['code'] = 0;
+        $response['msg'] = "已加入匹配队列";
+        return response()->json($response);
+    }
+
+    /**
+     * 退出房间
+     */
+    public function Quit(Request $request)
+    {
+        if(!$request->session()->has('user_id')){
+            $response['code'] = 400;
+            $response['msg'] = '请先登录';
+            return response()->json($response);
+        }
+        $username = $session->get('username');
+        $avatar = $session->get('avatar');
+
+        $user_id = $request->session()->get('user_id');
+        $username = $request->session()->get('username');
+        $avatar = $request->session()->get('avatar');
+        $client_id = $request->input('client_id');
+        $data = json_encode(['user_id'=>$user_id,'level'=>'T1','avatar'=>$avatar,'username'=>$username,'client_id'=>$client_id]);
+        Redis::lrem('match_list',1,$data);
+        //删除房间
+        Redis::del('T1');
+        
+        $response['code'] = 0;
+        $response['msg'] = "已取消匹配";
+        return response()->json($response);
+    }
+  
+
+}

+ 14 - 2
routes/home.php

@@ -1,7 +1,19 @@
 <?php
-
+//用户登陆页面
 Route::get('User/Login', 'UserController@Login');
+//用户登陆接口
 Route::post('User/Login', 'UserController@doLogin');
+//用户主页页面
 Route::get('User/Index', 'UserController@Index');
+//用户绑定接口
 Route::post('User/Bind', 'UserController@Bind');
-Route::get('User/Info', 'UserController@Info');
+//获取用户信息接口
+Route::get('User/Info', 'UserController@Info');
+//加入房间
+Route::get('Game/Join', 'GameController@Join');
+//退出房间
+Route::post('Game/Quit', 'GameController@Quit');
+//获取题目接口
+Route::get('Game/Question', 'GameController@Question');
+//答题接口
+Route::post('Game/Answer', 'GameController@Answer');