elections.pwn 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. /$$ /$$ /$$$$$$ /$$$$$$$ /$$$$$$$
  3. | $$$ | $$ /$$__ $$ | $$__ $$| $$__ $$
  4. | $$$$| $$| $$ \__/ | $$ \ $$| $$ \ $$
  5. | $$ $$ $$| $$ /$$$$ /$$$$$$| $$$$$$$/| $$$$$$$/
  6. | $$ $$$$| $$|_ $$|______/| $$__ $$| $$____/
  7. | $$\ $$$| $$ \ $$ | $$ \ $$| $$
  8. | $$ \ $$| $$$$$$/ | $$ | $$| $$
  9. |__/ \__/ \______/ |__/ |__/|__/
  10. Election System
  11. Jingles
  12. Next Generation Gaming, LLC
  13. (created by Next Generation Gaming Development Team)
  14. * Copyright (c) 2016, Next Generation Gaming, LLC
  15. *
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without modification,
  19. * are not permitted in any case.
  20. *
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  26. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <YSI\y_hooks>
  35. hook OnGameModeInit() {
  36. LoadElections();
  37. }
  38. hook OnGameModeExit() {
  39. UnloadElections();
  40. }
  41. LoadElections() {
  42. SetGVarInt("ElectionArea", CreateDynamicSphere(366.54, 159.09, 1008.38, 100));
  43. return 1;
  44. }
  45. UnloadElections() {
  46. DestroyDynamicArea(GetGVarInt("ElectionArea"));
  47. DeleteGVar("ElectionArea");
  48. }
  49. hook OnPlayerEnterDynamicArea(playerid, areaid) {
  50. if(areaid == GetGVarInt("ElectionArea")) SendClientMessage(playerid, COLOR_WHITE, "You have entered an election area - press ~k~~CONVERSATION_YES~ to vote");
  51. return 1;
  52. }
  53. hook OnPlayerKeyStateChange(playerid, newkeys, oldkeys) {
  54. if(newkeys & KEY_YES && IsPlayerInDynamicArea(playerid, GetGVarInt("ElectionArea"))) {
  55. ShowElectionMenu(playerid);
  56. }
  57. return 1;
  58. }
  59. hook OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  60. if(arrAntiCheat[playerid][ac_iFlags][AC_DIALOGSPOOFING] > 0) return 1;
  61. szMiscArray[0] = 0;
  62. switch(dialogid) {
  63. case ELECTIONS: {
  64. szMiscArray[0] = 0;
  65. if(!response) return SendClientMessage(playerid, COLOR_WHITE, "You have not cast a vote");
  66. if(strcmp(inputtext, "Add Candidate") == 0) {
  67. if(PlayerInfo[playerid][pAdmin] < 1337) return SendClientMessage(playerid, COLOR_GREY, "You're not an admin.");
  68. return ShowPlayerDialogEx(playerid, ELECTIONS_ADD, DIALOG_STYLE_INPUT, "Add candidate", "Please enter the candidates name", "Select", "Cancel");
  69. }
  70. if(strcmp(inputtext, "Remove Candidate") == 0) {
  71. if(PlayerInfo[playerid][pAdmin] < 1337) return SendClientMessage(playerid, COLOR_GREY, "You're not an admin.");
  72. new
  73. szTemp[MAX_PLAYER_NAME],
  74. iCount = GetGVarInt("CandidateCount");
  75. for(new i = 0; i < iCount; ++i) {
  76. szTemp[0] = 0;
  77. GetGVarString("CandidateName", szTemp, sizeof(szTemp), i);
  78. format(szMiscArray, sizeof(szMiscArray), "%s\n%s", szMiscArray, szTemp);
  79. }
  80. return ShowPlayerDialogEx(playerid, ELECTIONS_REMOVE, DIALOG_STYLE_LIST, "Gov Elections", szMiscArray, "Select", "Cancel");
  81. }
  82. if(strcmp(inputtext, "Start Elections") == 0) {
  83. if(PlayerInfo[playerid][pAdmin] < 1337) return SendClientMessage(playerid, COLOR_GREY, "You're not an admin.");
  84. SendClientMessageToAll(COLOR_WHITE, "[ELECTION NEWS] The elections have been started.");
  85. SendClientMessageToAll(COLOR_WHITE, "[ELECTION NEWS] Head over to your local city hall to vote!");
  86. SetGVarInt("ElectionActive", 1);
  87. return 1;
  88. }
  89. if(strcmp(inputtext, "End Elections") == 0) {
  90. if(PlayerInfo[playerid][pAdmin] < 1337) return SendClientMessage(playerid, COLOR_GREY, "You're not an admin.");
  91. DeleteGVar("ElectionActive");
  92. return CountVotes(playerid);
  93. }
  94. if(listitem <= GetGVarInt("CandidateCount")) {
  95. return CastVote(playerid, listitem);
  96. }
  97. }
  98. case ELECTIONS_ADD: {
  99. if(!response || isnull(inputtext) || IsNumeric(inputtext)) return 1;
  100. if(PlayerInfo[playerid][pAdmin] < 1337) return SendClientMessage(playerid, COLOR_GREY, "You're not an admin.");
  101. new iOptionID = GetGVarInt("CandidateCount");
  102. SetGVarString("CandidateName", inputtext, iOptionID);
  103. iOptionID++;
  104. SetGVarInt("CandidateCount", iOptionID);
  105. format(szMiscArray, sizeof(szMiscArray), "You added %s as candidate no. %d", inputtext, iOptionID);
  106. SendClientMessageEx(playerid, COLOR_YELLOW, szMiscArray);
  107. format(szMiscArray, sizeof(szMiscArray), "%s added %s as election candidate no. %d", GetPlayerNameExt(playerid), inputtext, iOptionID);
  108. Log("logs/elections.log", szMiscArray);
  109. }
  110. case ELECTIONS_REMOVE: {
  111. if(!response) return 1;
  112. if(PlayerInfo[playerid][pAdmin] < 1337) return SendClientMessage(playerid, COLOR_GREY, "You're not an admin.");
  113. new szName[MAX_PLAYER_NAME],
  114. iCount = GetGVarInt("CandidateCount");
  115. GetGVarString("CandidateName", szName, sizeof(szName), listitem);
  116. DeleteGVar("CandidateName", listitem);
  117. SetGVarInt("CandidateCount", iCount - 1);
  118. format(szMiscArray, sizeof(szMiscArray), "You removed %s as candidate no. %d", szName, listitem);
  119. SendClientMessageEx(playerid, COLOR_YELLOW, szMiscArray);
  120. format(szMiscArray, sizeof(szMiscArray), "%s removed %s as election candidate no. %d", GetPlayerNameExt(playerid), szName, listitem);
  121. Log("logs/elections.log", szMiscArray);
  122. }
  123. }
  124. return 0;
  125. }
  126. CastVote(playerid, iOptionID) {
  127. szMiscArray[0] = 0;
  128. if(PlayerInfo[playerid][pNation] != 0) return SendClientMessage(playerid, COLOR_GREY, "You're not a San Andreas Citizen.");
  129. mysql_format(MainPipeline, szMiscArray, sizeof(szMiscArray), "SELECT * FROM `electionresults` WHERE `ip` = '%s'", PlayerInfo[playerid][pIP]);
  130. mysql_tquery(MainPipeline, szMiscArray, "OnIPVoteCheck", "ii", playerid, iOptionID);
  131. return 1;
  132. }
  133. forward OnIPVoteCheck(playerid, iOptionID);
  134. public OnIPVoteCheck(playerid, iOptionID)
  135. {
  136. new iRows;
  137. cache_get_row_count(iRows);
  138. szMiscArray[0] = 0;
  139. if(iRows > 0) {
  140. return SendClientMessageEx(playerid, COLOR_WHITE, "You have voted already!");
  141. } else {
  142. mysql_format(MainPipeline, szMiscArray, sizeof(szMiscArray), "SELECT * FROM `electionresults` WHERE `accountid` = '%d'", PlayerInfo[playerid][pId]);
  143. mysql_tquery(MainPipeline, szMiscArray, "OnCastVote", "ii", playerid, iOptionID);
  144. }
  145. return 1;
  146. }
  147. forward OnCastVote(playerid, iOptionID);
  148. public OnCastVote(playerid, iOptionID) {
  149. new iRows;
  150. cache_get_row_count(iRows);
  151. szMiscArray[0] = 0;
  152. if(iRows > 0) {
  153. return SendClientMessageEx(playerid, COLOR_WHITE, "You have voted already!");
  154. }
  155. else {
  156. mysql_format(MainPipeline, szMiscArray, sizeof(szMiscArray), "INSERT INTO `electionresults` (`accountid`, `optionid`, `ip`) VALUES ('%d', '%d', '%s')", PlayerInfo[playerid][pId], iOptionID, PlayerInfo[playerid][pIP]);
  157. mysql_tquery(MainPipeline, szMiscArray, "OnFinaliseVote", "ii", playerid, iOptionID);
  158. }
  159. return 1;
  160. }
  161. forward OnFinaliseVote(playerid, iOptionID);
  162. public OnFinaliseVote(playerid, iOptionID) {
  163. if(!mysql_errno(MainPipeline)) SendClientMessage(playerid, COLOR_WHITE, "You have cast your vote!");
  164. return 1;
  165. }
  166. CountVotes(playerid) {
  167. if(GetGVarInt("CandidateCount") == 0) return SendClientMessageEx(playerid, COLOR_RED, "There are no candidates, therefore no count can be made.");
  168. else mysql_tquery(MainPipeline, "SELECT `optionid` FROM `electionresults`", "OnCountVotes", "i", playerid);
  169. return 1;
  170. }
  171. forward OnCountVotes(playerid);
  172. public OnCountVotes(playerid) {
  173. new
  174. iRows,
  175. iCount,
  176. iTemp,
  177. szTemp[MAX_PLAYER_NAME],
  178. iCandidates = GetGVarInt("CandidateCount");
  179. szMiscArray[0] = 0;
  180. cache_get_row_count(iRows);
  181. while(iCount < iRows) {
  182. cache_get_value_name_int(iCount, "optionid", iTemp);
  183. SetGVarInt("Results", GetGVarInt("Results", iTemp)+1, iTemp);
  184. iCount++;
  185. }
  186. SendClientMessageToAll(COLOR_WHITE, "[ELECTION NEWS] Ladies and gentlemen, the elections have concluded and here are the results:");
  187. for(new i = 0; i < iCandidates; ++i) {
  188. GetGVarString("CandidateName", szTemp, sizeof(szTemp), i);
  189. format(szMiscArray, sizeof(szMiscArray), "%s - %d votes.", szTemp, GetGVarInt("Results", i));
  190. SendClientMessageToAll(COLOR_WHITE, szMiscArray);
  191. }
  192. return 1;
  193. }
  194. ShowElectionMenu(playerid) {
  195. if(PlayerInfo[playerid][pConnectHours] < 32 && PlayerInfo[playerid][pAdmin] < 1337) return SendClientMessage(playerid, COLOR_WHITE, "You have not played enough to vote.");
  196. szMiscArray[0] = 0;
  197. new
  198. szTemp[MAX_PLAYER_NAME],
  199. iCount = GetGVarInt("CandidateCount");
  200. for(new i = 0; i < iCount; i++) {
  201. szTemp[0] = 0;
  202. GetGVarString("CandidateName", szTemp, sizeof(szTemp), i);
  203. format(szMiscArray, sizeof(szMiscArray), "%s\n%s", szMiscArray, szTemp);
  204. }
  205. if(PlayerInfo[playerid][pAdmin] >= 1337 && !GetGVarType("ElectionActive")) strcat(szMiscArray, "\nAdd Candidate\nRemove Candidate\nStart Elections");
  206. if(PlayerInfo[playerid][pAdmin] >= 1337 && GetGVarType("ElectionActive")) strcat(szMiscArray, "\nEnd Elections");
  207. return ShowPlayerDialogEx(playerid, ELECTIONS, DIALOG_STYLE_LIST, "Gov Elections", szMiscArray, "Select", "Close");
  208. }
  209. CMD:elections(playerid, params[]) {
  210. if(PlayerInfo[playerid][pAdmin] < 1337) return SendClientMessage(playerid, COLOR_WHITE, "You are not authorized to use this command.");
  211. switch(GetGVarInt("ElectionActive")) {
  212. case 0: {
  213. SendClientMessageEx(playerid, COLOR_YELLOW, "You have enabled the elections.");
  214. return SetGVarInt("ElectionActive", 1);
  215. }
  216. case 1: {
  217. SendClientMessageEx(playerid, COLOR_YELLOW, "You have disabled the elections.");
  218. return DeleteGVar("ElectionActive");
  219. }
  220. }
  221. return 1;
  222. }