game2006api/webapp/controller/TeamController.class.php
hujiabin 1766637e93 1
2022-09-19 14:55:38 +08:00

358 lines
12 KiB
PHP

<?php
require_once('models/User.php');
require_once('mt/PveGemini.php');
use phpcommon\SqlHelper;
use models\User;
class TeamController extends BaseAuthedController {
const MATCH_MODE_NORMAL = 0;
const MATCH_MODE_RANK = 1;
const MATCH_MODE_PVE = 2;
public function createTeam()
{
$nodeId = getReqVal('node_id', 1);
$matchMode = getReqVal('match_mode', 0);
$pveInstanceId = getReqVal('pve_instance_id', 0);
$zid = myself()->_getZid();
$teamUuid = $nodeId . '_' .
$zid . '_' .
md5($this->_getAccountId() . $this->_getNowTime());
/*if (!in_array(array
(
self::MATCH_MODE_NORMAL,
self::MATCH_MODE_RANK,
self::MATCH_MODE_PVE,
),
$matchMode)) {
$this->_rspErr(1, 'match mode error');
return;
}*/
$userDb = $this->_getOrmUserInfo();
//验证pve_instance_id合法性
if ($pveInstanceId){
$pveGame = \mt\PveGemini::get($pveInstanceId);
if (!$pveGame){
$this->_rspErr(1, 'pve_instance_id error');
return;
}
if (!in_array($pveInstanceId,\mt\PveGemini::getAbleCombatMeta($userDb['pve_instance_id']))){
$this->_rspErr(1, 'pve_instance_id error');
return;
}
}
$userDto = User::info($userDb);
$userDto['is_leader'] =1;
$userDto['createtime'] = $userDb['createtime'];
$teamDb = array(
'team_uuid' => $teamUuid,
'state' => 0,
'payload' => '',
'match_mode' => $matchMode,
'pve_instance_id' => $pveInstanceId,
'slot_num' => 4,
'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, 'The team has been disbanded');
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, 'The team has been disbanded');
return;
}
if (count($teamDb['member_list']) >= $teamDb['slot_num']) {
$this->_rspErr(2, 'The team is full');
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());
//验证pve_instance_id合法性
if ($teamDb['pve_instance_id']>0){
if (!in_array($teamDb['pve_instance_id'],\mt\PveGemini::getAbleCombatMeta($userDb['pve_instance_id']))) {
$this->_rspErr(1, 'No challenge');
return;
}
}
$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->_rspErr(1, 'The team has been disbanded');
return;
}
$newMemberList = array();
$isLeader = false;
foreach ($teamDb['member_list'] as $member) {
if ($member['account_id'] == $this->_getAccountId() && $member['is_leader'] == 1){
$isLeader = true;
}
if ($member['account_id'] != $this->_getAccountId()) {
array_push($newMemberList, $member);
}
}
if (count($newMemberList)<1){
$this->delTeamDb($r, $teamUuid);
$this->_rspOk();
return;
}
if ($isLeader){
$newMemberList[0]['is_leader'] = 1;
}
$teamDb['member_list'] = $newMemberList;
$this->saveTeamDb($r, $teamUuid, $teamDb);
$this->_rspOk();
}
public function kickout()
{
$teamUuid = getReqVal('team_uuid', '');
$account_id = getReqVal('target_id', '');
if (! $account_id){
$this->_rspErr(1, 'target_id param error');
return;
}
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (empty($teamDb)) {
$this->_rspErr(1, 'The team has been disbanded');
return;
}
if(! in_array($account_id,array_column($teamDb['member_list'],'account_id'))){
$this->_rspErr(1, 'The team do not have users');
return;
}
if ($account_id == $this->_getAccountId()){
$this->_rspErr(1, 'do not get myself out of line');
return;
}
$newMemberList = array();
foreach ($teamDb['member_list'] as $member) {
if ($member['account_id'] == $this->_getAccountId() && $member['is_leader'] != 1){
$this->_rspErr(1, 'You are not the captain.');
return;
}
if ($member['account_id'] != $account_id) {
array_push($newMemberList, $member);
}
}
$teamDb['member_list'] = $newMemberList;
$this->saveTeamDb($r, $teamUuid, $teamDb);
$this->_rspOk();
}
public function closeSlot()
{
$teamUuid = getReqVal('team_uuid', '');
$slot_id = getReqVal('slot_id', 0);
if (! in_array($slot_id,array(1,2,3,4)) ){
$this->_rspErr(1, 'slot_id param error');
return;
}
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (empty($teamDb)) {
$this->_rspErr(1, 'The team has been disbanded');
return;
}
foreach ($teamDb['member_list'] as $member) {
if ($member['account_id'] == $this->_getAccountId() && $member['is_leader'] != 1){
$this->_rspErr(1, 'You are not the captain.');
return;
}
}
if ($teamDb['slot_num'] <= 1){
$this->_rspErr(1, 'Minimum size of team');
return;
}
if ($teamDb['member_list'][$slot_id-1]['account_id'] == $this->_getAccountId()){
$this->_rspErr(1, 'do not get myself out of line');
return;
}
if(array_key_exists($slot_id-1,$teamDb['member_list'])){
unset($teamDb['member_list'][$slot_id-1]);
$teamDb['slot_num']-=1;
}else{
$teamDb['slot_num']-=1;
}
$this->saveTeamDb($r, $teamUuid, $teamDb);
$this->_rspOk();
}
public function openSlot()
{
$teamUuid = getReqVal('team_uuid', '');
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (empty($teamDb)) {
$this->_rspOk();
return;
}
foreach ($teamDb['member_list'] as $member) {
if ($member['account_id'] == $this->_getAccountId() && $member['is_leader'] != 1){
$this->_rspErr(1, 'You are not the captain.');
return;
}
}
if ($teamDb['slot_num'] >= 4){
$this->_rspErr(1, 'Maximum size of team');
return;
}
$teamDb['slot_num']+=1;
$this->saveTeamDb($r, $teamUuid, $teamDb);
$this->_rspOk();
}
public function handover()
{
$teamUuid = getReqVal('team_uuid', '');
$account_id = getReqVal('target_id', '');
if (! $account_id){
$this->_rspErr(1, 'target_id param error');
return;
}
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (empty($teamDb)) {
$this->_rspErr(1, 'The team has been disbanded');
return;
}
if(! in_array($account_id,array_column($teamDb['member_list'],'account_id'))){
$this->_rspErr(1, 'The team do not have users');
return;
}
foreach ($teamDb['member_list'] as $member) {
if ($member['account_id'] == $this->_getAccountId() && $member['is_leader'] != 1){
$this->_rspErr(1, 'You are not the captain.');
return;
}
}
if ($account_id == $this->_getAccountId()){
$this->_rspErr(1, " You're already the captain ");
return;
}
foreach ($teamDb['member_list'] as &$member) {
if ($member['account_id'] == $this->_getAccountId()){
$member['is_leader'] = 0;
}
if ($member['account_id'] == $account_id){
$member['is_leader'] = 1;
}
}
$this->saveTeamDb($r, $teamUuid, $teamDb);
$this->_rspOk();
}
public function cancel()
{
$teamUuid = getReqVal('team_uuid', '');
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (empty($teamDb)) {
$this->_rspErr(1, 'The team has been disbanded');
return;
}
foreach ($teamDb['member_list'] as $member) {
if ($member['account_id'] == $this->_getAccountId() && $member['is_leader'] != 1){
$this->_rspErr(1, 'You are not the captain.');
return;
}
}
$teamDb['state'] = 0;
$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, 'The team has been disbanded');
return;
}
foreach ($teamDb['member_list'] as $member) {
if ($member['account_id'] == $this->_getAccountId() && $member['is_leader'] != 1){
$this->_rspErr(1, 'You are not the captain.');
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);
}
private function delTeamDb($r, $teamUuid){
$r->del(TEAMID_KEY . $teamUuid);
}
}