added amb764, vote delay setting

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401297
This commit is contained in:
David Anderson 2007-08-09 19:05:34 +00:00
parent 3cd3a0a8d6
commit e6fa620ae2
7 changed files with 104 additions and 0 deletions

View File

@ -692,3 +692,8 @@ void MenuManager::CancelVoting()
{ {
s_VoteHandler.CancelVoting(); s_VoteHandler.CancelVoting();
} }
unsigned int MenuManager::GetRemainingVoteDelay()
{
return s_VoteHandler.GetRemainingVoteDelay();
}

View File

@ -89,6 +89,7 @@ public:
unsigned int flags=0); unsigned int flags=0);
bool IsVoteInProgress(); bool IsVoteInProgress();
void CancelVoting(); void CancelVoting();
unsigned int GetRemainingVoteDelay();
public: //IHandleTypeDispatch public: //IHandleTypeDispatch
void OnHandleDestroy(HandleType_t type, void *object); void OnHandleDestroy(HandleType_t type, void *object);
public: public:

View File

@ -36,6 +36,49 @@
#include "PlayerManager.h" #include "PlayerManager.h"
#include "sourcemm_api.h" #include "sourcemm_api.h"
float g_next_vote = 0.0f;
void OnVoteDelayChange(ConVar *cvar, const char *value);
ConVar sm_vote_delay("sm_vote_delay",
"30",
0,
"Sets the recommended time in between public votes",
false,
0.0,
false,
0.0,
OnVoteDelayChange);
void OnVoteDelayChange(ConVar *cvar, const char *value)
{
/* See if the new vote delay isn't something we need to account for */
if (sm_vote_delay.GetFloat() < 1.0f)
{
g_next_vote = 0.0f;
return;
}
/* If there was never a last vote, ignore this change */
if (g_next_vote < 0.1f)
{
return;
}
/* Subtract the original value, then add the new one. */
g_next_vote -= (float)atof(value);
g_next_vote += sm_vote_delay.GetFloat();
}
unsigned int VoteMenuHandler::GetRemainingVoteDelay()
{
if (g_next_vote <= gpGlobals->curtime)
{
return 0;
}
return (unsigned int)(g_next_vote - gpGlobals->curtime);
}
void VoteMenuHandler::OnSourceModAllInitialized() void VoteMenuHandler::OnSourceModAllInitialized()
{ {
g_Players.AddClientListener(this); g_Players.AddClientListener(this);
@ -46,6 +89,11 @@ void VoteMenuHandler::OnSourceModShutdown()
g_Players.RemoveClientListener(this); g_Players.RemoveClientListener(this);
} }
void VoteMenuHandler::OnSourceModLevelChange(const char *mapName)
{
g_next_vote = 0.0f;
}
unsigned int VoteMenuHandler::GetMenuAPIVersion2() unsigned int VoteMenuHandler::GetMenuAPIVersion2()
{ {
return m_pHandler->GetMenuAPIVersion2(); return m_pHandler->GetMenuAPIVersion2();
@ -81,6 +129,18 @@ bool VoteMenuHandler::StartVote(IBaseMenu *menu, unsigned int num_clients, int c
return false; return false;
} }
float fVoteDelay = sm_vote_delay.GetFloat();
if (fVoteDelay < 1.0)
{
g_next_vote = 0.0;
} else {
/* This little trick breaks for infinite votes!
* However, we just ignore that since those 1) shouldn't exist and
* 2) people must be checking IsVoteInProgress() beforehand anyway.
*/
g_next_vote = gpGlobals->curtime + fVoteDelay + (float)max_time;
}
for (unsigned int i=0; i<num_clients; i++) for (unsigned int i=0; i<num_clients; i++)
{ {
menu->Display(clients[i], max_time, this); menu->Display(clients[i], max_time, this);
@ -177,6 +237,18 @@ int SortVoteItems(const void *item1, const void *item2)
void VoteMenuHandler::EndVoting() void VoteMenuHandler::EndVoting()
{ {
/* Set when the next delay ends. We ignore cancellation because a menu
* was, at one point, displayed, which is all that counts. However, we
* do re-calculate the time just in case the menu had no time limit.
*/
float fVoteDelay = sm_vote_delay.GetFloat();
if (fVoteDelay < 1.0)
{
g_next_vote = 0.0;
} else {
g_next_vote = gpGlobals->curtime + fVoteDelay;
}
if (m_bCancelled) if (m_bCancelled)
{ {
/* If we were cancelled, don't bother tabulating anything. /* If we were cancelled, don't bother tabulating anything.

View File

@ -49,6 +49,7 @@ class VoteMenuHandler :
public: //SMGlobalClass public: //SMGlobalClass
void OnSourceModAllInitialized(); void OnSourceModAllInitialized();
void OnSourceModShutdown(); void OnSourceModShutdown();
void OnSourceModLevelChange(const char *mapName);
public: //IClientListener public: //IClientListener
void OnClientDisconnected(int client); void OnClientDisconnected(int client);
public: //IMenuHandler public: //IMenuHandler
@ -70,6 +71,7 @@ public:
void CancelVoting(); void CancelVoting();
IBaseMenu *GetCurrentMenu(); IBaseMenu *GetCurrentMenu();
bool IsCancelling(); bool IsCancelling();
unsigned int GetRemainingVoteDelay();
private: private:
void Reset(IMenuHandler *mh); void Reset(IMenuHandler *mh);
void DecrementPlayerCount(); void DecrementPlayerCount();

View File

@ -39,6 +39,7 @@
#include "HandleSys.h" #include "HandleSys.h"
#include "PluginSys.h" #include "PluginSys.h"
#include "sm_stringutil.h" #include "sm_stringutil.h"
#include "sourcemm_api.h"
#if defined MENU_DEBUG #if defined MENU_DEBUG
#include "Logger.h" #include "Logger.h"
#endif #endif
@ -1324,6 +1325,11 @@ static cell_t SetVoteResultCallback(IPluginContext *pContext, const cell_t *para
return 1; return 1;
} }
static cell_t CheckVoteDelay(IPluginContext *pContext, const cell_t *params)
{
return g_Menus.GetRemainingVoteDelay();
}
REGISTER_NATIVES(menuNatives) REGISTER_NATIVES(menuNatives)
{ {
{"AddMenuItem", AddMenuItem}, {"AddMenuItem", AddMenuItem},
@ -1331,6 +1337,7 @@ REGISTER_NATIVES(menuNatives)
{"CancelClientMenu", CancelClientMenu}, {"CancelClientMenu", CancelClientMenu},
{"CancelMenu", CancelMenu}, {"CancelMenu", CancelMenu},
{"CancelVote", CancelVote}, {"CancelVote", CancelVote},
{"CheckVoteDelay", CheckVoteDelay},
{"CreateMenu", CreateMenu}, {"CreateMenu", CreateMenu},
{"CreateMenuEx", CreateMenuEx}, {"CreateMenuEx", CreateMenuEx},
{"CreatePanel", CreatePanel}, {"CreatePanel", CreatePanel},

View File

@ -458,6 +458,15 @@ functag VoteHandler public(Handle:menu,
*/ */
native SetVoteResultCallback(Handle:menu, VoteHandler:callback); native SetVoteResultCallback(Handle:menu, VoteHandler:callback);
/**
* Returns the number of seconds you should "wait" before displaying
* a publicly invocable menu. This number is the time remaining until
* (last_vote + sm_vote_delay).
*
* @return Number of seconds to wait, or 0 for none.
*/
native CheckVoteDelay();
/** /**
* Returns a style's global Handle. * Returns a style's global Handle.
* *

View File

@ -807,6 +807,14 @@ namespace SourceMod
* @brief Cancels the vote in progress. This calls IBaseMenu::Cancel(). * @brief Cancels the vote in progress. This calls IBaseMenu::Cancel().
*/ */
virtual void CancelVoting() =0; virtual void CancelVoting() =0;
/**
* @brief Returns the remaining vote delay from the last menu. This delay is
* a suggestion for all public votes, and is not enforced.
*
* @return Number of seconds to wait.
*/
virtual unsigned int GetRemainingVoteDelay() =0;
}; };
} }