~ubuntu-branches/ubuntu/saucy/lordsawar/saucy

« back to all changes in this revision

Viewing changes to src/recently-played-game-list.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese
  • Date: 2008-12-20 13:52:12 UTC
  • mfrom: (1.1.6 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20081220135212-noeb2w3y98ebo7o9
Tags: 0.1.4-1
[ Barry deFreese ]
* New upstream release.
* Move 0.0.8-2.1 changelog entry to correct point in changelog.
* Make lordsawar-data suggest lordsawar.
* Update my e-mail address.
* Add build-depends on intltool, uuid-dev, and libboost-dev.
* Don't install locales since there are no translations currently.
* Add simple man page for new lordsawar-pbm binary.
* Drop gcc4.3 patches as they have been fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 2 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)
 
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, Uint32 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
      case GameScenario::CAMPAIGN:
 
173
          {
 
174
            RecentlyPlayedCampaignGame *g = NULL;
 
175
            g = new RecentlyPlayedCampaignGame(game_scenario);
 
176
            g->fillData(filename);
 
177
            push_back(g);
 
178
            break;
 
179
          }
 
180
      default:
 
181
        break;
 
182
    }
 
183
}
 
184
 
 
185
bool RecentlyPlayedGameList::orderByTime(RecentlyPlayedGame*rhs, RecentlyPlayedGame *lhs)
 
186
{
 
187
  if (rhs->getTimeOfLastPlay() > lhs->getTimeOfLastPlay())
 
188
    return true;
 
189
  else
 
190
    return false;
 
191
}
 
192
 
 
193
void RecentlyPlayedGameList::pruneGames()
 
194
{
 
195
  sort(orderByTime);
 
196
  pruneOldGames(TWO_WEEKS_OLD);
 
197
  pruneTooManyGames(10);
 
198
}
 
199
 
 
200
void RecentlyPlayedGameList::pruneTooManyGames(int too_many)
 
201
{
 
202
  int count = 0;
 
203
  for (RecentlyPlayedGameList::iterator it = begin(); it != end();)
 
204
    {
 
205
      count++;
 
206
      if (count > too_many)
 
207
        {
 
208
          delete *it;
 
209
          it = erase (it);
 
210
          continue;
 
211
        }
 
212
      it++;
 
213
    }
 
214
}
 
215
 
 
216
void RecentlyPlayedGameList::pruneOldGames(int stale)
 
217
{
 
218
  time_t now = time(NULL);
 
219
  for (RecentlyPlayedGameList::iterator it = begin(); it != end();)
 
220
    {
 
221
      if ((*it)->getTimeOfLastPlay() + stale < now)
 
222
        {
 
223
          delete *it;
 
224
          it = erase (it);
 
225
          continue;
 
226
        }
 
227
      it++;
 
228
    }
 
229
}
 
230
 
 
231
bool RecentlyPlayedGameList::removeEntry(std::string id)
 
232
{
 
233
  bool found = false;
 
234
  for (RecentlyPlayedGameList::iterator it = begin(); it != end();)
 
235
    {
 
236
      if ((*it)->getId() == id)
 
237
        {
 
238
          delete *it;
 
239
          it = erase (it);
 
240
          found = true;
 
241
          continue;
 
242
        }
 
243
      it++;
 
244
    }
 
245
  return found;
 
246
}
 
247
 
 
248
void RecentlyPlayedGameList::updateEntry(GameScenario *game_scenario)
 
249
{
 
250
  for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++)
 
251
    {
 
252
      if ((*it)->getId() == game_scenario->getId())
 
253
        {
 
254
          (*it)->setTimeOfLastPlay(time(NULL));
 
255
          (*it)->setRound(game_scenario->getRound());
 
256
        }
 
257
    }
 
258
}
 
259
        
 
260
void RecentlyPlayedGameList::removeAllNetworkedGames()
 
261
{
 
262
  for (RecentlyPlayedGameList::iterator it = begin(); it != end(); it++)
 
263
    {
 
264
      if ((*it)->getPlayMode() == GameScenario::NETWORKED)
 
265
        {
 
266
          erase (it);
 
267
          delete *it;
 
268
          it = begin();
 
269
          continue;
 
270
        }
 
271
    }
 
272
}
 
273
// End of file