oaaron99

Untitled

Feb 5th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.30 KB | None | 0 0
  1. #include <sdktools>
  2. #include <cstrike>
  3. #include <clientprefs>
  4. #include <colors_csgo>
  5. #include <addicttime>
  6. #include <menustuff/stocks>
  7. #pragma newdecls required
  8. #include <sourcemod>
  9.  
  10. #pragma semicolon 1
  11.  
  12. char PLUGIN_NAME[] = "Jailbreak Team Ratio";
  13. char PLUGIN_VERSION[] = "1.8";
  14.  
  15. public Plugin myinfo =
  16. {
  17. name = PLUGIN_NAME,
  18. author = "Addicted",
  19. description = "Manage Team Ratio",
  20. version = PLUGIN_VERSION,
  21. url = ""
  22. }
  23.  
  24. Handle g_aGuardQueue;
  25. Handle cookie_ct_banned;
  26. Handle cvar_prisoners_per_guard;
  27.  
  28. char g_szRestrictedSound[] = "buttons/button11.wav";
  29.  
  30. public void OnPluginStart()
  31. {
  32. CreateConVar("jb_team_ratio_ver", PLUGIN_VERSION, PLUGIN_NAME, FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_NOTIFY|FCVAR_PRINTABLEONLY);
  33.  
  34. cvar_prisoners_per_guard = CreateConVar("jb_prisoners_per_guard", "2", "How many prisoners for each guard.", _, true, 1.0);
  35.  
  36. if((cookie_ct_banned = FindClientCookie("Banned_From_CT")) == INVALID_HANDLE)
  37. cookie_ct_banned = RegClientCookie("Banned_From_CT", "Tells if you are restricted from joining the CT team", CookieAccess_Protected);
  38.  
  39. g_aGuardQueue = CreateArray();
  40.  
  41. AddCommandListener(OnJoinTeam, "jointeam");
  42. HookEvent("player_team", Event_PlayerTeam_Post, EventHookMode_Post);
  43. HookEvent("round_end", Event_RoundEnd_Post, EventHookMode_Post);
  44. HookEvent("player_spawn", Event_OnPlayerSpawn, EventHookMode_Post);
  45.  
  46. RegConsoleCmd("sm_guard", OnGuardQueue, "Adds you to the guard queue.");
  47. RegConsoleCmd("sm_viewqueue", ViewGuardQueue, "Adds you to the guard queue.");
  48. RegConsoleCmd("sm_vq", ViewGuardQueue, "Adds you to the guard queue.");
  49. }
  50.  
  51. public void OnConfigsExecuted()
  52. {
  53. Handle hConVar = FindConVar("mp_force_pick_time");
  54. if(hConVar == INVALID_HANDLE)
  55. return;
  56.  
  57. HookConVarChange(hConVar, OnForcePickTimeChanged);
  58. SetConVarInt(hConVar, 999999);
  59. }
  60.  
  61. public void OnForcePickTimeChanged(Handle hConVar, const char[] szOldValue, const char[] szNewValue)
  62. {
  63. SetConVarInt(hConVar, 999999);
  64. }
  65.  
  66. public void OnClientDisconnect_Post(int client)
  67. {
  68. RemovePlayerFromGuardQueue(client);
  69. }
  70.  
  71. public Action Event_OnPlayerSpawn(Event event, const char[] name, bool bDontBroadcast)
  72. {
  73. int client = GetClientOfUserId(GetEventInt(event, "userid"));
  74.  
  75. if (GetClientTeam(client) != 3)
  76. return Plugin_Continue;
  77.  
  78. char sData[2];
  79. GetClientCookie(client, cookie_ct_banned, sData, sizeof(sData));
  80.  
  81. if(sData[0] == '1')
  82. {
  83. CPrintToChat(client, "[{blue}Teams{default}] Cannot play on guards because you are CT banned.");
  84. PrintHintText(client, "Cannot play on guards because you are CT banned.");
  85. CreateTimer(5.0, SlayPlayer, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
  86. return Plugin_Continue;
  87. }
  88. if(AddictTime_GetTime(client) < 1800)
  89. {
  90. CPrintToChat(client, "[{blue}Teams{default}] Cannot play on guards because you have not played 30 minutes alive.");
  91. PrintHintText(client, "Cannot play on guards because you have not played 30 minutes alive.");
  92. CreateTimer(5.0, SlayPlayer, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
  93. return Plugin_Continue;
  94. }
  95.  
  96. return Plugin_Continue;
  97. }
  98.  
  99. public Action SlayPlayer(Handle hTimer, any iUserId)
  100. {
  101. int client = GetClientOfUserId(iUserId);
  102. if (!client)
  103. return Plugin_Stop;
  104. if (!IsClientInGame(client))
  105. return Plugin_Stop;
  106. if (!IsPlayerAlive(client))
  107. return Plugin_Stop;
  108. if (GetClientTeam(client) != 3)
  109. return Plugin_Stop;
  110.  
  111. ForcePlayerSuicide(client);
  112. ChangeClientTeam(client, 2);
  113. CS_RespawnPlayer(client);
  114. return Plugin_Stop;
  115. }
  116.  
  117. public void Event_PlayerTeam_Post(Handle hEvent, const char[] szName, bool bDontBroadcast)
  118. {
  119. if(GetEventInt(hEvent, "team") == CS_TEAM_T)
  120. return;
  121.  
  122. int client = GetClientOfUserId(GetEventInt(hEvent, "userid"));
  123. RemovePlayerFromGuardQueue(client);
  124. }
  125.  
  126. public Action Event_RoundEnd_Post(Handle hEvent, const char[] szName, bool bDontBroadcast)
  127. {
  128. FixTeamRatio();
  129. }
  130.  
  131. public Action OnJoinTeam(int client, const char[] szCommand, int iArgCount)
  132. {
  133. if(iArgCount < 1)
  134. return Plugin_Continue;
  135.  
  136. char szData[2];
  137. GetCmdArg(1, szData, sizeof(szData));
  138. int iTeam = StringToInt(szData);
  139.  
  140. if(!iTeam)
  141. {
  142. ClientCommand(client, "play %s", g_szRestrictedSound);
  143. PrintToChatAndConsole(client, "[{blue}Teams{default}] You cannot use auto select to join a team.");
  144. return Plugin_Handled;
  145. }
  146.  
  147. if(iTeam != CS_TEAM_CT)
  148. return Plugin_Continue;
  149.  
  150. GetClientCookie(client, cookie_ct_banned, szData, sizeof(szData));
  151.  
  152. if(szData[0] == '1')
  153. {
  154. ClientCommand(client, "play %s", g_szRestrictedSound);
  155. PrintToChatAndConsole(client, "[{blue}Teams{default}] Cannot join guards because you are CT banned.");
  156. FakeClientCommand(client, "sm_isbanned @me");
  157. return Plugin_Handled;
  158. }
  159.  
  160. int alivetime = AddictTime_GetTime(client);
  161.  
  162. if(alivetime < 1800)
  163. {
  164. ClientCommand(client, "play %s", g_szRestrictedSound);
  165. CReplyToCommand(client, "[{blue}Teams{default}] Cannot join guards because you have not played 30 minutes alive.");
  166. 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));
  167. return Plugin_Handled;
  168. }
  169.  
  170. if(!CanClientJoinGuards(client))
  171. {
  172. int iIndex = FindValueInArray(g_aGuardQueue, client);
  173. if(iIndex == -1)
  174. {
  175. CReplyToCommand(client, "[{blue}Teams{default}] Guards team full. Type !guard to join the queue.");
  176. }
  177. else
  178. {
  179. CReplyToCommand(client, "[{blue}Teams{default}] Guards team full. You are {green}#%i{default} in the queue.", iIndex + 1);
  180. }
  181.  
  182. ClientCommand(client, "play %s", g_szRestrictedSound);
  183. return Plugin_Handled;
  184. }
  185.  
  186. return Plugin_Continue;
  187. }
  188.  
  189. public Action ViewGuardQueue(int client, int args)
  190. {
  191. if(!client)
  192. {
  193. return Plugin_Handled;
  194. }
  195.  
  196. int iQueueSize = GetArraySize(g_aGuardQueue);
  197.  
  198. if(iQueueSize <= 0)
  199. {
  200. CReplyToCommand(client, "[{blue}Teams{default}] The queue is currently empty.");
  201. return Plugin_Handled;
  202. }
  203.  
  204. Handle MenuHandle = CreateMenu(ViewQueueMenuHandle);
  205. SetMenuTitle(MenuHandle, "Guard Queue:");
  206.  
  207. for (int i = 1; i <= MaxClients; i++)
  208. {
  209. int iIndex = FindValueInArray(g_aGuardQueue, i);
  210.  
  211. if(iIndex == -1)
  212. {
  213. continue;
  214. }
  215.  
  216. AddMenuItemFormat(MenuHandle, "", _, "%N", i);
  217. }
  218.  
  219. DisplayMenu(MenuHandle, client, MENU_TIME_FOREVER);
  220. SetMenuPagination(MenuHandle, 8);
  221.  
  222. return Plugin_Handled;
  223. }
  224.  
  225. public int ViewQueueMenuHandle(Handle menu, MenuAction action, int client, int option)
  226. {
  227. if (action == MenuAction_End)
  228. {
  229. CloneHandle(menu);
  230. }
  231. }
  232.  
  233. public Action OnGuardQueue(int client, int iArgNum)
  234. {
  235. if(!client)
  236. {
  237. return Plugin_Handled;
  238. }
  239.  
  240. if(GetClientTeam(client) != CS_TEAM_T)
  241. {
  242. ClientCommand(client, "play %s", g_szRestrictedSound);
  243. CReplyToCommand(client, "[{blue}Teams{default}] You must be a prisoner to join the queue.");
  244. return Plugin_Handled;
  245. }
  246.  
  247. char szCookie[2];
  248. GetClientCookie(client, cookie_ct_banned, szCookie, sizeof(szCookie));
  249. if(szCookie[0] == '1')
  250. {
  251. ClientCommand(client, "play %s", g_szRestrictedSound);
  252. CReplyToCommand(client, "[{blue}Teams{default}] Cannot join guards because you are CT banned.");
  253. FakeClientCommand(client, "sm_isbanned @me");
  254. return Plugin_Handled;
  255. }
  256.  
  257. if(AddictTime_GetTime(client) < 1800)
  258. {
  259. ClientCommand(client, "play %s", g_szRestrictedSound);
  260. CReplyToCommand(client, "[{blue}Teams{default}] Cannot join guards because you have not played 30 minutes alive.");
  261. return Plugin_Handled;
  262. }
  263.  
  264. int iIndex = FindValueInArray(g_aGuardQueue, client);
  265. int iQueueSize = GetArraySize(g_aGuardQueue);
  266.  
  267. if(iIndex == -1)
  268. {
  269. if (CheckCommandAccess(client, "", ADMFLAG_RESERVATION, true))
  270. {
  271. if (iQueueSize == 0)
  272. iIndex = PushArrayCell(g_aGuardQueue, client);
  273. else
  274. {
  275. ShiftArrayUp(g_aGuardQueue, 0);
  276. SetArrayCell(g_aGuardQueue, 0, client);
  277. }
  278.  
  279. 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}");
  280. CPrintToChat(client, "[{blue}Teams{default}] You are {green}#1{default} in the guard queue.");
  281. CPrintToChat(client, "[{blue}Teams{default}] Type !viewqueue to see who else is in the queue.");
  282. return Plugin_Handled;
  283. }
  284. else
  285. {
  286. iIndex = PushArrayCell(g_aGuardQueue, client);
  287.  
  288. CPrintToChat(client, "[{blue}Teams{default}] You are {green}#%i{default} in the guard queue.", iIndex + 1);
  289. CPrintToChat(client, "[{blue}Teams{default}] Get {darkred}VIP{default} to be automatically moved to the front of the queue.");
  290. CPrintToChat(client, "[{blue}Teams{default}] Type !viewqueue to see who else is in the queue.");
  291.  
  292. return Plugin_Handled;
  293. }
  294. }
  295. else
  296. {
  297. CPrintToChat(client, "[{blue}Teams{default}] You are {green}#%i{default} in the guard queue.", iIndex + 1);
  298. CPrintToChat(client, "[{blue}Teams{default}] Get {darkred}VIP{default} to be automatically moved to the front of the queue.");
  299. CPrintToChat(client, "[{blue}Teams{default}] Type !viewqueue to see who else is in the queue.");
  300. }
  301. return Plugin_Continue;
  302. }
  303.  
  304. stock bool RemovePlayerFromGuardQueue(int client)
  305. {
  306. int iIndex = FindValueInArray(g_aGuardQueue, client);
  307. if(iIndex == -1)
  308. return;
  309.  
  310. RemoveFromArray(g_aGuardQueue, iIndex);
  311. }
  312.  
  313. stock void FixTeamRatio()
  314. {
  315. bool bMovedPlayers;
  316. while(ShouldMovePrisonerToGuard())
  317. {
  318. int client;
  319. if(GetArraySize(g_aGuardQueue))
  320. {
  321. client = GetArrayCell(g_aGuardQueue, 0);
  322. RemovePlayerFromGuardQueue(client);
  323.  
  324. CPrintToChatAll("[{blue}Teams{default}] Finding a new guard from queue. Found %N.", client);
  325. }
  326. else
  327. {
  328. client = GetRandomClientFromTeam(CS_TEAM_T, true);
  329. CPrintToChatAll("[{blue}Teams{default}] Guard queue is empty. Finding a random new guard. Found %N.", client);
  330. }
  331.  
  332. if(!client)
  333. {
  334. CPrintToChatAll("[{blue}Teams{default}] Could not find a valid player to switch to guards. Ratio may be fucked.");
  335. break;
  336. }
  337.  
  338. SetClientPendingTeam(client, CS_TEAM_CT);
  339. bMovedPlayers = true;
  340. }
  341.  
  342. if(bMovedPlayers)
  343. return;
  344.  
  345. while(ShouldMoveGuardToPrisoner())
  346. {
  347. int client = GetRandomClientFromTeam(CS_TEAM_CT, true);
  348. if(!client)
  349. break;
  350.  
  351. SetClientPendingTeam(client, CS_TEAM_T);
  352. }
  353. }
  354.  
  355. stock int GetRandomClientFromTeam(int iTeam, bool bSkipCTBanned=true)
  356. {
  357. int iNumFound;
  358. int clients[MAXPLAYERS];
  359. char szCookie[2];
  360.  
  361. for(int client=1; client<=MaxClients; client++)
  362. {
  363. if(!IsClientInGame(client))
  364. continue;
  365.  
  366. if(GetClientPendingTeam(client) != iTeam)
  367. continue;
  368.  
  369. if(bSkipCTBanned)
  370. {
  371. if(!AreClientCookiesCached(client))
  372. continue;
  373.  
  374. GetClientCookie(client, cookie_ct_banned, szCookie, sizeof(szCookie));
  375. if(szCookie[0] == '1')
  376. continue;
  377. }
  378.  
  379. if(AddictTime_GetTime(client) < 1800)
  380. continue;
  381.  
  382. clients[iNumFound++] = client;
  383. }
  384.  
  385. if(!iNumFound)
  386. return 0;
  387.  
  388. return clients[GetRandomInt(0, iNumFound-1)];
  389. }
  390.  
  391. bool ShouldMoveGuardToPrisoner()
  392. {
  393. int iNumGuards, iNumPrisoners;
  394. for(int client=1; client<=MaxClients; client++)
  395. {
  396. if(!IsClientInGame(client))
  397. continue;
  398.  
  399. switch(GetClientPendingTeam(client))
  400. {
  401. case CS_TEAM_CT: iNumGuards++;
  402. case CS_TEAM_T: iNumPrisoners++;
  403. }
  404. }
  405.  
  406. if(iNumGuards <= 1)
  407. return false;
  408.  
  409. int iMaxGuards = RoundToFloor(float(iNumPrisoners) / GetConVarFloat(cvar_prisoners_per_guard));
  410. if(iNumGuards <= iMaxGuards)
  411. return false;
  412.  
  413. return true;
  414. }
  415.  
  416. bool ShouldMovePrisonerToGuard()
  417. {
  418. int iNumGuards, iNumPrisoners;
  419. for(int client=1; client<=MaxClients; client++)
  420. {
  421. if(!IsClientInGame(client))
  422. continue;
  423.  
  424. switch(GetClientPendingTeam(client))
  425. {
  426. case CS_TEAM_CT: iNumGuards++;
  427. case CS_TEAM_T: iNumPrisoners++;
  428. }
  429. }
  430.  
  431. iNumPrisoners--;
  432. iNumGuards++;
  433.  
  434. if(iNumPrisoners < 1)
  435. return false;
  436.  
  437. float fNumPrisonersPerGuard = float(iNumPrisoners) / float(iNumGuards);
  438. if(fNumPrisonersPerGuard < GetConVarFloat(cvar_prisoners_per_guard))
  439. return false;
  440.  
  441. return true;
  442. }
  443.  
  444. stock bool CanClientJoinGuards(int client)
  445. {
  446. int iNumGuards, iNumPrisoners;
  447. for(int iPlayer=1; iPlayer<=MaxClients; iPlayer++)
  448. {
  449. if(!IsClientInGame(iPlayer))
  450. continue;
  451.  
  452. switch(GetClientPendingTeam(iPlayer))
  453. {
  454. case CS_TEAM_CT: iNumGuards++;
  455. case CS_TEAM_T: iNumPrisoners++;
  456. }
  457. }
  458.  
  459. iNumGuards++;
  460. if(GetClientPendingTeam(client) == CS_TEAM_T)
  461. iNumPrisoners--;
  462.  
  463. if(iNumGuards <= 1)
  464. return true;
  465.  
  466. float fNumPrisonersPerGuard = float(iNumPrisoners) / float(iNumGuards);
  467. if(fNumPrisonersPerGuard < GetConVarFloat(cvar_prisoners_per_guard))
  468. return false;
  469.  
  470. int iGuardsNeeded = RoundToCeil(fNumPrisonersPerGuard - GetConVarFloat(cvar_prisoners_per_guard));
  471. if(iGuardsNeeded < 1)
  472. iGuardsNeeded = 1;
  473.  
  474. int iQueueSize = GetArraySize(g_aGuardQueue);
  475. if(iGuardsNeeded > iQueueSize)
  476. return true;
  477.  
  478. for(int i=0; i<iGuardsNeeded; i++)
  479. {
  480. if(client == GetArrayCell(g_aGuardQueue, i))
  481. return true;
  482. }
  483.  
  484. return false;
  485. }
  486.  
  487. stock int GetClientPendingTeam(int client)
  488. {
  489. return GetEntProp(client, Prop_Send, "m_iPendingTeamNum");
  490. }
  491.  
  492. stock void SetClientPendingTeam(int client, int iTeam)
  493. {
  494. SetEntProp(client, Prop_Send, "m_iPendingTeamNum", iTeam);
  495. }
  496.  
  497. stock void PrintToChatAndConsole(int client, const char[] szFormat, any ...)
  498. {
  499. char szBuffer[256];
  500. VFormat(szBuffer, sizeof(szBuffer), szFormat, 3);
  501.  
  502. CPrintToChat(client, szBuffer);
  503. PrintToConsole(client, szBuffer);
  504. }
Advertisement
Add Comment
Please, Sign In to add comment