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

309 lines
6.7 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-03-01 07:27:55 -05:00
class cTeam;
2014-01-21 08:58:17 -05:00
class cWorld;
2014-01-19 07:20:57 -05:00
typedef cItemCallback<cObjective> cObjectiveCallback;
2014-03-01 07:27:55 -05:00
typedef cItemCallback<cTeam> cTeamCallback;
2014-01-19 07:20:57 -05:00
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
{
2014-03-01 07:20:29 -05:00
otDummy,
2014-01-19 07:20:57 -05:00
2014-03-01 07:20:29 -05:00
otDeathCount,
otPlayerKillCount,
otTotalKillCount,
otHealth,
2014-01-19 07:20:57 -05:00
2014-03-01 07:20:29 -05:00
otAchievement,
2014-01-19 07:20:57 -05:00
2014-03-01 07:20:29 -05:00
otStat,
otStatItemCraft,
otStatItemUse,
otStatItemBreak,
2014-01-19 07:20:57 -05:00
2014-03-01 07:20:29 -05:00
otStatBlockMine,
otStatEntityKill,
otStatEntityKilledBy
2014-01-19 09:02:37 -05:00
};
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
2014-03-01 05:06:19 -05:00
/** Resets the objective */
2014-01-19 07:20:57 -05:00
void Reset(void);
2014-03-01 05:06:19 -05:00
/** Returns the score of the specified player */
2014-01-19 07:20:57 -05:00
Score GetScore(const AString & a_Name) const;
2014-03-01 05:06:19 -05:00
/** Sets the score of the specified player */
2014-01-19 07:20:57 -05:00
void SetScore(const AString & a_Name, Score a_Score);
2014-03-01 05:06:19 -05:00
/** Resets the score of the specified player */
2014-01-19 07:20:57 -05:00
void ResetScore(const AString & a_Name);
2014-03-01 05:06:19 -05:00
/** Adds a_Delta and returns the new score */
2014-01-19 07:20:57 -05:00
Score AddScore(const AString & a_Name, Score a_Delta);
2014-03-01 05:06:19 -05:00
/** Subtracts a_Delta and returns the new score */
2014-01-19 07:20:57 -05:00
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-03-01 05:06:19 -05:00
/** Send this objective to the specified client */
2014-01-21 12:43:13 -05:00
void SendTo(cClientHandle & a_Client);
2014-03-01 07:03:16 -05:00
static const char * GetClassStatic(void) // Needed for ManualBindings's ForEach templates
{
return "cObjective";
}
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-03-01 05:06:19 -05:00
}; // tolua_export
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
2014-03-01 05:06:19 -05:00
// tolua_begin
/** 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
2014-03-01 05:06:19 -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-03-01 05:06:19 -05:00
/** Returns whether the specified player is in this team */
2014-01-20 09:10:39 -05:00
bool HasPlayer(const AString & a_Name) const;
2014-03-01 05:06:19 -05:00
/** Removes all registered players */
2014-01-19 07:20:57 -05:00
void Reset(void);
2014-03-11 10:01:17 -04:00
// tolua_begin
2014-03-01 05:06:19 -05:00
/** Returns the number of registered players */
2014-05-08 14:16:35 -04:00
size_t GetNumPlayers(void) const;
2014-01-19 07:20:57 -05:00
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; }
2015-12-21 14:25:33 -05:00
const AString & GetName(void) const { return m_Name; }
2014-01-19 07:20:57 -05:00
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
2014-03-01 07:27:55 -05:00
static const char * GetClassStatic(void) // Needed for ManualBindings's ForEach templates
{
return "cTeam";
}
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-03-01 05:06:19 -05:00
}; // tolua_export
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
{
2014-03-01 07:20:29 -05:00
dsList = 0,
dsSidebar,
dsName,
2014-01-20 09:10:39 -05:00
2014-03-01 07:20:29 -05:00
dsCount
2014-01-20 09:10:39 -05:00
};
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-10-20 16:55:07 -04:00
/** Registers a new scoreboard objective, returns the cObjective instance, nullptr 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
2014-03-01 05:06:19 -05:00
/** Removes a registered objective, returns true if operation was successful */
2014-01-19 07:20:57 -05:00
bool RemoveObjective(const AString & a_Name);
2014-10-20 16:55:07 -04:00
/** Retrieves the objective with the specified name, nullptr 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-10-20 16:55:07 -04:00
/** Registers a new team, returns the cTeam instance, nullptr 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
2014-03-01 05:06:19 -05:00
/** Removes a registered team, returns true if operation was successful */
2014-01-19 07:20:57 -05:00
bool RemoveTeam(const AString & a_Name);
2014-10-20 16:55:07 -04:00
/** Retrieves the team with the specified name, nullptr if not found */
2014-01-20 09:10:39 -05:00
cTeam * GetTeam(const AString & a_Name);
void SetDisplay(const AString & 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
2014-05-08 14:16:35 -04:00
size_t GetNumObjectives(void) const;
2014-01-20 09:10:39 -05:00
2014-05-08 14:16:35 -04:00
size_t GetNumTeams(void) const;
2014-01-20 09:10:39 -05:00
2014-03-01 05:06:19 -05:00
void AddPlayerScore(const AString & a_Name, cObjective::eType a_Type, cObjective::Score a_Value = 1);
2014-01-22 08:49:21 -05:00
// tolua_end
/** Retrieves the list of team names */
AStringVector GetTeamNames();
2014-03-01 05:06:19 -05:00
/** Send this scoreboard to the specified client */
2014-01-22 08:49:21 -05:00
void SendTo(cClientHandle & a_Client);
cTeam * QueryPlayerTeam(const AString & a_Name); // WARNING: O(n logn)
2014-03-01 05:06:19 -05:00
/** Execute callback for each objective with the specified type
Returns true if all objectives processed, false if the callback aborted by returning true. */
bool ForEachObjectiveWith(cObjective::eType a_Type, cObjectiveCallback & a_Callback);
2014-03-01 07:03:16 -05:00
/** Execute callback for each objective.
Returns true if all objectives have been processed, false if the callback aborted by returning true. */
bool ForEachObjective(cObjectiveCallback & a_Callback); // Exported in ManualBindings.cpp
2014-03-01 05:06:19 -05:00
2014-03-01 07:27:55 -05:00
/** Execute callback for each team.
Returns true if all teams have been processed, false if the callback aborted by returning true. */
bool ForEachTeam(cTeamCallback & a_Callback); // Exported in ManualBindings.cpp
2014-03-01 07:27:55 -05:00
2014-03-01 05:06:19 -05:00
void SetDisplay(cObjective * a_Objective, eDisplaySlot a_Slot);
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-03-01 07:20:29 -05:00
cObjective * m_Display[dsCount];
2014-01-19 07:20:57 -05:00
2014-01-20 09:45:40 -05:00
friend class cScoreboardSerializer;
2014-03-01 05:06:19 -05:00
}; // tolua_export
2014-01-19 07:20:57 -05:00