Cleaned up Network code (hope that fixes the PSP compilation problems)

Added random seed logging
This commit is contained in:
Xawotihs@gmail.com
2013-01-25 21:47:16 +00:00
parent 4bf419fbaf
commit 2ba5eb0022
10 changed files with 39 additions and 56 deletions

View File

@@ -17,6 +17,8 @@ typedef void(*processCmd)(void*, stringstream&, stringstream&);
class JNetwork { class JNetwork {
private: private:
private:
boost::thread *mpWorkerThread;
struct CommandStruc{ struct CommandStruc{
void* object; void* object;
string command; string command;
@@ -31,13 +33,10 @@ private:
stringstream received; stringstream received;
stringstream toSend; stringstream toSend;
static map<string, CommandStruc> sCommandMap; static map<string, CommandStruc> sCommandMap;
static JNetwork* mInstance;
public: public:
JNetwork(); JNetwork();
~JNetwork(); ~JNetwork();
static JNetwork* GetInstance();
static void Destroy();
void Update(); void Update();
int connect(string serverIP = ""); int connect(string serverIP = "");
@@ -49,9 +48,6 @@ public:
#endif #endif
bool sendCommand(const string& command, const string& payload = "", const string& suffix = "Request"); bool sendCommand(const string& command, const string& payload = "", const string& suffix = "Request");
static void registerCommand(string command, void* object, processCmd processRequest, processCmd processResponse); static void registerCommand(string command, void* object, processCmd processRequest, processCmd processResponse);
private:
boost::thread *mpWorkerThread;
}; };
#endif #endif

View File

@@ -32,6 +32,8 @@ public:
void Disconnect(); void Disconnect();
private: private:
// socket state
SOCKET_STATE state;
// socket creation when server accepts a connection // socket creation when server accepts a connection
JSocket(int fd); JSocket(int fd);
// convert the socket into non-blocking state // convert the socket into non-blocking state
@@ -42,8 +44,6 @@ private:
#elif LINUX #elif LINUX
int mfd; int mfd;
#endif #endif
// socket state
SOCKET_STATE state;
}; };
#endif #endif

View File

