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

265 lines
5.6 KiB
C
Raw Normal View History

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