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

310 lines
6.8 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
#include "FunctionRef.h"
2014-01-19 12:20:57 +00:00
class cObjective;
2014-03-01 12:27:55 +00:00
class cTeam;
2014-01-21 13:58:17 +00:00
class cWorld;
2014-01-19 12:20:57 +00:00
using cObjectiveCallback = cFunctionRef<bool(cObjective &)>;
using cTeamCallback = cFunctionRef<bool(cTeam &)>;
2014-01-19 12:20:57 +00:00
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-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
{
2014-03-01 12:20:29 +00:00
otDummy,
2014-01-19 12:20:57 +00:00
2014-03-01 12:20:29 +00:00
otDeathCount,
otPlayerKillCount,
otTotalKillCount,
otHealth,
2014-01-19 12:20:57 +00:00
2014-03-01 12:20:29 +00:00
otAchievement,
2014-01-19 12:20:57 +00:00
2014-03-01 12:20:29 +00:00
otStat,
otStatItemCraft,
otStatItemUse,
otStatItemBreak,
2014-01-19 12:20:57 +00:00
2014-03-01 12:20:29 +00:00
otStatBlockMine,
otStatEntityKill,
otStatEntityKilledBy
2014-01-19 14:02:37 +00:00
};
2014-01-19 12:20:57 +00:00
2014-01-22 13:49:21 +00:00
// tolua_end
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-22 13:49:21 +00:00
// tolua_begin
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
2014-03-01 10:06:19 +00:00
/** Resets the objective */
2014-01-19 12:20:57 +00:00
void Reset(void);
2014-03-01 10:06:19 +00:00
/** Returns the score of the specified player */
2014-01-19 12:20:57 +00:00
Score GetScore(const AString & a_Name) const;
2014-03-01 10:06:19 +00:00
/** Sets the score of the specified player */
2014-01-19 12:20:57 +00:00
void SetScore(const AString & a_Name, Score a_Score);
2014-03-01 10:06:19 +00:00
/** Resets the score of the specified player */
2014-01-19 12:20:57 +00:00
void ResetScore(const AString & a_Name);
2014-03-01 10:06:19 +00:00
/** Adds a_Delta and returns the new score */
2014-01-19 12:20:57 +00:00
Score AddScore(const AString & a_Name, Score a_Delta);
2014-03-01 10:06:19 +00:00
/** Subtracts a_Delta and returns the new score */
2014-01-19 12:20:57 +00:00
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-22 13:49:21 +00:00
// tolua_end
2014-03-01 10:06:19 +00:00
/** Send this objective to the specified client */
2014-01-21 17:43:13 +00:00
void SendTo(cClientHandle & a_Client);
2014-03-01 12:03:16 +00:00
static const char * GetClassStatic(void) // Needed for ManualBindings's ForEach templates
{
return "cObjective";
}
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-03-01 10:06:19 +00:00
}; // tolua_export
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
2014-03-01 10:06:19 +00:00
// tolua_begin
/** 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
2014-03-01 10:06:19 +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-03-01 10:06:19 +00:00
/** Returns whether the specified player is in this team */
2014-01-20 14:10:39 +00:00
bool HasPlayer(const AString & a_Name) const;
2014-03-01 10:06:19 +00:00
/** Removes all registered players */
2014-01-19 12:20:57 +00:00
void Reset(void);
2014-03-11 14:01:17 +00:00
// tolua_begin
2014-03-01 10:06:19 +00:00
/** Returns the number of registered players */
2014-05-08 18:16:35 +00:00
size_t GetNumPlayers(void) const;
2014-01-19 12:20:57 +00:00
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; }
2015-12-21 19:25:33 +00:00
const AString & GetName(void) const { return m_Name; }
2014-01-19 12:20:57 +00:00
const AString & GetPrefix(void) const { return m_Prefix; }
const AString & GetSuffix(void) const { return m_Suffix; }
2014-01-22 13:49:21 +00:00
void SetFriendlyFire(bool a_Flag) { m_AllowsFriendlyFire = a_Flag; }
2014-01-20 14:10:39 +00:00
void SetCanSeeFriendlyInvisible(bool a_Flag) { m_CanSeeFriendlyInvisible = a_Flag; }
2014-01-19 12:20:57 +00:00
void SetDisplayName(const AString & a_Name);
2014-01-22 13:49:21 +00: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 12:20:57 +00:00
2014-03-01 12:27:55 +00:00
static const char * GetClassStatic(void) // Needed for ManualBindings's ForEach templates
{
return "cTeam";
}
2014-01-19 12:20:57 +00:00
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-03-01 10:06:19 +00:00
}; // tolua_export
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
enum eDisplaySlot
{
2014-03-01 12:20:29 +00:00
dsList = 0,
dsSidebar,
dsName,
2014-01-20 14:10:39 +00:00
2014-03-01 12:20:29 +00:00
dsCount
2014-01-20 14:10:39 +00:00
};
2014-01-22 13:49:21 +00:00
// tolua_end
2014-01-20 14:10:39 +00:00
public:
2014-01-21 13:58:17 +00:00
cScoreboard(cWorld * a_World);
2014-01-19 12:20:57 +00:00
2014-01-22 13:49:21 +00:00
// tolua_begin
2014-10-20 20:55:07 +00:00
/** Registers a new scoreboard objective, returns the cObjective instance, nullptr 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
2014-03-01 10:06:19 +00:00
/** Removes a registered objective, returns true if operation was successful */
2014-01-19 12:20:57 +00:00
bool RemoveObjective(const AString & a_Name);
2014-10-20 20:55:07 +00:00
/** Retrieves the objective with the specified name, nullptr 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-10-20 20:55:07 +00:00
/** Registers a new team, returns the cTeam instance, nullptr 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
2014-03-01 10:06:19 +00:00
/** Removes a registered team, returns true if operation was successful */
2014-01-19 12:20:57 +00:00
bool RemoveTeam(const AString & a_Name);
2014-10-20 20:55:07 +00:00
/** Retrieves the team with the specified name, nullptr if not found */
2014-01-20 14:10:39 +00:00
cTeam * GetTeam(const AString & a_Name);
void SetDisplay(const AString & 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
2014-05-08 18:16:35 +00:00
size_t GetNumObjectives(void) const;
2014-01-20 14:10:39 +00:00
2014-05-08 18:16:35 +00:00
size_t GetNumTeams(void) const;
2014-01-20 14:10:39 +00:00
2014-03-01 10:06:19 +00:00
void AddPlayerScore(const AString & a_Name, cObjective::eType a_Type, cObjective::Score a_Value = 1);
2014-01-22 13:49:21 +00:00
// tolua_end
/** Retrieves the list of team names */
AStringVector GetTeamNames();
2014-03-01 10:06:19 +00:00
/** Send this scoreboard to the specified client */
2014-01-22 13:49:21 +00:00
void SendTo(cClientHandle & a_Client);
cTeam * QueryPlayerTeam(const AString & a_Name); // WARNING: O(n logn)
2014-03-01 10:06:19 +00: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 12:03:16 +00: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 10:06:19 +00:00
2014-03-01 12:27:55 +00: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 12:27:55 +00:00
2014-03-01 10:06:19 +00:00
void SetDisplay(cObjective * a_Objective, eDisplaySlot a_Slot);
2014-01-20 14:10:39 +00:00
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-03-01 12:20:29 +00:00
cObjective * m_Display[dsCount];
2014-01-19 12:20:57 +00:00
2014-01-20 14:45:40 +00:00
friend class cScoreboardSerializer;
2014-03-01 10:06:19 +00:00
}; // tolua_export
2014-01-19 12:20:57 +00:00