initial commit, lordsawar source, slightly modified
[lordsawar] / src / recently-played-game-list.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 <sigc++/functors/mem_fun.h>
19
20 #include "recently-played-game-list.h"
21 #include "recently-played-game.h"
22 #include <limits.h>
23 #include <fstream>
24 #include <iostream>
25 #include "xmlhelper.h"
26 #include "Configuration.h"
27 #include <sigc++/functors/mem_fun.h>
28 #include "defs.h"
29
30 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
31 #define debug(x)
32
33 std::string RecentlyPlayedGameList::d_tag = "recentlyplayedgamelist";
34
35 RecentlyPlayedGameList* RecentlyPlayedGameList::s_instance = 0;
36
37 RecentlyPlayedGameList* RecentlyPlayedGameList::getInstance()
38 {
39   if (s_instance == 0)
40     s_instance = new RecentlyPlayedGameList();
41
42   return s_instance;
43 }
44
45 bool RecentlyPlayedGameList::saveToFile(std::string filename) const
46 {
47   bool retval = true;
48   XML_Helper helper(filename, std::ios::out, false);
49   retval &= save(&helper);
50   helper.close();
51   return retval;
52 }
53
54 bool RecentlyPlayedGameList::loadFromFile(std::string filename)
55 {
56   std::ifstream in(filename.c_str());
57   if (in)
58     {
59       XML_Helper helper(filename.c_str(), std::ios::in, false);
60       helper.registerTag(RecentlyPlayedGame::d_tag, sigc::mem_fun(this, &RecentlyPlayedGameList::load));
61       bool retval = helper.parse();
62       if (retval == false)
63         unlink(filename.c_str());
64       return retval;
65     }
66   return true;
67 }
68
69 RecentlyPlayedGameList* RecentlyPlayedGameList::getInstance(XML_Helper* helper)
70 {
71   if (s_instance)
72     deleteInstance();
73
74   s_instance = new RecentlyPlayedGameList(helper);
75   return s_instance;
76 }
77
78 void RecentlyPlayedGameList::deleteInstance()
79 {
80   if (s_instance)
81     delete s_instance;
82
83   s_instance = 0;
84 }
85
86 RecentlyPlayedGameList::RecentlyPlayedGameList()
87 {
88 }
89
90 RecentlyPlayedGameList::RecentlyPlayedGameList(XML_Helper* helper)
91 {
92   helper->registerTag(RecentlyPlayedGame::d_tag, sigc::mem_fun(this, &RecentlyPlayedGameList::load));
93 }
94
95 RecentlyPlayedGameList::~RecentlyPlayedGameList()
96 {
97   for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++)
98     delete *it;
99 }
100
101 bool RecentlyPlayedGameList::save(XML_Helper* helper) const
102 {
103   bool retval = true;
104
105   retval &= helper->begin(LORDSAWAR_RECENTLY_PLAYED_VERSION);
106   retval &= helper->openTag(RecentlyPlayedGameList::d_tag);
107
108   for (const_iterator it = begin(); it != end(); it++)
109     (*it)->save(helper);
110
111   retval &= helper->closeTag();
112
113   return retval;
114 }
115
116 bool RecentlyPlayedGameList::load(std::string tag, XML_Helper* helper)
117 {
118   if (helper->getVersion() != LORDSAWAR_RECENTLY_PLAYED_VERSION)
119     {
120       return false;
121     }
122   if (tag == RecentlyPlayedGame::d_tag)
123     {
124       RecentlyPlayedGame *g = RecentlyPlayedGame::handle_load(helper);
125       push_back(g);
126       return true;
127     }
128   return false;
129 }
130
131 void RecentlyPlayedGameList::addNetworkedEntry(GameScenario *game_scenario, std::string host, guint32 port)
132 {
133   if (Configuration::s_remember_recent_games == false)
134     return;
135   RecentlyPlayedNetworkedGame *g = NULL;
136   switch (GameScenario::PlayMode(game_scenario->getPlayMode()))
137     {
138       case GameScenario::NETWORKED:
139         g = new RecentlyPlayedNetworkedGame(game_scenario);
140         g->fillData(host, port);
141         break;
142       default:
143         break;
144     }
145   if (g)
146     push_back(g);
147   sort(orderByTime);
148 }
149
150 void RecentlyPlayedGameList::addEntry(GameScenario *game_scenario, std::string filename)
151 {
152   if (Configuration::s_remember_recent_games == false)
153     return;
154   switch (GameScenario::PlayMode(game_scenario->getPlayMode()))
155     {
156       case GameScenario::HOTSEAT:
157           {
158             RecentlyPlayedHotseatGame *g = NULL;
159             g = new RecentlyPlayedHotseatGame(game_scenario);
160             g->fillData(filename);
161             push_back(g);
162             break;
163           }
164       case GameScenario::PLAY_BY_MAIL:
165           {
166             RecentlyPlayedPbmGame *g = NULL;
167             g = new RecentlyPlayedPbmGame(game_scenario);
168             g->fillData(filename);
169             push_back(g);
170             break;
171           }
172       default:
173         break;
174     }
175 }
176
177 bool RecentlyPlayedGameList::orderByTime(RecentlyPlayedGame*rhs, RecentlyPlayedGame *lhs)
178 {
179   if (rhs->getTimeOfLastPlay() > lhs->getTimeOfLastPlay())
180     return true;
181   else
182     return false;
183 }
184
185 void RecentlyPlayedGameList::pruneGames()
186 {
187   sort(orderByTime);
188   pruneOldGames(TWO_WEEKS_OLD);
189   pruneTooManyGames(10);
190 }
191
192 void RecentlyPlayedGameList::pruneTooManyGames(int too_many)
193 {
194   int count = 0;
195   for (RecentlyPlayedGameList::iterator it = begin(); it != end();)
196     {
197       count++;
198       if (count > too_many)
199         {
200           delete *it;
201           it = erase (it);
202           continue;
203         }
204       it++;
205     }
206 }
207
208 void RecentlyPlayedGameList::pruneOldGames(int stale)
209 {
210   time_t now = time(NULL);
211   for (RecentlyPlayedGameList::iterator it = begin(); it != end();)
212     {
213       if ((*it)->getTimeOfLastPlay() + stale < now)
214         {
215           delete *it;
216           it = erase (it);
217           continue;
218         }
219       it++;
220     }
221 }
222
223 bool RecentlyPlayedGameList::removeEntry(std::string id)
224 {
225   bool found = false;
226   for (RecentlyPlayedGameList::iterator it = begin(); it != end();)
227     {
228       if ((*it)->getId() == id)
229         {
230           delete *it;
231           it = erase (it);
232           found = true;
233           continue;
234         }
235       it++;
236     }
237   return found;
238 }
239
240 void RecentlyPlayedGameList::updateEntry(GameScenario *game_scenario)
241 {
242   for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++)
243     {
244       if ((*it)->getId() == game_scenario->getId())
245         {
246           (*it)->setTimeOfLastPlay(time(NULL));
247           (*it)->setRound(game_scenario->getRound());
248         }
249     }
250 }
251         
252 void RecentlyPlayedGameList::removeAllNetworkedGames()
253 {
254   for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++)
255     {
256       if ((*it)->getPlayMode() == GameScenario::NETWORKED)
257         {
258           erase (it);
259           delete *it;
260           it = begin();
261           continue;
262         }
263     }
264 }
265 // End of file