Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
7.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Player-driven Voting System for Multiplayer Source games (currently implemented for TF2)
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef VOTE_CONTROLLER_H
  8. #define VOTE_CONTROLLER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "shareddefs.h"
  13. #define MAX_COMMAND_LENGTH 64
  14. #define MAX_CREATE_ERROR_STRING 96
  15. // TODO: look into doing enum instead of string compares here - mtw
  16. #define VOTEISSUE_NAME_KICK "Kick"
  17. #define VOTEISSUE_NAME_CHANGELEVEL "ChangeLevel"
  18. #define VOTEISSUE_NAME_NEXTLEVEL "NextLevel"
  19. #define VOTEISSUE_NAME_SWAPTEAMS "SwapTeams"
  20. #define VOTEISSUE_NAME_SCRAMBLE "ScrambleTeams"
  21. #define VOTEISSUE_NAME_RESTARTGAME "RestartGame"
  22. #define VOTEISSUE_NAME_SURRENDER "Surrender"
  23. #define VOTEISSUE_NAME_REMATCH "Rematch"
  24. #define VOTEISSUE_NAME_CONTINUE "ContinueGame"
  25. #define VOTEISSUE_NAME_PAUSEMATCH "PauseMatch"
  26. #define VOTEISSUE_NAME_UNPAUSEMATCH "UnpauseMatch"
  27. #define VOTEISSUE_NAME_LOADBACKUP "LoadBackup"
  28. #define VOTEISSUE_NAME_READYFORMATCH "ReadyForMatch"
  29. #define VOTEISSUE_NAME_NOTREADYFORMATCH "NotReadyForMatch"
  30. #define VOTEISSUE_NAME_STARTTIMEOUT "StartTimeOut"
  31. class CVoteController;
  32. extern CVoteController *g_voteControllerGlobal;
  33. extern CVoteController *g_voteControllerCT;
  34. extern CVoteController *g_voteControllerT;
  35. class CBaseIssue // Abstract base class for all things-that-can-be-voted-on.
  36. {
  37. public:
  38. CBaseIssue( const char *typeString, CVoteController *pVoteController );
  39. virtual ~CBaseIssue();
  40. const char *GetTypeString( void ); // Connection between console command and specific type of issue
  41. virtual const char *GetDetailsString();
  42. virtual void SetIssueDetails( const char *pszDetails ); // We need to know the details part of the con command for later
  43. virtual void OnVoteFailed( void ); // The moment the vote fails, also has some time for feedback before the window goes away
  44. virtual void OnVoteStarted( void ) {} // Called as soon as the vote starts
  45. virtual bool IsEnabled( void ) { return false; } // Query the issue to see if it's enabled
  46. virtual bool IsEnabledInQueuedMatchmaking( void ) { return false; } // Query if the issue is supported in queued matchmaking mode
  47. virtual bool IsEnabledDuringWarmup( void ) { return false; } // Can this vote be called during warmup?
  48. virtual float GetCommandDelay( void );
  49. virtual bool ShouldIgnoreCreationTimer( void ) { return false; } // should this issue ignore sv_vote_creation_timer that prevents spamming callvotes?
  50. virtual bool CanTeamCallVote( int iTeam ) const; // Can someone on the given team call this vote?
  51. virtual bool CanCallVote( int iEntIndex, const char *pszCommand, const char *pszDetails, vote_create_failed_t &nFailCode, int &nTime ); // Can this guy hold a vote on this issue?
  52. virtual bool IsAllyRestrictedVote( void ); // Can only members of the same team vote on this?
  53. virtual bool IsUnanimousVoteToPass( void ) {return false; } // Requires all potential voters to pass
  54. virtual bool IsVoteCallExclusiveToSpectators( void ) { return false; } // Whether only spectators can call the vote
  55. virtual int GetVotesRequiredToPass( void ); // how many votes are required to pass
  56. virtual const char *GetDisplayString( void ) = 0; // The string that will be passed to the client for display
  57. virtual const char *GetOtherTeamDisplayString( void ) = 0; // The string that will be passed to the client for a vote being cast by the other team
  58. virtual void ExecuteCommand( void ) = 0; // Where the magic happens. Do your thing.
  59. virtual void ListIssueDetails( CBasePlayer *pForWhom ) = 0; // Someone would like to know all your valid details
  60. virtual const char *GetVotePassedString( void ); // Get the string an issue would like to display when it passes.
  61. virtual int CountPotentialVoters( void );
  62. virtual int GetNumberVoteOptions( void ); // How many choices this vote will have. i.e. Returns 2 on a Yes/No issue (the default).
  63. virtual bool IsYesNoVote( void );
  64. virtual void SetYesNoVoteCount( int iNumYesVotes, int iNumNoVotes, int iNumPotentialVotes );
  65. virtual bool GetVoteOptions( CUtlVector <const char*> &vecNames ); // We use this to generate options for voting
  66. virtual int GetVoteIssue( void ) { return VOTEISSUE_UNDEFINED; }
  67. virtual float GetFailedVoteLockOutTime( void ); // How long to wait before a failed vote can be resubmitted.
  68. virtual vote_create_failed_t MakeVoteFailErrorCodeForClients( vote_create_failed_t eDefaultFailCode ) { return eDefaultFailCode; }
  69. protected:
  70. static void ListStandardNoArgCommand( CBasePlayer *forWhom, const char *issueString ); // List a Yes vote command
  71. struct FailedVote
  72. {
  73. char szFailedVoteCommand[MAX_COMMAND_LENGTH];
  74. char szFailedVoteParameter[MAX_VOTE_DETAILS_LENGTH];
  75. float flLockoutTime;
  76. };
  77. CUtlVector<FailedVote *> m_FailedVotes;
  78. char m_szTypeString[MAX_COMMAND_LENGTH];
  79. char m_szDetailsString[MAX_PATH];
  80. int m_iNumYesVotes;
  81. int m_iNumNoVotes;
  82. int m_iNumPotentialVotes;
  83. CVoteController *m_pVoteController;
  84. };
  85. class CVoteController : public CBaseEntity
  86. {
  87. DECLARE_CLASS( CVoteController, CBaseEntity );
  88. public:
  89. DECLARE_SERVERCLASS();
  90. DECLARE_DATADESC();
  91. virtual ~CVoteController();
  92. enum TryCastVoteResult
  93. {
  94. CAST_OK,
  95. CAST_FAIL_SERVER_DISABLE,
  96. CAST_FAIL_NO_ACTIVE_ISSUE,
  97. CAST_FAIL_TEAM_RESTRICTED,
  98. CAST_FAIL_NO_CHANGES,
  99. CAST_FAIL_DUPLICATE,
  100. CAST_FAIL_VOTE_CLOSED,
  101. CAST_FAIL_SYSTEM_ERROR
  102. };
  103. virtual void Spawn( void );
  104. virtual int UpdateTransmitState( void );
  105. bool SetupVote( int iEntIndex ); // This creates a list of issues for the UI
  106. bool CreateVote( int iEntIndex, const char *pszTypeString, const char *pszDetailString ); // This is what the UI passes in
  107. TryCastVoteResult TryCastVote( int iEntIndex, const char *pszVoteString );
  108. float GetAcceptingVotesTimeLeft() { return m_acceptingVotesTimer.GetRemainingTime(); }
  109. void RegisterIssue( CBaseIssue *pNewIssue );
  110. void ListIssues( CBasePlayer *pForWhom );
  111. bool IsValidVoter( CBasePlayer *pWhom );
  112. bool CanTeamCastVote( int iTeam ) const;
  113. void SendVoteFailedMessage( vote_create_failed_t nReason = VOTE_FAILED_GENERIC, CBasePlayer *pVoteCaller = NULL, int nTime = -1 );
  114. void VoteChoice_Increment( int nVoteChoice );
  115. void VoteChoice_Decrement( int nVoteChoice );
  116. int GetWinningVoteOption( void );
  117. int GetCallingEntity( void ) { return m_iEntityHoldingVote; }
  118. int GetPotentialVotes( void ) { return m_nPotentialVotes.Get(); }
  119. bool IsAVoteInProgress( void ) { return ( m_iActiveIssueIndex != INVALID_ISSUE ); }
  120. bool HasIssue( const char *pszIssue );
  121. void EndVoteImmediately( void );
  122. protected:
  123. void ResetData( void );
  124. void VoteControllerThink( void );
  125. void CheckForEarlyVoteClose( void ); // If everyone has voted (and changing votes is not allowed) then end early
  126. CNetworkVar( int, m_iActiveIssueIndex ); // Type of thing being voted on
  127. CNetworkVar( int, m_iOnlyTeamToVote ); // If an Ally restricted vote, the team number that is allowed to vote
  128. CNetworkArray( int, m_nVoteOptionCount, MAX_VOTE_OPTIONS ); // Vote options counter
  129. CNetworkVar( int, m_nPotentialVotes ); // How many votes could come in, so we can close ballot early
  130. CNetworkVar( bool, m_bIsYesNoVote ); // Is the current issue Yes/No?
  131. CountdownTimer m_acceptingVotesTimer; // How long from vote start until we count the ballots
  132. CountdownTimer m_executeCommandTimer; // How long after end of vote time until we execute a passed vote
  133. CountdownTimer m_resetVoteTimer; // when the current vote will end
  134. CUtlVector< uint64 > m_arrVotedUsers; // SteamIDs of users who voted already
  135. int m_nVotesCast[MAX_PLAYERS]; // votes cast by each entity
  136. int m_iEntityHoldingVote;
  137. int m_nHighestCountIndex;
  138. CUtlVector <CBaseIssue *> m_potentialIssues;
  139. CUtlVector <const char *> m_VoteOptions;
  140. };
  141. #endif // VOTE_CONTROLLER_H