initial commit, lordsawar source, slightly modified
[lordsawar] / src / pbm-game-server.cpp
1 // Copyright (C) 2008 Ben Asselstine
2 //
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.
7 //
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.
12 //
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 
16 //  02110-1301, USA.
17
18 #include <iostream>
19 #include <sstream>
20 #include <list>
21
22 #include "pbm-game-server.h"
23
24 #include "game.h"
25 #include "xmlhelper.h"
26 #include "GameScenario.h"
27 #include "playerlist.h"
28 #include "player.h"
29 #include "network-action.h"
30 #include "network-history.h"
31 #include "Configuration.h"
32
33
34 PbmGameServer * PbmGameServer::s_instance = 0;
35
36
37 PbmGameServer* PbmGameServer::getInstance()
38 {
39     if (s_instance == 0)
40         s_instance = new PbmGameServer();
41
42     return s_instance;
43 }
44
45 void PbmGameServer::deleteInstance()
46 {
47     if (s_instance)
48         delete s_instance;
49
50     s_instance = 0;
51 }
52
53 PbmGameServer::PbmGameServer()
54 {
55 }
56
57 PbmGameServer::~PbmGameServer()
58 {
59   clearNetworkActionlist();
60   clearNetworkHistorylist();
61 }
62
63 void PbmGameServer::start()
64 {
65   listenForActions();
66   listenForHistories();
67 }
68
69 void PbmGameServer::listenForActions()
70 {
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));
74 }
75
76 void PbmGameServer::listenForHistories()
77 {
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));
81 }
82
83 void PbmGameServer::clearNetworkActionlist()
84 {
85     for (std::list<NetworkAction*>::iterator it = d_actions.begin();
86         it != d_actions.end(); it++)
87       delete (*it);
88     d_actions.clear();
89 }
90
91 void PbmGameServer::clearNetworkHistorylist()
92 {
93     for (std::list<NetworkHistory*>::iterator it = d_histories.begin();
94         it != d_histories.end(); it++)
95       delete (*it);
96     d_histories.clear();
97 }
98
99 void PbmGameServer::onActionDone(NetworkAction *action)
100 {
101   std::string desc = action->toString();
102   std::cerr << "Play By Mail Game Server got " << desc <<"\n";
103
104   d_actions.push_back(action);
105 }
106
107 void PbmGameServer::onHistoryDone(NetworkHistory *history)
108 {
109   std::string desc = history->toString();
110   std::cerr << "Play By Mail Game Server got " << desc <<"\n";
111   d_histories.push_back(history);
112 }
113
114 bool PbmGameServer::dumpActionsAndHistories(XML_Helper *helper)
115 {
116   for (std::list<NetworkHistory *>::iterator i = d_histories.begin(),
117        end = d_histories.end(); i != end; ++i)
118     (**i).save(helper);
119   for (std::list<NetworkAction *>::iterator i = d_actions.begin(),
120        end = d_actions.end(); i != end; ++i)
121     (**i).save(helper);
122   return true;
123 }
124
125 bool PbmGameServer::endTurn(std::string turnfile, bool &broken)
126 {
127   bool retval = true;
128   XML_Helper helper(turnfile, std::ios::out, Configuration::s_zipfiles);
129   retval &= helper.openTag("lordsawarturn");
130   broken = dumpActionsAndHistories(&helper);
131   helper.closeTag();
132   helper.close();
133   return retval;
134 }
135
136 // End of file