initial commit, lordsawar source, slightly modified
[lordsawar] / src / recently-played-game.h
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 #ifndef RECENTLY_PLAYED_GAME_H
19 #define RECENTLY_PLAYED_GAME_H
20
21 #include <gtkmm.h>
22 #include <string>
23
24
25 #include <sys/time.h>
26
27 #include "GameScenario.h"
28 class XML_Helper;
29
30 //! A single game entry in the recently played games list.
31 /**
32  *
33  */
34 class RecentlyPlayedGame
35 {
36     public:
37
38         //! The xml tag of this object in a recently played game file.
39         static std::string d_tag; 
40
41         //! Loading constructor.
42         /**
43          * Make a new recently played game object by reading it in from an 
44          * opened recently played games list file.
45          *
46          * @param helper  The opened recently played games list file to read the
47          *                game entry from.
48          */
49         RecentlyPlayedGame(XML_Helper* helper);
50
51         //! Default constructor.
52         /**
53          * Make a new recently played game object by taking values from the
54          * GameScenario.
55          */
56         RecentlyPlayedGame(GameScenario *game_scenario);
57         
58         //! Destructor.
59         virtual ~RecentlyPlayedGame();
60
61         // Get Methods
62
63         //! Get the scenario id of the recently played game entry.
64         std::string getId() const {return d_id;};
65
66         //! Get time of when this game was last played (seconds past the epoch).
67         time_t getTimeOfLastPlay() const { return d_time;};
68
69         //! Get the round that we last saw this game at..
70         guint32 getRound() const { return d_round;};
71
72         //! Get the number of cities in the game.
73         guint32 getNumberOfCities() const {return d_number_of_cities;};
74
75         //! Get the number of players in the game.
76         guint32 getNumberOfPlayers() const {return d_number_of_players;};
77
78         //! Get the kind of game.
79         GameScenario::PlayMode getPlayMode() const {return d_playmode;};
80
81         //! Get the name of the scenario.
82         std::string getName() const {return d_name;};
83
84
85         // Set Methods
86
87         //! Set the last time we saw something happen in this game.
88         void setTimeOfLastPlay(time_t then) { d_time = then;};
89
90         //! Set the round that we last saw this game at.
91         void setRound(guint32 round) { d_round = round;};
92
93
94         // Methods that operate on the class data but do not modify it.
95
96         //! Save the game entry to an opened file.
97         bool save(XML_Helper* helper) const;
98
99         //! Save the game entry, but not the enclosing tags.
100         bool saveContents(XML_Helper *helper) const;
101
102
103         // Static Methods
104
105         /**
106          * static load function (see XML_Helper)
107          * 
108          * Whenever a game entry is loaded, this function is called. It
109          * examines the stored id and calls the constructor of the appropriate
110          * recently played game class.
111          *
112          * @param helper       the XML_Helper instance for the savegame
113          */
114         static RecentlyPlayedGame* handle_load(XML_Helper *helper);
115
116     protected:
117
118         //! Save the entry to an opened file.
119         virtual bool doSave(XML_Helper *helper) const = 0;
120
121         // DATA
122         
123         //! The id of the game.
124         std::string d_id;
125
126         //! When the game was last played.
127         time_t d_time;
128
129         //! What round the game was at.
130         guint32 d_round;
131
132         //! How many cities the game has.
133         guint32 d_number_of_cities;
134
135         //! How many players the game had at the start of the game.
136         guint32 d_number_of_players;
137
138         //! The kind of game.
139         GameScenario::PlayMode d_playmode;
140
141         //! The name of the game.
142         std::string d_name;
143
144 };
145
146 class RecentlyPlayedHotseatGame : public RecentlyPlayedGame
147 {
148     public:
149         //! Make a new hotseat game entry.
150         RecentlyPlayedHotseatGame(GameScenario *game_scenario);
151
152         //! Load a new hotseat game from an opened file.
153         RecentlyPlayedHotseatGame(XML_Helper *helper);
154
155         //! Destroy a hotseat game entry.
156         ~RecentlyPlayedHotseatGame();
157
158
159         // Methods that operate on the class data but do not modify it.
160         
161         //! Save the hotseat game entry to an opened file.
162         virtual bool doSave(XML_Helper *helper) const;
163
164
165         // Methods that operate on the class data and modify it.
166
167         //! Assign the filename to the entry.
168         bool fillData(std::string filename);
169
170     private:
171         std::string d_filename;
172 };
173
174 class RecentlyPlayedPbmGame : public RecentlyPlayedGame
175 {
176     public:
177         //! Make a new pbm game entry.
178         RecentlyPlayedPbmGame(GameScenario *game_scenario);
179
180         //! Load a new pbm game from an opened saved-game file.
181         RecentlyPlayedPbmGame(XML_Helper *helper);
182
183         //! Destroy a pbm game entry.
184         ~RecentlyPlayedPbmGame();
185
186
187         // Methods that operate on the class data but do not modify it.
188         
189         //! Save the play-by-mail game entry to an opened file.
190         virtual bool doSave(XML_Helper *helper) const;
191
192
193         // Methods that operate on the class data and modify it.
194
195         //! Assign the filename to the entry.
196         bool fillData(std::string filename);
197     private:
198
199         // DATA
200         
201         //! The filename of the play-by-mail game for this entry.
202         std::string d_filename;
203 };
204
205 class RecentlyPlayedNetworkedGame : public RecentlyPlayedGame
206 {
207     public:
208         //! Make a new networked game entry.
209         RecentlyPlayedNetworkedGame(GameScenario *game_scenario);
210
211         //! Load a new networked game from an opened file.
212         RecentlyPlayedNetworkedGame(XML_Helper *helper);
213
214         //! Destroy a networked game entry.
215         ~RecentlyPlayedNetworkedGame();
216
217
218         // Get Methods
219         
220         //! Get the hostname associated with the game.
221         std::string getHost() const {return d_host;};
222
223         //! Get the port associated with the host, and game.
224         guint32 getPort() const {return d_port;};
225
226
227         // Methods that operate on the class data but do not modify it.
228
229         //! Save the networked game entry to an opened file.
230         virtual bool doSave(XML_Helper *helper) const;
231
232
233         // Methods that operate on the class data and modify it.
234
235         bool fillData(std::string host, guint32 port);
236
237     private:
238
239         // DATA
240         
241         //! The hostname that the network game was hosted at.
242         std::string d_host;
243
244         //! The port on the hostname that the network game was hosted at.
245         guint32 d_port;
246 };
247
248 #endif // RECENTLY_PLAYED_GAME_H