People should mention in first post,
what game we talk about.
I assumed, cs:go
here one example.
- Plugin look events "round_announce_match_point" & "round_announce_final", and start vote after 2 seconds.
- Match point happen earlier, when losing team have no chance to win if winning team score one more time. (
mp_match_can_clinch 1)
- Vote start once per match.
- If there is already another vote in progress, this plugin try create vote again after vote cooldown (sm_vote_delay),
plugin attempt this two times.
Of course if round end already, no vote.
PHP Code:
#include <cstrike>
#include <sdktools>
enum
{
GAMEPHASE_WARMUP_ROUND,
GAMEPHASE_PLAYING_STANDARD,
GAMEPHASE_PLAYING_FIRST_HALF,
GAMEPHASE_PLAYING_SECOND_HALF,
GAMEPHASE_HALFTIME,
GAMEPHASE_MATCH_ENDED,
GAMEPHASE_MAX
};
ConVar mp_maxrounds;
public void OnPluginStart()
{
//RegConsoleCmd("sm_test", test);
HookEventEx("round_announce_match_point", events);
HookEventEx("round_announce_final", events);
HookEventEx("begin_new_match", events);
mp_maxrounds = FindConVar("mp_maxrounds");
if(mp_maxrounds == null)
SetFailState("This game mod do not have ConVar mp_maxrounds");
}
public void events(Event event, const char[] name, bool dontBroadcast)
{
//static bool round_announce_match_point = false; // mp_match_can_clinch 1
static bool round_announce_final = false;
if(StrEqual(name, "begin_new_match", false))
{
//round_announce_match_point = false;
round_announce_final = false;
return;
}
//if(StrEqual(name, "round_announce_match_point", false))
//{
// // Has been announced once.
// if(round_announce_match_point)
// return;
//
// round_announce_match_point = true;
//}
//if(StrEqual(name, "round_announce_final", false))
//{
// // Has been announced once.
// if(round_announce_final)
// return;
//
// round_announce_final = true;
//}
if(round_announce_final)
return;
round_announce_final = true;
StartVote();
}
public Action test(int client, int args)
{
//StartVote();
return Plugin_Handled;
}
void StartVote()
{
CreateTimer(2.0, VoteStart, 0, TIMER_FLAG_NO_MAPCHANGE);
}
public Action VoteStart(Handle timer, any data)
{
static int x;
if(data == 0)
x = 0;
// Do not create vote when mp_maxrounds is disabled, for some reason
if(mp_maxrounds.IntValue == 0)
return Plugin_Continue;
// skip these phases
if(GameRules_GetProp("m_bWarmupPeriod"))
return Plugin_Continue;
if(!GameRules_GetProp("m_bHasMatchStarted"))
return Plugin_Continue;
if(GameRules_GetProp("m_bGameRestart"))
return Plugin_Continue;
int gamephase = GameRules_GetProp("m_gamePhase");
if(gamephase < GAMEPHASE_PLAYING_STANDARD || gamephase > GAMEPHASE_HALFTIME)
return Plugin_Continue;
if(IsVoteInProgress())
{
// When fail to create vote, try few times again.
if(data <= 2)
{
float delay = float(CheckVoteDelay());
CreateTimer(delay, VoteStart, ++x, TIMER_FLAG_NO_MAPCHANGE);
}
return Plugin_Continue;
}
int targets[MAXPLAYERS+1];
int numtargets;
for(int i = 1; i <= MaxClients; i++)
{
if(!IsClientInGame(i) || IsFakeClient(i))
continue;
targets[numtargets] = i;
numtargets++;
}
Menu menu = new Menu(menu_handler, MenuAction_End)
menu.VoteResultCallback = vote_handler;
menu.AddItem("10", "+10");
menu.AddItem("15", "+15");
menu.AddItem("20", "+20");
menu.AddItem("0", "No more rounds");
menu.ExitButton = false;
menu.SetTitle("Vote Extend -> Max +Rounds");
menu.ShufflePerClient(0, -1);
menu.DisplayVote(targets, numtargets, 10);
return Plugin_Continue
}
public int menu_handler(Menu menu, MenuAction action, int param1, int param2)
{
if(action == MenuAction_End)
delete menu;
return 0;
}
public void vote_handler(Menu menu, int num_votes, int num_clients, const int[][] client_info, int num_items, const int[][] item_info)
{
int winner = 0;
if(num_items > 1 &&
item_info[0][VOTEINFO_ITEM_VOTES] == item_info[1][VOTEINFO_ITEM_VOTES])
{
winner = GetRandomInt(0, 1);
}
char info[3], display[50];
menu.GetItem(item_info[winner][VOTEINFO_ITEM_INDEX], info, sizeof(info), _, display, sizeof(display), -1);
int extend = StringToInt(info);
if(extend == 0)
{
PrintToChatAll("[SM] Vote end: Vote result %s", display);
return;
}
int maxrounds = mp_maxrounds.IntValue;
if(maxrounds == 0)
{
LogError("Vote Extend Max Rounds: Cancel change mp_maxrounds cvar, mp_maxrounds value is 0 (disabled)");
return;
}
mp_maxrounds.IntValue = maxrounds + extend;
PrintToChatAll("[SM] Vote end: Vote result, extend rounds by %s", display);
}
__________________