Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sdktools>
- #include <cstrike>
- #include <clientprefs>
- #include <colors_csgo>
- #include <addicttime>
- #include <menustuff/stocks>
- #pragma newdecls required
- #include <sourcemod>
- #pragma semicolon 1
- char PLUGIN_NAME[] = "Jailbreak Team Ratio";
- char PLUGIN_VERSION[] = "1.8";
- public Plugin myinfo =
- {
- name = PLUGIN_NAME,
- author = "Addicted",
- description = "Manage Team Ratio",
- version = PLUGIN_VERSION,
- url = ""
- }
- Handle g_aGuardQueue;
- Handle cookie_ct_banned;
- Handle cvar_prisoners_per_guard;
- char g_szRestrictedSound[] = "buttons/button11.wav";
- public void OnPluginStart()
- {
- CreateConVar("jb_team_ratio_ver", PLUGIN_VERSION, PLUGIN_NAME, FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_NOTIFY|FCVAR_PRINTABLEONLY);
- cvar_prisoners_per_guard = CreateConVar("jb_prisoners_per_guard", "2", "How many prisoners for each guard.", _, true, 1.0);
- if((cookie_ct_banned = FindClientCookie("Banned_From_CT")) == INVALID_HANDLE)
- cookie_ct_banned = RegClientCookie("Banned_From_CT", "Tells if you are restricted from joining the CT team", CookieAccess_Protected);
- g_aGuardQueue = CreateArray();
- AddCommandListener(OnJoinTeam, "jointeam");
- HookEvent("player_team", Event_PlayerTeam_Post, EventHookMode_Post);
- HookEvent("round_end", Event_RoundEnd_Post, EventHookMode_Post);
- HookEvent("player_spawn", Event_OnPlayerSpawn, EventHookMode_Post);
- RegConsoleCmd("sm_guard", OnGuardQueue, "Adds you to the guard queue.");
- RegConsoleCmd("sm_viewqueue", ViewGuardQueue, "Adds you to the guard queue.");
- RegConsoleCmd("sm_vq", ViewGuardQueue, "Adds you to the guard queue.");
- }
- public void OnConfigsExecuted()
- {
- Handle hConVar = FindConVar("mp_force_pick_time");
- if(hConVar == INVALID_HANDLE)
- return;
- HookConVarChange(hConVar, OnForcePickTimeChanged);
- SetConVarInt(hConVar, 999999);
- }
- public void OnForcePickTimeChanged(Handle hConVar, const char[] szOldValue, const char[] szNewValue)
- {
- SetConVarInt(hConVar, 999999);
- }
- public void OnClientDisconnect_Post(int client)
- {
- RemovePlayerFromGuardQueue(client);
- }
- public Action Event_OnPlayerSpawn(Event event, const char[] name, bool bDontBroadcast)
- {
- int client = GetClientOfUserId(GetEventInt(event, "userid"));
- if (GetClientTeam(client) != 3)
- return Plugin_Continue;
- char sData[2];
- GetClientCookie(client, cookie_ct_banned, sData, sizeof(sData));
- if(sData[0] == '1')
- {
- CPrintToChat(client, "[{blue}Teams{default}] Cannot play on guards because you are CT banned.");
- PrintHintText(client, "Cannot play on guards because you are CT banned.");
- CreateTimer(5.0, SlayPlayer, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
- return Plugin_Continue;
- }
- if(AddictTime_GetTime(client) < 1800)
- {
- CPrintToChat(client, "[{blue}Teams{default}] Cannot play on guards because you have not played 30 minutes alive.");
- PrintHintText(client, "Cannot play on guards because you have not played 30 minutes alive.");
- CreateTimer(5.0, SlayPlayer, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
- return Plugin_Continue;
- }
- return Plugin_Continue;
- }
- public Action SlayPlayer(Handle hTimer, any iUserId)
- {
- int client = GetClientOfUserId(iUserId);
- if (!client)
- return Plugin_Stop;
- if (!IsClientInGame(client))
- return Plugin_Stop;
- if (!IsPlayerAlive(client))
- return Plugin_Stop;
- if (GetClientTeam(client) != 3)
- return Plugin_Stop;
- ForcePlayerSuicide(client);
- ChangeClientTeam(client, 2);
- CS_RespawnPlayer(client);
- return Plugin_Stop;
- }
- public void Event_PlayerTeam_Post(Handle hEvent, const char[] szName, bool bDontBroadcast)
- {
- if(GetEventInt(hEvent, "team") == CS_TEAM_T)
- return;
- int client = GetClientOfUserId(GetEventInt(hEvent, "userid"));
- RemovePlayerFromGuardQueue(client);
- }
- public Action Event_RoundEnd_Post(Handle hEvent, const char[] szName, bool bDontBroadcast)
- {
- FixTeamRatio();
- }
- public Action OnJoinTeam(int client, const char[] szCommand, int iArgCount)
- {
- if(iArgCount < 1)
- return Plugin_Continue;
- char szData[2];
- GetCmdArg(1, szData, sizeof(szData));
- int iTeam = StringToInt(szData);
- if(!iTeam)
- {
- ClientCommand(client, "play %s", g_szRestrictedSound);
- PrintToChatAndConsole(client, "[{blue}Teams{default}] You cannot use auto select to join a team.");
- return Plugin_Handled;
- }
- if(iTeam != CS_TEAM_CT)
- return Plugin_Continue;
- GetClientCookie(client, cookie_ct_banned, szData, sizeof(szData));
- if(szData[0] == '1')
- {
- ClientCommand(client, "play %s", g_szRestrictedSound);
- PrintToChatAndConsole(client, "[{blue}Teams{default}] Cannot join guards because you are CT banned.");
- FakeClientCommand(client, "sm_isbanned @me");
- return Plugin_Handled;
- }
- int alivetime = AddictTime_GetTime(client);
- if(alivetime < 1800)
- {
- ClientCommand(client, "play %s", g_szRestrictedSound);
- CReplyToCommand(client, "[{blue}Teams{default}] Cannot join guards because you have not played 30 minutes alive.");
- CReplyToCommand(client, "[{blue}Teams{default}] Your current time is {green}%i{default} (You need to play for %i more mins)", alivetime / 60, 30 - (alivetime / 60));
- return Plugin_Handled;
- }
- if(!CanClientJoinGuards(client))
- {
- int iIndex = FindValueInArray(g_aGuardQueue, client);
- if(iIndex == -1)
- {
- CReplyToCommand(client, "[{blue}Teams{default}] Guards team full. Type !guard to join the queue.");
- }
- else
- {
- CReplyToCommand(client, "[{blue}Teams{default}] Guards team full. You are {green}#%i{default} in the queue.", iIndex + 1);
- }
- ClientCommand(client, "play %s", g_szRestrictedSound);
- return Plugin_Handled;
- }
- return Plugin_Continue;
- }
- public Action ViewGuardQueue(int client, int args)
- {
- if(!client)
- {
- return Plugin_Handled;
- }
- int iQueueSize = GetArraySize(g_aGuardQueue);
- if(iQueueSize <= 0)
- {
- CReplyToCommand(client, "[{blue}Teams{default}] The queue is currently empty.");
- return Plugin_Handled;
- }
- Handle MenuHandle = CreateMenu(ViewQueueMenuHandle);
- SetMenuTitle(MenuHandle, "Guard Queue:");
- for (int i = 1; i <= MaxClients; i++)
- {
- int iIndex = FindValueInArray(g_aGuardQueue, i);
- if(iIndex == -1)
- {
- continue;
- }
- AddMenuItemFormat(MenuHandle, "", _, "%N", i);
- }
- DisplayMenu(MenuHandle, client, MENU_TIME_FOREVER);
- SetMenuPagination(MenuHandle, 8);
- return Plugin_Handled;
- }
- public int ViewQueueMenuHandle(Handle menu, MenuAction action, int client, int option)
- {
- if (action == MenuAction_End)
- {
- CloneHandle(menu);
- }
- }
- public Action OnGuardQueue(int client, int iArgNum)
- {
- if(!client)
- {
- return Plugin_Handled;
- }
- if(GetClientTeam(client) != CS_TEAM_T)
- {
- ClientCommand(client, "play %s", g_szRestrictedSound);
- CReplyToCommand(client, "[{blue}Teams{default}] You must be a prisoner to join the queue.");
- return Plugin_Handled;
- }
- char szCookie[2];
- GetClientCookie(client, cookie_ct_banned, szCookie, sizeof(szCookie));
- if(szCookie[0] == '1')
- {
- ClientCommand(client, "play %s", g_szRestrictedSound);
- CReplyToCommand(client, "[{blue}Teams{default}] Cannot join guards because you are CT banned.");
- FakeClientCommand(client, "sm_isbanned @me");
- return Plugin_Handled;
- }
- if(AddictTime_GetTime(client) < 1800)
- {
- ClientCommand(client, "play %s", g_szRestrictedSound);
- CReplyToCommand(client, "[{blue}Teams{default}] Cannot join guards because you have not played 30 minutes alive.");
- return Plugin_Handled;
- }
- int iIndex = FindValueInArray(g_aGuardQueue, client);
- int iQueueSize = GetArraySize(g_aGuardQueue);
- if(iIndex == -1)
- {
- if (CheckCommandAccess(client, "", ADMFLAG_RESERVATION, true))
- {
- if (iQueueSize == 0)
- iIndex = PushArrayCell(g_aGuardQueue, client);
- else
- {
- ShiftArrayUp(g_aGuardQueue, 0);
- SetArrayCell(g_aGuardQueue, 0, client);
- }
- CPrintToChat(client, "[{blue}Teams{default}] Thank you for being a {darkred}VIP{default}! You have been moved to the {green}front of the queue!{default}");
- CPrintToChat(client, "[{blue}Teams{default}] You are {green}#1{default} in the guard queue.");
- CPrintToChat(client, "[{blue}Teams{default}] Type !viewqueue to see who else is in the queue.");
- return Plugin_Handled;
- }
- else
- {
- iIndex = PushArrayCell(g_aGuardQueue, client);
- CPrintToChat(client, "[{blue}Teams{default}] You are {green}#%i{default} in the guard queue.", iIndex + 1);
- CPrintToChat(client, "[{blue}Teams{default}] Get {darkred}VIP{default} to be automatically moved to the front of the queue.");
- CPrintToChat(client, "[{blue}Teams{default}] Type !viewqueue to see who else is in the queue.");
- return Plugin_Handled;
- }
- }
- else
- {
- CPrintToChat(client, "[{blue}Teams{default}] You are {green}#%i{default} in the guard queue.", iIndex + 1);
- CPrintToChat(client, "[{blue}Teams{default}] Get {darkred}VIP{default} to be automatically moved to the front of the queue.");
- CPrintToChat(client, "[{blue}Teams{default}] Type !viewqueue to see who else is in the queue.");
- }
- return Plugin_Continue;
- }
- stock bool RemovePlayerFromGuardQueue(int client)
- {
- int iIndex = FindValueInArray(g_aGuardQueue, client);
- if(iIndex == -1)
- return;
- RemoveFromArray(g_aGuardQueue, iIndex);
- }
- stock void FixTeamRatio()
- {
- bool bMovedPlayers;
- while(ShouldMovePrisonerToGuard())
- {
- int client;
- if(GetArraySize(g_aGuardQueue))
- {
- client = GetArrayCell(g_aGuardQueue, 0);
- RemovePlayerFromGuardQueue(client);
- CPrintToChatAll("[{blue}Teams{default}] Finding a new guard from queue. Found %N.", client);
- }
- else
- {
- client = GetRandomClientFromTeam(CS_TEAM_T, true);
- CPrintToChatAll("[{blue}Teams{default}] Guard queue is empty. Finding a random new guard. Found %N.", client);
- }
- if(!client)
- {
- CPrintToChatAll("[{blue}Teams{default}] Could not find a valid player to switch to guards. Ratio may be fucked.");
- break;
- }
- SetClientPendingTeam(client, CS_TEAM_CT);
- bMovedPlayers = true;
- }
- if(bMovedPlayers)
- return;
- while(ShouldMoveGuardToPrisoner())
- {
- int client = GetRandomClientFromTeam(CS_TEAM_CT, true);
- if(!client)
- break;
- SetClientPendingTeam(client, CS_TEAM_T);
- }
- }
- stock int GetRandomClientFromTeam(int iTeam, bool bSkipCTBanned=true)
- {
- int iNumFound;
- int clients[MAXPLAYERS];
- char szCookie[2];
- for(int client=1; client<=MaxClients; client++)
- {
- if(!IsClientInGame(client))
- continue;
- if(GetClientPendingTeam(client) != iTeam)
- continue;
- if(bSkipCTBanned)
- {
- if(!AreClientCookiesCached(client))
- continue;
- GetClientCookie(client, cookie_ct_banned, szCookie, sizeof(szCookie));
- if(szCookie[0] == '1')
- continue;
- }
- if(AddictTime_GetTime(client) < 1800)
- continue;
- clients[iNumFound++] = client;
- }
- if(!iNumFound)
- return 0;
- return clients[GetRandomInt(0, iNumFound-1)];
- }
- bool ShouldMoveGuardToPrisoner()
- {
- int iNumGuards, iNumPrisoners;
- for(int client=1; client<=MaxClients; client++)
- {
- if(!IsClientInGame(client))
- continue;
- switch(GetClientPendingTeam(client))
- {
- case CS_TEAM_CT: iNumGuards++;
- case CS_TEAM_T: iNumPrisoners++;
- }
- }
- if(iNumGuards <= 1)
- return false;
- int iMaxGuards = RoundToFloor(float(iNumPrisoners) / GetConVarFloat(cvar_prisoners_per_guard));
- if(iNumGuards <= iMaxGuards)
- return false;
- return true;
- }
- bool ShouldMovePrisonerToGuard()
- {
- int iNumGuards, iNumPrisoners;
- for(int client=1; client<=MaxClients; client++)
- {
- if(!IsClientInGame(client))
- continue;
- switch(GetClientPendingTeam(client))
- {
- case CS_TEAM_CT: iNumGuards++;
- case CS_TEAM_T: iNumPrisoners++;
- }
- }
- iNumPrisoners--;
- iNumGuards++;
- if(iNumPrisoners < 1)
- return false;
- float fNumPrisonersPerGuard = float(iNumPrisoners) / float(iNumGuards);
- if(fNumPrisonersPerGuard < GetConVarFloat(cvar_prisoners_per_guard))
- return false;
- return true;
- }
- stock bool CanClientJoinGuards(int client)
- {
- int iNumGuards, iNumPrisoners;
- for(int iPlayer=1; iPlayer<=MaxClients; iPlayer++)
- {
- if(!IsClientInGame(iPlayer))
- continue;
- switch(GetClientPendingTeam(iPlayer))
- {
- case CS_TEAM_CT: iNumGuards++;
- case CS_TEAM_T: iNumPrisoners++;
- }
- }
- iNumGuards++;
- if(GetClientPendingTeam(client) == CS_TEAM_T)
- iNumPrisoners--;
- if(iNumGuards <= 1)
- return true;
- float fNumPrisonersPerGuard = float(iNumPrisoners) / float(iNumGuards);
- if(fNumPrisonersPerGuard < GetConVarFloat(cvar_prisoners_per_guard))
- return false;
- int iGuardsNeeded = RoundToCeil(fNumPrisonersPerGuard - GetConVarFloat(cvar_prisoners_per_guard));
- if(iGuardsNeeded < 1)
- iGuardsNeeded = 1;
- int iQueueSize = GetArraySize(g_aGuardQueue);
- if(iGuardsNeeded > iQueueSize)
- return true;
- for(int i=0; i<iGuardsNeeded; i++)
- {
- if(client == GetArrayCell(g_aGuardQueue, i))
- return true;
- }
- return false;
- }
- stock int GetClientPendingTeam(int client)
- {
- return GetEntProp(client, Prop_Send, "m_iPendingTeamNum");
- }
- stock void SetClientPendingTeam(int client, int iTeam)
- {
- SetEntProp(client, Prop_Send, "m_iPendingTeamNum", iTeam);
- }
- stock void PrintToChatAndConsole(int client, const char[] szFormat, any ...)
- {
- char szBuffer[256];
- VFormat(szBuffer, sizeof(szBuffer), szFormat, 3);
- CPrintToChat(client, szBuffer);
- PrintToConsole(client, szBuffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment