Cuberite
A lightweight, fast and extensible game server for Minecraft
Scoreboard.h
Go to the documentation of this file.
1 
2 // Scoreboard.h
3 
4 // Implementation of a scoreboard that keeps track of specified objectives
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "FunctionRef.h"
13 
14 
15 
16 
17 
18 class cClientHandle;
19 class cObjective;
20 class cTeam;
21 class cWorld;
22 
24 using cTeamCallback = cFunctionRef<bool(cTeam &)>;
25 
26 
27 
28 
29 
30 // tolua_begin
32 {
33 public:
34 
35  typedef int Score;
36 
37  enum eType
38  {
40 
45 
47 
52 
56  };
57 
58  // tolua_end
59 
60  static AString TypeToString(eType a_Type);
61 
62  static eType StringToType(const AString & a_Name);
63 
64 public:
65 
66  cObjective(const AString & a_Name, const AString & a_DisplayName, eType a_Type, cWorld * a_World);
67 
68  // tolua_begin
69 
70  eType GetType(void) const { return m_Type; }
71 
72  const AString & GetName(void) const { return m_Name; }
73  const AString & GetDisplayName(void) const { return m_DisplayName; }
74 
76  void Reset(void);
77 
79  Score GetScore(const AString & a_Name) const;
80 
82  void SetScore(const AString & a_Name, Score a_Score);
83 
85  void ResetScore(const AString & a_Name);
86 
88  Score AddScore(const AString & a_Name, Score a_Delta);
89 
91  Score SubScore(const AString & a_Name, Score a_Delta);
92 
93  void SetDisplayName(const AString & a_Name);
94 
95  // tolua_end
96 
98  void SendTo(cClientHandle & a_Client);
99 
100 private:
101 
102  typedef std::pair<AString, Score> cTrackedPlayer;
103 
104  typedef std::map<AString, Score> cScoreMap;
105 
107 
110 
112 
114 
115  friend class cScoreboardSerializer;
116 
117 
118 }; // tolua_export
119 
120 
121 
122 
123 
124 // tolua_begin
125 class cTeam
126 {
127 public:
128 
129  // tolua_end
130 
131  cTeam(
132  const AString & a_Name, const AString & a_DisplayName,
133  const AString & a_Prefix, const AString & a_Suffix
134  );
135 
136  // tolua_begin
137 
139  bool AddPlayer(const AString & a_Name);
140 
142  bool RemovePlayer(const AString & a_Name);
143 
145  bool HasPlayer(const AString & a_Name) const;
146 
148  void Reset(void);
149 
150  // tolua_begin
151 
153  size_t GetNumPlayers(void) const;
154 
155  bool AllowsFriendlyFire(void) const { return m_AllowsFriendlyFire; }
157 
158  const AString & GetDisplayName(void) const { return m_DisplayName; }
159  const AString & GetName(void) const { return m_Name; }
160 
161  const AString & GetPrefix(void) const { return m_Prefix; }
162  const AString & GetSuffix(void) const { return m_Suffix; }
163 
164  void SetFriendlyFire(bool a_Flag) { m_AllowsFriendlyFire = a_Flag; }
166 
167  void SetDisplayName(const AString & a_Name);
168 
169  void SetPrefix(const AString & a_Prefix) { m_Prefix = a_Prefix; }
170  void SetSuffix(const AString & a_Suffix) { m_Suffix = a_Suffix; }
171 
172  // tolua_end
173 
174 private:
175 
176  typedef std::set<AString> cPlayerNameSet;
177 
180 
183 
186 
188 
189  friend class cScoreboardSerializer;
190 
191 
192 }; // tolua_export
193 
194 
195 
196 
197 
198 // tolua_begin
200 {
201 public:
202 
204  {
205  dsList = 0,
208 
209  dsCount
210  };
211 
212  // tolua_end
213 
214 
215 public:
216 
217  cScoreboard(cWorld * a_World);
218 
219  // tolua_begin
220 
222  cObjective * RegisterObjective(const AString & a_Name, const AString & a_DisplayName, cObjective::eType a_Type);
223 
225  bool RemoveObjective(const AString & a_Name);
226 
228  cObjective * GetObjective(const AString & a_Name);
229 
231  cTeam * RegisterTeam(const AString & a_Name, const AString & a_DisplayName, const AString & a_Prefix, const AString & a_Suffix);
232 
234  bool RemoveTeam(const AString & a_Name);
235 
237  cTeam * GetTeam(const AString & a_Name);
238 
239  void SetDisplay(const AString & a_Objective, eDisplaySlot a_Slot);
240 
242 
243  size_t GetNumObjectives(void) const;
244 
245  size_t GetNumTeams(void) const;
246 
247  void AddPlayerScore(const AString & a_Name, cObjective::eType a_Type, cObjective::Score a_Value = 1);
248 
249  // tolua_end
250 
253 
255  void SendTo(cClientHandle & a_Client);
256 
257  cTeam * QueryPlayerTeam(const AString & a_Name); // WARNING: O(n logn)
258 
262 
265  bool ForEachObjective(cObjectiveCallback a_Callback); // Exported in ManualBindings.cpp
266 
269  bool ForEachTeam(cTeamCallback a_Callback); // Exported in ManualBindings.cpp
270 
271  void SetDisplay(cObjective * a_Objective, eDisplaySlot a_Slot);
272 
273 
274 private:
275 
276  typedef std::pair<AString, cObjective> cNamedObjective;
277  typedef std::pair<AString, cTeam> cNamedTeam;
278 
279  typedef std::map<AString, cObjective> cObjectiveMap;
280  typedef std::map<AString, cTeam> cTeamMap;
281 
282  // TODO 2014-01-19 xdot: Potential optimization - Sort objectives based on type
285 
288 
290 
292 
293  friend class cScoreboardSerializer;
294 
295 
296 }; // tolua_export
297 
298 
299 
300 
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
cScoreMap m_Scores
Definition: Scoreboard.h:106
Score AddScore(const AString &a_Name, Score a_Delta)
Adds a_Delta and returns the new score.
Definition: Scoreboard.cpp:143
std::pair< AString, Score > cTrackedPlayer
Definition: Scoreboard.h:102
@ otStatItemCraft
Definition: Scoreboard.h:49
@ otStatEntityKill
Definition: Scoreboard.h:54
@ otTotalKillCount
Definition: Scoreboard.h:43
@ otAchievement
Definition: Scoreboard.h:46
@ otStatBlockMine
Definition: Scoreboard.h:53
@ otStatItemBreak
Definition: Scoreboard.h:51
@ otStatEntityKilledBy
Definition: Scoreboard.h:55
@ otStatItemUse
Definition: Scoreboard.h:50
@ otDeathCount
Definition: Scoreboard.h:41
@ otPlayerKillCount
Definition: Scoreboard.h:42
void SetDisplayName(const AString &a_Name)
Definition: Scoreboard.cpp:171
static eType StringToType(const AString &a_Name)
Definition: Scoreboard.cpp:41
eType m_Type
Definition: Scoreboard.h:111
void Reset(void)
Resets the objective.
Definition: Scoreboard.cpp:89
std::map< AString, Score > cScoreMap
Definition: Scoreboard.h:104
eType GetType(void) const
Definition: Scoreboard.h:70
cObjective(const AString &a_Name, const AString &a_DisplayName, eType a_Type, cWorld *a_World)
Definition: Scoreboard.cpp:77
void SetScore(const AString &a_Name, Score a_Score)
Sets the score of the specified player.
Definition: Scoreboard.cpp:121
cWorld * m_World
Definition: Scoreboard.h:113
const AString & GetName(void) const
Definition: Scoreboard.h:72
AString m_DisplayName
Definition: Scoreboard.h:108
static AString TypeToString(eType a_Type)
Definition: Scoreboard.cpp:16
Score SubScore(const AString &a_Name, Score a_Delta)
Subtracts a_Delta and returns the new score.
Definition: Scoreboard.cpp:157
void ResetScore(const AString &a_Name)
Resets the score of the specified player.
Definition: Scoreboard.cpp:132
const AString & GetDisplayName(void) const
Definition: Scoreboard.h:73
Score GetScore(const AString &a_Name) const
Returns the score of the specified player.
Definition: Scoreboard.cpp:103
AString m_Name
Definition: Scoreboard.h:109
void SendTo(cClientHandle &a_Client)
Send this objective to the specified client.
Definition: Scoreboard.cpp:182
const AString & GetDisplayName(void) const
Definition: Scoreboard.h:158
cPlayerNameSet m_Players
Definition: Scoreboard.h:187
bool m_CanSeeFriendlyInvisible
Definition: Scoreboard.h:179
const AString & GetPrefix(void) const
Definition: Scoreboard.h:161
size_t GetNumPlayers(void) const
Returns the number of registered players.
Definition: Scoreboard.cpp:264
bool CanSeeFriendlyInvisible(void) const
Definition: Scoreboard.h:156
cTeam(const AString &a_Name, const AString &a_DisplayName, const AString &a_Prefix, const AString &a_Suffix)
Definition: Scoreboard.cpp:196
void SetDisplayName(const AString &a_Name)
Definition: Scoreboard.cpp:253
void SetCanSeeFriendlyInvisible(bool a_Flag)
Definition: Scoreboard.h:165
void SetPrefix(const AString &a_Prefix)
Definition: Scoreboard.h:169
const AString & GetSuffix(void) const
Definition: Scoreboard.h:162
AString m_Suffix
Definition: Scoreboard.h:185
void SetSuffix(const AString &a_Suffix)
Definition: Scoreboard.h:170
bool AllowsFriendlyFire(void) const
Definition: Scoreboard.h:155
AString m_DisplayName
Definition: Scoreboard.h:181
const AString & GetName(void) const
Definition: Scoreboard.h:159
void Reset(void)
Removes all registered players.
Definition: Scoreboard.cpp:242
AString m_Name
Definition: Scoreboard.h:182
AString m_Prefix
Definition: Scoreboard.h:184
bool m_AllowsFriendlyFire
Definition: Scoreboard.h:178
bool HasPlayer(const AString &a_Name) const
Returns whether the specified player is in this team.
Definition: Scoreboard.cpp:231
void SetFriendlyFire(bool a_Flag)
Definition: Scoreboard.h:164
bool RemovePlayer(const AString &a_Name)
Removes a player from the team.
Definition: Scoreboard.cpp:222
std::set< AString > cPlayerNameSet
Definition: Scoreboard.h:176
bool AddPlayer(const AString &a_Name)
Adds a new player to the team.
Definition: Scoreboard.cpp:213
size_t GetNumObjectives(void) const
Definition: Scoreboard.cpp:592
cObjective * GetObjective(const AString &a_Name)
Retrieves the objective with the specified name, nullptr if not found.
Definition: Scoreboard.cpp:339
AStringVector GetTeamNames()
Retrieves the list of team names.
Definition: Scoreboard.cpp:419
std::pair< AString, cTeam > cNamedTeam
Definition: Scoreboard.h:277
cTeam * RegisterTeam(const AString &a_Name, const AString &a_DisplayName, const AString &a_Prefix, const AString &a_Suffix)
Registers a new team, returns the cTeam instance, nullptr on name collision.
Definition: Scoreboard.cpp:359
cTeam * QueryPlayerTeam(const AString &a_Name)
Definition: Scoreboard.cpp:435
size_t GetNumTeams(void) const
Definition: Scoreboard.cpp:601
cObjectiveMap m_Objectives
Definition: Scoreboard.h:284
cObjective * GetObjectiveIn(eDisplaySlot a_Slot)
Definition: Scoreboard.cpp:479
bool ForEachObjectiveWith(cObjective::eType a_Type, cObjectiveCallback a_Callback)
Execute callback for each objective with the specified type Returns true if all objectives processed,...
Definition: Scoreboard.cpp:490
cTeam * GetTeam(const AString &a_Name)
Retrieves the team with the specified name, nullptr if not found.
Definition: Scoreboard.cpp:399
bool ForEachTeam(cTeamCallback a_Callback)
Execute callback for each team.
Definition: Scoreboard.cpp:531
void AddPlayerScore(const AString &a_Name, cObjective::eType a_Type, cObjective::Score a_Value=1)
Definition: Scoreboard.cpp:550
std::map< AString, cObjective > cObjectiveMap
Definition: Scoreboard.h:279
void SendTo(cClientHandle &a_Client)
Send this scoreboard to the specified client.
Definition: Scoreboard.cpp:567
cCriticalSection m_CSObjectives
Definition: Scoreboard.h:283
std::map< AString, cTeam > cTeamMap
Definition: Scoreboard.h:280
cObjective * m_Display[dsCount]
Definition: Scoreboard.h:291
cScoreboard(cWorld *a_World)
Definition: Scoreboard.cpp:273
cObjective * RegisterObjective(const AString &a_Name, const AString &a_DisplayName, cObjective::eType a_Type)
Registers a new scoreboard objective, returns the cObjective instance, nullptr on name collision.
Definition: Scoreboard.cpp:285
void SetDisplay(const AString &a_Objective, eDisplaySlot a_Slot)
Definition: Scoreboard.cpp:454
bool ForEachObjective(cObjectiveCallback a_Callback)
Execute callback for each objective.
Definition: Scoreboard.cpp:512
std::pair< AString, cObjective > cNamedObjective
Definition: Scoreboard.h:276
bool RemoveObjective(const AString &a_Name)
Removes a registered objective, returns true if operation was successful.
Definition: Scoreboard.cpp:308
cTeamMap m_Teams
Definition: Scoreboard.h:287
cWorld * m_World
Definition: Scoreboard.h:289
bool RemoveTeam(const AString &a_Name)
Removes a registered team, returns true if operation was successful.
Definition: Scoreboard.cpp:379
cCriticalSection m_CSTeams
Definition: Scoreboard.h:286
Definition: World.h:53