X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Frecently-played-game-list.cpp;fp=src%2Frecently-played-game-list.cpp;h=2b95a4210ee9fba1b924ae45c3392228fe0a52f9;hb=9eda00ff73353c55ecef6f82131166d5d4a85e29;hp=0000000000000000000000000000000000000000;hpb=3d34d4aa85a929f912464f71158396a388274f27;p=lordsawar diff --git a/src/recently-played-game-list.cpp b/src/recently-played-game-list.cpp new file mode 100644 index 0000000..2b95a42 --- /dev/null +++ b/src/recently-played-game-list.cpp @@ -0,0 +1,265 @@ +// Copyright (C) 2008 Ben Asselstine +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +// 02110-1301, USA. + +#include + +#include "recently-played-game-list.h" +#include "recently-played-game.h" +#include +#include +#include +#include "xmlhelper.h" +#include "Configuration.h" +#include +#include "defs.h" + +//#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<registerTag(RecentlyPlayedGame::d_tag, sigc::mem_fun(this, &RecentlyPlayedGameList::load)); +} + +RecentlyPlayedGameList::~RecentlyPlayedGameList() +{ + for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++) + delete *it; +} + +bool RecentlyPlayedGameList::save(XML_Helper* helper) const +{ + bool retval = true; + + retval &= helper->begin(LORDSAWAR_RECENTLY_PLAYED_VERSION); + retval &= helper->openTag(RecentlyPlayedGameList::d_tag); + + for (const_iterator it = begin(); it != end(); it++) + (*it)->save(helper); + + retval &= helper->closeTag(); + + return retval; +} + +bool RecentlyPlayedGameList::load(std::string tag, XML_Helper* helper) +{ + if (helper->getVersion() != LORDSAWAR_RECENTLY_PLAYED_VERSION) + { + return false; + } + if (tag == RecentlyPlayedGame::d_tag) + { + RecentlyPlayedGame *g = RecentlyPlayedGame::handle_load(helper); + push_back(g); + return true; + } + return false; +} + +void RecentlyPlayedGameList::addNetworkedEntry(GameScenario *game_scenario, std::string host, guint32 port) +{ + if (Configuration::s_remember_recent_games == false) + return; + RecentlyPlayedNetworkedGame *g = NULL; + switch (GameScenario::PlayMode(game_scenario->getPlayMode())) + { + case GameScenario::NETWORKED: + g = new RecentlyPlayedNetworkedGame(game_scenario); + g->fillData(host, port); + break; + default: + break; + } + if (g) + push_back(g); + sort(orderByTime); +} + +void RecentlyPlayedGameList::addEntry(GameScenario *game_scenario, std::string filename) +{ + if (Configuration::s_remember_recent_games == false) + return; + switch (GameScenario::PlayMode(game_scenario->getPlayMode())) + { + case GameScenario::HOTSEAT: + { + RecentlyPlayedHotseatGame *g = NULL; + g = new RecentlyPlayedHotseatGame(game_scenario); + g->fillData(filename); + push_back(g); + break; + } + case GameScenario::PLAY_BY_MAIL: + { + RecentlyPlayedPbmGame *g = NULL; + g = new RecentlyPlayedPbmGame(game_scenario); + g->fillData(filename); + push_back(g); + break; + } + default: + break; + } +} + +bool RecentlyPlayedGameList::orderByTime(RecentlyPlayedGame*rhs, RecentlyPlayedGame *lhs) +{ + if (rhs->getTimeOfLastPlay() > lhs->getTimeOfLastPlay()) + return true; + else + return false; +} + +void RecentlyPlayedGameList::pruneGames() +{ + sort(orderByTime); + pruneOldGames(TWO_WEEKS_OLD); + pruneTooManyGames(10); +} + +void RecentlyPlayedGameList::pruneTooManyGames(int too_many) +{ + int count = 0; + for (RecentlyPlayedGameList::iterator it = begin(); it != end();) + { + count++; + if (count > too_many) + { + delete *it; + it = erase (it); + continue; + } + it++; + } +} + +void RecentlyPlayedGameList::pruneOldGames(int stale) +{ + time_t now = time(NULL); + for (RecentlyPlayedGameList::iterator it = begin(); it != end();) + { + if ((*it)->getTimeOfLastPlay() + stale < now) + { + delete *it; + it = erase (it); + continue; + } + it++; + } +} + +bool RecentlyPlayedGameList::removeEntry(std::string id) +{ + bool found = false; + for (RecentlyPlayedGameList::iterator it = begin(); it != end();) + { + if ((*it)->getId() == id) + { + delete *it; + it = erase (it); + found = true; + continue; + } + it++; + } + return found; +} + +void RecentlyPlayedGameList::updateEntry(GameScenario *game_scenario) +{ + for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++) + { + if ((*it)->getId() == game_scenario->getId()) + { + (*it)->setTimeOfLastPlay(time(NULL)); + (*it)->setRound(game_scenario->getRound()); + } + } +} + +void RecentlyPlayedGameList::removeAllNetworkedGames() +{ + for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++) + { + if ((*it)->getPlayMode() == GameScenario::NETWORKED) + { + erase (it); + delete *it; + it = begin(); + continue; + } + } +} +// End of file