_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']); } { $currMatchDb = $this->readCurrMatchTeam($r); if (!empty($currMatchDb)) { unset($currMatchDb[$teamUuid]); $r->set(MATCH_CURRENT_TEAM_KEY, json_encode($currMatchDb)); $this->refreshKeyExpire($r, MATCH_CURRENT_TEAM_KEY, 1000*600); } } } $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 { $delTeams = array(); $selfTeamDb = $currMatchDb[$teamUuid]; if ($selfTeamDb && myself()->_getNowTime() - $selfTeamDb['match_time'] > 7) { $this->matchOk($r, $teamUuid, $selfTeamDb); $this->fillMatchInfo($r, $teamUuid, $matchInfo, $this->readMatchOk($r, $teamUuid)); array_push($delTeams, $teamUuid); } else { foreach ($currMatchDb as $key => $val) { $tmpTeamDb = $this->readTeamDb($r, $key); if ($tmpTeamDb['zid'] != $teamDb['zid'] || $tmpTeamDb['node_id'] != $teamDb['node_id'] || $tmpTeamDb['map_id'] != $teamDb['map_id']) { continue; } if (!empty($tmpTeamDb) && $this->_getNowTime() - $val['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($currMatchDb[$id]); } $r->set(MATCH_CURRENT_TEAM_KEY, json_encode($currMatchDb)); $this->refreshKeyExpire($r, MATCH_CURRENT_TEAM_KEY, 1000*600); } } } private function matchOk($r, $teamUuid, $currMatchDb) { { $matchOkDb = array( 'target_team' => $currMatchDb['team_uuid'], 'match_time' => $this->_getNowTime() ); $r->set(MATCH_OK_KEY . $teamUuid, json_encode($matchOkDb)); $this->refreshKeyExpire($r, MATCH_OK_KEY . $teamUuid, 1000*600); } { $matchOkDb = array( 'target_team' => $teamUuid, 'match_time' => $this->_getNowTime() ); $r->set(MATCH_OK_KEY . $currMatchDb['team_uuid'], json_encode($matchOkDb)); $this->refreshKeyExpire($r, MATCH_OK_KEY . $currMatchDb['team_uuid'], 1000*600); } } private function fillMatchInfo($r, $teamUuid, &$matchInfo, $matchOkDb) { if (empty($matchOkDb)) { return; } error_log(json_encode($matchOkDb)); $matchInfo['state'] = 1; $data = array( 'zid' => '', 'node_id' => '', 'room_uuid' => '', 'start_time' => $matchOkDb['match_time'], 'team_list' => array() ); $teamList = array(); { $teamDb = $this->readTeamDb($r, $teamUuid); array_push($matchInfo['team_list'], $teamDb); $teamInfo = array( 'team_uuid' => $teamDb['team_uuid'], 'members' => array() ); foreach ($teamDb['member_list'] as $val) { array_push($teamInfo['members'], array( 'account_id' => $val['account_id'] )); } array_push($teamList, $teamInfo); $data['zid'] = $teamDb['zid']; $data['node_id'] = $teamDb['node_id']; $data['room_uuid'] = $teamDb['team_uuid']; } { $teamDb = $this->readTeamDb($r, $matchOkDb['target_team']); if (!empty($teamDb) && $teamDb['team_uuid'] != $teamUuid) { array_push($matchInfo['team_list'], $teamDb); $teamInfo = array( 'team_uuid' => $teamDb['team_uuid'], 'members' => array() ); foreach ($teamDb['member_list'] as $val) { array_push($teamInfo['members'], array( 'account_id' => $val['account_id'] )); } array_push($teamList, $teamInfo); if (strcasecmp($teamDb['team_uuid'], $data['room_uuid']) < 0) { $data['room_uuid'] = $teamDb['team_uuid']; } } } $data['team_list'] = $teamList; $payload = md5(json_encode($data) . '520d8eAbB(8cf1^#$^&!@d833a42c820432PDAFE^^)') . ":moba_room|" . json_encode($data); $matchInfo['join_msg'] = array( 'team_uuid' => $teamUuid, 'payload' => $payload ); } }