From 365fe107652676fd3b5030ed2eb482a06d252688 Mon Sep 17 00:00:00 2001 From: "Xawotihs@gmail.com" Date: Sat, 26 Jan 2013 08:07:31 +0000 Subject: [PATCH] Fixed another dumb warning reported in the forum and added a nice assert for synchronization issues during network game. So, don't try it if you're not ready to get crashes in the face. --- projects/mtg/include/GameObserver.h | 6 +++--- projects/mtg/src/GameObserver.cpp | 26 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/projects/mtg/include/GameObserver.h b/projects/mtg/include/GameObserver.h index d49e67bed..3b438d383 100644 --- a/projects/mtg/include/GameObserver.h +++ b/projects/mtg/include/GameObserver.h @@ -30,7 +30,7 @@ using namespace std; class GameObserver{ protected: - + unsigned int mSeed; GameType mGameType; MTGCardInstance * cardWaitingForTargets; queue eventsQueue; @@ -40,7 +40,6 @@ class GameObserver{ list loadingList; list::iterator loadingite; RandomGenerator randomGenerator; - unsigned int mSeed; WResourceManager* mResourceManager; JGE* mJGE; DeckManager* mDeckManager; @@ -54,7 +53,7 @@ class GameObserver{ bool parseLine(const string& s); virtual void logAction(const string& s); bool processAction(const string& s, bool swapPlayer = false); - bool processActions(bool undo + bool processActions(bool undo, bool swapPlayer #ifdef TESTSUITE , TestSuiteGame* testgame #endif @@ -201,6 +200,7 @@ public: static void loadPlayer(void*pThis, stringstream& in, stringstream& out); static void sendAction(void*pThis, stringstream& in, stringstream& out); static void synchronize(void*pThis, stringstream& in, stringstream& out); + static void checkSynchro(void*pxThis, stringstream& in, stringstream& out); static void ignoreResponse(void*pThis, stringstream& in, stringstream& out){}; }; #endif diff --git a/projects/mtg/src/GameObserver.cpp b/projects/mtg/src/GameObserver.cpp index df94687ca..f5dc1fb68 100644 --- a/projects/mtg/src/GameObserver.cpp +++ b/projects/mtg/src/GameObserver.cpp @@ -1646,7 +1646,7 @@ bool GameObserver::load(const string& ss, bool undo, bool swapPlayers // take a snapshot before processing the actions resetStartupGame(); - mRules->initGame(this, currentPlayerSet); + if(mRules) mRules->initGame(this, currentPlayerSet); phaseRing->goToPhase(0, currentPlayer, false); phaseRing->goToPhase(mCurrentGamePhase, currentPlayer); @@ -1655,7 +1655,7 @@ bool GameObserver::load(const string& ss, bool undo, bool swapPlayers testgame->initGame(); #endif //TESTSUITE - processActions(undo + processActions(undo, swapPlayers #ifdef TESTSUITE , testgame #endif //TESTSUITE @@ -1729,7 +1729,7 @@ bool GameObserver::processAction(const string& s, bool swapPlayer) return true; } -bool GameObserver::processActions(bool undo +bool GameObserver::processActions(bool undo, bool swapPlayer #ifdef TESTSUITE , TestSuiteGame* testgame #endif @@ -1767,7 +1767,7 @@ bool GameObserver::processActions(bool undo for(loadingite = loadingList.begin(); loadingite != loadingList.end(); loadingite++, cmdIndex++) { - processAction(*loadingite); + processAction(*loadingite, swapPlayer); size_t nb = actionsList.size(); @@ -1954,8 +1954,8 @@ NetworkGameObserver::NetworkGameObserver(JNetwork* pNetwork, WResourceManager* o : GameObserver(output, input), mpNetworkSession(pNetwork), mSynchronized(false) { mpNetworkSession->registerCommand("loadPlayer", this, loadPlayer, ignoreResponse); - mpNetworkSession->registerCommand("synchronize", this, synchronize, ignoreResponse); - mpNetworkSession->registerCommand("sendAction", this, sendAction, ignoreResponse); + mpNetworkSession->registerCommand("synchronize", this, synchronize, checkSynchro); + mpNetworkSession->registerCommand("sendAction", this, sendAction, checkSynchro); } NetworkGameObserver::~NetworkGameObserver() @@ -2017,6 +2017,19 @@ void NetworkGameObserver::synchronize(void*pxThis, stringstream& in, stringstrea NetworkGameObserver* pThis = (NetworkGameObserver*)pxThis; // now, we need to swap players as player1 for host is player 2 for guest pThis->load(in.str(), false, true); + out << *pThis; +} + + +void NetworkGameObserver::checkSynchro(void*pxThis, stringstream& in, stringstream& out) +{ + NetworkGameObserver* pThis = (NetworkGameObserver*)pxThis; + + GameObserver aGame; + aGame.mRules = pThis->mRules; + aGame.load(in.str(), false, true); + + assert(aGame == *pThis); } void NetworkGameObserver::sendAction(void*pxThis, stringstream& in, stringstream& out) @@ -2026,6 +2039,7 @@ void NetworkGameObserver::sendAction(void*pxThis, stringstream& in, stringstream pThis->mForwardAction = false; pThis->processAction(in.str(), true); pThis->mForwardAction = true; + out << *pThis; } void NetworkGameObserver::logAction(const string& s)