1
0
cuberite-2a/src/Scoreboard.h

277 lines
5.8 KiB
C
Raw Normal View History

2014-01-19 07:20:57 -05:00
// Scoreboard.h
// Implementation of a scoreboard that keeps track of specified objectives
#pragma once
class cObjective;
2014-01-21 08:58:17 -05:00
class cWorld;
2014-01-19 07:20:57 -05:00
typedef cItemCallback<cObjective> cObjectiveCallback;
2014-01-21 12:43:13 -05:00
// tolua_begin
2014-01-19 09:02:37 -05:00
class cObjective
2014-01-19 07:20:57 -05:00
{
2014-01-19 09:02:37 -05:00
public:
2014-01-20 09:10:39 -05:00
2014-01-19 09:02:37 -05:00
typedef int Score;
2014-01-19 07:20:57 -05:00
2014-01-19 09:02:37 -05:00
enum eType
{
E_TYPE_DUMMY,
2014-01-19 07:20:57 -05:00
2014-01-19 09:02:37 -05:00
E_TYPE_DEATH_COUNT,
E_TYPE_PLAYER_KILL_COUNT,
E_TYPE_TOTAL_KILL_COUNT,
E_TYPE_HEALTH,
2014-01-19 07:20:57 -05:00
2014-01-19 09:02:37 -05:00
E_TYPE_ACHIEVEMENT,
2014-01-19 07:20:57 -05:00
2014-01-19 09:02:37 -05:00
E_TYPE_STAT,
E_TYPE_STAT_ITEM_CRAFT,
E_TYPE_STAT_ITEM_USE,
E_TYPE_STAT_ITEM_BREAK,
2014-01-19 07:20:57 -05:00
2014-01-19 09:02:37 -05:00
E_TYPE_STAT_BLOCK_MINE,
E_TYPE_STAT_ENTITY_KILL,
E_TYPE_STAT_ENTITY_KILLED_BY
};
2014-01-19 07:20:57 -05:00
2014-01-22 08:49:21 -05:00
// tolua_end
2014-01-20 09:10:39 -05:00
static AString TypeToString(eType a_Type);
static eType StringToType(const AString & a_Name);
2014-01-19 07:20:57 -05:00
public:
2014-01-21 08:58:17 -05:00
cObjective(const AString & a_Name, const AString & a_DisplayName, eType a_Type, cWorld * a_World);
2014-01-19 07:20:57 -05:00
2014-01-22 08:49:21 -05:00
// tolua_begin
2014-01-20 09:10:39 -05:00
eType GetType(void) const { return m_Type; }
2014-01-19 07:20:57 -05:00
2014-01-20 09:45:40 -05:00
const AString & GetName(void) const { return m_Name; }
2014-01-20 09:10:39 -05:00
const AString & GetDisplayName(void) const { return m_DisplayName; }
2014-01-19 07:20:57 -05:00
/// 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);
2014-01-21 08:58:17 -05:00
void SetDisplayName(const AString & a_Name);
2014-01-22 08:49:21 -05:00
// tolua_end
2014-01-21 12:43:13 -05:00
/// Send this objective to the specified client
void SendTo(cClientHandle & a_Client);
2014-01-19 07:20:57 -05:00
private:
2014-01-20 09:10:39 -05:00
2014-01-20 09:45:40 -05:00
typedef std::pair<AString, Score> cTrackedPlayer;
2014-01-19 07:20:57 -05:00
2014-01-20 09:45:40 -05:00
typedef std::map<AString, Score> cScoreMap;
2014-01-19 07:20:57 -05:00
2014-01-20 09:45:40 -05:00
cScoreMap m_Scores;
2014-01-19 07:20:57 -05:00
2014-01-20 09:10:39 -05:00
AString m_DisplayName;
2014-01-20 09:45:40 -05:00
AString m_Name;
2014-01-20 09:10:39 -05:00
2014-01-19 09:02:37 -05:00
eType m_Type;
2014-01-19 07:20:57 -05:00
2014-01-21 08:58:17 -05:00
cWorld * m_World;
2014-01-20 09:45:40 -05:00
friend class cScoreboardSerializer;
2014-01-19 07:20:57 -05:00
};
2014-01-21 12:43:13 -05:00
// tolua_begin
2014-01-19 07:20:57 -05:00
class cTeam
{
public:
2014-01-19 09:02:37 -05:00
2014-01-21 12:43:13 -05:00
// tolua_end
2014-01-19 09:02:37 -05:00
cTeam(
const AString & a_Name, const AString & a_DisplayName,
const AString & a_Prefix, const AString & a_Suffix
);
2014-01-19 07:20:57 -05:00
/// Adds a new player to the team
2014-01-19 09:02:37 -05:00
bool AddPlayer(const AString & a_Name);
2014-01-19 07:20:57 -05:00
/// Removes a player from the team
2014-01-19 09:02:37 -05:00
bool RemovePlayer(const AString & a_Name);
2014-01-19 07:20:57 -05:00
2014-01-20 09:10:39 -05:00
/// Returns whether the specified player is in this team
bool HasPlayer(const AString & a_Name) const;
2014-01-19 07:20:57 -05:00
/// Removes all registered players
void Reset(void);
2014-01-22 08:49:21 -05:00
// tolua_begin
2014-01-19 07:20:57 -05:00
/// Returns the number of registered players
unsigned int GetNumPlayers(void) const;
2014-01-19 09:02:37 -05:00
bool AllowsFriendlyFire(void) const { return m_AllowsFriendlyFire; }
bool CanSeeFriendlyInvisible(void) const { return m_CanSeeFriendlyInvisible; }
2014-01-19 07:20:57 -05:00
2014-01-19 09:02:37 -05:00
const AString & GetDisplayName(void) const { return m_DisplayName; }
2014-01-19 07:20:57 -05:00
const AString & GetName(void) const { return m_DisplayName; }
const AString & GetPrefix(void) const { return m_Prefix; }
const AString & GetSuffix(void) const { return m_Suffix; }
2014-01-22 08:49:21 -05:00
void SetFriendlyFire(bool a_Flag) { m_AllowsFriendlyFire = a_Flag; }
2014-01-20 09:10:39 -05:00
void SetCanSeeFriendlyInvisible(bool a_Flag) { m_CanSeeFriendlyInvisible = a_Flag; }
2014-01-19 07:20:57 -05:00
void SetDisplayName(const AString & a_Name);
2014-01-22 08:49:21 -05:00
void SetPrefix(const AString & a_Prefix) { m_Prefix = a_Prefix; }
void SetSuffix(const AString & a_Suffix) { m_Suffix = a_Suffix; }
// tolua_end
2014-01-19 07:20:57 -05:00
private:
2014-01-20 09:10:39 -05:00
typedef std::set<AString> cPlayerNameSet;
2014-01-19 09:02:37 -05:00
bool m_AllowsFriendlyFire;
bool m_CanSeeFriendlyInvisible;
2014-01-19 07:20:57 -05:00
AString m_DisplayName;
AString m_Name;
AString m_Prefix;
AString m_Suffix;
2014-01-20 09:10:39 -05:00
cPlayerNameSet m_Players;
2014-01-19 09:02:37 -05:00
2014-01-20 09:45:40 -05:00
friend class cScoreboardSerializer;
2014-01-19 07:20:57 -05:00
};
2014-01-21 12:43:13 -05:00
// tolua_begin
2014-01-19 07:20:57 -05:00
class cScoreboard
{
public:
2014-01-20 09:10:39 -05:00
enum eDisplaySlot
{
E_DISPLAY_SLOT_LIST = 0,
E_DISPLAY_SLOT_SIDEBAR,
E_DISPLAY_SLOT_NAME,
E_DISPLAY_SLOT_COUNT
};
2014-01-22 08:49:21 -05:00
// tolua_end
2014-01-20 09:10:39 -05:00
public:
2014-01-21 08:58:17 -05:00
cScoreboard(cWorld * a_World);
2014-01-19 07:20:57 -05:00
2014-01-22 08:49:21 -05:00
// tolua_begin
2014-01-19 09:02:37 -05:00
/// Registers a new scoreboard objective, returns the cObjective instance, NULL on name collision
2014-01-20 09:10:39 -05:00
cObjective * RegisterObjective(const AString & a_Name, const AString & a_DisplayName, cObjective::eType a_Type);
2014-01-19 07:20:57 -05:00
/// 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
2014-01-20 09:10:39 -05:00
cObjective * GetObjective(const AString & a_Name);
2014-01-19 07:20:57 -05:00
2014-01-19 09:02:37 -05:00
/// Registers a new team, returns the cTeam instance, NULL on name collision
2014-01-20 09:10:39 -05:00
cTeam * RegisterTeam(const AString & a_Name, const AString & a_DisplayName, const AString & a_Prefix, const AString & a_Suffix);
2014-01-19 07:20:57 -05:00
/// 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
2014-01-20 09:10:39 -05:00
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);
2014-01-21 12:43:13 -05:00
void SetDisplay(cObjective * a_Objective, eDisplaySlot a_Slot);
2014-01-20 09:45:40 -05:00
cObjective * GetObjectiveIn(eDisplaySlot a_Slot);
2014-01-19 07:20:57 -05:00
/// Execute callback for each objective with the specified type
2014-01-19 09:02:37 -05:00
void ForEachObjectiveWith(cObjective::eType a_Type, cObjectiveCallback& a_Callback);
2014-01-19 07:20:57 -05:00
2014-01-20 09:10:39 -05:00
unsigned int GetNumObjectives(void) const;
unsigned int GetNumTeams(void) const;
2014-01-22 08:49:21 -05:00
// tolua_end
/// Send this scoreboard to the specified client
void SendTo(cClientHandle & a_Client);
2014-01-20 09:10:39 -05:00
2014-01-19 07:20:57 -05:00
private:
2014-01-20 09:10:39 -05:00
typedef std::pair<AString, cObjective> cNamedObjective;
typedef std::pair<AString, cTeam> cNamedTeam;
typedef std::map<AString, cObjective> cObjectiveMap;
typedef std::map<AString, cTeam> cTeamMap;
2014-01-19 07:20:57 -05:00
// TODO 2014-01-19 xdot: Potential optimization - Sort objectives based on type
2014-01-21 08:58:17 -05:00
cCriticalSection m_CSObjectives;
2014-01-20 09:10:39 -05:00
cObjectiveMap m_Objectives;
2014-01-21 08:58:17 -05:00
cCriticalSection m_CSTeams;
2014-01-20 09:10:39 -05:00
cTeamMap m_Teams;
2014-01-21 08:58:17 -05:00
cWorld * m_World;
2014-01-20 09:10:39 -05:00
cObjective* m_Display[E_DISPLAY_SLOT_COUNT];
2014-01-19 07:20:57 -05:00
2014-01-20 09:45:40 -05:00
friend class cScoreboardSerializer;
2014-01-19 07:20:57 -05:00
} ;