Advertisement
Guest User

RockTheVote

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