summaryrefslogtreecommitdiffstats
path: root/src/Scoreboard.h
blob: f64ba2bceed33c29bc15cd655015b5a8709eb655 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

// Scoreboard.h

// Implementation of a scoreboard that keeps track of specified objectives





#pragma once





class cObjective;
class cWorld;

typedef cItemCallback<cObjective> cObjectiveCallback;





// tolua_begin
class cObjective
{
public:

	typedef int Score;

	enum eType
	{
		E_TYPE_DUMMY,

		E_TYPE_DEATH_COUNT,
		E_TYPE_PLAYER_KILL_COUNT,
		E_TYPE_TOTAL_KILL_COUNT,
		E_TYPE_HEALTH,

		E_TYPE_ACHIEVEMENT,

		E_TYPE_STAT,
		E_TYPE_STAT_ITEM_CRAFT,
		E_TYPE_STAT_ITEM_USE,
		E_TYPE_STAT_ITEM_BREAK,

		E_TYPE_STAT_BLOCK_MINE,
		E_TYPE_STAT_ENTITY_KILL,
		E_TYPE_STAT_ENTITY_KILLED_BY
	};

	// tolua_end

	static AString TypeToString(eType a_Type);

	static eType StringToType(const AString & a_Name);

public:

	cObjective(const AString & a_Name, const AString & a_DisplayName, eType a_Type, cWorld * a_World);

	// tolua_begin

	eType GetType(void) const { return m_Type; }

	const AString & GetName(void)        const { return m_Name; }
	const AString & GetDisplayName(void) const { return m_DisplayName; }

	/// Resets the objective
	void Reset(void);

	/// Returns the score of the specified player
	Score GetScore(const AString & a_Name) const;

	/// Sets the score of the specified player
	void SetScore(const AString & a_Name, Score a_Score);

	/// Resets the score of the specified player
	void ResetScore(const AString & a_Name);

	/// Adds a_Delta and returns the new score
	Score AddScore(const AString & a_Name, Score a_Delta);

	/// Subtracts a_Delta and returns the new score
	Score SubScore(const AString & a_Name, Score a_Delta);

	void SetDisplayName(const AString & a_Name);

	// tolua_end

	/// Send this objective to the specified client
	void SendTo(cClientHandle & a_Client);

private:

	typedef std::pair<AString, Score> cTrackedPlayer;

	typedef std::map<AString, Score> cScoreMap;

	cScoreMap m_Scores;

	AString m_DisplayName;
	AString m_Name;

	eType m_Type;

	cWorld * m_World;

	friend class cScoreboardSerializer;

};





// tolua_begin
class cTeam
{
public:

	// tolua_end

	cTeam(
		const AString & a_Name, const AString & a_DisplayName,
		const AString & a_Prefix, const AString & a_Suffix
	);

	/// Adds a new player to the team
	bool AddPlayer(const AString & a_Name);

	/// Removes a player from the team
	bool RemovePlayer(const AString & a_Name);

	/// Returns whether the specified player is in this team
	bool HasPlayer(const AString & a_Name) const;

	/// Removes all registered players
	void Reset(void);

	// tolua_begin

	/// Returns the number of registered players
	unsigned int GetNumPlayers(void) const;

	bool AllowsFriendlyFire(void)      const { return m_AllowsFriendlyFire; }
	bool CanSeeFriendlyInvisible(void) const { return m_CanSeeFriendlyInvisible; }

	const AString & GetDisplayName(void) const { return m_DisplayName; }
	const AString & GetName(void)        const { return m_DisplayName; }

	const AString & GetPrefix(void) const { return m_Prefix; }
	const AString & GetSuffix(void) const { return m_Suffix; }

	void SetFriendlyFire(bool a_Flag)            { m_AllowsFriendlyFire      = a_Flag; }
	void SetCanSeeFriendlyInvisible(bool a_Flag) { m_CanSeeFriendlyInvisible = a_Flag; }

	void SetDisplayName(const AString & a_Name);

	void SetPrefix(const AString & a_Prefix) { m_Prefix = a_Prefix; }
	void SetSuffix(const AString & a_Suffix) { m_Suffix = a_Suffix; }

	// tolua_end

private:

	typedef std::set<AString> cPlayerNameSet;

	bool m_AllowsFriendlyFire;
	bool m_CanSeeFriendlyInvisible;

	AString m_DisplayName;
	AString m_Name;

	AString m_Prefix;
	AString m_Suffix;

	cPlayerNameSet m_Players;

	friend class cScoreboardSerializer;

};





// tolua_begin
class cScoreboard
{
public:

	enum eDisplaySlot
	{
		E_DISPLAY_SLOT_LIST = 0,
		E_DISPLAY_SLOT_SIDEBAR,
		E_DISPLAY_SLOT_NAME,

		E_DISPLAY_SLOT_COUNT
	};

	// tolua_end


public:

	cScoreboard(cWorld * a_World);

	// tolua_begin

	/// Registers a new scoreboard objective, returns the cObjective instance, NULL on name collision
	cObjective * RegisterObjective(const AString & a_Name, const AString & a_DisplayName, cObjective::eType a_Type);

	/// Removes a registered objective, returns true if operation was successful
	bool RemoveObjective(const AString & a_Name);

	/// Retrieves the objective with the specified name, NULL if not found
	cObjective * GetObjective(const AString & a_Name);

	/// Registers a new team, returns the cTeam instance, NULL on name collision
	cTeam * RegisterTeam(const AString & a_Name, const AString & a_DisplayName, const AString & a_Prefix, const AString & a_Suffix);

	/// Removes a registered team, returns true if operation was successful
	bool RemoveTeam(const AString & a_Name);

	/// Retrieves the team with the specified name, NULL if not found
	cTeam * GetTeam(const AString & a_Name);

	cTeam * QueryPlayerTeam(const AString & a_Name); // WARNING: O(n logn)

	void SetDisplay(const AString & a_Objective, eDisplaySlot a_Slot);

	void SetDisplay(cObjective * a_Objective, eDisplaySlot a_Slot);

	cObjective * GetObjectiveIn(eDisplaySlot a_Slot);

	/// Execute callback for each objective with the specified type
	void ForEachObjectiveWith(cObjective::eType a_Type, cObjectiveCallback& a_Callback);

	unsigned int GetNumObjectives(void) const;

	unsigned int GetNumTeams(void) const;

	// tolua_end

	/// Send this scoreboard to the specified client
	void SendTo(cClientHandle & a_Client);


private:

	typedef std::pair<AString, cObjective> cNamedObjective;
	typedef std::pair<AString, cTeam>      cNamedTeam;

	typedef std::map<AString, cObjective> cObjectiveMap;
	typedef std::map<AString, cTeam>      cTeamMap;

	// TODO 2014-01-19 xdot: Potential optimization - Sort objectives based on type
	cCriticalSection m_CSObjectives;
	cObjectiveMap m_Objectives;

	cCriticalSection m_CSTeams;
	cTeamMap m_Teams;

	cWorld * m_World;

	cObjective* m_Display[E_DISPLAY_SLOT_COUNT];

	friend class cScoreboardSerializer;

} ;




hhhhhh