搖色子中的一些業(yè)務代碼從匹配房間到開始游戲最后清算,都是寫在一起的,自己也不知道性能怎么樣,請大佬指點一下
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;
}
}
}