Advertisement
Guest User

RockTheVote

a guest
May 15th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. //constants
  2. //WARNING, CANNOT JUST INCREASE RTV_LIMIT, bool doCommand IS HARD-CODED IN HANDLING ONLY 5 MAPS BECAUSE I'M LAZY AS SHIT TO MAKE A INTPARSER FUNCTION
  3. uint RTV_LIMIT = 5;
  4.  
  5. //variables
  6. int counter;
  7. int required;
  8. bool voteStarted;
  9. array<string> g_RockTheVote = {};
  10. array<string> g_voting = {};
  11. array<string> rtvList = {};
  12. array<string> mapList = {};
  13. array<uint> twoMaps = {};
  14. array<uint> voting = {};
  15. string chosenMap = "";
  16.  
  17. void PluginInit()
  18. {
  19. g_Module.ScriptInfo.SetAuthor("Some Faggot");
  20. g_Module.ScriptInfo.SetContactInfo("asdf");
  21. g_Hooks.RegisterHook(Hooks::Player::ClientSay, @ClientSay);
  22. g_Hooks.RegisterHook(Hooks::Player::ClientConnected, @ClientConnected);
  23. g_Hooks.RegisterHook(Hooks::Player::ClientDisconnect, @ClientDisconnect);
  24. g_Hooks.RegisterHook(Hooks::Player::ClientPutInServer, @ClientPutInServer);
  25. }
  26.  
  27. void MapInit()
  28. {
  29. //reset variables
  30. chosenMap = "";
  31. voteStarted = false;
  32. counter = 0;
  33. required = -1; //not set until a player connects
  34. g_RockTheVote.resize(0);
  35. g_voting.resize(0);
  36. rtvList.resize(0);
  37. twoMaps.resize(0);
  38. mapList = g_MapCycle.GetMapCycle();
  39. voting.resize(RTV_LIMIT);
  40. for(uint i = 0; i < RTV_LIMIT; i++)
  41. {
  42. voting[i] = 0;
  43. }
  44. }
  45.  
  46. CClientCommand rtv("rtv", "Rock the Vote Menu", @rtvCmd);
  47.  
  48. void rtvCmd(const CCommand@ args)
  49. {
  50. CBasePlayer@ plr = g_ConCommandSystem.GetCurrentPlayer();
  51.  
  52. }
  53.  
  54. bool doCommand(CBasePlayer@ plr, const CCommand@ args)
  55. {
  56. const string steamId = g_EngineFuncs.GetPlayerAuthId(plr.edict());
  57.  
  58. //debugging
  59. if(args[0] == "rtv_status")
  60. {
  61. g_PlayerFuncs.SayTextAll(plr, "counter: " + counter);
  62. g_PlayerFuncs.SayTextAll(plr, "required: " + required);
  63. return true;
  64. }
  65. if(args.ArgC() > 0 && !voteStarted)
  66. {
  67. if(args[0] == "rtv" || args[0] == "rockthevote")
  68. {
  69. //check if unique player
  70. if(g_RockTheVote.find(steamId) < 0)
  71. {
  72. g_RockTheVote.insertLast(steamId);
  73. counter++;
  74. g_PlayerFuncs.SayText(plr, "Rocked the vote.\n");
  75. }
  76. else
  77. {
  78. g_PlayerFuncs.SayText(plr, "Already rocked the vote!\n");
  79. }
  80. //start rtv if possible
  81. if(counter >= required && !voteStarted)
  82. {
  83. startVote(plr);
  84. }
  85. return true;
  86. }
  87. if(args[0] == "map" && args.ArgC() == 2)
  88. {
  89. string plr_map = args[1];
  90. //g_PlayerFuncs.SayText(plr, "Voting for " + plr_map + "\n");
  91. //check if mapcycle is valid
  92. if (g_MapCycle.Count() <= 0)
  93. {
  94. g_PlayerFuncs.SayText(plr, "MapCycle invalid!\n");
  95. }
  96. //check if argument is valid map
  97. else if(mapList.find(plr_map) < 0)
  98. {
  99. g_PlayerFuncs.SayText(plr, "Map not found!\n");
  100. }
  101. //check if list is not full
  102. else if(rtvList.length() == RTV_LIMIT)
  103. {
  104. g_PlayerFuncs.SayText(plr, "Rock The Vote list is full!\n");
  105. }
  106. //add map if not already in list
  107. else if(rtvList.find(plr_map) < 0)
  108. {
  109. rtvList.insertLast(plr_map);
  110. g_PlayerFuncs.SayText(plr, "Added " + plr_map +".\n");
  111. }
  112. else
  113. {
  114. g_PlayerFuncs.SayText(plr, "Map already included!\n");
  115. }
  116. return true;
  117. }
  118. }
  119. if(voteStarted && args.ArgC() == 2 && args[0] == "rtv")
  120. {
  121. //would opt for a switch-case but intparser doesnt seem to work
  122. string option = args[1];
  123. uint option_int = 0;
  124. if(option == "1")
  125. {
  126. option_int = 1;
  127. }
  128. else if(option == "2")
  129. {
  130. option_int = 2;
  131. }
  132. else if(option == "3")
  133. {
  134. option_int = 3;
  135. }
  136. else if(option == "4")
  137. {
  138. option_int = 4;
  139. }
  140. else if(option == "5")
  141. {
  142. option_int = 5;
  143. }
  144. else
  145. {
  146. g_PlayerFuncs.SayText(plr, "Invalid map choice!\n");
  147. return true;
  148. }
  149. //add the vote
  150. if(g_voting.find(steamId) < 0)
  151. {
  152. g_voting.insertLast(steamId);
  153. voting[option_int - 1]++;
  154. g_PlayerFuncs.SayText(plr, "Voted for " + rtvList[option_int - 1] + ".\n");
  155. }
  156. else
  157. {
  158. g_PlayerFuncs.SayText(plr, "Already voted!\n");
  159. }
  160. return true;
  161. }
  162. if(args[0] == "listmaps" && args.ArgC() == 1)
  163. {
  164. listmaps(plr);
  165. }
  166. return false;
  167. }
  168.  
  169. void listmaps(CBasePlayer@ plr)
  170. {
  171. string text_maplist = "Maps in Rock The Vote list:";
  172. uint rtvList_size = rtvList.length();
  173. for(uint i = 0; i < rtvList_size - 1; i++)
  174. {
  175. text_maplist += " " + rtvList[i] + ",";
  176. }
  177. text_maplist += " " + rtvList[rtvList_size - 1];
  178. g_PlayerFuncs.SayTextAll(plr, text_maplist + "\n");
  179. }
  180.  
  181. void startVote(CBasePlayer@ plr)
  182. {
  183. if(rtvList.length() < RTV_LIMIT)
  184. {
  185. addRandomMaps();
  186. }
  187. g_PlayerFuncs.SayTextAll(plr, "Enough players have rocked the vote.\n");
  188. voteStarted = true;
  189. g_PlayerFuncs.SayTextAll(plr, "There is 20 seconds to vote.\n");
  190. listmaps(plr);
  191. g_PlayerFuncs.SayTextAll(plr, "Type \"rtv [number in respect to map listed]\" to vote for a particular map\n");
  192. g_Scheduler.SetTimeout("vote", 25, @plr);
  193. }
  194.  
  195. void vote(CBasePlayer@ plr)
  196. {
  197. //get two of the highest voted maps
  198. uint highest = 0;
  199. int highestIndex = -1;
  200. uint secondHighest = 0;
  201. int secondHighestIndex = -1;
  202. for(uint i = 0; i < voting.length(); i++)
  203. {
  204. if(voting[i] > highest)
  205. {
  206. secondHighest = highest;
  207. secondHighestIndex = highestIndex;
  208. highest = voting[i];
  209. highestIndex = i;
  210. }
  211. }
  212. if(highestIndex == -1)
  213. {
  214. twoMaps.insertLast(0);
  215. twoMaps.insertLast(1);
  216. }
  217. twoMaps.insertLast(highestIndex);
  218. twoMaps.insertLast(secondHighestIndex);
  219. if(highest == secondHighest)
  220. {
  221. g_PlayerFuncs.SayTextAll(plr, "Tie detected, choosing randomly choosing between: " + rtvList[highestIndex] + " and " + rtvList[secondHighestIndex] + "\n");
  222. uint randomTieBreaker = Math.RandomLong(0, 1);
  223. chosenMap = rtvList[twoMaps[randomTieBreaker]];
  224. }
  225. else
  226. {
  227. chosenMap = rtvList[highestIndex];
  228. }
  229. //debugging
  230. /*
  231. g_PlayerFuncs.SayTextAll(plr, "voting length: " + voting.length() + "\n");
  232. for(uint k = 0; k < voting.length(); k++)
  233. {
  234. g_PlayerFuncs.SayTextAll(plr, rtvList[k] + " " + voting[k] + "\n");
  235. }
  236. */
  237. g_PlayerFuncs.SayTextAll(plr, "Changing to " + chosenMap + " in 5 seconds....\n");
  238. g_Scheduler.SetTimeout("changeChosenMap", 5);
  239. }
  240.  
  241. string tiebreaker()
  242. {
  243. return "";
  244. }
  245.  
  246. void changeChosenMap()
  247. {
  248. g_EngineFuncs.ChangeLevel(chosenMap);
  249. }
  250.  
  251. void addRandomMaps()
  252. {
  253. //duplicate maps are not checked for performance reasons
  254. uint rtvList_size = rtvList.length();
  255. uint mapList_size = mapList.length();
  256. for(uint i = 0; i < RTV_LIMIT - rtvList_size; i++)
  257. {
  258. rtvList.insertLast(mapList[Math.RandomLong(0, mapList_size - 1)]);
  259. }
  260. }
  261.  
  262. HookReturnCode ClientSay(SayParameters@ pParams)
  263. {
  264. CBasePlayer@ plr = pParams.GetPlayer();
  265. const CCommand@ args = pParams.GetArguments();
  266.  
  267. if(doCommand(plr, args))
  268. {
  269. pParams.ShouldHide = true;
  270. return HOOK_HANDLED;
  271. }
  272.  
  273. return HOOK_CONTINUE;
  274. }
  275.  
  276. //first player joined
  277. HookReturnCode ClientPutInServer(CBasePlayer@ plr)
  278. {
  279. float math = g_PlayerFuncs.GetNumPlayers() * 0.66;
  280. required = uint(math + 0.5);
  281. return HOOK_CONTINUE;
  282. }
  283.  
  284. //player joined
  285. HookReturnCode ClientConnected(edict_t@, const string& in, const string& in, bool& out, string& out)
  286. {
  287. //update rtv player requirement
  288. float math = g_PlayerFuncs.GetNumPlayers() * 0.66;
  289. required = uint(math + 0.5);
  290. return HOOK_CONTINUE;
  291. }
  292.  
  293. //player left
  294. HookReturnCode ClientDisconnect(CBasePlayer@ plr)
  295. {
  296. //update player requirement
  297. float math = g_PlayerFuncs.GetNumPlayers() * 0.66;
  298. required = uint(math + 0.5);
  299. return HOOK_CONTINUE;
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement