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

View File

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

View File

@@ -34,7 +34,6 @@
#include "../include/JSocket.h"
map<string, JNetwork::CommandStruc> JNetwork::sCommandMap;
JNetwork* JNetwork::mInstance = NULL;
bool JNetwork::isConnected(){
if (connected_to_ap !=1 || !socket) return false;
@@ -63,21 +62,6 @@ JNetwork::~JNetwork()
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)
{
string aString;

View File

@@ -40,6 +40,7 @@ class GameObserver{
list<string> loadingList;
list<string>::iterator loadingite;
RandomGenerator randomGenerator;
unsigned int mSeed;
WResourceManager* mResourceManager;
JGE* mJGE;
DeckManager* mDeckManager;
@@ -143,7 +144,7 @@ class GameObserver{
logAction(players[playerId], s);
};
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
, TestSuiteGame* testgame = 0
#endif

View File

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

View File

@@ -73,7 +73,7 @@ public:
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
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
void postUpdateInit(GameObserver* observer);
void cleanup();

View File

@@ -73,11 +73,12 @@ protected:
list<int> usedRandomValues;
bool log;
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);
ostream& saveUsedRandValues(ostream& out) const;
ostream& saveLoadedRandValues(ostream& out);
int random();
void setSeed(unsigned int seed) { srand(seed); };
template<typename Iter> void random_shuffle(Iter first, Iter last)
{
ptrdiff_t i, n;

View File

@@ -79,7 +79,7 @@ GameObserver::~GameObserver()
}
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]();
@@ -1491,6 +1491,9 @@ ostream& operator<<(ostream& out, const GameObserver& g)
}
else
{
out << "seed:";
out << g.mSeed;
out << endl;
out << "rvalues:";
out << g.randomGenerator.saveUsedRandValues(out);
out << endl;
@@ -1531,13 +1534,14 @@ bool GameObserver::parseLine(const string& s)
return false;
}
bool GameObserver::load(const string& ss, bool undo
bool GameObserver::load(const string& ss, bool undo, bool swapPlayers
#ifdef TESTSUITE
, TestSuiteGame* testgame
#endif
)
{
int state = -1;
bool currentPlayerSet = false;
int state = -1;
string s;
stringstream stream(ss);
@@ -1555,7 +1559,8 @@ bool GameObserver::load(const string& ss, bool undo
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
if (s.find("seed ") == 0)
{
// seed = atoi(s.substr(5).c_str());
mSeed = atoi(s.substr(5).c_str());
randomGenerator.setSeed(mSeed);
continue;
}
if (s.find("rvalues:") == 0)
@@ -1576,7 +1581,7 @@ bool GameObserver::load(const string& ss, bool undo
}
else
{
parseLine(s);
currentPlayerSet = parseLine(s);
}
break;
case 1:
@@ -1624,7 +1629,14 @@ bool GameObserver::load(const string& ss, bool undo
case 3:
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->init(this);
currentPlayer = players[currentPlayerId];
@@ -1634,7 +1646,7 @@ bool GameObserver::load(const string& ss, bool undo
// take a snapshot before processing the actions
resetStartupGame();
mRules->initGame(this);
mRules->initGame(this, currentPlayerSet);
phaseRing->goToPhase(0, currentPlayer, false);
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));
}
}
#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
{ //AI Player, chooses deck
AIPlayerFactory playerCreator;
@@ -2014,9 +2015,8 @@ void NetworkGameObserver::synchronize()
void NetworkGameObserver::synchronize(void*pxThis, stringstream& in, stringstream& out)
{
NetworkGameObserver* pThis = (NetworkGameObserver*)pxThis;
pThis->load(in.str());
// 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)

View File

@@ -843,7 +843,7 @@ void GameStateMenu::ButtonPressed(int controllerId, int controlId)
{
if(!mParent->mpNetwork)
{
mParent->mpNetwork = JNetwork::GetInstance();
mParent->mpNetwork = new JNetwork();
}
mParent->mpNetwork->connect();
subMenuController->Close();
@@ -854,7 +854,7 @@ void GameStateMenu::ButtonPressed(int controllerId, int controlId)
{
if(!mParent->mpNetwork)
{
mParent->mpNetwork = JNetwork::GetInstance();
mParent->mpNetwork = new JNetwork();
}
// FIXME needs to be able to specify the server ip
mParent->mpNetwork->connect("127.0.0.1");
@@ -882,8 +882,7 @@ void GameStateMenu::ButtonPressed(int controllerId, int controlId)
subMenuController->Close();
}
#ifdef NETWORK_SUPPORT
JNetwork::Destroy();
mParent->mpNetwork=0;
SAFE_DELETE(mParent->mpNetwork);
#endif //NETWORK_SUPPORT
currentState = MENU_STATE_MAJOR_MAINMENU | MENU_STATE_MINOR_SUBMENU_CLOSING;
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");
@@ -372,9 +372,12 @@ void Rules::initGame(GameObserver *g)
if(OptionWhosFirst::WHO_O == options[Options::FIRSTPLAYER].number)
initState.player = 1;
}
g->currentPlayer = g->players[initState.player];
g->currentActionPlayer = g->currentPlayer;
g->currentPlayerId = initState.player;
if(!currentPlayerSet)
{
g->currentPlayerId = initState.player;
}
g->currentPlayer = g->players[g->currentPlayerId];
g->currentActionPlayer = g->currentPlayer;
g->phaseRing->goToPhase(0, g->currentPlayer, false);
g->phaseRing->goToPhase(initState.phase, g->currentPlayer);
g->setCurrentGamePhase(initState.phase);