1 // Copyright (C) 2008 Ben Asselstine
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "pbm-game-server.h"
25 #include "xmlhelper.h"
26 #include "GameScenario.h"
27 #include "playerlist.h"
29 #include "network-action.h"
30 #include "network-history.h"
31 #include "Configuration.h"
34 PbmGameServer * PbmGameServer::s_instance = 0;
37 PbmGameServer* PbmGameServer::getInstance()
40 s_instance = new PbmGameServer();
45 void PbmGameServer::deleteInstance()
53 PbmGameServer::PbmGameServer()
57 PbmGameServer::~PbmGameServer()
59 clearNetworkActionlist();
60 clearNetworkHistorylist();
63 void PbmGameServer::start()
69 void PbmGameServer::listenForActions()
71 Playerlist *pl = Playerlist::getInstance();
72 for (Playerlist::iterator i = pl->begin(); i != pl->end(); ++i)
73 (*i)->acting.connect(sigc::mem_fun(this, &PbmGameServer::onActionDone));
76 void PbmGameServer::listenForHistories()
78 Playerlist *pl = Playerlist::getInstance();
79 for (Playerlist::iterator i = pl->begin(); i != pl->end(); ++i)
80 (*i)->history_written.connect(sigc::mem_fun(this, &PbmGameServer::onHistoryDone));
83 void PbmGameServer::clearNetworkActionlist()
85 for (std::list<NetworkAction*>::iterator it = d_actions.begin();
86 it != d_actions.end(); it++)
91 void PbmGameServer::clearNetworkHistorylist()
93 for (std::list<NetworkHistory*>::iterator it = d_histories.begin();
94 it != d_histories.end(); it++)
99 void PbmGameServer::onActionDone(NetworkAction *action)
101 std::string desc = action->toString();
102 std::cerr << "Play By Mail Game Server got " << desc <<"\n";
104 d_actions.push_back(action);
107 void PbmGameServer::onHistoryDone(NetworkHistory *history)
109 std::string desc = history->toString();
110 std::cerr << "Play By Mail Game Server got " << desc <<"\n";
111 d_histories.push_back(history);
114 bool PbmGameServer::dumpActionsAndHistories(XML_Helper *helper)
116 for (std::list<NetworkHistory *>::iterator i = d_histories.begin(),
117 end = d_histories.end(); i != end; ++i)
119 for (std::list<NetworkAction *>::iterator i = d_actions.begin(),
120 end = d_actions.end(); i != end; ++i)
125 bool PbmGameServer::endTurn(std::string turnfile, bool &broken)
128 XML_Helper helper(turnfile, std::ios::out, Configuration::s_zipfiles);
129 retval &= helper.openTag("lordsawarturn");
130 broken = dumpActionsAndHistories(&helper);