Advertisement
Hattiwatti

VoteChange + VoteMode for BFHL

Apr 7th, 2015
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.36 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.         List<string[]> modeVotes = new List<string[]>();
  40.  
  41.         string currentMap = "MP_Desert05";
  42.         string currentMode = "TurfwarLarge0";
  43.        
  44.         string[,] Maps = new string[,] { {"Downtown", "MP_Downtown"},
  45.                                         {"The Block", "MP_Bloodout"},
  46.                                         {"Derailed", "MP_Eastside"},
  47.                                         {"Dust Bowl", "MP_Desert05"},
  48.                                         {"Bank Job", "MP_Bank"},
  49.                                         {"Riptide", "MP_Offshore"},
  50.                                         {"Hollywood Heights", "MP_Hills"},
  51.                                         {"Everglades", "MP_Glades"},
  52.                                         {"Growhouse", "MP_Growhouse"}
  53.                                     };
  54.  
  55.         string[,] Modes = new string[,] { {"Blood Money", "BloodMoney0"},
  56.                                         {"Hotwire", "Hotwire0"},
  57.                                         {"Heist", "Heist0"},
  58.                                         {"Crosshair", "Hit0"},
  59.                                         {"Rescue", "Hostage0"},
  60.                                         {"Team Deathmatch", "TeamDeathMatch0"},
  61.                                         {"Conquest Large", "TurfwarLarge0"},
  62.                                         {"Conquest Small", "TurfwarSmall0"},
  63.                                     };
  64.  
  65.         string[,] MapModes = new string[,] { {"BloodMoney0", "all"},
  66.                                         {"Hotwire0", "MP_Downtown MP_Eastside MP_Desert05 MP_Offshore MP_Glades"},
  67.                                         {"Heist0", "all"},
  68.                                         {"Hit0", "all"},
  69.                                         {"Hostage0", "all"},
  70.                                         {"TeamDeathMatch0", "all"},
  71.                                         {"TurfwarLarge0", "MP_Downtown MP_Eastside MP_Desert05 MP_Offshore MP_Glades MP_Bloodout MP_Bank MP_Hills"},
  72.                                         {"TurfwarSmall0", "all"},
  73.                                     };
  74.        
  75.         public VoteChange()
  76.         {
  77.         }
  78.  
  79.         public void ServerCommand(params String[] args)
  80.         {
  81.             List<String> list = new List<String>();
  82.             list.Add("procon.protected.send");
  83.             list.AddRange(args);
  84.             this.ExecuteCommand(list.ToArray());
  85.         }
  86.  
  87.         bool checkMapMode(string map, string mode)
  88.         {
  89.             for(int i = 0; i < 7; ++i)
  90.             {
  91.                 bool contains = MapModes[i,0].ToLower().Contains(mode.ToLower());
  92.                 if(contains)
  93.                 {
  94.                     if(MapModes[i,1] == "all")
  95.                         return true;
  96.                     if (MapModes[i,1].ToLower().Contains(map.ToLower()))
  97.                         return true;
  98.                     else    
  99.                         return false;
  100.                 }
  101.             }
  102.             return false;
  103.         }
  104.        
  105.         bool isModeCorrect(string vote, string speaker)
  106.         {
  107.             bool alreadyFound = false;
  108.  
  109.             string mode = "";
  110.  
  111.             for(int i = 0; i < 7; ++i)
  112.             {
  113.                 bool contains = Modes[i,0].ToLower().Contains(vote);
  114.                 if(contains)
  115.                 {
  116.                     if(!alreadyFound)
  117.                     {
  118.                         mode = Modes[i,1];
  119.                         alreadyFound = true;
  120.                     }
  121.                     else
  122.                     {
  123.                         ServerCommand("admin.say", "Found more than one mode. Please be more specific!", "player", speaker);
  124.                         return false;
  125.                     }
  126.                 }
  127.             }
  128.  
  129.             if(alreadyFound)
  130.             {
  131.                 if (checkMapMode(currentMap, mode))
  132.                 {
  133.                     return true;
  134.                 }
  135.                 else
  136.                 {
  137.                     ServerCommand("admin.say", "Mode is not available for current map", "player", speaker);
  138.                     return false;
  139.                 }
  140.             }
  141.             ServerCommand("admin.say", "No modes corresponding to your vote were found", "player", speaker);
  142.             return false;
  143.         }
  144.  
  145.         bool isVoteCorrect(string vote, string speaker)
  146.         {
  147.             bool alreadyFound = false;
  148.             for(int i = 0; i < 9; i++)
  149.             {
  150.                 bool contains = Maps[i,0].ToLower().Contains(vote);
  151.                 if(contains)
  152.                 {
  153.                     if(!alreadyFound)
  154.                     {
  155.                         alreadyFound = true;
  156.                     }
  157.                     else
  158.                     {
  159.                         ServerCommand("admin.say", "Found more than one map. Please be more specific!", "player", speaker);
  160.                         return false;
  161.                     }
  162.                 }
  163.             }
  164.             if(alreadyFound)
  165.             {
  166.                 return true;
  167.             }
  168.             return false;
  169.         }
  170.  
  171.         string[] getMap(string vote)
  172.         {
  173.             string[] mapInfo = new string[2];
  174.             for(int i = 0; i < 9; i++)
  175.             {
  176.                 bool contains = Maps[i,0].ToLower().Contains(vote);
  177.                 if(contains)
  178.                 {
  179.                     mapInfo[0] = Maps[i,0];
  180.                     mapInfo[1] = Maps[i,1];
  181.                     return mapInfo;
  182.                 }
  183.             }
  184.             return mapInfo;
  185.         }
  186.  
  187.         string[] getMode(string vote)
  188.         {
  189.             string[] mapInfo = new string[2];
  190.             for(int i = 0; i < 7; i++)
  191.             {
  192.                 bool contains = Modes[i,0].ToLower().Contains(vote);
  193.                 if(contains)
  194.                 {
  195.                     mapInfo[0] = Modes[i,0];
  196.                     mapInfo[1] = Modes[i,1];
  197.                     return mapInfo;
  198.                 }
  199.             }
  200.             return mapInfo;
  201.         }
  202.  
  203.         float countVotes(string map)
  204.         {
  205.             float votes = 0;
  206.             foreach(string[] vote in Votes)
  207.             {
  208.                 if(vote[1] == map)
  209.                 {
  210.                     //ServerCommand("admin.say", "Vote found", "all");
  211.                     votes += 1;
  212.                 }
  213.             }
  214.  
  215.             return votes;
  216.         }
  217.  
  218.         int hasVoted(string speaker)
  219.         {
  220.             int index = 0;
  221.             foreach(string[] vote in Votes)
  222.             {
  223.                 if(vote[0] == speaker)
  224.                     return index;
  225.                 index++;
  226.             }
  227.             return -1;
  228.         }
  229.  
  230.         bool teamVoteIsCorrect(string arg1, string arg2)
  231.         {
  232.             if(!arg1.Equals("US", StringComparison.InvariantCultureIgnoreCase) && !arg1.Equals("CH", StringComparison.InvariantCultureIgnoreCase) && !arg1.Equals("RU", StringComparison.InvariantCultureIgnoreCase))
  233.                 return false;
  234.             if(!arg2.Equals("US", StringComparison.InvariantCultureIgnoreCase) && !arg2.Equals("CH", StringComparison.InvariantCultureIgnoreCase) && !arg2.Equals("RU", StringComparison.InvariantCultureIgnoreCase))
  235.                 return false;
  236.             return true;
  237.         }
  238.  
  239.         int teamVoteStringToID(string team)
  240.          {
  241.             if(team.Equals("us", StringComparison.InvariantCultureIgnoreCase))
  242.                 return 0;
  243.             if(team.Equals("ru", StringComparison.InvariantCultureIgnoreCase))
  244.                 return 1;
  245.             if(team.Equals("ch", StringComparison.InvariantCultureIgnoreCase))
  246.                 return 2;
  247.             else
  248.                 return -1;
  249.          }
  250.  
  251.          int countTeamVotes(string team1, string team2)
  252.          {
  253.             int votes = 0;
  254.             foreach(string[] vote in teamVotes)
  255.             {
  256.                 if(vote[1] == team1 && vote[2] == team2)
  257.                     votes++;
  258.             }
  259.             return votes;
  260.          }
  261.  
  262.          int countModeVotes(string mode)
  263.          {
  264.             int votes = 0;
  265.             foreach(string[] vote in modeVotes)
  266.             {
  267.                 if(vote[1] == mode)
  268.                     votes++;
  269.             }
  270.             return votes;
  271.          }
  272.  
  273.         int hasVoted_mode(string speaker)
  274.         {
  275.             int index = 0;
  276.             foreach(string[] vote in modeVotes)
  277.             {
  278.                 if(vote[0] == speaker)
  279.                     return index;
  280.                 index++;
  281.             }
  282.             return -1;
  283.         }
  284.  
  285.         void parseMessage(string speaker, string message)
  286.         {
  287.             string[] arguments = message.Split(' ');
  288.             if (arguments[0].Equals("!votechange", StringComparison.InvariantCultureIgnoreCase))
  289.             {
  290.                     string vote = "";
  291.                     for(int i=1; i<arguments.Length; i++)
  292.                     {
  293.                         if(i == arguments.Length-1)
  294.                             vote += arguments[i].ToLower();
  295.                         else
  296.                             vote += arguments[i].ToLower() + " ";
  297.                     }
  298.                    
  299.                     if (isVoteCorrect(vote, speaker))
  300.                     {
  301.                         string[] Map = getMap(vote);
  302.                         int voteIndex = hasVoted(speaker);
  303.  
  304.                         if(voteIndex == -1)
  305.                         {
  306.                             ServerCommand("admin.say", speaker + " voted for " + Map[0], "all");
  307.                             string[] voteToAdd = new string[] { speaker, Map[0] };
  308.                             Votes.Add(voteToAdd);
  309.                         }
  310.                         else
  311.                         {
  312.                             ServerCommand("admin.say", speaker + " changed vote to " + Map[0], "all");
  313.                             Votes[voteIndex][1] = Map[0];
  314.                         }
  315.  
  316.                         float voteCount = countVotes(Map[0]);
  317.                         float players = (float)playerCount;
  318.                         double ratio = voteCount/players;
  319.  
  320.                         if (ratio >= votePercentage)
  321.                         {
  322.                             Votes.Clear();
  323.                             ServerCommand("mapList.clear");
  324.                             currentMap = Map[1];
  325.                             ServerCommand("admin.say", (Math.Round(votePercentage,2)*100).ToString() + "% exceeded. Changing map to " + Map[0] + " in 10 seconds", "all");
  326.                             if(checkMapMode(currentMap, currentMode))
  327.                                 ServerCommand("mapList.add", Map[1], currentMode, "1");
  328.                             else
  329.                             {
  330.                                 ServerCommand("mapList.add", Map[1], "TurfwarSmall0", "1");
  331.                                 ServerCommand("admin.say", "Changing mode to Conquest Small");
  332.                             }
  333.                             ServerCommand("mapList.save");
  334.                             this.ExecuteCommand("procon.protected.tasks.add", "10", "1", "1", "procon.protected.send", "mapList.runNextRound");
  335.                         }
  336.                         else
  337.                             ServerCommand("admin.say", Math.Round((float)(ratio*100), 2).ToString() + "% of the players have voted for " + Map[0], "all");
  338.                     }
  339.                     else
  340.                         ServerCommand("admin.say", "No maps corresponding to your vote were found", "player", speaker);
  341.                
  342.             }
  343.             else if(arguments[0].Equals("!help", StringComparison.InvariantCultureIgnoreCase))
  344.             {
  345.                 ServerCommand("admin.say", "!voteChange <map>", "player", speaker);
  346.                 ServerCommand("admin.say", "!voteMode <mode>", "player", speaker);
  347.                 //ServerCommand("admin.say", "!killProtection to punish your killers", "player", speaker);
  348.                 ServerCommand("admin.say", "Votes need " + (Math.Round(votePercentage,2)*100).ToString() + "% to pass.", "player", speaker);
  349.             }
  350.             else if (arguments[0].Equals("!votemode", StringComparison.InvariantCultureIgnoreCase))
  351.             {
  352.                 string vote = "";
  353.                 for(int i=1; i<arguments.Length; i++)
  354.                 {
  355.                     if(i == arguments.Length-1)
  356.                         vote += arguments[i].ToLower();
  357.                     else
  358.                         vote += arguments[i].ToLower() + " ";
  359.                 }
  360.  
  361.                 if (isModeCorrect(vote, speaker))
  362.                 {
  363.                     string[] Mode = getMode(vote);
  364.                     int voteIndex = hasVoted_mode(speaker);
  365.  
  366.                     if(voteIndex == -1)
  367.                     {
  368.                         ServerCommand("admin.say", speaker + " voted for " + Mode[0], "all");
  369.                         string[] voteToAdd = new string[] { speaker, Mode[0] };
  370.                         modeVotes.Add(voteToAdd);
  371.                     }
  372.                     else
  373.                     {
  374.                         ServerCommand("admin.say", speaker + " changed mode vote to " + Mode[0], "all");
  375.                         modeVotes[voteIndex][1] = Mode[0];
  376.                     }
  377.  
  378.                     float voteCount = countModeVotes(Mode[0]);
  379.                     float players = (float)playerCount;
  380.                     double ratio = voteCount/players;
  381.  
  382.                     if (ratio >= votePercentage)
  383.                     {
  384.                         modeVotes.Clear();
  385.                         ServerCommand("mapList.clear");
  386.                         currentMode = Mode[1];
  387.                         ServerCommand("admin.say", (Math.Round(votePercentage,2)*100).ToString() + "% exceeded. Changing mode to " + Mode[0] + " in 10 seconds", "all");
  388.                         ServerCommand("mapList.add", currentMap, currentMode, "1");
  389.                         ServerCommand("mapList.save");
  390.                         this.ExecuteCommand("procon.protected.tasks.add", "10", "1", "1", "procon.protected.send", "mapList.runNextRound");
  391.                     }
  392.                     else
  393.                         ServerCommand("admin.say", Math.Round((float)(ratio*100), 2).ToString() + "% of the players have voted for " + Mode[0], "all");
  394.                 }
  395.             }      
  396.         }
  397.        
  398.         public void LogWrite(String msg)
  399.         {
  400.             this.ExecuteCommand("procon.protected.pluginconsole.write", msg);
  401.         }
  402.        
  403.         public String GetPluginName()
  404.         {
  405.             return "Vote Change!";
  406.         }
  407.  
  408.         public String GetPluginVersion()
  409.         {
  410.             return "1.2.0.0";
  411.         }
  412.  
  413.         public String GetPluginAuthor()
  414.         {
  415.             return "Hattiwatti";
  416.         }
  417.  
  418.         public String GetPluginWebsite()
  419.         {
  420.             return "www.BFCinematicTools.com";
  421.         }
  422.  
  423.         public String GetPluginDescription()
  424.         {
  425.             return "This plugin lets players to vote a map change. Ideal for filming servers.";
  426.         }
  427.        
  428.          public List<CPluginVariable> GetDisplayPluginVariables()
  429.         {
  430.             List<CPluginVariable> lstReturn = new List<CPluginVariable>();
  431.  
  432.             lstReturn.Add(new CPluginVariable("Settings|Vote percentage to win", votePercentage.GetType(), votePercentage));
  433.             lstReturn.Add(new CPluginVariable("Settings|rnd", cmdthing.GetType(), cmdthing));
  434.  
  435.             return lstReturn;
  436.         }
  437.  
  438.         public List<CPluginVariable> GetPluginVariables()
  439.         {
  440.             return GetDisplayPluginVariables();
  441.         }
  442.  
  443.         public void SetPluginVariable(String strVariable, String strValue)
  444.         {
  445.             if (Regex.Match(strVariable, @"Vote percentage to win").Success)
  446.             {
  447.                 float tmp = 0.66f;
  448.                 float.TryParse(strValue, out tmp);
  449.                 votePercentage = tmp;
  450.             }
  451.         }
  452.  
  453.         public void OnPluginEnable()
  454.         {
  455.             ServerCommand("admin.listPlayers", "all");
  456.             this.ExecuteCommand("procon.protected.tasks.add", "1", "300", "-1", "procon.protected.send", "admin.say", "Type !help to list available commands", "all");
  457.         }
  458.  
  459.         public void OnPluginDisable()
  460.         {
  461.         }
  462.        
  463.         public override void OnVersion(String serverType, String version) { }
  464.  
  465.         public override void OnServerInfo(CServerInfo serverInfo)
  466.         {
  467.            
  468.         }
  469.  
  470.         public override void OnResponseError(List<String> requestWords, String error) { }
  471.  
  472.         public override void OnListPlayers(List<CPlayerInfo> players, CPlayerSubset subset)
  473.         {
  474.             playerCount = players.Count;
  475.         }
  476.  
  477.         public override void OnPlayerJoin(string SoldierName)
  478.         {
  479.             ServerCommand("admin.say", "Welcome! Type !help for available commands", "player", SoldierName);
  480.         }
  481.  
  482.         public override void OnPlayerLeft(CPlayerInfo playerInfo)
  483.         {
  484.             foreach (string[] vote in Votes)
  485.             {
  486.                 if (vote[0] == playerInfo.SoldierName)
  487.                 {
  488.                     Votes.Remove(vote);
  489.                     return;
  490.                 }
  491.             }
  492.             foreach (string[] vote in teamVotes)
  493.             {
  494.                 if (vote[0] == playerInfo.SoldierName)
  495.                 {
  496.                     teamVotes.Remove(vote);
  497.                     return;
  498.                 }
  499.             }
  500.         }
  501.  
  502.         public override void OnGlobalChat(String speaker, String message)
  503.         {
  504.            parseMessage(speaker, message);
  505.         }
  506.  
  507.         public override void OnTeamChat(String speaker, String message, int teamId)
  508.         {
  509.             parseMessage(speaker, message);
  510.         }
  511.  
  512.         public override void OnSquadChat(String speaker, String message, int teamId, int squadId)
  513.         {
  514.             parseMessage(speaker, message);
  515.         }
  516.  
  517.         public override void OnLoadingLevel(String mapFileName, int roundsPlayed, int roundsTotal)
  518.         {
  519.             Votes.Clear();
  520.             teamVotes.Clear();
  521.         }
  522.  
  523.         public override void OnLevelStarted()
  524.         {
  525.         }
  526.        
  527.         public void OnPluginLoaded(String strHostName, String strPort, String strPRoConVersion)
  528.         {
  529.             this.RegisterEvents(this.GetType().Name, "OnVersion", "OnServerInfo", "OnResponseError", "OnListPlayers", "OnPlayerJoin", "OnPlayerLeft", "OnPlayerKilled", "OnPlayerSpawned", "OnPlayerTeamChange", "OnGlobalChat", "OnTeamChat", "OnSquadChat", "OnRoundOverPlayers", "OnRoundOver", "OnRoundOverTeamScores", "OnLoadingLevel", "OnLevelStarted", "OnLevelLoaded");
  530.         }
  531.     }
  532.  
  533. } // end namespace PRoConEvents
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement