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 2 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
18
#include <sigc++/functors/mem_fun.h>
20
#include "recently-played-game-list.h"
21
#include "recently-played-game.h"
25
#include "xmlhelper.h"
26
#include "Configuration.h"
27
#include <sigc++/functors/mem_fun.h>
30
//#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
33
std::string RecentlyPlayedGameList::d_tag = "recentlyplayedgamelist";
35
RecentlyPlayedGameList* RecentlyPlayedGameList::s_instance = 0;
37
RecentlyPlayedGameList* RecentlyPlayedGameList::getInstance()
40
s_instance = new RecentlyPlayedGameList();
45
bool RecentlyPlayedGameList::saveToFile(std::string filename)
48
XML_Helper helper(filename, std::ios::out, false);
49
retval &= save(&helper);
54
bool RecentlyPlayedGameList::loadFromFile(std::string filename)
56
std::ifstream in(filename.c_str());
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();
63
unlink(filename.c_str());
69
RecentlyPlayedGameList* RecentlyPlayedGameList::getInstance(XML_Helper* helper)
74
s_instance = new RecentlyPlayedGameList(helper);
78
void RecentlyPlayedGameList::deleteInstance()
86
RecentlyPlayedGameList::RecentlyPlayedGameList()
90
RecentlyPlayedGameList::RecentlyPlayedGameList(XML_Helper* helper)
92
helper->registerTag(RecentlyPlayedGame::d_tag, sigc::mem_fun(this, &RecentlyPlayedGameList::load));
95
RecentlyPlayedGameList::~RecentlyPlayedGameList()
97
for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++)
101
bool RecentlyPlayedGameList::save(XML_Helper* helper) const
105
retval &= helper->begin(LORDSAWAR_RECENTLY_PLAYED_VERSION);
106
retval &= helper->openTag(RecentlyPlayedGameList::d_tag);
108
for (const_iterator it = begin(); it != end(); it++)
111
retval &= helper->closeTag();
116
bool RecentlyPlayedGameList::load(std::string tag, XML_Helper* helper)
118
if (helper->getVersion() != LORDSAWAR_RECENTLY_PLAYED_VERSION)
122
if (tag == RecentlyPlayedGame::d_tag)
124
RecentlyPlayedGame *g = RecentlyPlayedGame::handle_load(helper);
131
void RecentlyPlayedGameList::addNetworkedEntry(GameScenario *game_scenario, std::string host, Uint32 port)
133
if (Configuration::s_remember_recent_games == false)
135
RecentlyPlayedNetworkedGame *g = NULL;
136
switch (GameScenario::PlayMode(game_scenario->getPlayMode()))
138
case GameScenario::NETWORKED:
139
g = new RecentlyPlayedNetworkedGame(game_scenario);
140
g->fillData(host, port);
150
void RecentlyPlayedGameList::addEntry(GameScenario *game_scenario, std::string filename)
152
if (Configuration::s_remember_recent_games == false)
154
switch (GameScenario::PlayMode(game_scenario->getPlayMode()))
156
case GameScenario::HOTSEAT:
158
RecentlyPlayedHotseatGame *g = NULL;
159
g = new RecentlyPlayedHotseatGame(game_scenario);
160
g->fillData(filename);
164
case GameScenario::PLAY_BY_MAIL:
166
RecentlyPlayedPbmGame *g = NULL;
167
g = new RecentlyPlayedPbmGame(game_scenario);
168
g->fillData(filename);
172
case GameScenario::CAMPAIGN:
174
RecentlyPlayedCampaignGame *g = NULL;
175
g = new RecentlyPlayedCampaignGame(game_scenario);
176
g->fillData(filename);
185
bool RecentlyPlayedGameList::orderByTime(RecentlyPlayedGame*rhs, RecentlyPlayedGame *lhs)
187
if (rhs->getTimeOfLastPlay() > lhs->getTimeOfLastPlay())
193
void RecentlyPlayedGameList::pruneGames()
196
pruneOldGames(TWO_WEEKS_OLD);
197
pruneTooManyGames(10);
200
void RecentlyPlayedGameList::pruneTooManyGames(int too_many)
203
for (RecentlyPlayedGameList::iterator it = begin(); it != end();)
206
if (count > too_many)
216
void RecentlyPlayedGameList::pruneOldGames(int stale)
218
time_t now = time(NULL);
219
for (RecentlyPlayedGameList::iterator it = begin(); it != end();)
221
if ((*it)->getTimeOfLastPlay() + stale < now)
231
bool RecentlyPlayedGameList::removeEntry(std::string id)
234
for (RecentlyPlayedGameList::iterator it = begin(); it != end();)
236
if ((*it)->getId() == id)
248
void RecentlyPlayedGameList::updateEntry(GameScenario *game_scenario)
250
for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++)
252
if ((*it)->getId() == game_scenario->getId())
254
(*it)->setTimeOfLastPlay(time(NULL));
255
(*it)->setRound(game_scenario->getRound());
260
void RecentlyPlayedGameList::removeAllNetworkedGames()
262
for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++)
264
if ((*it)->getPlayMode() == GameScenario::NETWORKED)