From 8414077d7a6b2a827d292f1b1347c7c7b3ed8ddb Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 22 Nov 2013 21:17:38 +0000 Subject: [PATCH] - bugfix for headless server's reporting of winning players and team. - masterserver returns proper JSON header now --- source/glest_game/game/game.cpp | 61 +++++++++------ source/glest_game/game/game_settings.h | 4 + source/glest_game/main/battle_end.cpp | 6 +- source/glest_game/world/world.cpp | 99 ++++++++++++++----------- source/glest_game/world/world.h | 4 +- source/masterserver/showServersJson.php | 1 + 6 files changed, 106 insertions(+), 69 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index e17f9bfc..bc0fb829 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -3489,11 +3489,13 @@ void Game::tryPauseToggle(bool pauseValue) { void Game::startMarkCell() { int totalMarkedCellsForPlayer = 0; - for(std::map::iterator iterMap = mapMarkedCellList.begin(); - iterMap != mapMarkedCellList.end(); ++iterMap) { - MarkedCell &bm = iterMap->second; - if(bm.getPlayerIndex() == world.getThisFaction()->getStartLocationIndex()) { - totalMarkedCellsForPlayer++; + if(world.getThisFaction() != NULL) { + for(std::map::iterator iterMap = mapMarkedCellList.begin(); + iterMap != mapMarkedCellList.end(); ++iterMap) { + MarkedCell &bm = iterMap->second; + if(bm.getPlayerIndex() == world.getThisFaction()->getStartLocationIndex()) { + totalMarkedCellsForPlayer++; + } } } @@ -5677,9 +5679,17 @@ void Game::checkWinnerStandard() { // did some team win if(teamsAlive.size() <= 1) { + if(this->masterserverMode == true) { + printf("Game finished...\n"); + } for(int i=0; i< world.getFactionCount(); ++i) { - if(i != world.getThisFactionIndex() && teamsAlive.find(world.getFaction(i)->getTeam()) != teamsAlive.end()) { + Faction *faction = world.getFaction(i); + + if(i != world.getThisFactionIndex() && teamsAlive.find(faction->getTeam()) != teamsAlive.end()) { world.getStats()->setVictorious(i); + if(this->masterserverMode == true) { + printf("Player: %s is on the winning team #: %d\n",this->gameSettings.getNetworkPlayerName(i).c_str(),faction->getTeam()); + } } } gameOver= true; @@ -5811,13 +5821,18 @@ void Game::checkWinnerScripted() { // END } - scriptManager.onGameOver(scriptManager.getPlayerModifiers(world.getThisFactionIndex())->getWinner()); - - if(scriptManager.getPlayerModifiers(world.getThisFactionIndex())->getWinner()){ + if(this->masterserverMode == true || world.getThisFaction()->getPersonalityType() == fpt_Observer) { showWinMessageBox(); } - else{ - showLoseMessageBox(); + else { + scriptManager.onGameOver(scriptManager.getPlayerModifiers(world.getThisFactionIndex())->getWinner()); + + if(scriptManager.getPlayerModifiers(world.getThisFactionIndex())->getWinner()){ + showWinMessageBox(); + } + else{ + showLoseMessageBox(); + } } } } @@ -5827,24 +5842,28 @@ bool Game::factionLostGame(int factionIndex) { } bool Game::factionLostGame(const Faction *faction) { - for(int i=0; igetUnitCount(); ++i) { - const UnitType *ut = faction->getUnit(i)->getType(); - if(ut->getCountInVictoryConditions() == ucvcNotSet) { - if(faction->getUnit(i)->getType()->hasSkillClass(scBeBuilt)) { + if(faction != NULL) { + for(int i=0; igetUnitCount(); ++i) { + const UnitType *ut = faction->getUnit(i)->getType(); + if(ut->getCountInVictoryConditions() == ucvcNotSet) { + if(faction->getUnit(i)->getType()->hasSkillClass(scBeBuilt)) { + return false; + } + } + else if(ut->getCountInVictoryConditions() == ucvcTrue) { return false; } } - else if(ut->getCountInVictoryConditions() == ucvcTrue) { - return false; - } } return true; } bool Game::hasBuilding(const Faction *faction) { - for(int i=0; igetUnitCount(); ++i) { - if(faction->getUnit(i)->getType()->hasSkillClass(scBeBuilt)) { - return true; + if(faction != NULL) { + for(int i=0; igetUnitCount(); ++i) { + if(faction->getUnit(i)->getType()->hasSkillClass(scBeBuilt)) { + return true; + } } } return false; diff --git a/source/glest_game/game/game_settings.h b/source/glest_game/game/game_settings.h index 9a4b7c63..521398c0 100644 --- a/source/glest_game/game/game_settings.h +++ b/source/glest_game/game/game_settings.h @@ -196,6 +196,10 @@ public: const string &getScenario() const {return scenario;} const string &getScenarioDir() const {return scenarioDir;} const string &getFactionTypeName(int factionIndex) const { + if(factionIndex == -1) { + static string HEADLESS_FACTION = "headless-server"; + return HEADLESS_FACTION; + } if(factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { char szBuf[8096]=""; snprintf(szBuf,8096,"In [%s] Invalid factionIndex = %d\n",__FUNCTION__,factionIndex); diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index 0e67b983..911d5f49 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -264,7 +264,7 @@ string BattleEnd::getBattleEndMusic(bool won) { void BattleEnd::initBackgroundMusic() { string music = ""; - if(stats.getTeam(stats.getThisFactionIndex()) != GameConstants::maxPlayers -1 + fpt_Observer) { + if(stats.getThisFactionIndex() > 0 && stats.getTeam(stats.getThisFactionIndex()) != GameConstants::maxPlayers -1 + fpt_Observer) { if(stats.getVictory(stats.getThisFactionIndex())){ //header += lang.getString("Victory"); music = getBattleEndMusic(true); @@ -299,7 +299,7 @@ void BattleEnd::initBackgroundVideo() { string videoFile = ""; string videoFileFallback = ""; - if(stats.getTeam(stats.getThisFactionIndex()) != GameConstants::maxPlayers -1 + fpt_Observer) { + if(stats.getThisFactionIndex() > 0 && stats.getTeam(stats.getThisFactionIndex()) != GameConstants::maxPlayers -1 + fpt_Observer) { if(stats.getVictory(stats.getThisFactionIndex())){ //header += lang.getString("Victory"); @@ -703,7 +703,7 @@ void BattleEnd::render() { string header = stats.getDescription() + " - "; - if(stats.getTeam(stats.getThisFactionIndex()) != GameConstants::maxPlayers -1 + fpt_Observer) { + if(stats.getThisFactionIndex() > 0 && stats.getTeam(stats.getThisFactionIndex()) != GameConstants::maxPlayers -1 + fpt_Observer) { if(stats.getVictory(stats.getThisFactionIndex())){ header += lang.getString("Victory"); } diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index c612e530..b9de5458 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -1978,12 +1978,20 @@ void World::initFactionTypes(GameSettings *gs) { //create stats //printf("World gs->getThisFactionIndex() = %d\n",gs->getThisFactionIndex()); - stats.init(gs->getFactionCount(), gs->getThisFactionIndex(), gs->getDescription(),gs->getTech()); + if(this->game->isMasterserverMode() == true) { + this->thisFactionIndex = -1; + } + else { + this->thisFactionIndex= gs->getThisFactionIndex(); + } + + stats.init(gs->getFactionCount(), this->thisFactionIndex, gs->getDescription(),gs->getTech()); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //create factions - this->thisFactionIndex= gs->getThisFactionIndex(); + //printf("this->thisFactionIndex = %d\n",this->thisFactionIndex); + //factions.resize(gs->getFactionCount()); for(int i= 0; i < gs->getFactionCount(); ++i){ Faction *newFaction = new Faction(); @@ -2086,7 +2094,12 @@ void World::initFactionTypes(GameSettings *gs) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(factions.empty() == false) { - thisTeamIndex= getFaction(thisFactionIndex)->getTeam(); + if(this->game->isMasterserverMode() == true) { + thisTeamIndex = -1; + } + else { + thisTeamIndex = getFaction(thisFactionIndex)->getTeam(); + } } if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } @@ -2384,49 +2397,49 @@ ExploredCellsLookupItem World::exploreCells(const Vec2i &newPos, int sightRange, bool World::showWorldForPlayer(int factionIndex, bool excludeFogOfWarCheck) const { bool ret = false; + if(factionIndex >= 0) { + if(excludeFogOfWarCheck == false && + fogOfWarSkillTypeValue == 0 && fogOfWarOverride == true) { + ret = true; + } + else if(factionIndex == thisFactionIndex && game != NULL) { + // Player is an Observer + if(thisTeamIndex == GameConstants::maxPlayers -1 + fpt_Observer) { + ret = true; + } + // Game over and not a network game + else if(game->getGameOver() == true && + game->getGameSettings()->isNetworkGame() == false) { + ret = true; + } + // Game is over but playing a Network game so check if we can + // turn off fog of war? + else if(game->getGameOver() == true && + game->getGameSettings()->isNetworkGame() == true && + game->getGameSettings()->getEnableObserverModeAtEndGame() == true) { + ret = true; - if(excludeFogOfWarCheck == false && - fogOfWarSkillTypeValue == 0 && fogOfWarOverride == true) { - ret = true; - } - else if(factionIndex == thisFactionIndex && game != NULL) { - // Player is an Observer - if(thisTeamIndex == GameConstants::maxPlayers -1 + fpt_Observer) { - ret = true; - } - // Game over and not a network game - else if(game->getGameOver() == true && - game->getGameSettings()->isNetworkGame() == false) { - ret = true; - } - // Game is over but playing a Network game so check if we can - // turn off fog of war? - else if(game->getGameOver() == true && - game->getGameSettings()->isNetworkGame() == true && - game->getGameSettings()->getEnableObserverModeAtEndGame() == true) { - ret = true; + // If the faction is NOT on the winning team, don't let them see the map + // until all mobile units are dead + if(getStats()->getVictory(factionIndex) == false) { + // If the player has at least 1 Unit alive that is mobile (can move) + // then we cannot turn off fog of war - // If the faction is NOT on the winning team, don't let them see the map - // until all mobile units are dead - if(getStats()->getVictory(factionIndex) == false) { - // If the player has at least 1 Unit alive that is mobile (can move) - // then we cannot turn off fog of war - - const Faction *faction = getFaction(factionIndex); -// for(int i = 0; i < faction->getUnitCount(); ++i) { -// Unit *unit = getFaction(factionIndex)->getUnit(i); -// if(unit != NULL && unit->isAlive() && unit->getType()->isMobile() == true) { -// ret = false; -// break; -// } -// } - if(faction->hasAliveUnits(true,false) == true) { - ret = false; - } - } - } + const Faction *faction = getFaction(factionIndex); + // for(int i = 0; i < faction->getUnitCount(); ++i) { + // Unit *unit = getFaction(factionIndex)->getUnit(i); + // if(unit != NULL && unit->isAlive() && unit->getType()->isMobile() == true) { + // ret = false; + // break; + // } + // } + if(faction->hasAliveUnits(true,false) == true) { + ret = false; + } + } + } + } } - return ret; } diff --git a/source/glest_game/world/world.h b/source/glest_game/world/world.h index ceca8acb..6c36fa14 100644 --- a/source/glest_game/world/world.h +++ b/source/glest_game/world/world.h @@ -160,8 +160,8 @@ public: inline int getThisTeamIndex() const {return thisTeamIndex;} inline void setThisTeamIndex(int team) { thisTeamIndex=team;} - inline const Faction *getThisFaction() const {return factions[thisFactionIndex];} - inline Faction *getThisFactionPtr() {return factions[thisFactionIndex];} + inline const Faction *getThisFaction() const {return (thisFactionIndex >= 0 ? factions[thisFactionIndex] : NULL);} + inline Faction *getThisFactionPtr() {return (thisFactionIndex >= 0 ? factions[thisFactionIndex] : NULL);} inline int getFactionCount() const {return (int)factions.size();} inline const Map *getMap() const {return ↦} diff --git a/source/masterserver/showServersJson.php b/source/masterserver/showServersJson.php index d82fe0e0..cafd5a5c 100644 --- a/source/masterserver/showServersJson.php +++ b/source/masterserver/showServersJson.php @@ -30,6 +30,7 @@ db_disconnect( DB_LINK ); unset( $linkid ); + header('Content-type: application/json'); echo json_encode($all_servers); unset( $all_servers ); ?>