game2004api/webapp/controller/TeamController.class.php

438 lines
14 KiB
PHP

<?php
class TeamController{
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 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' => DBNAME_PREFIX . $mysql_conf['instance_id']
));
return $conn;
}
public function createTeam()
{
$node_id = 1;
if (isset($_REQUEST['node_id'])) {
$node_id = (int)$_REQUEST['node_id'];
}
$team_uuid = $node_id . '_' . md5($_REQUEST['account_id']) . time();
$cloth_id = 0;
$hair_id = 0;
$color_id = 0;
$hat_id = 0;
$rank = 1;
$equip_id = 0;
$model = false;
if (isset($_REQUEST['cloth_id'])) {
$cloth_id = $_REQUEST['cloth_id'];
}
if (isset($_REQUEST['hair_id'])) {
$hair_id = $_REQUEST['hair_id'];
}
if (isset($_REQUEST['color_id'])) {
$color_id = $_REQUEST['color_id'];
}
if (isset($_REQUEST['hat_id'])) {
$hat_id = $_REQUEST['hat_id'];
}
if (isset($_REQUEST['rank'])) {
$rank = $_REQUEST['rank'];
}
if (isset($_REQUEST['equip_id'])) {
$equip_id = $_REQUEST['equip_id'];
}
if (isset($_REQUEST['model'])) {
$model = $_REQUEST['model'];
}
$account_id = $_REQUEST['account_id'];
$conn = $this->getMysql($account_id);
$row = $conn->execQueryOne('SELECT game_times, win_times, kills, create_time FROM user WHERE accountid=:accountid;',
array(
'accountid' => $account_id,
));
$team_db = array(
'team_uuid' => $team_uuid,
'auto_fill' => $_REQUEST['auto_fill'],
'state' => 0,
'model' => $model,
'member_list' => array(
array(
'idx' => 1,
'account_id' => $_REQUEST['account_id'],
'name' => $_REQUEST['name'],
'avatar_url' => $_REQUEST['avatar_url'],
'cloth_id' => $cloth_id,
'hair_id' => $hair_id,
'color_id' => $color_id,
'hat_id' => $hat_id,
'rank' => $rank,
'equip_id' => $equip_id,
'game_times' => $row['game_times'],
'win_times' => $row['win_times'],
'kills' => $row['kills'],
'create_time' => $row['create_time'],
)
));
$r = $this->getRedis($team_uuid);
$r -> set(TEAMID_KEY . $team_uuid, json_encode($team_db));
$r -> pexpire(TEAMID_KEY . $team_uuid, 1000 * 600);
echo json_encode (array(
'errcode' => 0,
'errmsg' => '',
'team_uuid' => $team_uuid
));
}
public function teamInfo()
{
if (!isset($_REQUEST['team_uuid'])) {
return;
}
$team_uuid = $_REQUEST['team_uuid'];
$r = $this->getRedis($team_uuid);
$accountid = $_REQUEST['account_id'];
$sessionid = $_REQUEST['session_id'];
$user_db_str = $r->get(TEAMID_KEY . $team_uuid);
if (empty($user_db_str)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效1');
return;
}
$user_db = json_decode($user_db_str, true);
if (empty($user_db)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效2');
return;
}
$member_list = array();
foreach ($user_db['member_list'] as $member) {
array_push($member_list, array(
'idx' => $member['idx'],
'account_id' => $member['account_id'],
'name' => $member['name'],
'avatar_url' => $member['avatar_url'],
'cloth_id' => $member['cloth_id'],
'hair_id' => $member['hair_id'],
'color_id' => $member['color_id'],
'hat_id' => $member['hat_id'],
'rank' => $member['rank'],
'equip_id' => $member['equip_id'],
'game_times' => $member['game_times'],
'win_times' => $member['win_times'],
'kills' => $member['kills'],
'create_time' => $member['create_time'],
));
}
echo json_encode(array(
'errcode' => 0,
'errmsg'=> '',
'team_uuid' => $team_uuid,
'auto_fill' => $user_db['auto_fill'],
'state' => $user_db['state'],
'member_list' => $member_list,
'model' => $user_db['model'],
));
}
public function joinTeam()
{
if (!isset($_REQUEST['team_uuid'])) {
return;
}
$team_uuid = $_REQUEST['team_uuid'];
$r = $this->getRedis($team_uuid);
$user_db_str = $r->get(TEAMID_KEY.$team_uuid);
if (empty($user_db_str)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效1');
return;
}
$user_db = json_decode($user_db_str, true);
if (empty($user_db)) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session失效2');
return;
}
$member_num = count($user_db['member_list']);
if ($member_num >= 4) {
phpcommon\sendError(ERR_USER_BASE + 2,'队伍人数已满');
return;
}
$flag = 0;
foreach ($user_db['member_list'] as $member) {
if ($member['account_id'] == $_REQUEST['account_id']) {
$flag = 1;
break;
}
}
if ($flag == 1) {
phpcommon\sendError(ERR_USER_BASE + 3,'已在队伍中');
return;
}
$cloth_id = 0;
$hair_id = 0;
$color_id = 0;
$hat_id = 0;
$rank = 1;
$equip_id = 0;
if (isset($_REQUEST['cloth_id'])) {
$cloth_id = $_REQUEST['cloth_id'];
}
if (isset($_REQUEST['hair_id'])) {
$hair_id = $_REQUEST['hair_id'];
}
if (isset($_REQUEST['color_id'])) {
$color_id = $_REQUEST['color_id'];
}
if (isset($_REQUEST['hat_id'])) {
$hat_id = $_REQUEST['hat_id'];
}
if (isset($_REQUEST['rank'])) {
$rank = $_REQUEST['rank'];
}
if (isset($_REQUEST['equip_id'])) {
$equip_id = $_REQUEST['equip_id'];
}
$accountid = $_REQUEST['account_id'];
$conn = $this->getMysql($accountid);
$row = $conn->execQueryOne('SELECT game_times, win_times, kills, create_time FROM user WHERE accountid=:accountid;',
array(
'accountid' => $accountid,
));
array_push ($user_db['member_list'], array(
'idx' => $member_num + 1,
'account_id' => $_REQUEST['account_id'],
'name' => $_REQUEST['name'],
'avatar_url' => $_REQUEST['avatar_url'],
'cloth_id' => $cloth_id,
'hair_id' => $hair_id,
'color_id' => $color_id,
'hat_id' => $hat_id,
'rank' => $rank,
'equip_id' => $equip_id,
'game_times' => $row['game_times'],
'win_times' => $row['win_times'],
'kills' => $row['kills'],
'create_time' => $row['create_time'],
));
$r->set(TEAMID_KEY . $team_uuid, json_encode($user_db));
$r -> pexpire(TEAMID_KEY . $team_uuid, 1000 * 600);
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function kickoutMember()
{
if (!isset($_REQUEST['team_uuid'])) {
return;
}
$team_uuid = $_REQUEST['team_uuid'];
$r = $this->getRedis($team_uuid);
if (!$r) {
echo 'is null';
} else {
$user_db_str = $r->get(TEAMID_KEY . $team_uuid);
if (empty($user_db_str)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效1');
return;
}
$user_db = json_decode($user_db_str, true);
if (empty($user_db)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效2');
return;
}
$flag = 0;
foreach ($user_db['member_list'] as $member) {
if ($member['account_id'] == $_REQUEST['account_id']) {
if ($member['idx'] != 1) {
phpcommon\sendError(ERR_USER_BASE + 4,'你不是队长');
return;
}
foreach ($user_db['member_list'] as $member) {
if ($member['account_id'] == $_REQUEST['member_id']) {
$flag = $member['idx'] - 1;
break;
}
}
break;
}
}
unset($user_db['member_list'][$flag]);
$user_db['member_list'] = array_values($user_db['member_list']);
$i = 1;
foreach ($user_db['member_list'] as &$memberlist) {
$memberlist['idx'] = $i;
$i++;
}
$r->set(TEAMID_KEY . $team_uuid, json_encode($user_db));
$r -> pexpire(TEAMID_KEY . $team_uuid, 1000 * 600);
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function leaveTeam()
{
if (!isset($_REQUEST['team_uuid'])) {
return;
}
$team_uuid = $_REQUEST['team_uuid'];
$r = $this->getRedis($team_uuid);
if (!$r) {
echo 'is null';
} else {
$user_db_str = $r->get(TEAMID_KEY . $team_uuid);
if (empty($user_db_str)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效1');
return;
}
$user_db = json_decode($user_db_str, true);
if (empty($user_db)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效2');
return;
}
$flag = 0;
foreach ($user_db['member_list'] as $member) {
if ($member['account_id'] == $_REQUEST['account_id']) {
$flag = $member['idx'] - 1;
break;
}
}
unset($user_db['member_list'][$flag]);
$user_db['member_list'] = array_values($user_db['member_list']);
$i = 1;
foreach ($user_db['member_list'] as &$memberlist) {
$memberlist['idx'] = $i;
$i++;
}
$r->set(TEAMID_KEY . $team_uuid, json_encode($user_db));
$r -> pexpire(TEAMID_KEY . $team_uuid, 1000 * 600);
if (count($user_db['member_list']) == 0) {
$r->del(TEAMID_KEY . $team_uuid, json_encode($user_db));
}
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function updateTeam()
{
if (!isset($_REQUEST['team_uuid'])) {
return;
}
$team_uuid = $_REQUEST['team_uuid'];
$r = $this->getRedis($team_uuid);
if (!$r){
echo 'is null';
} else {
$user_db_str = $r->get(TEAMID_KEY . $team_uuid);
if (empty($user_db_str)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效1');
return;
}
$user_db = json_decode($user_db_str, true);
if (empty($user_db)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效2');
return;
}
foreach ($user_db['member_list'] as $member) {
if ($member['account_id'] == $_REQUEST['account_id']) {
if($member['idx'] != 1){
phpcommon\sendError(ERR_USER_BASE + 4,'你不是队长');
return;
}
}
}
$user_db['auto_fill'] = $_REQUEST['auto_fill'];
$r->set(TEAMID_KEY . $team_uuid, json_encode($user_db));
$r -> pexpire(TEAMID_KEY . $team_uuid, 1000 * 600);
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function startGame()
{
if (!isset($_REQUEST['team_uuid'])) {
return;
}
$team_uuid = $_REQUEST['team_uuid'];
$r = $this->getRedis($team_uuid);
if (!$r) {
echo 'is null';
} else {
$user_db_str = $r->get(TEAMID_KEY . $team_uuid);
if (empty($user_db_str)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效1');
return;
}
$user_db = json_decode($user_db_str, true);
if (empty($user_db)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效2');
return;
}
foreach ($user_db['member_list'] as $member) {
if ($member['account_id'] == $_REQUEST['account_id']) {
if($member['idx'] != 1) {
phpcommon\sendError(ERR_USER_BASE + 4,'你不是队长');
return;
}
}
}
$user_db['state'] = $_REQUEST['state'];
$r->set(TEAMID_KEY . $team_uuid, json_encode($user_db));
$r->pexpire(TEAMID_KEY . $team_uuid, 10 * 1000);
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
}
?>