SuperTuxKart
Loading...
Searching...
No Matches
network_player_profile.hpp
1//
2// SuperTuxKart - a fun racing game with go-kart
3// Copyright (C) 2013-2015 SuperTuxKart-Team
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
22#ifndef HEADER_NETWORK_PLAYER_PROFILE
23#define HEADER_NETWORK_PLAYER_PROFILE
24
25#include "network/kart_data.hpp"
26#include "utils/types.hpp"
27
28#include "irrString.h"
29#include <atomic>
30#include <limits>
31#include <memory>
32#include <string>
33
34class STKPeer;
35enum KartTeam : int8_t;
36enum HandicapLevel : uint8_t;
37
42{
43private:
44 std::weak_ptr<STKPeer> m_peer;
45
47 irr::core::stringw m_player_name;
48
50 uint32_t m_host_id;
51
52 float m_default_kart_color;
53
54 uint32_t m_online_id;
55
57 std::atomic<HandicapLevel> m_handicap;
58
60 std::string m_kart_name;
61
64
67
70
71 std::atomic<KartTeam> m_team;
72
74 std::string m_country_code;
75
76 KartData m_kart_data;
77public:
78 // ------------------------------------------------------------------------
79 static std::shared_ptr<NetworkPlayerProfile>
80 getReservedProfile(KartTeam team)
81 {
82 return std::make_shared<NetworkPlayerProfile>(team);
83 }
84 // ------------------------------------------------------------------------
85 /* Placeholder profile for reserved player in live join, which its host id
86 * is uint32_t max. */
87 NetworkPlayerProfile(KartTeam team)
88 {
89 m_kart_name = "tux";
90 m_host_id = std::numeric_limits<uint32_t>::max();
91 m_default_kart_color = 0.0f;
92 m_online_id = 0;
93 m_handicap.store((HandicapLevel)0);
95 m_team.store(team);
96 resetGrandPrixData();
97 }
98 // ------------------------------------------------------------------------
99 NetworkPlayerProfile(std::shared_ptr<STKPeer> peer,
100 const irr::core::stringw &name, uint32_t host_id,
101 float default_kart_color, uint32_t online_id,
102 HandicapLevel handicap,
103 uint8_t local_player_id, KartTeam team,
104 const std::string& country_code)
105 {
106 m_peer = peer;
107 m_player_name = name;
108 m_host_id = host_id;
109 m_default_kart_color = default_kart_color;
110 m_online_id = online_id;
111 m_handicap.store(handicap);
112 m_local_player_id = local_player_id;
113 m_team.store(team);
114 m_country_code = country_code;
115 resetGrandPrixData();
116 }
117 // ------------------------------------------------------------------------
119 // ------------------------------------------------------------------------
120 bool isLocalPlayer() const;
121 // ------------------------------------------------------------------------
123 uint32_t getHostId() const { return m_host_id; }
124 // ------------------------------------------------------------------------
126 void setKartName(const std::string &kart_name)
127 {
128 m_kart_name = kart_name;
129 m_kart_data = KartData();
130 }
131 // ------------------------------------------------------------------------
133 const std::string &getKartName() const { return m_kart_name; }
134 // ------------------------------------------------------------------------
136 uint8_t getLocalPlayerId() const { return m_local_player_id; }
137 // ------------------------------------------------------------------------
139 HandicapLevel getHandicap() const { return m_handicap.load(); }
140 // ------------------------------------------------------------------------
141 void setHandicap(HandicapLevel h) { m_handicap.store(h); }
142 // ------------------------------------------------------------------------
144 const irr::core::stringw& getName() const { return m_player_name; }
145 // ------------------------------------------------------------------------
146 float getDefaultKartColor() const { return m_default_kart_color; }
147 // ------------------------------------------------------------------------
148 uint32_t getOnlineId() const { return m_online_id; }
149 // ------------------------------------------------------------------------
150 bool isOfflineAccount() const { return m_online_id == 0; }
151 // ------------------------------------------------------------------------
152 std::shared_ptr<STKPeer> getPeer() const { return m_peer.lock(); }
153 // ------------------------------------------------------------------------
154 int getScore() const { return m_score; }
155 // ------------------------------------------------------------------------
156 float getOverallTime() const { return m_overall_time; }
157 // ------------------------------------------------------------------------
158 void setScore(int score) { m_score = score; }
159 // ------------------------------------------------------------------------
160 void setOverallTime(float time) { m_overall_time = time; }
161 // ------------------------------------------------------------------------
162 void resetGrandPrixData()
163 {
164 m_score = 0;
165 m_overall_time = 0.0f;
166 }
167 // ------------------------------------------------------------------------
168 void setTeam(KartTeam team) { m_team.store(team); }
169 // ------------------------------------------------------------------------
170 KartTeam getTeam() const { return m_team.load(); }
171 // ------------------------------------------------------------------------
172 const std::string& getCountryCode() const { return m_country_code; }
173 // ------------------------------------------------------------------------
174 void setKartData(const KartData& data) { m_kart_data = data; }
175 // ------------------------------------------------------------------------
176 const KartData& getKartData() const { return m_kart_data; }
177}; // class NetworkPlayerProfile
178
179#endif // HEADER_NETWORK_PLAYER_PROFILE
Definition: kart_data.hpp:12
Contains the profile of a player.
Definition: network_player_profile.hpp:42
const irr::core::stringw & getName() const
Returns the name of this player.
Definition: network_player_profile.hpp:144
uint8_t m_local_player_id
The local player id relative to each peer.
Definition: network_player_profile.hpp:63
const std::string & getKartName() const
Returns the name of the kart this player has selected.
Definition: network_player_profile.hpp:133
uint8_t getLocalPlayerId() const
Retuens the local player id for this player.
Definition: network_player_profile.hpp:136
irr::core::stringw m_player_name
The name of the player.
Definition: network_player_profile.hpp:47
std::string m_country_code
2-letter country code of player.
Definition: network_player_profile.hpp:74
void setKartName(const std::string &kart_name)
Sets the kart name for this player.
Definition: network_player_profile.hpp:126
int m_score
Score if grand prix.
Definition: network_player_profile.hpp:66
std::string m_kart_name
The selected kart id.
Definition: network_player_profile.hpp:60
HandicapLevel getHandicap() const
Returns the player's handicap.
Definition: network_player_profile.hpp:139
uint32_t getHostId() const
Returns the host id of this player.
Definition: network_player_profile.hpp:123
float m_overall_time
Overall time if grand prix.
Definition: network_player_profile.hpp:69
bool isLocalPlayer() const
Returns true if this player is local, i.e.
Definition: network_player_profile.cpp:28
std::atomic< HandicapLevel > m_handicap
Handicap level of this player.
Definition: network_player_profile.hpp:57
uint32_t m_host_id
Host id of this player.
Definition: network_player_profile.hpp:50
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
HandicapLevel
Handicap per player.
Definition: remote_kart_info.hpp:42
Declares the general types that are used by the network.