摇色子中的一些业务代码从匹配房间到开始游戏最后清算

xiaoxin

摇色子中的一些业务代码从匹配房间到开始游戏最后清算,都是写在一起的,自己也不知道性能怎么样,请大佬指点一下

   public function matchRoom()
    {
        $user = $this->user;
        $room_prefix = 'hltz_room';
        $hourse_grade = Cache::get('hourse_grade' . $user);
        $client_id = Cache::get('client_id' . $user);
        if (!$this->isEnoughMoney($hourse_grade, $user)) {
            return ReturnService::returnError(10002, '余额不足');
        }
        /*
        |--------------------------------------------------------------------------
        | 匹配房间。(初级房间hltz_room100-200|中级房间hltz_room200-300|高级房间hltz_room300-400)
        |--------------------------------------------------------------------------
        */
        for ($i = $hourse_grade * 100; $i < $hourse_grade * 100 + 100; $i++) {
            $room = $room_prefix . $i;
            $room_count = Gateway::getUidCountByGroup($room);
            $time2 = !!Cache::get($room) ? Cache::get($room) : time();
            $room_time = time() - $time2;
            /*
            |--------------------------------------------------------------------------
            | 匹配房间成功。(房间人数小于8|非房间人数大于2并且超时的)
            |--------------------------------------------------------------------------
            */
            if ($room_count < 8 || !($room_time >= 20 && $room_count >= 2)) {
                if ($room_count == 0) {
                    Cache::set($room, time());
                }
                Gateway::joinGroup($client_id, $room);
                $result = [];
                $user_res = $user;
                $user_res = $user;
                $result = 'join';
                $result = $user_res;
                Gateway::sendToGroup($room, json_encode($result));
                $room_last_count = Gateway::getUidCountByGroup($room);
                $room_time = time() - Cache::get($room);
                $grade_money = $hourse_grade == 1 ? 10 : ($hourse_grade == 2 ? 50 : ($hourse_grade == 3 ? 100 : ''));
                /*
                |--------------------------------------------------------------------------
                | 游戏开始。(在20秒内房间人数达到八个人或者超过20秒房间人数达到两人)
                |--------------------------------------------------------------------------
                */
                if ($room_time >= 20 && $room_last_count >= 2 || $room_last_count == 8) {
                    //告诉玩家游戏开始了,准备摇色子,并进入三秒倒计时
                    $result_game_start = 'start';
                    Gateway::sendToGroup($room, json_encode($result_game_start));
                    $group_uid_list = Gateway::getUidListByGroup($room);
                    /*
                    |--------------------------------------------------------------------------
                    | 游戏开始倒计时三秒
                    |--------------------------------------------------------------------------
                    */
                    sleep(3);
                    /*
                    |--------------------------------------------------------------------------
                    | 告诉玩家开始掷骰,并将扣钱之后的结果告诉玩家
                    |--------------------------------------------------------------------------
                    */
                    //摇色子,并扣除玩家金额
                    $this->roll($room, $grade_money);
                    /*
                   |--------------------------------------------------------------------------
                   | 等待色子转动三秒
                   |--------------------------------------------------------------------------
                   */
                    //三秒色子的旋转动画
                    sleep(3);
                    /*
                   |--------------------------------------------------------------------------
                   | 返回结果。(随机玩家点数,计算出最大的一个或几个,将玩家的所有钱分给最大的那些人)
                   |--------------------------------------------------------------------------
                   */
                    $this->roll_end($group_uid_list, $room_last_count, $grade_money, $room);
                    /*
                   |--------------------------------------------------------------------------
                   | 睡眠3秒
                   |--------------------------------------------------------------------------
                   */
                    //sleep(3);
                    /*
                   |--------------------------------------------------------------------------
                   | 解散群和清除相关redis数据
                   |--------------------------------------------------------------------------
                   */
                    Cache::rm('name');
                    foreach ($group_uid_list as $key => $val) {
                        Cache::rm('hourse_grade' . $val);
                    }
                    Cache::rm($room);
                    Gateway::ungroup($room);
                }
                return;
            }
        }
    }
阅读 942
0个回答