Advertisement
Guest User

RockTheVote Final

a guest
Jun 2nd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.45 KB | None | 0 0
  1. //constants
  2. const uint RTV_LIMIT = 5;
  3. const float percentage = 0.66;
  4.  
  5. //variables
  6. int counter;
  7. int required;
  8. bool voteStarted;
  9. array<string> g_RockTheVote;
  10. array<string> g_nomination;
  11. array<string> g_voting;
  12. array<string> rtvList;
  13. array<string> mapList;
  14. array<string> twoMaps;
  15. array<uint> voting;
  16. string chosenMap;
  17.  
  18. void PluginInit()
  19. {
  20. g_Module.ScriptInfo.SetAuthor("Some Faggot");
  21. g_Module.ScriptInfo.SetContactInfo("asdf");
  22. g_Hooks.RegisterHook(Hooks::Player::ClientSay, @ClientSay);
  23. g_Hooks.RegisterHook(Hooks::Player::ClientConnected, @ClientConnected);
  24. g_Hooks.RegisterHook(Hooks::Player::ClientDisconnect, @ClientDisconnect);
  25. g_Hooks.RegisterHook(Hooks::Player::ClientPutInServer, @ClientPutInServer);
  26. }
  27.  
  28. void MapInit()
  29. {
  30. //reset variables
  31. chosenMap = "";
  32. voteStarted = false;
  33. counter = 0;
  34. required = -1; //not set until a player connects
  35. g_RockTheVote.resize(0);
  36. g_nomination.resize(0);
  37. g_voting.resize(0);
  38. rtvList.resize(0);
  39. twoMaps.resize(0);
  40. mapList = g_MapCycle.GetMapCycle();
  41. voting.resize(RTV_LIMIT);
  42. for(uint i = 0; i < RTV_LIMIT; i++)
  43. {
  44. voting[i] = 0;
  45. }
  46. }
  47.  
  48. CClientCommand rtv("rtv", "Rock the Vote Menu", @rtvCmd);
  49.  
  50. void rtvCmd(const CCommand@ args)
  51. {
  52. CBasePlayer@ plr = g_ConCommandSystem.GetCurrentPlayer();
  53.  
  54. }
  55.  
  56. bool doCommand(CBasePlayer@ plr, const CCommand@ args)
  57. {
  58. const string steamId = g_EngineFuncs.GetPlayerAuthId(plr.edict());
  59. //maps output to console
  60. if(args[0] == "maplist" && args.ArgC() == 1)
  61. {
  62. g_PlayerFuncs.SayText(plr, "Maps outputted to console.\n");
  63. g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "Maps in MapCycle:\n");
  64. string returnMaps = "";
  65. for(uint i = 0; i < mapList.length(); i++)
  66. {
  67. returnMaps += mapList[i] + " ";
  68. if(i % 9 == 0)
  69. {
  70. g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, returnMaps);
  71. returnMaps = "";
  72. }
  73. }
  74. g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "\n");
  75. return true;
  76. }
  77. if(args.ArgC() > 0 && !voteStarted)
  78. {
  79. if(args.ArgC() == 1 && (args[0] == "rtv" || args[0] == "rockthevote"))
  80. {
  81. //check if unique player
  82. if(g_RockTheVote.find(steamId) < 0)
  83. {
  84. g_RockTheVote.insertLast(steamId);
  85. counter++;
  86. g_PlayerFuncs.SayText(plr, "Rocked the vote.\n");
  87. }
  88. else
  89. {
  90. g_PlayerFuncs.SayText(plr, "Already rocked the vote!\n");
  91. }
  92. g_PlayerFuncs.SayText(plr, "" + counter + " out of " + required + " required RTV have rocked the vote!\n");
  93. //start rtv if possible
  94. if(counter >= required && !voteStarted)
  95. {
  96. startVote(plr);
  97. }
  98. return true;
  99. }
  100. if((args[0] == "nominate" || args[0] == "nom") && args.ArgC() == 2)
  101. {
  102. string plr_map = args[1];
  103. //g_PlayerFuncs.SayText(plr, "Voting for " + plr_map + "\n");
  104. //check if mapcycle is valid
  105. if(g_MapCycle.Count() <= 0)
  106. {
  107. g_PlayerFuncs.SayText(plr, "MapCycle invalid!\n");
  108. }
  109. //check if player already nominated
  110. else if(g_nomination.find(steamId) >= 0)
  111. {
  112. g_PlayerFuncs.SayText(plr, "Already nominated!\n");
  113. }
  114. //check if argument is valid map
  115. else if(mapList.find(plr_map) < 0)
  116. {
  117. g_PlayerFuncs.SayText(plr, "Map not found!\n");
  118. }
  119. //check if list is not full
  120. else if(rtvList.length() == RTV_LIMIT)
  121. {
  122. g_PlayerFuncs.SayText(plr, "Rock The Vote list is full!\n");
  123. }
  124. //add map if not already in list
  125. else if(rtvList.find(plr_map) < 0)
  126. {
  127. rtvList.insertLast(plr_map);
  128. g_nomination.insertLast(steamId);
  129. g_PlayerFuncs.SayText(plr, "Added " + plr_map +".\n");
  130. }
  131. else
  132. {
  133. g_PlayerFuncs.SayText(plr, "Map already included!\n");
  134. }
  135. return true;
  136. }
  137. }
  138. if(voteStarted && args.ArgC() == 2 && args[0] == "rtv")
  139. {
  140. string option = args[1];
  141. int option_int = intParser(option);
  142. if(option_int > 0 && option_int <= RTV_LIMIT)
  143. {
  144. //add the vote
  145. if(g_voting.find(steamId) < 0)
  146. {
  147. g_voting.insertLast(steamId);
  148. voting[option_int - 1]++;
  149. g_PlayerFuncs.SayText(plr, "Voted for " + rtvList[option_int - 1] + ".\n");
  150. }
  151. else
  152. {
  153. g_PlayerFuncs.SayText(plr, "Already voted!\n");
  154. }
  155. }
  156. else
  157. {
  158. g_PlayerFuncs.SayText(plr, "Invalid rtv choice!\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. //helper method, does not check for overflows
  170. int intParser(string str)
  171. {
  172. //LOL SWITCH-CASE DOES NOT SUPPORT CHAR AS INTEGRAL TYPE AND CAN'T CONVERT CHAR TO INT, I HATE ANGELSCRIPT
  173. int str_toInt = 0;
  174. uint str_size = str.Length();
  175. uint uint_add = 0;
  176. bool invalid_str = false;
  177. for(uint i = 0; i < str_size && !invalid_str; i++)
  178. {
  179. char str_char = str.opIndex(i);
  180. if(str_char == '0')
  181. {
  182. uint_add = 0;
  183. }
  184. else if(str_char == '1')
  185. {
  186. uint_add = 1;
  187. }
  188. else if(str_char == '2')
  189. {
  190. uint_add = 2;
  191. }
  192. else if(str_char == '3')
  193. {
  194. uint_add = 3;
  195. }
  196. else if(str_char == '4')
  197. {
  198. uint_add = 4;
  199. }
  200. else if(str_char == '5')
  201. {
  202. uint_add = 5;
  203. }
  204. else if(str_char == '6')
  205. {
  206. uint_add = 6;
  207. }
  208. else if(str_char == '7')
  209. {
  210. uint_add = 7;
  211. }
  212. else if(str_char == '8')
  213. {
  214. uint_add = 8;
  215. }
  216. else if(str_char == '9')
  217. {
  218. uint_add = 9;
  219. }
  220. else
  221. {
  222. invalid_str = true;
  223. }
  224. if(invalid_str)
  225. {
  226. str_toInt = -1;
  227. }
  228. else
  229. {
  230. str_toInt += int(uint_add) * power(10, str_size - 1 - i);
  231. }
  232. }
  233. return str_toInt;
  234. }
  235.  
  236. //exponent helper method
  237. int power(int base, int exponent)
  238. {
  239. if(exponent == 0)
  240. {
  241. return 1;
  242. }
  243. int modulo = exponent % 2;
  244. int temp = 0;
  245. switch(modulo)
  246. {
  247. case 0:
  248. temp = power(base, exponent >>> 1);
  249. return temp * temp;
  250. default:
  251. return base * power(base, exponent - 1);
  252. }
  253. return -1;
  254. }
  255.  
  256. void listmaps(CBasePlayer@ plr)
  257. {
  258. uint rtvList_size = rtvList.length();
  259. if(rtvList_size > 0)
  260. {
  261. string text_maplist = "Maps in Rock The Vote list:";
  262. for(uint i = 0; i < rtvList_size - 1; i++)
  263. {
  264. text_maplist += " " + rtvList[i] + ",";
  265. }
  266. text_maplist += " " + rtvList[rtvList_size - 1];
  267. g_PlayerFuncs.SayTextAll(plr, text_maplist + "\n");
  268. }
  269. else
  270. {
  271. g_PlayerFuncs.SayTextAll(plr, "No maps have been nominated!\n");
  272. }
  273. }
  274.  
  275. void startVote(CBasePlayer@ plr)
  276. {
  277. if(rtvList.length() < RTV_LIMIT)
  278. {
  279. addRandomMaps();
  280. }
  281. g_PlayerFuncs.SayTextAll(plr, "Enough players have rocked the vote.\n");
  282. voteStarted = true;
  283. g_PlayerFuncs.SayTextAll(plr, "There is 20 seconds to vote.\n");
  284. listmaps(plr);
  285. g_PlayerFuncs.SayTextAll(plr, "Type \"rtv [number in respect to map listed]\" to vote for a particular map\n");
  286. g_Scheduler.SetTimeout("vote", 20, @plr);
  287. }
  288.  
  289. void vote(CBasePlayer@ plr)
  290. {
  291. //get two of the highest voted maps
  292. uint highest = 0;
  293. int highestIndex = -1;
  294. uint secondHighest = 0;
  295. int secondHighestIndex = -1;
  296. for(uint i = 0; i < voting.length(); i++)
  297. {
  298. if(voting[i] != 0 && voting[i] >= highest)
  299. {
  300. secondHighest = highest;
  301. secondHighestIndex = highestIndex;
  302. highest = voting[i];
  303. highestIndex = i;
  304. }
  305. }
  306. //no one voted
  307. if(highestIndex == -1 && secondHighestIndex == -1)
  308. {
  309. g_PlayerFuncs.SayTextAll(plr, "No one voted, choosing map randomly.\n");
  310. uint randomMap = Math.RandomLong(0, rtvList.length() - 1);
  311. chosenMap = rtvList[randomMap];
  312. }
  313. //only one voted
  314. else if(secondHighestIndex == -1 && highestIndex != -1)
  315. {
  316. chosenMap = rtvList[highestIndex];
  317. }
  318. //more than one voted
  319. else
  320. {
  321. //tiebreaker
  322. if(highest == secondHighest)
  323. {
  324. g_PlayerFuncs.SayTextAll(plr, "Tie detected, choosing randomly between: " + rtvList[highestIndex] + " and "
  325. + rtvList[secondHighestIndex] + "\n");
  326. twoMaps.insertLast(rtvList[highestIndex]);
  327. twoMaps.insertLast(rtvList[secondHighestIndex]);
  328. uint randomTieBreaker = Math.RandomLong(0, 1);
  329. chosenMap = twoMaps[randomTieBreaker];
  330. }
  331. else
  332. {
  333. chosenMap = rtvList[highestIndex];
  334. }
  335. }
  336. //debugging
  337. /*
  338. g_PlayerFuncs.SayTextAll(plr, "voting length: " + voting.length() + "\n");
  339. for(uint k = 0; k < voting.length(); k++)
  340. {
  341. g_PlayerFuncs.SayTextAll(plr, rtvList[k] + " " + voting[k] + "\n");
  342. }
  343. */
  344. g_PlayerFuncs.SayTextAll(plr, "Changing to " + chosenMap + " in 5 seconds....\n");
  345. g_Scheduler.SetTimeout("changeChosenMap", 5);
  346. }
  347.  
  348. void changeChosenMap()
  349. {
  350. g_EngineFuncs.ChangeLevel(chosenMap);
  351. }
  352.  
  353. void addRandomMaps()
  354. {
  355. //duplicate maps are not checked for performance reasons
  356. uint rtvList_size = rtvList.length();
  357. uint mapList_size = mapList.length();
  358. for(uint i = 0; i < RTV_LIMIT - rtvList_size; i++)
  359. {
  360. rtvList.insertLast(mapList[Math.RandomLong(0, mapList_size - 1)]);
  361. }
  362. }
  363.  
  364. HookReturnCode ClientSay(SayParameters@ pParams)
  365. {
  366. CBasePlayer@ plr = pParams.GetPlayer();
  367. const CCommand@ args = pParams.GetArguments();
  368.  
  369. if(doCommand(plr, args))
  370. {
  371. pParams.ShouldHide = true;
  372. return HOOK_HANDLED;
  373. }
  374.  
  375. return HOOK_CONTINUE;
  376. }
  377.  
  378. //first player joined
  379. HookReturnCode ClientPutInServer(CBasePlayer@ plr)
  380. {
  381. float math = g_PlayerFuncs.GetNumPlayers() * percentage;
  382. required = uint(math + 0.5);
  383. return HOOK_CONTINUE;
  384. }
  385.  
  386. //player joined
  387. HookReturnCode ClientConnected(edict_t@, const string& in, const string& in, bool& out, string& out)
  388. {
  389. //update rtv player requirement
  390. float math = g_PlayerFuncs.GetNumPlayers() * percentage;
  391. required = uint(math + 0.5);
  392. return HOOK_CONTINUE;
  393. }
  394.  
  395. //player left
  396. HookReturnCode ClientDisconnect(CBasePlayer@ plr)
  397. {
  398. //update player requirement
  399. float math = g_PlayerFuncs.GetNumPlayers() * percentage;
  400. required = uint(math + 0.5);
  401. return HOOK_CONTINUE;
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement