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