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 
13 
14 #include "FunctionRef.h"
15 
16 
17 class cObjective;
18 class cTeam;
19 class cWorld;
20 
23 
24 
25 
26 
27 
28 // tolua_begin
30 {
31 public:
32 
33  typedef int Score;
34 
35  enum eType
36  {
38 
43 
45 
50 
54  };
55 
56  // tolua_end
57 
58  static AString TypeToString(eType a_Type);
59 
60  static eType StringToType(const AString & a_Name);
61 
62 public:
63 
64  cObjective(const AString & a_Name, const AString & a_DisplayName, eType a_Type, cWorld * a_World);
65 
66  // tolua_begin
67 
68  eType GetType(void) const { return m_Type; }
69 
70  const AString & GetName(void) const { return m_Name; }
71  const AString & GetDisplayName(void) const { return m_DisplayName; }
72 
74  void Reset(void);
75 
77  Score GetScore(const AString & a_Name) const;
78 
80  void SetScore(const AString & a_Name, Score a_Score);
81 
83  void ResetScore(const AString & a_Name);
84 
86  Score AddScore(const AString & a_Name, Score a_Delta);
87 
89  Score SubScore(const AString & a_Name, Score a_Delta);
90 
91  void SetDisplayName(const AString & a_Name);
92 
93  // tolua_end
94 
96  void SendTo(cClientHandle & a_Client);
97 
98  static const char * GetClassStatic(void) // Needed for ManualBindings's ForEach templates
99  {
100  return "cObjective";
101  }
102 
103 
104 private:
105 
106  typedef std::pair<AString, Score> cTrackedPlayer;
107 
108  typedef std::map<AString, Score> cScoreMap;
109 
110  cScoreMap m_Scores;
111 
114 
116 
118 
119  friend class cScoreboardSerializer;
120 
121 
122 }; // tolua_export
123 
124 
125 
126 
127 
128 // tolua_begin
129 class cTeam
130 {
131 public:
132 
133  // tolua_end
134 
135  cTeam(
136  const AString & a_Name, const AString & a_DisplayName,
137  const AString & a_Prefix, const AString & a_Suffix
138  );
139 
140  // tolua_begin
141 
143  bool AddPlayer(const AString & a_Name);
144 
146  bool RemovePlayer(const AString & a_Name);
147 
149  bool HasPlayer(const AString & a_Name) const;
150 
152  void Reset(void);
153 
154  // tolua_begin
155 
157  size_t GetNumPlayers(void) const;
158 
159  bool AllowsFriendlyFire(void) const { return m_AllowsFriendlyFire; }
160  bool CanSeeFriendlyInvisible(void) const { return m_CanSeeFriendlyInvisible; }
161 
162  const AString & GetDisplayName(void) const { return m_DisplayName; }
163  const AString & GetName(void) const { return m_Name; }
164 
165  const AString & GetPrefix(void) const { return m_Prefix; }
166  const AString & GetSuffix(void) const { return m_Suffix; }
167 
168  void SetFriendlyFire(bool a_Flag) { m_AllowsFriendlyFire = a_Flag; }
169  void SetCanSeeFriendlyInvisible(bool a_Flag) { m_CanSeeFriendlyInvisible = a_Flag; }
170 
171  void SetDisplayName(const AString & a_Name);
172 
173  void SetPrefix(const AString & a_Prefix) { m_Prefix = a_Prefix; }
174  void SetSuffix(const AString & a_Suffix) { m_Suffix = a_Suffix; }
175 
176  // tolua_end
177 
178  static const char * GetClassStatic(void) // Needed for ManualBindings's ForEach templates
179  {
180  return "cTeam";
181  }
182 
183 private:
184 
185  typedef std::set<AString> cPlayerNameSet;
186 
189 
192 
195 
196  cPlayerNameSet m_Players;
197 
198  friend class cScoreboardSerializer;
199 
200 
201 }; // tolua_export
202 
203 
204 
205 
206 
207 // tolua_begin
209 {
210 public:
211 
213  {
214  dsList = 0,
217 
218  dsCount
219  };
220 
221  // tolua_end
222 
223 
224 public:
225 
226  cScoreboard(cWorld * a_World);
227 
228  // tolua_begin
229 
231  cObjective * RegisterObjective(const AString & a_Name, const AString & a_DisplayName, cObjective::eType a_Type);
232 
234  bool RemoveObjective(const AString & a_Name);
235 
237  cObjective * GetObjective(const AString & a_Name);
238 
240  cTeam * RegisterTeam(const AString & a_Name, const AString & a_DisplayName, const AString & a_Prefix, const AString & a_Suffix);
241 
243  bool RemoveTeam(const AString & a_Name);
244 
246  cTeam * GetTeam(const AString & a_Name);
247 
248  void SetDisplay(const AString & a_Objective, eDisplaySlot a_Slot);
249 
250  cObjective * GetObjectiveIn(eDisplaySlot a_Slot);
251 
252  size_t GetNumObjectives(void) const;
253 
254  size_t GetNumTeams(void) const;
255 
256  void AddPlayerScore(const AString & a_Name, cObjective::eType a_Type, cObjective::Score a_Value = 1);
257 
258  // tolua_end
259 
261  AStringVector GetTeamNames();
262 
264  void SendTo(cClientHandle & a_Client);
265 
266  cTeam * QueryPlayerTeam(const AString & a_Name); // WARNING: O(n logn)
267 
270  bool ForEachObjectiveWith(cObjective::eType a_Type, cObjectiveCallback a_Callback);
271 
274  bool ForEachObjective(cObjectiveCallback a_Callback); // Exported in ManualBindings.cpp
275 
278  bool ForEachTeam(cTeamCallback a_Callback); // Exported in ManualBindings.cpp
279 
280  void SetDisplay(cObjective * a_Objective, eDisplaySlot a_Slot);
281 
282 
283 private:
284 
285  typedef std::pair<AString, cObjective> cNamedObjective;
286  typedef std::pair<AString, cTeam> cNamedTeam;
287 
288  typedef std::map<AString, cObjective> cObjectiveMap;
289  typedef std::map<AString, cTeam> cTeamMap;
290 
291  // TODO 2014-01-19 xdot: Potential optimization - Sort objectives based on type
293  cObjectiveMap m_Objectives;
294 
296  cTeamMap m_Teams;
297 
299 
300  cObjective * m_Display[dsCount];
301 
302  friend class cScoreboardSerializer;
303 
304 
305 }; // tolua_export
306 
307 
308 
309 
cWorld * m_World
Definition: Scoreboard.h:117
void SendTo(cClientHandle &a_Client)
Send this objective to the specified client.
Definition: Scoreboard.cpp:182
const AString & GetSuffix(void) const
Definition: Scoreboard.h:166
Score GetScore(const AString &a_Name) const
Returns the score of the specified player.
Definition: Scoreboard.cpp:103
bool AllowsFriendlyFire(void) const
Definition: Scoreboard.h:159
AString m_Prefix
Definition: Scoreboard.h:193
static eType StringToType(const AString &a_Name)
Definition: Scoreboard.cpp:41
void SetPrefix(const AString &a_Prefix)
Definition: Scoreboard.h:173
cPlayerNameSet m_Players
Definition: Scoreboard.h:196
eType GetType(void) const
Definition: Scoreboard.h:68
AString m_DisplayName
Definition: Scoreboard.h:190
static const char * GetClassStatic(void)
Definition: Scoreboard.h:178
cCriticalSection m_CSTeams
Definition: Scoreboard.h:295
std::pair< AString, Score > cTrackedPlayer
Definition: Scoreboard.h:106
const AString & GetPrefix(void) const
Definition: Scoreboard.h:165
void ResetScore(const AString &a_Name)
Resets the score of the specified player.
Definition: Scoreboard.cpp:132
cObjective(const AString &a_Name, const AString &a_DisplayName, eType a_Type, cWorld *a_World)
Definition: Scoreboard.cpp:77
const AString & GetName(void) const
Definition: Scoreboard.h:163
Score AddScore(const AString &a_Name, Score a_Delta)
Adds a_Delta and returns the new score.
Definition: Scoreboard.cpp:143
bool CanSeeFriendlyInvisible(void) const
Definition: Scoreboard.h:160
std::set< AString > cPlayerNameSet
Definition: Scoreboard.h:185
static const char * GetClassStatic(void)
Definition: Scoreboard.h:98
cTeamMap m_Teams
Definition: Scoreboard.h:296
std::vector< AString > AStringVector
Definition: StringUtils.h:14
std::pair< AString, cTeam > cNamedTeam
Definition: Scoreboard.h:286
cObjectiveMap m_Objectives
Definition: Scoreboard.h:293
AString m_Name
Definition: Scoreboard.h:191
std::map< AString, cTeam > cTeamMap
Definition: Scoreboard.h:289
Definition: World.h:65
AString m_Name
Definition: Scoreboard.h:113
void SetSuffix(const AString &a_Suffix)
Definition: Scoreboard.h:174
AString m_DisplayName
Definition: Scoreboard.h:112
cCriticalSection m_CSObjectives
Definition: Scoreboard.h:292
cWorld * m_World
Definition: Scoreboard.h:298
Score SubScore(const AString &a_Name, Score a_Delta)
Subtracts a_Delta and returns the new score.
Definition: Scoreboard.cpp:157
const AString & GetName(void) const
Definition: Scoreboard.h:70
const AString & GetDisplayName(void) const
Definition: Scoreboard.h:71
bool m_CanSeeFriendlyInvisible
Definition: Scoreboard.h:188
void SetScore(const AString &a_Name, Score a_Score)
Sets the score of the specified player.
Definition: Scoreboard.cpp:121
std::string AString
Definition: StringUtils.h:13
std::pair< AString, cObjective > cNamedObjective
Definition: Scoreboard.h:285
std::map< AString, Score > cScoreMap
Definition: Scoreboard.h:108
void SetCanSeeFriendlyInvisible(bool a_Flag)
Definition: Scoreboard.h:169
AString m_Suffix
Definition: Scoreboard.h:194
static AString TypeToString(eType a_Type)
Definition: Scoreboard.cpp:16
const AString & GetDisplayName(void) const
Definition: Scoreboard.h:162
void SetFriendlyFire(bool a_Flag)
Definition: Scoreboard.h:168
bool m_AllowsFriendlyFire
Definition: Scoreboard.h:187
eType m_Type
Definition: Scoreboard.h:115
void SetDisplayName(const AString &a_Name)
Definition: Scoreboard.cpp:171
std::map< AString, cObjective > cObjectiveMap
Definition: Scoreboard.h:288
cScoreMap m_Scores
Definition: Scoreboard.h:110
void Reset(void)
Resets the objective.
Definition: Scoreboard.cpp:89