game2006api/webapp/controller/MatchController.class.php
aozhiwei 596204edca 1
2023-12-26 18:10:57 +08:00

205 lines
6.3 KiB
PHP

<?php
require_once('models/User.php');
require_once('models/Hero.php');
require_once('models/Gun.php');
require_once('models/ChipPage.php');
require_once('models/HeroPreset.php');
require_once('models/HeroSkin.php');
require_once('mt/PveGemini.php');
require_once('mt/Skill.php');
require_once('mt/StarLevel.php');
require_once('services/PropertyChgService.php');
use phpcommon\SqlHelper;
use models\User;
use models\Hero;
use models\Gun;
use models\ChipPage;
use models\HeroPreset;
use models\HeroSkin;
class MatchController extends BaseAuthedController {
public function getMatchInfo()
{
$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;
}
$matchInfo = array(
'state' => 0,
'team_list' => array()
);
$matchOkDb = $this->readMatchOk($r, $teamUuid);
if ($matchOkDb) {
$this->refreshKeyExpire($r, MATCH_OK_KEY . $teamUuid, 1000*600);
$this->refreshKeyExpire($r, MATCH_OK_KEY . $matchOkDb['target_team'], 1000*600);
$this->refreshKeyExpire($r, TEAMID_KEY . $matchOkDb['target_team'], 1000*600);
$this->fillMatchInfo($r, $teamUuid, $matchInfo, $matchOkDb);
} else {
$this->execMatch($r, $teamUuid, $teamDb, $matchInfo);
}
$this->_rspData($matchInfo);
}
public function cancel()
{
$teamUuid = getReqVal('team_uuid', '');
$r = $this->_getRedis($teamUuid);
$teamDb = $this->readTeamDb($r, $teamUuid);
if (!empty($teamDb)) {
$r->del(MATCH_OK_KEY . $teamUuid);
$matchOkDb = $this->readMatchOk($r, $teamUuid);
if ($matchOkDb) {
$r->del(MATCH_OK_KEY . $matchOkDb['target_team']);
}
}
$this->_rspOk();
}
private function readTeamDb($r, $teamUuid)
{
$teamDbStr = $r->get(TEAMID_KEY . $teamUuid);
if (empty($teamDbStr)) {
return null;
}
$this->refreshKeyExpire($r, TEAMID_KEY . $teamUuid, 1000*600);
$teamDb = json_decode($teamDbStr, true);
return $teamDb;
}
/*
{
"": {
"team_uuid": "dafsdf"
"match_time": 231434
}
}
*/
private function readCurrMatchTeam($r)
{
$teamDbStr = $r->get(MATCH_CURRENT_TEAM_KEY);
if (empty($teamDbStr)) {
return null;
}
$teamDb = json_decode($teamDbStr, true);
return $teamDb;
}
/*
{
"target_team": "dafsdf"
"match_time": 231434
}
*/
private function readMatchOk($r, $teamUuid)
{
$teamDbStr = $r->get(MATCH_OK_KEY . $teamUuid);
if (empty($teamDbStr)) {
return null;
}
$this->refreshKeyExpire($r, MATCH_OK_KEY . $teamUuid, 1000*600);
$teamDb = json_decode($teamDbStr, true);
return $teamDb;
}
private function refreshKeyExpire($r, $key, $time)
{
$r->pexpire($key, $time);
}
private function execMatch($r, $teamUuid, $teamDb, &$matchInfo)
{
$currMatchDb = $this->readCurrMatchTeam($r);
if (empty($currMatchDb)) {
$currMatchDb = array(
$teamUuid => array(
'team_uuid' => $teamUuid,
'match_time' => $this->_getNowTime()
)
);
$r->set(MATCH_CURRENT_TEAM_KEY, json_encode($currMatchDb));
$this->refreshKeyExpire($r, MATCH_CURRENT_TEAM_KEY, 1000*600);
} else {
$currMatchDb = json_decode($currMatchDb, true);
$delTeams = array();
foreach ($currMatchDb as $key => $val) {
$tmpTeamDb = $this->readTeamDb($r, $key);
if (!empty($tmpTeamDb) || $this->_getNowTime() - $tmpTeamDb['match_time'] > 120) {
array_push($delTeams, $key);
} else {
$found = false;
if ($key == $teamUuid) {
$found = true;
} else {
foreach ($val['member_list'] as $member) {
if ($member['account_id'] == myself()->_getAccountId()) {
$found = true;
break;
}
}
}
if (!$found) {
$this->matchOk($r, $teamUuid, $val);
$this->fillMatchInfo($r, $teamUuid, $matchInfo, $this->readMatchOk($r, $teamUuid));
array_push($delTeams, $key);
}
}
}//end foreach $currMatchDb
if (count($delTeams) > 0) {
foreach ($delTeams as $id) {
unset($delTeams[$id]);
}
}
}
}
private function matchOk($r, $teamUuid, $currMatchDb)
{
{
$r->del(MATCH_CURRENT_TEAM_KEY);
}
{
$matchOkDb = array(
'target_team' => $currMatchDb['current_team'],
'match_time' => $this->_getNowTime()
);
$r->set(MATCH_OK_KEY . $teamUuid, json_encode($currMatchDb));
$this->refreshKeyExpire($r, MATCH_OK_KEY . $teamUuid, 1000*600);
}
{
$matchOkDb = array(
'target_team' => $teamUuid,
'match_time' => $this->_getNowTime()
);
$r->set(MATCH_OK_KEY . $currMatchDb['current_team'], json_encode($currMatchDb));
$this->refreshKeyExpire($r, MATCH_OK_KEY . $currMatchDb['current_team'], 1000*600);
}
}
private function fillMatchInfo($r, $teamUuid, &$matchInfo, $matchOkDb)
{
if (empty($matchOkDb)) {
return;
}
$matchInfo['state'] = 1;
{
$teamDb = $this->readTeamDb($r, $teamUuid);
array_push($matchInfo['team_list'], $teamDb);
}
{
$teamDb = $this->readTeamDb($r, $matchOkDb['target_team']);
if (!empty($teamDb) && $teamDb['team_uuid'] != $teamUuid) {
array_push($matchInfo['team_list'], $teamDb);
}
}
}
}