SuperTuxKart
Loading...
Searching...
No Matches
player_manager.hpp
1//
2// SuperTuxKart - a fun racing game with go-kart
3// Copyright (C) 2010-2015 Joerg Henrichs
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19#ifndef HEADER_PLAYER_MANAGER_HPP
20#define HEADER_PLAYER_MANAGER_HPP
21
22#include "achievements/achievement.hpp"
23#include "achievements/achievements_status.hpp"
24#include "config/player_profile.hpp"
25#include "utils/no_copy.hpp"
26#include "utils/ptr_vector.hpp"
27
28#include <irrString.h>
29
30#include <cstddef> // NULL
31#include <memory>
32
34
35namespace Online
36{
37 class CurrentUser;
38 class HTTPRequest;
39 class OnlineProfile;
40 class XMLRequest;
41}
42class PlayerProfile;
43
55class PlayerManager : public NoCopy
56{
57private:
58 static PlayerManager* m_player_manager;
59
60 PtrVector<PlayerProfile> m_all_players;
61
64
68
69 void load();
72
73
74public:
75 static void create();
76 // ------------------------------------------------------------------------
79 {
80 assert(m_player_manager);
81 return m_player_manager;
82 } // get
83 // ------------------------------------------------------------------------
84 static void destroy()
85 {
86 delete m_player_manager;
87 m_player_manager = NULL;
88 } // destroy
89
90 void save();
91 void initRemainingData();
92 unsigned int getUniqueId() const;
93 void addDefaultPlayer();
94 PlayerProfile* addNewPlayer(const irr::core::stringw& name);
95 void createGuestPlayers(int n);
96 void deletePlayer(PlayerProfile *player);
97 void setCurrentPlayer(PlayerProfile *player);
98 const PlayerProfile *getPlayerById(unsigned int id);
100 unsigned int getNumNonGuestPlayers() const;
101 static void setUserDetails(std::shared_ptr<Online::HTTPRequest> request,
102 const std::string &action,
103 const std::string &php_name = "");
104 static unsigned int getCurrentOnlineId();
105 static bool isCurrentLoggedIn();
107
109 static void requestOnlinePoll();
110 static void resumeSavedSession();
111 static void onSTKQuit();
112 static void requestSignOut();
113 static void requestSignIn(const irr::core::stringw &username,
114 const irr::core::stringw &password);
115
116 // ------------------------------------------------------------------------
119 {
120 return get()->m_current_player;
121 } // getCurrentPlayer
122
123 // ------------------------------------------------------------------------
124 PlayerProfile *getPlayer(const irr::core::stringw &name);
125 // ------------------------------------------------------------------------
127 unsigned int getNumPlayers() const { return m_all_players.size(); }
128 // ------------------------------------------------------------------------
130 const PlayerProfile *getPlayer(unsigned int n) const
131 {
132 return &m_all_players[n];
133 } // getPlayer
134 // ------------------------------------------------------------------------
136 PlayerProfile *getPlayer(unsigned int n) { return &m_all_players[n];}
137 // ------------------------------------------------------------------------
140 {
141 return PlayerManager::getCurrentPlayer()->getAchievementsStatus();
142 } // getCurrentAchievementsStatus
143
144 // ------------------------------------------------------------------------
150 static void increaseAchievement(unsigned int achievement_data_id,
151 int increase = 1)
152 {
154 ->increaseDataVar(achievement_data_id, increase);
155 } // increaseAchievement
156
157 // ------------------------------------------------------------------------
161 static void resetAchievementData(unsigned int achievement_data_id)
162 {
164 ->resetDataVar(achievement_data_id);
165 } // resetAchievementData
166
167 // ------------------------------------------------------------------------
171 static void onRaceEnd(bool restart)
172 {
173 getCurrentAchievementsStatus()->onRaceEnd(restart);
174 } // onRaceEnd
175
176
177 // ----------------------------------------------------------------------------
181 static void trackEvent(std::string track_ident, AchievementsStatus::TrackData event)
182 {
183 getCurrentAchievementsStatus()->trackEvent(track_ident, event);
184 } // trackEvent
185
186 // ----------------------------------------------------------------------------
187 static void resetKartHits(int num_karts)
188 {
189 getCurrentAchievementsStatus()->resetKartHits(num_karts);
190 } // resetKartHits
191
192 // ----------------------------------------------------------------------------
193 static void addKartHit(int kart_id)
194 {
195 getCurrentAchievementsStatus()->addKartHit(kart_id);
196 } // addKartHit
197
198 // ------------------------------------------------------------------------
199}; // PlayerManager
200#endif
This class keeps tracks of all achievements of one player.
Definition: achievements_status.hpp:43
void trackEvent(std::string track_ident, AchievementsStatus::TrackData event)
Use the event type to increment the correct track event counter.
Definition: achievements_status.cpp:601
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Class that represents an online profile.
Definition: online_profile.hpp:42
A special class that manages all local player accounts.
Definition: player_manager.hpp:56
static bool isCurrentLoggedIn()
Returns whether a user is signed in or not.
Definition: player_manager.cpp:62
static void onRaceEnd(bool restart)
Reset achievements which have to be done in one race.
Definition: player_manager.hpp:171
void addDefaultPlayer()
Called when no player profiles exists.
Definition: player_manager.cpp:352
static Online::OnlineProfile * getCurrentOnlineProfile()
Returns the current online profile (which is the list of all achievements and friends).
Definition: player_manager.cpp:140
void save()
Saves all player profiles to players.xml.
Definition: player_manager.cpp:249
const PlayerProfile * getPlayerById(unsigned int id)
Returns a PlayerProfile with a given id.
Definition: player_manager.cpp:442
static PlayerProfile * getCurrentPlayer()
Returns the current player.
Definition: player_manager.hpp:118
static void resetAchievementData(unsigned int achievement_data_id)
Reset an achievement data for current player.
Definition: player_manager.hpp:161
const PlayerProfile * getPlayer(unsigned int n) const
Returns a player with a given unique id.
Definition: player_manager.hpp:130
static void increaseAchievement(unsigned int achievement_data_id, int increase=1)
A handy shortcut to increase points for an achievement data of the current player.
Definition: player_manager.hpp:150
PlayerProfile * addNewPlayer(const irr::core::stringw &name)
Adds a new player to the list of all players.
Definition: player_manager.cpp:290
static PlayerProfile::OnlineState getCurrentOnlineState()
Returns the online state of the current player.
Definition: player_manager.cpp:80
void deletePlayer(PlayerProfile *player)
Deletes a player profile from the list of all profiles.
Definition: player_manager.cpp:300
unsigned int getNumNonGuestPlayers() const
Returns the number of 'real' (non-guest) players.
Definition: player_manager.cpp:412
static void create()
Create the instance of the player manager.
Definition: player_manager.cpp:41
void setCurrentPlayer(PlayerProfile *player)
Sets the current player.
Definition: player_manager.cpp:471
static void onSTKQuit()
Sends a message to the server that the client has been closed, if a user is signed in.
Definition: player_manager.cpp:106
PlayerProfile * m_current_player
A pointer to the current player.
Definition: player_manager.hpp:63
static void trackEvent(std::string track_ident, AchievementsStatus::TrackData event)
Transmit an incrementation request of one of the track event counters.
Definition: player_manager.hpp:181
PlayerProfile * getPlayer(unsigned int n)
Returns a player with a given unique id.
Definition: player_manager.hpp:136
void createGuestPlayers(int n)
Makes sure at least n guest players exist.
Definition: player_manager.cpp:387
void initRemainingData()
The 2nd loading stage.
Definition: player_manager.cpp:220
unsigned int getUniqueId() const
This returns a unique id.
Definition: player_manager.cpp:425
void load()
Manages the loading of saved player data from the players.xml file.
Definition: player_manager.cpp:178
static void requestSignIn(const irr::core::stringw &username, const irr::core::stringw &password)
Create a signin request.
Definition: player_manager.cpp:122
static PlayerManager * get()
Static singleton get function.
Definition: player_manager.hpp:78
unsigned int getNumPlayers() const
Returns the number of players in the config file.
Definition: player_manager.hpp:127
const XMLNode * m_player_data
Saves the XML tree from players.xml for use in the 2nd loading stage (initRemainingData).
Definition: player_manager.hpp:67
~PlayerManager()
Destructor.
Definition: player_manager.cpp:157
PlayerProfile * getPlayer(const irr::core::stringw &name)
Returns a player with a given name.
Definition: player_manager.cpp:457
static void requestOnlinePoll()
Sends a request to the server to see if any new information is available.
Definition: player_manager.cpp:89
static AchievementsStatus * getCurrentAchievementsStatus()
A handy shortcut funtion.
Definition: player_manager.hpp:139
static unsigned int getCurrentOnlineId()
Returns the online id of the current player.
Definition: player_manager.cpp:71
static void setUserDetails(std::shared_ptr< Online::HTTPRequest > request, const std::string &action, const std::string &php_name="")
Adds the login credential to a http request.
Definition: player_manager.cpp:53
PlayerManager()
Constructor.
Definition: player_manager.cpp:148
void enforceCurrentPlayer()
This function makes sure that a current player is defined.
Definition: player_manager.cpp:315
static void resumeSavedSession()
Reconnect to the server using the saved session data.
Definition: player_manager.cpp:97
static void requestSignOut()
Signs the current user out.
Definition: player_manager.cpp:131
Class for managing player profiles (name, usage frequency, etc.).
Definition: player_profile.hpp:54
OnlineState
The online state a player can be in.
Definition: player_profile.hpp:58
Definition: ptr_vector.hpp:44
utility class used to parse XML files
Definition: xml_node.hpp:48