Sabur

Vote System

Mar 13th, 2017
1,505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.24 KB | None | 0 0
  1. #include <a_samp>
  2. #include <sscanf2>
  3. #include <Pawn.CMD>
  4.  
  5. enum voteData
  6. {
  7.     Option[12],
  8.     OptionTwo[12],
  9.     VoteCount[2]
  10. };
  11. new Votes[voteData];
  12.  
  13. new
  14. VoteOn,
  15. VotedFor[MAX_PLAYERS],
  16. Voted[MAX_PLAYERS];
  17.  
  18. public OnPlayerConnect(playerid)
  19. {
  20.     Voted[playerid] = 0;
  21.     VotedFor[playerid] = 0;
  22.     return 1;
  23. }
  24.  
  25. public OnPlayerDisconnect(playerid, reason)
  26. {
  27.     if(Voted[playerid])
  28.     {
  29.         Voted[playerid] = 0;
  30.  
  31.         if(VotedFor[playerid] == 1)
  32.             Votes[VoteCount][0]--;
  33.         else
  34.             Votes[VoteCount][1]--;
  35.  
  36.         VotedFor[playerid] = 0;
  37.  
  38.     }
  39.     return 1;
  40. }
  41.  
  42. CMD:vote(playerid, params[])
  43. {
  44.     if(!VoteOn)
  45.         return SendClientMessage(playerid, -1, "There is no vote going on.");
  46.  
  47.     new string[144];
  48.  
  49.     if(Voted[playerid])
  50.     {
  51.         if(VotedFor[playerid] == 1)
  52.             format(string, sizeof(string), "You have already voted for %s, if you wish to vote for another option - /cancelvote and try voting again.", Votes[Option]);
  53.         else if(VotedFor[playerid] == 2)
  54.             format(string, sizeof(string), "You have already voted for %s, if you wish to vote for another option - /cancelvote and try voting again.", Votes[OptionTwo]);
  55.         SendClientMessage(playerid, -1, string);
  56.         return 0;
  57.     }
  58.  
  59.     if(isnull(params))
  60.     {
  61.         SendClientMessage(playerid, -1, "USAGE: /vote [option]");
  62.         format(string, sizeof(string), "Options: %s | %s", Votes[Option], Votes[OptionTwo]);
  63.         SendClientMessage(playerid, -1, string);
  64.     }
  65.  
  66.     if(!strcmp(params, Votes[Option], true))
  67.     {
  68.         if(isnull(params))
  69.             return 0;
  70.  
  71.         format(string, sizeof(string), "You have voted for %s.", Votes[Option]);
  72.         SendClientMessage(playerid, -1, string);
  73.  
  74.         Votes[VoteCount][0]++;
  75.         Voted[playerid] = 1;
  76.         VotedFor[playerid] = 1;
  77.     }
  78.     else if(!strcmp(params, Votes[OptionTwo], true))
  79.     {
  80.         if(isnull(params))
  81.             return 0;
  82.        
  83.         format(string, sizeof(string), "You have voted for %s.", Votes[OptionTwo]);
  84.         SendClientMessage(playerid, -1, string);
  85.  
  86.         Votes[VoteCount][1]++;
  87.         Voted[playerid] = 1;
  88.         VotedFor[playerid] = 2;
  89.     }
  90.     else return SendClientMessage(playerid, -1, "You did not enter any specified option.");
  91.  
  92.     return 1;
  93. }
  94.  
  95.  
  96. CMD:startvote(playerid, params[])
  97. {
  98.     if(VoteOn)
  99.         return SendClientMessage(playerid, -1, "There is already an vote going on.");
  100.  
  101.     if(!IsPlayerAdmin(playerid))
  102.         return SendClientMessage(playerid, -1, "You are not an administrator.");
  103.  
  104.     new opt1[12], opt2[12];
  105.     if(sscanf(params, "s[12]s[12]", opt1, opt2))
  106.         return SendClientMessage(playerid, -1, "USAGE: /startvote [option1] [option2]");
  107.  
  108.     if(isnull(opt1) || isnull(opt2))
  109.         return SendClientMessage(playerid, -1, "Options must have atleast 1 character.");
  110.  
  111.     if(strcmp(opt1, opt2, true) == 0)
  112.         return SendClientMessage(playerid, -1, "Option 1 and 2 can not be the same.");
  113.  
  114.     format(Votes[Option], 12, opt1);
  115.     format(Votes[OptionTwo], 12, opt2);
  116.     VoteOn = 1;
  117.  
  118.     new string[144];
  119.     format(string, sizeof(string), "Voting has started! Option: %s or %s", Votes[Option], Votes[OptionTwo]);
  120.     SendClientMessageToAll(-1, string);
  121.  
  122.     return 1;
  123. }
  124.  
  125. CMD:endvote(playerid, params[])
  126. {
  127.     if(!VoteOn)
  128.         return SendClientMessage(playerid, -1, "There is no vote going on.");
  129.  
  130.     if(!IsPlayerAdmin(playerid))
  131.     return SendClientMessage(playerid, -1, "You are not an administrator.");
  132.  
  133.     new
  134.     string[144];
  135.  
  136.     if(Votes[VoteCount][0] > Votes[VoteCount][1])
  137.     {
  138.         format(string, sizeof(string), "%s has won the vote.", Votes[Option]);
  139.         SendClientMessageToAll(-1, string);
  140.     }
  141.     else if(Votes[VoteCount][0] < Votes[VoteCount][1])
  142.     {
  143.         format(string, sizeof(string), "%s has won the vote.", Votes[OptionTwo]);
  144.         SendClientMessageToAll(-1, string);
  145.     }
  146.     else if(Votes[VoteCount][0] == Votes[VoteCount][1])
  147.     {
  148.         format(string, sizeof(string), "No one won! Votes are equal (%s | %s)", Votes[Option], Votes[OptionTwo]);
  149.         SendClientMessageToAll(-1, string);
  150.     }
  151.  
  152.     for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
  153.     {
  154.         if(Voted[i])
  155.         {
  156.             Voted[i] = 0;
  157.             VotedFor[i] = 0;
  158.         }
  159.     }
  160.  
  161.     Votes[Option] = EOS;
  162.     Votes[OptionTwo] = EOS;
  163.     Votes[VoteCount][0] = 0;
  164.     Votes[VoteCount][1] = 0;
  165.  
  166.     VoteOn = 0;
  167.     return 1;
  168. }
  169.  
  170. CMD:cancelvote(playerid, params[])
  171. {
  172.     if(!VoteOn)
  173.         return SendClientMessage(playerid, -1, "There's no vote going on.");
  174.  
  175.     if(!Voted[playerid])
  176.         return SendClientMessage(playerid, -1, "You haven't voted for anything to cancel your vote.");
  177.  
  178.     if(VotedFor[playerid] == 1)
  179.     {
  180.         Votes[VoteCount][0]--;
  181.     }
  182.     else if(VotedFor[playerid] == 2)
  183.     {
  184.         Votes[VoteCount][1]--;
  185.     }
  186.  
  187.     Voted[playerid] = 0;
  188.     VotedFor[playerid] = 0;
  189.  
  190.     SendClientMessage(playerid, -1, "You have cancelled your vote.");
  191.     return 1;
  192. }
  193.  
  194. CMD:votes(playerid)
  195. {
  196.     if(!VoteOn)
  197.         return SendClientMessage(playerid, -1, "There is no vote going on.");
  198.  
  199.     new string[144];
  200.  
  201.     SendClientMessage(playerid, -1, "|__________Number of votes_________|");
  202.     format(string, sizeof(string), "Option: %s (Votes: %d)", Votes[Option], Votes[VoteCount][0]);
  203.     SendClientMessage(playerid, -1, string);
  204.  
  205.     format(string, sizeof(string), "Option: %s (Votes: %d)", Votes[OptionTwo], Votes[VoteCount][1]);
  206.     SendClientMessage(playerid, -1, string);
  207.     return 1;
  208. }
  209.  
  210. CMD:votehelp(playerid)
  211. {
  212.     SendClientMessage(playerid, -1, "Vote Help: /vote /cancelvote /votes");
  213.  
  214.     if(IsPlayerAdmin(playerid))
  215.     {
  216.         SendClientMessage(playerid, -1, "Admin: /startvote /endvote");
  217.     }
  218.     return 1;
  219. }
Add Comment
Please, Sign In to add comment