@@ -34,7 +34,6 @@
#include "../include/JSocket.h" #include "../include/JSocket.h"
map<string, JNetwork::CommandStruc> JNetwork::sCommandMap; map<string, JNetwork::CommandStruc> JNetwork::sCommandMap;
JNetwork* JNetwork::mInstance = NULL;
bool JNetwork::isConnected(){ bool JNetwork::isConnected(){
if (connected_to_ap !=1 || !socket) return false; if (connected_to_ap !=1 || !socket) return false;
@@ -63,21 +62,6 @@ JNetwork::~JNetwork()
delete socket; delete socket;
} }
JNetwork* JNetwork::GetInstance()
{
if (mInstance == NULL) mInstance = new JNetwork();
return mInstance;
}
void JNetwork::Destroy()
{
if (mInstance)
{
delete mInstance;
mInstance = NULL;
}
}
bool JNetwork::sendCommand(const string& xString, const string& payload, const string& suffix) bool JNetwork::sendCommand(const string& xString, const string& payload, const string& suffix)
{ {
string aString; string aString;

View File

@@ -40,6 +40,7 @@ class GameObserver{
list<string> loadingList; list<string> loadingList;
list<string>::iterator loadingite; list<string>::iterator loadingite;
RandomGenerator randomGenerator; RandomGenerator randomGenerator;
unsigned int mSeed;
WResourceManager* mResourceManager; WResourceManager* mResourceManager;
JGE* mJGE; JGE* mJGE;
DeckManager* mDeckManager; DeckManager* mDeckManager;
@@ -143,7 +144,7 @@ class GameObserver{
logAction(players[playerId], s); logAction(players[playerId], s);
}; };
void logAction(MTGCardInstance* card, MTGGameZone* zone, size_t index, int result); void logAction(MTGCardInstance* card, MTGGameZone* zone, size_t index, int result);
bool load(const string& s, bool undo = false bool load(const string& s, bool undo = false, bool swapPlayers = false
#ifdef TESTSUITE #ifdef TESTSUITE
, TestSuiteGame* testgame = 0 , TestSuiteGame* testgame = 0
#endif #endif

View File

@@ -31,7 +31,6 @@ private:
Credits * credits; Credits * credits;
int mGamePhase; int mGamePhase;
Player * mCurrentPlayer;
GameObserver * game; GameObserver * game;
DeckMenu * deckmenu; DeckMenu * deckmenu;
DeckMenu * opponentMenu; DeckMenu * opponentMenu;

View File

@@ -73,7 +73,7 @@ public:
void initPlayers(GameObserver *observer); void initPlayers(GameObserver *observer);
bool canChooseDeck(); //True if the players get to select their decks, false if the decks are automatically generated by the mode bool canChooseDeck(); //True if the players get to select their decks, false if the decks are automatically generated by the mode
void addExtraRules(GameObserver *observer); void addExtraRules(GameObserver *observer);
void initGame(GameObserver* observer); void initGame(GameObserver* observer, bool currentPlayerSet = false);
//second part of the initialization, needs to happen after the first update call //second part of the initialization, needs to happen after the first update call
void postUpdateInit(GameObserver* observer); void postUpdateInit(GameObserver* observer);
void cleanup(); void cleanup();

View File

@@ -73,11 +73,12 @@ protected:
list<int> usedRandomValues; list<int> usedRandomValues;
bool log; bool log;
public: public:
RandomGenerator(bool doLog = false) : log(doLog) {}; RandomGenerator(unsigned int seed = -1, bool doLog = false) : log(doLog) { if(seed != -1) srand(seed);};
void loadRandValues(string s); void loadRandValues(string s);
ostream& saveUsedRandValues(ostream& out) const; ostream& saveUsedRandValues(ostream& out) const;
ostream& saveLoadedRandValues(ostream& out); ostream& saveLoadedRandValues(ostream& out);
int random(); int random();
void setSeed(unsigned int seed) { srand(seed); };
template<typename Iter> void random_shuffle(Iter first, Iter last) template<typename Iter> void random_shuffle(Iter first, Iter last)
{ {
ptrdiff_t i, n; ptrdiff_t i, n;

View File

@@ -79,7 +79,7 @@ GameObserver::~GameObserver()
} }
GameObserver::GameObserver(WResourceManager *output, JGE* input) GameObserver::GameObserver(WResourceManager *output, JGE* input)
: randomGenerator(true), mResourceManager(output), mJGE(input) : mSeed((unsigned int)time(0)), randomGenerator(mSeed, true), mResourceManager(output), mJGE(input)
{ {
ExtraRules = new MTGCardInstance[2](); ExtraRules = new MTGCardInstance[2]();
@@ -1491,6 +1491,9 @@ ostream& operator<<(ostream& out, const GameObserver& g)
} }
else else
{ {
out << "seed:";
out << g.mSeed;
out << endl;
out << "rvalues:"; out << "rvalues:";
out << g.randomGenerator.saveUsedRandValues(out); out << g.randomGenerator.saveUsedRandValues(out);
out << endl; out << endl;
@@ -1531,13 +1534,14 @@ bool GameObserver::parseLine(const string& s)
return false; return false;
} }
bool GameObserver::load(const string& ss, bool undo bool GameObserver::load(const string& ss, bool undo, bool swapPlayers
#ifdef TESTSUITE #ifdef TESTSUITE
, TestSuiteGame* testgame , TestSuiteGame* testgame
#endif #endif
) )
{ {
int state = -1; bool currentPlayerSet = false;
int state = -1;
string s; string s;
stringstream stream(ss); stringstream stream(ss);
@@ -1555,7 +1559,8 @@ bool GameObserver::load(const string& ss, bool undo
std::transform(s.begin(), s.end(), s.begin(), ::tolower); std::transform(s.begin(), s.end(), s.begin(), ::tolower);
if (s.find("seed ") == 0) if (s.find("seed ") == 0)
{ {
// seed = atoi(s.substr(5).c_str()); mSeed = atoi(s.substr(5).c_str());
randomGenerator.setSeed(mSeed);
continue; continue;
} }
if (s.find("rvalues:") == 0) if (s.find("rvalues:") == 0)
@@ -1576,7 +1581,7 @@ bool GameObserver::load(const string& ss, bool undo
} }
else else
{ {
parseLine(s); currentPlayerSet = parseLine(s);
} }
break; break;
case 1: case 1:
@@ -1624,7 +1629,14 @@ bool GameObserver::load(const string& ss, bool undo
case 3: case 3:
if (s.compare("[end]") == 0) if (s.compare("[end]") == 0)
{ {
turn = 0; if(swapPlayers) {
std::swap(players[0], players[1]);
currentPlayerId = (currentPlayerId + 1) % players.size();
currentPlayer = players[currentPlayerId];
currentActionPlayer = currentPlayer;
}
turn = 0;
mLayers = NEW DuelLayers(); mLayers = NEW DuelLayers();
mLayers->init(this); mLayers->init(this);
currentPlayer = players[currentPlayerId]; currentPlayer = players[currentPlayerId];
@@ -1634,7 +1646,7 @@ bool GameObserver::load(const string& ss, bool undo
// take a snapshot before processing the actions // take a snapshot before processing the actions
resetStartupGame(); resetStartupGame();
mRules->initGame(this); mRules->initGame(this, currentPlayerSet);
phaseRing->goToPhase(0, currentPlayer, false); phaseRing->goToPhase(0, currentPlayer, false);
phaseRing->goToPhase(mCurrentGamePhase, currentPlayer); phaseRing->goToPhase(mCurrentGamePhase, currentPlayer);
@@ -1904,17 +1916,6 @@ void GameObserver::loadPlayer(int playerId, PlayerType playerType, int decknb, b
loadPlayer(playerId, NEW HumanPlayer(this, deckFile, deckFileSmall, premadeDeck)); loadPlayer(playerId, NEW HumanPlayer(this, deckFile, deckFileSmall, premadeDeck));
} }
} }
#ifdef NETWORK_SUPPORT
else if(playerType == PLAYER_TYPE_REMOTE)
{
//Player 0 is the human player
ProxyPlayer* mProxy;
mProxy = NEW ProxyPlayer(players[0], JNetwork::GetInstance());
//Player 1 is the remote player
loadPlayer(playerId, NEW RemotePlayer(this, JNetwork::GetInstance()));
}
#endif //NETWORK_SUPPORT
else else
{ //AI Player, chooses deck { //AI Player, chooses deck
AIPlayerFactory playerCreator; AIPlayerFactory playerCreator;
@@ -2014,9 +2015,8 @@ void NetworkGameObserver::synchronize()
void NetworkGameObserver::synchronize(void*pxThis, stringstream& in, stringstream& out) void NetworkGameObserver::synchronize(void*pxThis, stringstream& in, stringstream& out)
{ {
NetworkGameObserver* pThis = (NetworkGameObserver*)pxThis; NetworkGameObserver* pThis = (NetworkGameObserver*)pxThis;
pThis->load(in.str());
// now, we need to swap players as player1 for host is player 2 for guest // now, we need to swap players as player1 for host is player 2 for guest
std::swap(pThis->players[0], pThis->players[1]); pThis->load(in.str(), false, true);
} }
void NetworkGameObserver::sendAction(void*pxThis, stringstream& in, stringstream& out) void NetworkGameObserver::sendAction(void*pxThis, stringstream& in, stringstream& out)

View File

@@ -843,7 +843,7 @@ void GameStateMenu::ButtonPressed(int controllerId, int controlId)
{ {
if(!mParent->mpNetwork) if(!mParent->mpNetwork)
{ {
mParent->mpNetwork = JNetwork::GetInstance(); mParent->mpNetwork = new JNetwork();
} }
mParent->mpNetwork->connect(); mParent->mpNetwork->connect();
subMenuController->Close(); subMenuController->Close();
@@ -854,7 +854,7 @@ void GameStateMenu::ButtonPressed(int controllerId, int controlId)
{ {
if(!mParent->mpNetwork) if(!mParent->mpNetwork)
{ {
mParent->mpNetwork = JNetwork::GetInstance(); mParent->mpNetwork = new JNetwork();
} }
// FIXME needs to be able to specify the server ip // FIXME needs to be able to specify the server ip
mParent->mpNetwork->connect("127.0.0.1"); mParent->mpNetwork->connect("127.0.0.1");
@@ -882,8 +882,7 @@ void GameStateMenu::ButtonPressed(int controllerId, int controlId)
subMenuController->Close(); subMenuController->Close();
} }
#ifdef NETWORK_SUPPORT #ifdef NETWORK_SUPPORT
JNetwork::Destroy(); SAFE_DELETE(mParent->mpNetwork);
mParent->mpNetwork=0;
#endif //NETWORK_SUPPORT #endif //NETWORK_SUPPORT
currentState = MENU_STATE_MAJOR_MAINMENU | MENU_STATE_MINOR_SUBMENU_CLOSING; currentState = MENU_STATE_MAJOR_MAINMENU | MENU_STATE_MINOR_SUBMENU_CLOSING;
break; break;

View File

@@ -360,7 +360,7 @@ void Rules::initPlayers(GameObserver *g)
} }
} }
void Rules::initGame(GameObserver *g) void Rules::initGame(GameObserver *g, bool currentPlayerSet)
{ {
DebugTrace("RULES Init Game\n"); DebugTrace("RULES Init Game\n");
@@ -372,9 +372,12 @@ void Rules::initGame(GameObserver *g)
if(OptionWhosFirst::WHO_O == options[Options::FIRSTPLAYER].number) if(OptionWhosFirst::WHO_O == options[Options::FIRSTPLAYER].number)
initState.player = 1; initState.player = 1;
} }
g->currentPlayer = g->players[initState.player]; if(!currentPlayerSet)
g->currentActionPlayer = g->currentPlayer; {
g->currentPlayerId = initState.player; g->currentPlayerId = initState.player;
}
g->currentPlayer = g->players[g->currentPlayerId];
g->currentActionPlayer = g->currentPlayer;
g->phaseRing->goToPhase(0, g->currentPlayer, false); g->phaseRing->goToPhase(0, g->currentPlayer, false);
g->phaseRing->goToPhase(initState.phase, g->currentPlayer); g->phaseRing->goToPhase(initState.phase, g->currentPlayer);
g->setCurrentGamePhase(initState.phase); g->setCurrentGamePhase(initState.phase);