From c0cd52e80d9eb965c2d5ce6a25bf913cf821821a Mon Sep 17 00:00:00 2001 From: wangwei01 Date: Thu, 28 Mar 2019 11:17:18 +0800 Subject: [PATCH] 1 --- webapp/bootstrap/init.php | 4 + webapp/controller/TeamController.class.php | 107 ++++++++++++++++++--- 2 files changed, 95 insertions(+), 16 deletions(-) diff --git a/webapp/bootstrap/init.php b/webapp/bootstrap/init.php index b2355bd..ec5b1df 100644 --- a/webapp/bootstrap/init.php +++ b/webapp/bootstrap/init.php @@ -3,4 +3,8 @@ ini_set('date.timezone','Asia/Shanghai'); require 'phpcommon/common.php'; +define('TEAMID_KEY', 'team_uuid:'); + require 'config_loader.php'; + + diff --git a/webapp/controller/TeamController.class.php b/webapp/controller/TeamController.class.php index bbf9e11..03a2498 100644 --- a/webapp/controller/TeamController.class.php +++ b/webapp/controller/TeamController.class.php @@ -31,10 +31,10 @@ class teamController{ )); $r = $this->getRedis($team_uuid); - $r->set(TEAMID_KEY . $team_uuid, json_encode($team_db)); - $r->pexpire(TEAMID_KEY . $team_uuid, 1000 * 3600); + $r -> set(TEAMID_KEY . $team_uuid, json_encode($team_db)); + $r -> pexpire(TEAMID_KEY . $team_uuid, 1000 * 3600); - echo json_encode(array( + echo json_encode (array( 'errcode' => 0, 'errmsg' => '', 'team_uuid' => $team_uuid @@ -48,7 +48,7 @@ class teamController{ if(!$r){ echo 'is null'; }else{ - $accountid = $_REQUEST['accout_id']; + $accountid = $_REQUEST['account_id']; $sessionid = $_REQUEST['session_id']; $team_uuid = $_REQUEST['team_uuid']; $user_db_str = $r->get(TEAMID_KEY . $team_uuid); @@ -82,45 +82,120 @@ class teamController{ public function joinTeam() { + $team_uuid = $_REQUEST['team_uuid']; $r = $this->getRedis($team_uuid); - if(!$r){ + if (!$r){ echo 'is null'; - }else{ + }else { $user_db_str = $r->get(TEAMID_KEY.$team_uuid); - if(empty($user_db_str)){ + if (empty($user_db_str)){ phpcommon\sendError(ERR_USER_BASE + 1,'session失效'); return; } $user_db = json_decode($user_db_str, true); - if(empty($user_db)){ + if (empty($user_db)){ phpcommon\sendError(ERR_USER_BASE + 1, 'session失效'); 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)); + $r->pexpire(TEAMID_KEY . $team_uuid, 1000 * 3600); } + 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; + } + 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']); + var_dump($user_db['member_list']); + + $i = 1; + foreach ($user_db['member_list'] as &$memberlist){ + $memberlist['idx'] = $i; + $i++; + } + var_dump($user_db); + + $r->set(TEAMID_KEY . $team_uuid, json_encode($user_db)); + $r->pexpire(TEAMID_KEY . $team_uuid, 1000 * 3600); + } + echo json_encode(array( + 'errcode' => 0, + 'errmsg' => '', + )); } public function updateTeam() { + $team_uuid = $_REQUEST['team_uuid']; $r = $this->getRedis($team_uuid); - if(!$r){ + 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失效'); + $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失效'); + if (empty($user_db)){ + phpcommon\sendError(ERR_USER_BASE + 1,'session失效2'); return; } $user_db['auto_fill'] = $_REQUEST['auto_fill']; - $redis->set(TEAMID_KEY.$team_uuid, json_encode($user_db)); - $redis->pexpire(TEAMID_KEY.$team_uuid, 1000 * 3600); + $r->set(TEAMID_KEY . $team_uuid, json_encode($user_db)); + $r->pexpire(TEAMID_KEY . $team_uuid, 1000 * 3600); } echo json_encode(array( 'errcode' => 0,