game2005api/webapp/controller/TeamController.class.php
aozhiwei 18e134bb3d 1
2021-12-10 19:25:24 +08:00

129 lines
3.6 KiB
PHP

<?php
require_once('models/User.php');
use phpcommon\SqlHelper;
use models\User;
class TeamController extends BaseAuthedController {
public function createTeam()
{
$nodeId = getReqVal('node_id', 1);
$teamUuid = $nodeId . '_' . md5($this->_getAccountId()) . $this->_getNowTime();
$userDb = $this->_getOrmUserInfo();
$userDto = User::info($userDb);
$userDto['createtime'] = $userDb['createtime'];
$teamDb = array(
'team_uuid' => $teamUuid,
'state' => 0,
'member_list' => array($userDto));
$r = $this->_getRedis($teamUuid);
$this->saveTeamDb($r, $teamUuid, $teamDb);
$this->_rspData(array(
'team_uuid' => $teamUuid
));
}
public function teamInfo()
{
$teamUuid = getReqVal('team_uuid', '');
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (empty($teamDb)) {
$this->_rspErr(1, '队伍已经解散');
return;
}
$r->pexpire(TEAMID_KEY . $teamUuid, 1000*600);
$this->_rspData(array(
'team_info' => $teamDb
));
}
public function joinTeam()
{
$teamUuid = getReqVal('team_uuid', '');
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (empty($teamDb)) {
$this->_rspErr(1, '队伍已经解散');
return;
}
if (count($teamDb['member_list']) >= 4) {
$this->_rspErr(2, '队伍已满');
return;
}
foreach ($teamDb['member_list'] as $member) {
if ($member['account_id'] == $this->_getAccountId()) {
$this->_rspData(array(
'team_uuid' => $teamUuid
));
return;
}
}
$userDb = User::find($this->_getAccountId());
$userDto = User::info($userDb);
$userDto['createtime'] = $userDb['createtime'];
array_push($teamDb['member_list'], $userDto);
$this->saveTeamDb($r, $teamUuid, $teamDb);
$this->_rspData(array(
'team_uuid' => $teamUuid
));
}
public function leaveTeam()
{
$teamUuid = getReqVal('team_uuid', '');
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (empty($teamDb)) {
$this->_rspOk();
return;
}
$newMemberList = array();
foreach ($teamDb['member_list'] as $member) {
if ($member['account_id'] != $this->_getAccountId()) {
array_push($newMemberList, $member);
}
}
$teamDb['member_list'] = $newMemberList;
$this->saveTeamDb($r, $teamUuid, $teamDb);
$this->_rspOk();
}
public function startGame()
{
$teamUuid = getReqVal('team_uuid', '');
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (empty($teamDb)) {
$this->_rspErr(1, '队伍已经解散');
return;
}
$teamDb['state'] = 1;
$this->saveTeamDb($r, $teamUuid, $teamDb);
$this->_rspOk();
}
private function readTeamDb($r, $teamUuid)
{
$teamDbStr = $r->get(TEAMID_KEY . $teamUuid);
if (empty($teamDbStr)) {
return null;
}
$teamDb = json_decode($teamDbStr, true);
return $teamDb;
}
private function saveTeamDb($r, $teamUuid, $teamDb)
{
$r->set(TEAMID_KEY . $teamUuid, json_encode($teamDb));
$r->pexpire(TEAMID_KEY . $teamUuid, 1000*600);
}
}