308 lines
9.5 KiB
PHP
308 lines
9.5 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;
|
|
}
|
|
|
|
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();
|
|
|
|
$team_db = array(
|
|
'team_uuid' => $team_uuid,
|
|
'auto_fill' => $_REQUEST['auto_fill'],
|
|
'state' => 0,
|
|
'member_list' => array(
|
|
array(
|
|
'idx' => 1,
|
|
'account_id' => $_REQUEST['account_id'],
|
|
'name' => $_REQUEST['name'],
|
|
'avatar_url' => $_REQUEST['avatar_url'],
|
|
)
|
|
));
|
|
|
|
$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()
|
|
{
|
|
$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'],
|
|
));
|
|
}
|
|
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,
|
|
));
|
|
}
|
|
|
|
|
|
public function joinTeam()
|
|
{
|
|
$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;
|
|
}
|
|
|
|
array_push ($user_db['member_list'], array(
|
|
'idx' => $member_num + 1,
|
|
'account_id' => $_REQUEST['account_id'],
|
|
'name' => $_REQUEST['name'],
|
|
'avatar_url' => $_REQUEST['avatar_url'],
|
|
));
|
|
$r->set(TEAMID_KEY . $team_uuid, json_encode($user_db));
|
|
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => '',
|
|
));
|
|
}
|
|
|
|
public function kickoutMember()
|
|
{
|
|
$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));
|
|
}
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => '',
|
|
));
|
|
}
|
|
|
|
public function leaveTeam()
|
|
{
|
|
$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));
|
|
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()
|
|
{
|
|
$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));
|
|
}
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => '',
|
|
));
|
|
}
|
|
|
|
public function startGame()
|
|
{
|
|
$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' => '',
|
|
));
|
|
}
|
|
}
|
|
?>
|