Advertisement
Hattiwatti

VoteChange

Apr 7th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.72 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Collections.Generic;
  6. using System.Collections;
  7. using System.Net;
  8. using System.Web;
  9. using System.Data;
  10. using System.Threading;
  11. using System.Security;
  12. using System.Timers;
  13. using System.Diagnostics;
  14. using System.Reflection;
  15. using System.Runtime.InteropServices;
  16. using System.Windows.Forms;
  17.  
  18. using PRoCon.Core;
  19. using PRoCon.Core.Plugin;
  20. using PRoCon.Core.Plugin.Commands;
  21. using PRoCon.Core.Players;
  22. using PRoCon.Core.Players.Items;
  23. using PRoCon.Core.Battlemap;
  24. using PRoCon.Core.Maps;
  25.  
  26.  
  27. namespace PRoConEvents
  28. {
  29.     using EventType = PRoCon.Core.Events.EventType;
  30.     using CapturableEvent = PRoCon.Core.Events.CapturableEvents;
  31.    
  32.     public class VoteChange : PRoConPluginAPI, IPRoConPluginInterface
  33.     {
  34.         double votePercentage = 0.66;
  35.         int playerCount = 0;
  36.         string cmdthing = "";
  37.         List<string[]> Votes = new List<string[]>();
  38.         List<string[]> teamVotes = new List<string[]>();
  39.        
  40.         string[,] Maps = new string[,] { {"Zavod 311", "MP_Abandoned"},
  41.                                         {"Lancang Dam", "MP_Damage"},
  42.                                         {"Flood Zone", "MP_Flooded"},
  43.                                         {"Golmud Railway", "MP_Journey"},
  44.                                         {"Paracel Storm", "MP_Naval"},
  45.                                         {"Operation Locker", "MP_Prison"},
  46.                                         {"Hainan Resort", "MP_Resort"},
  47.                                         {"Siege of Shanghai", "MP_Siege"},
  48.                                         {"Rogue Transmission", "MP_TheDish"},
  49.                                         {"Dawnbreaker", "MP_Tremors"},
  50.                                         {"Silk Road", "XP1_001"},
  51.                                         {"Altai Range", "XP1_002"},
  52.                                         {"Guilin Peaks", "XP1_003"},
  53.                                         {"Dragon Pass", "XP1_004"},
  54.                                         {"Caspian Border", "XP0_Caspian"},
  55.                                         {"Operation Firestorm", "XP0_Firestorm"},
  56.                                         {"Operation Metro", "XP0_Metro"},
  57.                                         {"Gulf of Oman", "XP0_Oman"},
  58.                                         {"Lost Islands", "XP2_001"},
  59.                                         {"Nansha Strike", "XP2_002"},
  60.                                         {"Wavebreaker", "XP2_003"},
  61.                                         {"Operation Mortar", "XP2_004"},
  62.                                         {"Propaganda", "XP3_Prpganda"},
  63.                                         {"Sunken Dragon", "XP3_WtrFront"},
  64.                                         {"Pearl Market", "XP3_MarketPl"},
  65.                                         {"Lumphini Garden", "XP3_UrbanGdn"},
  66.                                         {"Operation Whiteout", "XP4_Arctic"},
  67.                                         {"Hammerhead", "XP4_SubBase"},
  68.                                         {"Hangar 21", "XP4_Titan"},
  69.                                         {"Giants of Karelia", "XP4_Wlkrftry"}
  70.                                     };
  71.        
  72.         public VoteChange()
  73.         {
  74.         }
  75.  
  76.         public void ServerCommand(params String[] args)
  77.         {
  78.             List<String> list = new List<String>();
  79.             list.Add("procon.protected.send");
  80.             list.AddRange(args);
  81.             this.ExecuteCommand(list.ToArray());
  82.         }
  83.        
  84.         bool isVoteCorrect(string vote, string speaker)
  85.         {
  86.             bool alreadyFound = false;
  87.             for(int i = 0; i < 30; i++)
  88.             {
  89.                 bool contains = Maps[i,0].ToLower().Contains(vote);
  90.                 if(contains)
  91.                 {
  92.                     if(!alreadyFound)
  93.                     {
  94.                         alreadyFound = true;
  95.                     }
  96.                     else
  97.                     {
  98.                         ServerCommand("admin.say", "Found more than one map. Please be more specific!", "player", speaker);
  99.                         return false;
  100.                     }
  101.                 }
  102.             }
  103.             if(alreadyFound)
  104.             {
  105.                 return true;
  106.             }
  107.             return false;
  108.         }
  109.  
  110.         string[] getMap(string vote)
  111.         {
  112.             string[] mapInfo = new string[2];
  113.             for(int i = 0; i < Maps.Length; i++)
  114.             {
  115.                 bool contains = Maps[i,0].ToLower().Contains(vote);
  116.                 if(contains)
  117.                 {
  118.                     mapInfo[0] = Maps[i,0];
  119.                     mapInfo[1] = Maps[i,1];
  120.                     return mapInfo;
  121.                 }
  122.             }
  123.             return mapInfo;
  124.         }
  125.  
  126.         float countVotes(string map)
  127.         {
  128.             float votes = 0;
  129.             foreach(string[] vote in Votes)
  130.             {
  131.                 if(vote[1] == map)
  132.                 {
  133.                     //ServerCommand("admin.say", "Vote found", "all");
  134.                     votes += 1;
  135.                 }
  136.             }
  137.  
  138.             return votes;
  139.         }
  140.  
  141.         int hasVoted(string speaker)
  142.         {
  143.             int index = 0;
  144.             foreach(string[] vote in Votes)
  145.             {
  146.                 if(vote[0] == speaker)
  147.                     return index;
  148.                 index++;
  149.             }
  150.             return -1;
  151.         }
  152.  
  153.         bool teamVoteIsCorrect(string arg1, string arg2)
  154.         {
  155.             if(!arg1.Equals("US", StringComparison.InvariantCultureIgnoreCase) && !arg1.Equals("CH", StringComparison.InvariantCultureIgnoreCase) && !arg1.Equals("RU", StringComparison.InvariantCultureIgnoreCase))
  156.                 return false;
  157.             if(!arg2.Equals("US", StringComparison.InvariantCultureIgnoreCase) && !arg2.Equals("CH", StringComparison.InvariantCultureIgnoreCase) && !arg2.Equals("RU", StringComparison.InvariantCultureIgnoreCase))
  158.                 return false;
  159.             return true;
  160.         }
  161.  
  162.         int teamVoteStringToID(string team)
  163.          {
  164.             if(team.Equals("us", StringComparison.InvariantCultureIgnoreCase))
  165.                 return 0;
  166.             if(team.Equals("ru", StringComparison.InvariantCultureIgnoreCase))
  167.                 return 1;
  168.             if(team.Equals("ch", StringComparison.InvariantCultureIgnoreCase))
  169.                 return 2;
  170.             else
  171.                 return -1;
  172.          }
  173.  
  174.          int countTeamVotes(string team1, string team2)
  175.          {
  176.             int votes = 0;
  177.             foreach(string[] vote in teamVotes)
  178.             {
  179.                 if(vote[1] == team1 && vote[2] == team2)
  180.                     votes++;
  181.             }
  182.             return votes;
  183.          }
  184.  
  185.         void parseMessage(string speaker, string message)
  186.         {
  187.             string[] arguments = message.Split(' ');
  188.             if (arguments[0].Equals("!votechange", StringComparison.InvariantCultureIgnoreCase))
  189.             {
  190.                     string vote = "";
  191.                     for(int i=1; i<arguments.Length; i++)
  192.                     {
  193.                         if(i == arguments.Length-1)
  194.                             vote += arguments[i].ToLower();
  195.                         else
  196.                             vote += arguments[i].ToLower() + " ";
  197.                     }
  198.                    
  199.                     if (isVoteCorrect(vote, speaker))
  200.                     {
  201.                         string[] Map = getMap(vote);
  202.                         int voteIndex = hasVoted(speaker);
  203.                         if(voteIndex < 0)
  204.                         {
  205.                             ServerCommand("admin.say", speaker + " voted for " + Map[0], "all");
  206.                             string[] voteToAdd = new string[] { speaker, Map[0] };
  207.                             Votes.Add(voteToAdd);
  208.                         }
  209.                         else
  210.                         {
  211.                             ServerCommand("admin.say", speaker + " changed vote to " + Map[0], "all");
  212.                             Votes[voteIndex][1] = Map[0];
  213.                         }
  214.                         float voteCount = countVotes(Map[0]);
  215.                         float players = (float)playerCount;
  216.                         //ServerCommand("admin.say", "Votecount: " + voteCount.ToString() + " Players: " + players.ToString(), "all");
  217.                         double ratio = voteCount/players;
  218.                         if (ratio >= votePercentage)
  219.                         {
  220.                             Votes.Clear();
  221.                             ServerCommand("mapList.clear");
  222.                             ServerCommand("admin.say", (Math.Round(votePercentage,2)*100).ToString() + "% exceeded. Changing map to " + Map[0] + " in 10 seconds", "all");
  223.                             this.ExecuteCommand("procon.protected.tasks.add", "2", "1", "1", "procon.protected.send", "mapList.add", Map[1], "ConquestLarge0", "1");
  224.                             this.ExecuteCommand("procon.protected.tasks.add", "5", "1", "1", "procon.protected.send", "mapList.save");
  225.                             this.ExecuteCommand("procon.protected.tasks.add", "10", "1", "1", "procon.protected.send", "mapList.runNextRound");
  226.                         }
  227.                         else
  228.                             ServerCommand("admin.say", Math.Round((float)(ratio*100), 2).ToString() + "% of the players have voted for " + Map[0], "all");
  229.                     }
  230.                     else
  231.                         ServerCommand("admin.say", "No maps corresponding to your vote were found", "player", speaker);
  232.                
  233.             }
  234.             else if (arguments[0].Equals("!revote", StringComparison.InvariantCultureIgnoreCase))
  235.             {
  236.                 foreach (string[] vote in Votes)
  237.                 {
  238.                     if (vote[0] == speaker)
  239.                     {
  240.                         Votes.Remove(vote);
  241.                         ServerCommand("admin.say", "Vote removed", "player", speaker);
  242.                         return;
  243.                     }
  244.                 }
  245.                 ServerCommand("admin.say", "You haven't voted for a map", "player", speaker);
  246.             }
  247.             else if(arguments[0].Equals("!voteteams", StringComparison.InvariantCultureIgnoreCase))
  248.             {
  249.                 if(arguments.Length == 3)
  250.                 {
  251.                     if(teamVoteIsCorrect(arguments[1].ToLower(), arguments[2].ToLower()))
  252.                     {
  253.                         string[] voteToAdd = new string[] {speaker, arguments[1].ToLower(), arguments[2].ToLower()};
  254.                         teamVotes.Add(voteToAdd);
  255.                         float voteCount = countTeamVotes(arguments[1].ToLower(), arguments[2].ToLower());
  256.                         float players = (float)playerCount;
  257.                         double ratio = voteCount/players;
  258.                         if (ratio >= votePercentage)
  259.                         {
  260.                             teamVotes.Clear();
  261.                             ServerCommand("admin.say", (Math.Round(votePercentage,2)*100).ToString() + "% exceeded. Changing teams to " + arguments[1].ToUpper() + " vs. " + arguments[2].ToUpper() + " in 10 seconds", "all");
  262.                             this.ExecuteCommand("procon.protected.tasks.add", "2", "1", "1", "procon.protected.send", "vars.teamFactionOverride", "1", teamVoteStringToID(arguments[1]).ToString());
  263.                             this.ExecuteCommand("procon.protected.tasks.add", "2", "1", "1", "procon.protected.send", "vars.teamFactionOverride", "2", teamVoteStringToID(arguments[2]).ToString());
  264.                             this.ExecuteCommand("procon.protected.tasks.add", "10", "1", "1", "procon.protected.send", "mapList.restartRound");
  265.                         }
  266.                         else
  267.                             ServerCommand("admin.say", Math.Round((float)(ratio*100), 2).ToString() + "% of the players have voted for " + arguments[1].ToUpper() + " vs. " + arguments[2].ToUpper(), "all");
  268.                     }
  269.                     else
  270.                         ServerCommand("admin.say", "Your vote has invalid arguments. Use US, CH or RU.", "player", speaker);
  271.                 }
  272.                 else
  273.                     ServerCommand("admin.say", "Not enough arguments. !voteTeams <team1> <team2>", "player", speaker);
  274.             }
  275.             else if(arguments[0].Equals("!help", StringComparison.InvariantCultureIgnoreCase))
  276.             {
  277.                 ServerCommand("admin.say", "!voteChange <map>", "player", speaker);
  278.                 ServerCommand("admin.say", "!voteTeams <Team1> <Team2>", "player", speaker);
  279.                 ServerCommand("admin.say", "Votes need " + (Math.Round(votePercentage,2)*100).ToString() + "% to pass.", "player", speaker);
  280.             }
  281.         }
  282.        
  283.         public void LogWrite(String msg)
  284.         {
  285.             this.ExecuteCommand("procon.protected.pluginconsole.write", msg);
  286.         }
  287.        
  288.         public String GetPluginName()
  289.         {
  290.             return "Vote Change!";
  291.         }
  292.  
  293.         public String GetPluginVersion()
  294.         {
  295.             return "1.3.0.1";
  296.         }
  297.  
  298.         public String GetPluginAuthor()
  299.         {
  300.             return "Hattiwatti";
  301.         }
  302.  
  303.         public String GetPluginWebsite()
  304.         {
  305.             return "www.BFCinematicTools.com";
  306.         }
  307.  
  308.         public String GetPluginDescription()
  309.         {
  310.             return "This plugin lets players to vote a map change. Ideal for filming servers.";
  311.         }
  312.        
  313.          public List<CPluginVariable> GetDisplayPluginVariables()
  314.         {
  315.             List<CPluginVariable> lstReturn = new List<CPluginVariable>();
  316.  
  317.             lstReturn.Add(new CPluginVariable("Settings|Vote percentage to win", votePercentage.GetType(), votePercentage));
  318.  
  319.             return lstReturn;
  320.         }
  321.  
  322.         public List<CPluginVariable> GetPluginVariables()
  323.         {
  324.             return GetDisplayPluginVariables();
  325.         }
  326.  
  327.         public void SetPluginVariable(String strVariable, String strValue)
  328.         {
  329.             if (Regex.Match(strVariable, @"Vote percentage to win").Success)
  330.             {
  331.                 float tmp = 0.66f;
  332.                 float.TryParse(strValue, out tmp);
  333.                 votePercentage = tmp;
  334.             }
  335.         }
  336.  
  337.         public void OnPluginEnable()
  338.         {
  339.             ServerCommand("admin.listPlayers", "all");
  340.             this.ExecuteCommand("procon.protected.tasks.add", "1", "300", "-1", "procon.protected.send", "admin.say", "Type !help to list available commands", "all");
  341.         }
  342.  
  343.         public void OnPluginDisable()
  344.         {
  345.         }
  346.        
  347.         public override void OnVersion(String serverType, String version) { }
  348.  
  349.         public override void OnServerInfo(CServerInfo serverInfo)
  350.         {
  351.            
  352.         }
  353.  
  354.         public override void OnResponseError(List<String> requestWords, String error) { }
  355.  
  356.         public override void OnListPlayers(List<CPlayerInfo> players, CPlayerSubset subset)
  357.         {
  358.             playerCount = players.Count;
  359.         }
  360.  
  361.         public override void OnPlayerJoin(string SoldierName)
  362.         {
  363.             ServerCommand("admin.say", "Welcome! Type !help for available commands", "player", SoldierName);
  364.         }
  365.  
  366.         public override void OnPlayerLeft(CPlayerInfo playerInfo)
  367.         {
  368.             foreach (string[] vote in Votes)
  369.             {
  370.                 if (vote[0] == playerInfo.SoldierName)
  371.                 {
  372.                     Votes.Remove(vote);
  373.                     return;
  374.                 }
  375.             }
  376.             foreach (string[] vote in teamVotes)
  377.             {
  378.                 if (vote[0] == playerInfo.SoldierName)
  379.                 {
  380.                     teamVotes.Remove(vote);
  381.                     return;
  382.                 }
  383.             }
  384.         }
  385.  
  386.         public override void OnGlobalChat(String speaker, String message)
  387.         {
  388.            parseMessage(speaker, message);
  389.         }
  390.  
  391.         public override void OnTeamChat(String speaker, String message, int teamId)
  392.         {
  393.             parseMessage(speaker, message);
  394.         }
  395.  
  396.         public override void OnSquadChat(String speaker, String message, int teamId, int squadId)
  397.         {
  398.             parseMessage(speaker, message);
  399.         }
  400.  
  401.         public override void OnLoadingLevel(String mapFileName, int roundsPlayed, int roundsTotal)
  402.         {
  403.             Votes.Clear();
  404.             teamVotes.Clear();
  405.         }
  406.  
  407.         public override void OnLevelStarted()
  408.         {
  409.         }
  410.        
  411.         public void OnPluginLoaded(String strHostName, String strPort, String strPRoConVersion)
  412.         {
  413.             this.RegisterEvents(this.GetType().Name, "OnVersion", "OnServerInfo", "OnResponseError", "OnListPlayers", "OnPlayerJoin", "OnPlayerLeft", "OnPlayerKilled", "OnPlayerSpawned", "OnPlayerTeamChange", "OnGlobalChat", "OnTeamChat", "OnSquadChat", "OnRoundOverPlayers", "OnRoundOver", "OnRoundOverTeamScores", "OnLoadingLevel", "OnLevelStarted", "OnLevelLoaded");
  414.         }
  415.     }
  416.  
  417. } // end namespace PRoConEvents
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement