307 lines
12 KiB
PHP
307 lines
12 KiB
PHP
<?php
|
|
|
|
|
|
class MatchController{
|
|
|
|
protected function getRedis($team_uuid)
|
|
{
|
|
$redis_conf = getRedisConfig(crc32($team_uuid));
|
|
$r = new phpcommon\Redis(array(
|
|
'host' => $redis_conf['host'],
|
|
'port' => $redis_conf['port'],
|
|
'passwd' => $redis_conf['passwd']
|
|
|
|
));
|
|
return $r;
|
|
}
|
|
|
|
protected function getParameter($para_id)
|
|
{
|
|
$parameter_meta_cluster = require('../res/parameter@parameter.php');
|
|
$parameter_meta = getParameterConfig($parameter_meta_cluster, $para_id);
|
|
$p = array(
|
|
'id' => $parameter_meta['id'],
|
|
'param_name' => $parameter_meta['param_name'],
|
|
'param_value' => $parameter_meta['param_value'],
|
|
);
|
|
return $p;
|
|
}
|
|
|
|
protected function getRobot($robot_id, $num)
|
|
{
|
|
$address = '../res/robot@robot' . $num . '.php';
|
|
$robot_meta_cluster = require($address);
|
|
$robot_meta = getRobotConfig($robot_meta_cluster, $robot_id);
|
|
$rob = array(
|
|
'id' => $robot_meta['id'],
|
|
'name' => $robot_meta['name'],
|
|
'avatar_url' => $robot_meta['avatar_url'],
|
|
);
|
|
return $rob;
|
|
}
|
|
|
|
public function randMatch()
|
|
{
|
|
$node_id = 1;
|
|
if (isset($_REQUEST['node_id'])) {
|
|
$node_id = (int)$_REQUEST['node_id'];
|
|
}
|
|
$room_uuid = $_REQUEST['room_uuid'];
|
|
//查找当前房间
|
|
$r = array();
|
|
if ($room_uuid == null) {
|
|
$r = $this->getRedis($node_id);
|
|
$current_db_str = $r->get('game2002:match:current_room');
|
|
if (empty($current_db_str)) {
|
|
$room_uuid = $node_id . '_' . md5($_REQUEST['account_id']) . time();
|
|
$current_db = array(
|
|
'room_uuid' => $room_uuid,
|
|
);
|
|
$r -> set('game2002:match:current_room', json_encode($current_db));
|
|
} else {
|
|
$current_db = json_decode($current_db_str, true);
|
|
$room_uuid = $current_db['room_uuid'];
|
|
$r = $this->getRedis($current_db['room_uuid']);
|
|
}
|
|
} else {
|
|
$r = $this->getRedis($room_uuid);
|
|
}
|
|
//获取当前房间信息
|
|
$join_time = 2;
|
|
$room_state = 0;
|
|
$downtime = 0;
|
|
$tank_id = $this->getTankid($_REQUEST['account_id']);
|
|
$tank_skin = $this->getTankSkinid($_REQUEST['account_id']);
|
|
$room = $this->getRedis($room_uuid);
|
|
$room_db_str = $room->get($room_uuid);
|
|
if (empty($room_db_str)) {
|
|
$room_db = array(
|
|
'room_uuid' => $room_uuid,
|
|
'create_time' => time(),
|
|
'last_join_time' => time(),
|
|
'prepare_time' => 0,
|
|
'member_list' => array(
|
|
array(
|
|
'account_id' => $_REQUEST['account_id'],
|
|
'name' => $_REQUEST['name'],
|
|
'avatar_url' => $_REQUEST['avatar_url'],
|
|
'tank_id' => $tank_id,
|
|
'tankskin_id' => $tank_skin
|
|
)
|
|
));
|
|
$room -> set($room_uuid, json_encode($room_db));
|
|
$room -> pexpire($room_uuid, 1000 * 300);
|
|
} else {
|
|
$room_db = json_decode($room_db_str, true);
|
|
if (empty($room_db)) {
|
|
phpcommon\sendError(ERR_USER_BASE + 1,'session失效');
|
|
return;
|
|
}
|
|
//判断玩家是否在当前房间
|
|
$flag = 0;
|
|
foreach ($room_db['member_list'] as $member) {
|
|
if ($member['account_id'] == $_REQUEST['account_id']) {
|
|
$flag = 1;
|
|
break;
|
|
}
|
|
}
|
|
//判断是否需要创建新房间
|
|
if ((count($room_db['member_list']) >= 10 && $flag != 1)
|
|
|| (time() - $room_db['prepare_time'] >= 3 && $room_db['prepare_time'] != 0)) {
|
|
//创建新房间
|
|
$room_uuid = $node_id . '_' . md5($_REQUEST['account_id']) . time();
|
|
$room_db = array(
|
|
'room_uuid' => $room_uuid,
|
|
'create_time' => time(),
|
|
'last_join_time' => time(),
|
|
'prepare_time' => 0,
|
|
'member_list' => array(
|
|
array(
|
|
'account_id' => $_REQUEST['account_id'],
|
|
'name' => $_REQUEST['name'],
|
|
'avatar_url' => $_REQUEST['avatar_url'],
|
|
'tank_id' => $tank_id,
|
|
'tankskin_id' => $tank_skin
|
|
)
|
|
));
|
|
$room -> set($room_uuid, json_encode($room_db));
|
|
$room -> pexpire($room_uuid, 1000 * 300);
|
|
//改变当前房间room_uuid
|
|
$current_db_str = $r->get('game2001:match:current_room');
|
|
$current_db = json_decode($current_db_str, true);
|
|
$current_db['room_uuid'] = $room_uuid;
|
|
$r -> set('game2002:match:current_room', json_encode($current_db));
|
|
} else if (count($room_db['member_list']) < 10) {
|
|
//判断是否添加机器人
|
|
if ($flag == 1) {
|
|
$p_min = $this->getParameter(16);
|
|
$min_num = $p_min['param_value'];
|
|
$p_max = $this->getParameter(17);
|
|
$max_num = $p_max['param_value'];
|
|
$randrobot_num = Rand($min_num, $max_num);
|
|
$robot_num = min((10 - count($room_db['member_list'])), $randrobot_num);
|
|
if (time() >= $join_time + $room_db['last_join_time']) {
|
|
for ($i = 0; $i < $robot_num; $i++) {
|
|
//添加机器人
|
|
$num = count($room_db['member_list']);
|
|
$random = Rand(1, 100);
|
|
$robot_id = 1000 + ($num - 1) * 100 + $random;
|
|
$rob = $this->getRobot($robot_id, $num);
|
|
$tank_id = Rand(1, 9) + 15000;
|
|
array_push ($room_db['member_list'], array(
|
|
'account_id' => $rob['id'],
|
|
'name' => $rob['name'],
|
|
'avatar_url' => $rob['avatar_url'],
|
|
'tank_id' => $tank_id,
|
|
'tankskin_id' => 0
|
|
));
|
|
}
|
|
$room_db['last_join_time'] = time();
|
|
}
|
|
} else {
|
|
//加入房间
|
|
array_push ($room_db['member_list'], array(
|
|
'account_id' => $_REQUEST['account_id'],
|
|
'name' => $_REQUEST['name'],
|
|
'avatar_url' => $_REQUEST['avatar_url'],
|
|
'tank_id' => $tank_id,
|
|
'tankskin_id' => $tank_skin
|
|
));
|
|
$room_db['last_join_time'] = time();
|
|
}
|
|
//更新倒计时状态或者开始游戏
|
|
if (count($room_db['member_list']) >= 10) {
|
|
if ($room_db['prepare_time'] == 0) {
|
|
$room_db['prepare_time'] = time();
|
|
$downtime = 3 - (time() - $room_db['prepare_time']);
|
|
$room_state = 1;
|
|
} else {
|
|
if (time() - $room_db['prepare_time'] >= 3) {
|
|
$downtime = 0;
|
|
$room_state = 2;
|
|
} else {
|
|
$downtime = 3 - (time() - $room_db['prepare_time']);
|
|
}
|
|
}
|
|
}
|
|
$room -> set($room_uuid, json_encode($room_db));
|
|
$room -> pexpire($room_uuid, 1000 * 300);
|
|
} else if ($room_db['prepare_time'] != 0) {
|
|
if (time() - $room_db['prepare_time'] >= 3) {
|
|
$downtime = 0;
|
|
$room_state = 2;
|
|
} else {
|
|
$downtime = 3 - (time() - $room_db['prepare_time']);
|
|
}
|
|
}
|
|
}
|
|
$room = $this->getRedis($room_uuid);
|
|
$room_db_str = $room->get($room_uuid);
|
|
$room_db = json_decode($room_db_str, true);
|
|
$member_list = array();
|
|
foreach ($room_db['member_list'] as $member) {
|
|
array_push($member_list, array(
|
|
'account_id' => $member['account_id'],
|
|
'name' => $member['name'],
|
|
'avatar_url' => $member['avatar_url'],
|
|
'tank_id' => $member['tank_id'],
|
|
'tankskin_id' => $member['tankskin_id'],
|
|
));
|
|
}
|
|
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg'=> '',
|
|
'room_uuid' => $room_uuid,
|
|
'room_state' => $room_state,
|
|
'downtime' => $downtime,
|
|
'member_list' => $member_list,
|
|
));
|
|
}
|
|
|
|
public function cancleMatch()
|
|
{
|
|
$account_id = $_REQUEST['account_id'];
|
|
$room_uuid = $_REQUEST['room_uuid'];
|
|
$r = $this->getRedis($room_uuid);
|
|
if (!$r) {
|
|
die();
|
|
return;
|
|
}
|
|
$room_db_str = $r->get($room_uuid);
|
|
if (empty($room_db_str)) {
|
|
phpcommon\sendError(ERR_USER_BASE + 1,'session失效1');
|
|
return;
|
|
}
|
|
$room_db = json_decode($room_db_str, true);
|
|
if (empty($room_db)) {
|
|
phpcommon\sendError(ERR_USER_BASE + 1,'session失效2');
|
|
return;
|
|
}
|
|
if ($room_db['prepare_time'] != 0) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2,'游戏即将开始,无法退出匹配');
|
|
return;
|
|
}
|
|
$flag = 0;
|
|
foreach ($room_db['member_list'] as $member) {
|
|
if ($member['account_id'] == $account_id) {
|
|
break;
|
|
}
|
|
$flag++;
|
|
}
|
|
unset($room_db['member_list'][$flag]);
|
|
$room_db['member_list'] = array_values($room_db['member_list']);
|
|
$r->set($room_uuid, json_encode($room_db));
|
|
$r -> pexpire($room_uuid, 1000 * 300);
|
|
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => '',
|
|
));
|
|
}
|
|
|
|
public function startGame()
|
|
{
|
|
$account_id = $_REQUEST['account_id'];
|
|
$room_uuid = $_REQUEST['room_uuid'];
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg'=> '',
|
|
));
|
|
}
|
|
|
|
protected function getTankid($accountid)
|
|
{
|
|
$conn = $this->getMysql($accountid);
|
|
$row = $conn->execQueryOne('SELECT tank_id FROM tank WHERE accountid=:account_id AND tank_status=0;',
|
|
array(
|
|
':account_id' => $accountid,
|
|
));
|
|
return $row['tank_id'];
|
|
}
|
|
|
|
protected function getTankSkinid($accountid)
|
|
{
|
|
$conn = $this->getMysql($accountid);
|
|
$row = $conn->execQueryOne('SELECT skin_id FROM skin WHERE accountid=:account_id AND skin_status=0;',
|
|
array(
|
|
':account_id' => $accountid,
|
|
));
|
|
return $row['skin_id'];
|
|
}
|
|
|
|
protected function getMysql($account_id)
|
|
{
|
|
$mysql_conf = getMysqlConfig(crc32($account_id));
|
|
$conn = new phpcommon\Mysql(array(
|
|
'host' => $mysql_conf['host'],
|
|
'port' => $mysql_conf['port'],
|
|
'user' => $mysql_conf['user'],
|
|
'passwd' => $mysql_conf['passwd'],
|
|
'dbname' => 'gamedb2002_' . $mysql_conf['instance_id']
|
|
));
|
|
return $conn;
|
|
}
|
|
}
|
|
?>
|