Guest User

Untitled

a guest
Aug 8th, 2017
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.91 KB | None | 0 0
  1. // Reference: MySql.Data
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using MySql.Data;
  8. using MySql.Data.MySqlClient;
  9. using MySql.Data.Common;
  10. using UnityEngine;
  11.  
  12. namespace Oxide.Plugins
  13. {
  14.     [Info("PoliticalSurvival", "Jonty", 0.2)]
  15.     [Description("Political Survival - Become the President, tax your subjects and keep them in line!")]
  16.     class PoliticalSurvival : RustPlugin
  17.     {
  18.         static void Main(string[] args) { }
  19.  
  20.         public class StrayPlayer
  21.         {
  22.             public ulong SteamId;
  23.             public bool IsSettingTaxChest;
  24.  
  25.             public StrayPlayer(ulong pSteamId)
  26.             {
  27.                 this.SteamId = pSteamId;
  28.                 this.IsSettingTaxChest = false;
  29.             }
  30.         }
  31.  
  32.         Dictionary<BasePlayer, StrayPlayer> OnlinePlayers;
  33.         Dictionary<string, string> ServerMessages;
  34.  
  35.         MySqlConnection Database;
  36.  
  37.         string DatabaseHost = "185.117.88.19";
  38.         string DatabasePort = "3306";
  39.         string DatabaseUsername = "rust38544";
  40.         string DatabasePassword = "ErikD223";
  41.         string DatabaseName = "rust38544";
  42.  
  43.         ulong President = 0;
  44.         double TaxLevel = 20.0;
  45.         string RealmName = "";
  46.  
  47.         float TaxChestX = 0;
  48.         float TaxChestY = 0;
  49.         float TaxChestZ = 0;
  50.  
  51.         StorageContainer TaxContainer = null;
  52.  
  53.         private void LoadDefaultConfig()
  54.         {
  55.             CreateConfigEntry("Database", "Host", "127.0.0.1");
  56.             CreateConfigEntry("Database", "Port", "3306");
  57.             CreateConfigEntry("Database", "Username", "root");
  58.             CreateConfigEntry("Database", "Password", "lol123");
  59.             CreateConfigEntry("Database", "Name", "rust");
  60.  
  61.             SaveConfig();
  62.         }
  63.  
  64.         void Init()
  65.         {
  66.             DatabaseHost = Config["Database", "Host"].ToString();
  67.             DatabasePort = Config["Database", "Port"].ToString();
  68.             DatabaseUsername = Config["Database", "Username"].ToString();
  69.             DatabasePassword = Config["Database", "Password"].ToString();
  70.             DatabaseName = Config["Database", "Name"].ToString();
  71.             RealmName = lang.GetMessage("DefaultRealm", this);
  72.  
  73.             Puts("Political Survival is starting...");
  74.  
  75.             OnlinePlayers = new Dictionary<BasePlayer, StrayPlayer>();
  76.             LoadServerMessages();
  77.  
  78.             try
  79.             {
  80.                 Database = new MySqlConnection();
  81.                 Database.ConnectionString = "server=" + DatabaseHost + ";port=" + DatabasePort + ";uid=" + DatabaseUsername + ";pwd=" + DatabasePassword + ";database=" + DatabaseName + ";";
  82.                 Database.Open();
  83.  
  84.                 MySqlCommand GetSettings = new MySqlCommand("SELECT president,tax_level,realm_name,tax_chest FROM settings LIMIT 1", Database);
  85.                 MySqlDataReader SettingsReader = GetSettings.ExecuteReader();
  86.  
  87.                 while (SettingsReader.Read())
  88.                 {
  89.                     President = SettingsReader.GetUInt64(0);
  90.                     TaxLevel = SettingsReader.GetInt32(1);
  91.                     RealmName = SettingsReader.GetString(2);
  92.  
  93.                     string[] TaxCoordinates = SettingsReader.GetString(3).Split(';');
  94.                     TaxChestX = Convert.ToSingle(TaxCoordinates[0]);
  95.                     TaxChestY = Convert.ToSingle(TaxCoordinates[1]);
  96.                     TaxChestZ = Convert.ToSingle(TaxCoordinates[2]);               
  97.                 }
  98.  
  99.                 SettingsReader.Dispose();
  100.                 GetSettings.Dispose();
  101.             }
  102.             catch (Exception e)
  103.             {
  104.                 PrintToConsole(e.ToString());
  105.                 PrintToConsole("If this is the first time running the plugin, please edit the configuration!");
  106.             }
  107.          
  108.             Puts("Realm name is " + RealmName);
  109.             Puts("Tax level is " + TaxLevel);
  110.             Puts("President is " + President);
  111.  
  112.             LoadTaxContainer();
  113.  
  114.             if (BasePlayer.activePlayerList.Count >= 1)
  115.             {
  116.                 foreach (BasePlayer iPlayer in BasePlayer.activePlayerList)
  117.                 {
  118.                     AddPlayer(iPlayer);
  119.                 }
  120.  
  121.                 Puts(OnlinePlayers.Count + " players cached.");
  122.             }
  123.  
  124.             Puts("Political Survival: Started");
  125.         }
  126.  
  127.         void OnPlayerInit(BasePlayer Player)
  128.         {
  129.  
  130.         }
  131.  
  132.         void OnPlayerDisconnected(BasePlayer Player, string Reason)
  133.         {
  134.  
  135.         }
  136.  
  137.         void OnDispenserGather(ResourceDispenser Dispenser, BaseEntity Entity, Item Item)
  138.         {
  139.             if (TaxLevel > 0 && President > 0)
  140.             {
  141.                 int Tax = Convert.ToInt32(Math.Round((Item.amount * TaxLevel) / 100));
  142.                 Item.amount = Item.amount - Tax;
  143.  
  144.                 if (TaxContainer == null)
  145.                 {
  146.                     TaxChestX = 0;
  147.                     TaxChestY = 0;
  148.                     TaxChestZ = 0;
  149.                     SaveTaxContainer();
  150.                     LoadTaxContainer();
  151.                     return;
  152.                 }
  153.  
  154.                 if (!TaxContainer.inventory.IsFull())
  155.                 {
  156.                     ItemDefinition ToAdd = ItemManager.FindItemDefinition(Item.info.itemid);
  157.  
  158.                     if (ToAdd != null)
  159.                     {
  160.                         TaxContainer.inventory.AddItem(ToAdd, Tax);
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.  
  166.         void OnPlantGather(PlantEntity Plant, Item Item, BasePlayer Player)
  167.         {
  168.             int Tax = Convert.ToInt32(Math.Round((Item.amount * TaxLevel) / 100));
  169.             Item.amount = Item.amount - Tax;
  170.         }
  171.  
  172.         void OnEntityDeath(BaseCombatEntity Entity, HitInfo Info)
  173.         {
  174.             BasePlayer Player = Entity.ToPlayer();
  175.  
  176.             if (Player != null)
  177.             {
  178.                 if (IsPresident(Player.userID))
  179.                 {
  180.                     BasePlayer Killer = null;
  181.  
  182.                     if (Info != null)
  183.                         Killer = Info.Initiator.ToPlayer();
  184.  
  185.                     if (Killer != null && Killer.userID != Player.userID)
  186.                     {
  187.                         SetPresident(Killer.userID);
  188.                         PrintToChat(string.Format(lang.GetMessage("PresidentMurdered", this), Killer.displayName));
  189.                     }
  190.                     else
  191.                     {
  192.                         SetPresident(0);
  193.                         PrintToChat(string.Format(lang.GetMessage("PresidentDied", this)));
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.  
  199.         void OnPlayerAttack(BasePlayer Attacker, HitInfo Info)
  200.         {
  201.             BasePlayer Defender = Info.HitEntity.ToPlayer();
  202.  
  203.             if (Defender != null)
  204.             {
  205.                 // Is a person
  206.             }
  207.             else
  208.             {
  209.                 uint EntityId = Info.HitEntity.prefabID;
  210.  
  211.                 if (EntityId == 2014947887 || EntityId == 3439001196)
  212.                 {
  213.                     StrayPlayer Stray = OnlinePlayers[Attacker.ToPlayer()];
  214.  
  215.                     if (Stray == null)
  216.                         return;
  217.  
  218.                     if (Stray.IsSettingTaxChest)
  219.                     {
  220.                         Vector3 BoxPosition = Info.HitEntity.transform.position;
  221.                         float x = BoxPosition.x;
  222.                         float y = BoxPosition.y;
  223.                         float z = BoxPosition.z;
  224.  
  225.                         SendReply(Attacker.ToPlayer(), lang.GetMessage("SetNewTaxChest", this, Attacker.ToPlayer().UserIDString));
  226.  
  227.                         TaxChestX = x;
  228.                         TaxChestY = y;
  229.                         TaxChestZ = z;
  230.                        
  231.                         SaveTaxContainer();
  232.                         LoadTaxContainer();
  233.  
  234.                         Stray.IsSettingTaxChest = false;
  235.                     }
  236.                 }
  237.             }
  238.         }
  239.  
  240.         [ChatCommand("settaxchest")]
  241.         void SetTaxChestCommand(BasePlayer Player, string Command, string[] Arguments)
  242.         {
  243.             if (!IsPresident(Player.userID))
  244.             {
  245.                 SendReply(Player, lang.GetMessage("PresidentError", this, Player.UserIDString));
  246.                 return;
  247.             }
  248.  
  249.             StrayPlayer Stray = OnlinePlayers[Player];
  250.  
  251.             if (Stray.IsSettingTaxChest)
  252.             {
  253.                 Stray.IsSettingTaxChest = false;
  254.                 SendReply(Player, lang.GetMessage("NotSettingNewTaxChest", this, Player.UserIDString));
  255.             }
  256.             else
  257.             {
  258.                 Stray.IsSettingTaxChest = true;
  259.                 SendReply(Player, lang.GetMessage("SettingNewTaxChest", this, Player.UserIDString));
  260.             }
  261.         }
  262.  
  263.         [ChatCommand("king")]
  264.         void InfoCommand(BasePlayer Player, string Command, string[] Arguments)
  265.         {
  266.             string PresidentName = "";
  267.  
  268.             if (President > 0)
  269.             {
  270.                 BasePlayer BasePresident = BasePlayer.FindByID(President);
  271.  
  272.                 if (BasePresident != null)
  273.                 {
  274.                     PresidentName = BasePresident.displayName;
  275.                 }
  276.                 else
  277.                 {
  278.                     BasePlayer SleepingPresident = BasePlayer.FindSleeping(President);
  279.  
  280.                     if (SleepingPresident != null)
  281.                     {
  282.                         PresidentName = SleepingPresident.displayName;
  283.                     }
  284.                     else
  285.                     {
  286.                         PresidentName = lang.GetMessage("ClaimPresident", this, Player.UserIDString);
  287.                         President = 0;
  288.                     }
  289.                 }
  290.             }
  291.             else
  292.                 PresidentName = lang.GetMessage("ClaimPresident", this, Player.UserIDString);
  293.  
  294.             SendReply(Player, lang.GetMessage("InfoPresident", this, Player.UserIDString) + ": " + PresidentName);
  295.             SendReply(Player, lang.GetMessage("InfoRealmName", this, Player.UserIDString) + ": " + RealmName);
  296.             SendReply(Player, lang.GetMessage("InfoTaxLevel", this, Player.UserIDString) + ": " + TaxLevel + "%");        
  297.         }
  298.  
  299.         [ChatCommand("imking")]
  300.         void ClaimPresident(BasePlayer Player, string Command, string[] Arguments)
  301.         {
  302.             if (President < 1)
  303.             {
  304.                 PrintToChat("<color=#008080ff><b>" + Player.displayName + "</b></color> " + lang.GetMessage("IsNowPresident", this));
  305.                 SetPresident(Player.userID);
  306.             }
  307.         }
  308.  
  309.         [ChatCommand("settax")]
  310.         void SetTaxCommand(BasePlayer Player, string Command, string[] Arguments)
  311.         {
  312.             if (IsPresident(Player.userID))
  313.             {
  314.                 double NewTaxLevel = Convert.ToDouble(MergeParams(Arguments, 0));
  315.  
  316.                 if (NewTaxLevel > 25.0)
  317.                     NewTaxLevel = 25.0;
  318.  
  319.                 if (NewTaxLevel == TaxLevel)
  320.                     return;
  321.  
  322.                 if (NewTaxLevel < 1)
  323.                     NewTaxLevel = 0;
  324.  
  325.                 SetTaxLevel(NewTaxLevel);
  326.  
  327.                 PrintToChat(string.Format(lang.GetMessage("UpdateTaxMessage", this), Player.displayName, NewTaxLevel));
  328.             }
  329.             else
  330.                 SendReply(Player, lang.GetMessage("PresidentError", this, Player.UserIDString));
  331.         }
  332.  
  333.         [ChatCommand("realmname")]
  334.         void RealmNameCommand(BasePlayer Player, string Command, string[] Arguments)
  335.         {
  336.             if (IsPresident(Player.userID))
  337.             {
  338.                 string NewName = MergeParams(Arguments, 0);
  339.  
  340.                 if (!String.IsNullOrEmpty(NewName))
  341.                 {
  342.                     SetRealmName(NewName);
  343.                 }
  344.             }
  345.             else
  346.                 SendReply(Player, lang.GetMessage("PresidentError", this, Player.UserIDString));
  347.         }
  348.  
  349.         [ChatCommand("pm2")]
  350.         void PrivateMessage(BasePlayer Player, string Command, string[] Arguments)
  351.         {
  352.             string Name = Arguments[0];
  353.             string Message = MergeParams(Arguments, 1);
  354.  
  355.             if (IsPlayerOnline(Name))
  356.             {
  357.                 BasePlayer Reciever = GetPlayer(Name);
  358.                 SendReply(Reciever, "<color=#ffff00ff>" + lang.GetMessage("PrivateFrom", this, Reciever.UserIDString) + " " + Player.displayName + "</color>: " + Message);
  359.                 SendReply(Player, "<color=#ffff00ff>" + lang.GetMessage("PrivateTo", this, Reciever.UserIDString) + " " + Reciever.displayName + "</color>: " + Message);
  360.             }
  361.             else
  362.                 SendReply(Player, Name + lang.GetMessage("PrivateError", this));
  363.         }
  364.  
  365.         [ChatCommand("players")]
  366.         void PlayersCommand(BasePlayer Player, string Command, string[] Arguments)
  367.         {
  368.             StringBuilder Builder = new StringBuilder();
  369.             int PlayerCount = BasePlayer.activePlayerList.Count;
  370.             int Cycle = 1;
  371.            
  372.             Builder.Append(string.Format(lang.GetMessage("OnlinePlayers", this), PlayerCount) + " ");
  373.  
  374.             foreach (BasePlayer iPlayer in BasePlayer.activePlayerList)
  375.             {
  376.                 Builder.Append(iPlayer.displayName);
  377.  
  378.                 if (Cycle < PlayerCount)
  379.                     Builder.Append(", ");
  380.  
  381.                 Cycle++;
  382.             }
  383.  
  384.             SendReply(Player, Builder.ToString());
  385.         }
  386.  
  387.         void AddPlayer(BasePlayer Player)
  388.         {
  389.             GetPlayerFromDatabase(Player);
  390.         }
  391.  
  392.         void RemovePlayer(BasePlayer Player)
  393.         {
  394.             OnlinePlayers.Remove(Player);
  395.         }
  396.  
  397.         StrayPlayer GetStrayPlayer(string Username)
  398.         {
  399.             return OnlinePlayers[BasePlayer.Find(Username)];
  400.         }
  401.  
  402.         bool IsPlayerOnline(string Username)
  403.         {
  404.             if (BasePlayer.Find(Username) != null)
  405.                 return true;
  406.  
  407.             return false;
  408.         }
  409.  
  410.         BasePlayer GetPlayer(string Username)
  411.         {
  412.             return BasePlayer.Find(Username);
  413.         }
  414.  
  415.         string MergeParams(string[] Params, int Start)
  416.         {
  417.             var Merged = new StringBuilder();
  418.             for (int i = Start; i < Params.Length; i++)
  419.             {
  420.                 if (i > Start)
  421.                     Merged.Append(" ");
  422.                 Merged.Append(Params[i]);
  423.             }
  424.  
  425.             return Merged.ToString();
  426.         }
  427.  
  428.         bool IsPresident(ulong SteamId)
  429.         {
  430.             if (President == SteamId)
  431.                 return true;
  432.  
  433.             return false;
  434.         }
  435.  
  436.         void SetPresident(ulong SteamId)
  437.         {
  438.             President = SteamId;
  439.             RealmName = lang.GetMessage("DefaultRealm", this);
  440.             TaxLevel = 0.0;
  441.  
  442.             string PresidentText = "UPDATE settings SET tax_level = " + TaxLevel + ", realm_name = '" + RealmName + "', president = " + President;
  443.  
  444.             MySqlCommand UpdatePresident = new MySqlCommand(PresidentText, Database);
  445.             UpdatePresident.ExecuteNonQuery();
  446.             UpdatePresident.Dispose();
  447.         }
  448.  
  449.         void SetTaxLevel(double NewTaxLevel)
  450.         {
  451.             TaxLevel = NewTaxLevel;
  452.  
  453.             string TaxText = "UPDATE settings SET tax_level = " + NewTaxLevel;
  454.  
  455.             MySqlCommand UpdateTax = new MySqlCommand(TaxText, Database);
  456.             UpdateTax.ExecuteNonQuery();
  457.             UpdateTax.Dispose();
  458.         }
  459.  
  460.         void SetRealmName(string NewName)
  461.         {
  462.             if (NewName.Length > 36)
  463.                 NewName = NewName.Substring(0, 36);
  464.  
  465.             RealmName = NewName;
  466.             PrintToChat(string.Format(lang.GetMessage("RealmRenamed", this), NewName));
  467.  
  468.             string RealmText = "UPDATE settings SET realm_name = '" + RealmName + "'";
  469.  
  470.             MySqlCommand RealmCommand = new MySqlCommand(RealmText, Database);
  471.             RealmCommand.ExecuteNonQuery();
  472.             RealmCommand.Dispose();
  473.         }
  474.  
  475.         void GetPlayerFromDatabase(BasePlayer Player)
  476.         {
  477.             StrayPlayer IPlayer = null;
  478.  
  479.             string CommandText = "SELECT id FROM players WHERE steam_id = " + Player.userID;
  480.             MySqlCommand Command = new MySqlCommand(CommandText, Database);
  481.             bool Exists = Command.ExecuteScalar() != null ? true : false;
  482.             Command.Dispose();
  483.  
  484.             if (!Exists)
  485.             {
  486.                 string InsertText = "INSERT INTO players (steam_id) VALUES ('" + Player.userID + "')";
  487.                 MySqlCommand InsertCommand = new MySqlCommand(InsertText, Database);
  488.                 InsertCommand.ExecuteNonQuery();
  489.                 InsertCommand.Dispose();
  490.             }
  491.  
  492.             string InfoText = "SELECT * FROM players WHERE steam_id = " + Player.userID;
  493.             MySqlCommand InfoCommand = new MySqlCommand(InfoText, Database);
  494.             MySqlDataReader InfoReader = InfoCommand.ExecuteReader();
  495.  
  496.             while (InfoReader.Read())
  497.             {
  498.                 IPlayer = new StrayPlayer(Player.userID);
  499.             }
  500.  
  501.             InfoReader.Dispose();
  502.             InfoCommand.Dispose();
  503.  
  504.             OnlinePlayers.Add(Player, IPlayer);
  505.         }
  506.  
  507.         void LoadTaxContainer()
  508.              
  509.         {
  510.             foreach (StorageContainer Cont in StorageContainer.FindObjectsOfType<StorageContainer>())
  511.             {
  512.                 Vector3 ContPosition = Cont.transform.position;
  513.                 if (TaxChestX == 0 && TaxChestY == 0 && TaxChestZ == 0) { return; }
  514.                 if (((-0.1 < (ContPosition.x - TaxChestX)) && ((ContPosition.x - TaxChestX) < 0.1)) &&
  515.                    ((-0.1 < (ContPosition.y - TaxChestY)) && ((ContPosition.y - TaxChestY) < 0.1)) &&
  516.                    ((-0.1 < (ContPosition.z - TaxChestZ)) && ((ContPosition.z - TaxChestZ) < 0.1)))
  517.                 {
  518.                     Puts("Tax Container instance found: " + Cont.GetEntity().GetInstanceID());
  519.                     TaxContainer = Cont;
  520.                 }
  521.             }
  522.         }
  523.  
  524.         void SaveTaxContainer()
  525.        
  526.         {
  527.             string TaxCommandText = "UPDATE settings SET tax_chest = '" + TaxChestX + ";" + TaxChestY + ";" + TaxChestZ + "'";
  528.             MySqlCommand TaxCommand = new MySqlCommand(TaxCommandText, Database);
  529.             TaxCommand.ExecuteNonQuery();
  530.             TaxCommand.Dispose();
  531.         }
  532.  
  533.         private void CreateConfigEntry(string Key, string SubKey, string Value)
  534.         {
  535.             if (TaxChestX == 0 && TaxChestY == 0 && TaxChestZ == 0) { return; }
  536.             if (Config[Key, SubKey] != null)
  537.                 return;
  538.  
  539.             Config[Key, SubKey] = Value;
  540.         }
  541.  
  542.         private void LoadServerMessages()
  543.         {
  544.             ServerMessages = new Dictionary<string, string>();
  545.  
  546.             ServerMessages.Add("StartingInformation", "<color=yellow>Welcome to {0}</color>. If you are new, we run a custom plugin where you can become King of the North, tax players, and control the economy. Type /king for more information.");
  547.             ServerMessages.Add("PlayerConnected", "has connected to");
  548.             ServerMessages.Add("PlayerDisconnected", "has disconnected from");
  549.             ServerMessages.Add("PresidentDied", "<color=#ff0000ff>King of the North has died! Hurry.. You can become King!</color>");
  550.             ServerMessages.Add("PresidentMurdered", "<color=#ff0000ff>King of the North has been murdered by {0}, who is now the King.</color>");
  551.             ServerMessages.Add("RealmRenamed", "The realm has been renamed to <color=#008080ff>{0}</color>");
  552.             ServerMessages.Add("DefaultRealm", "The land of the Free");
  553.             ServerMessages.Add("OnlinePlayers", "Online players ({0}):");
  554.             ServerMessages.Add("PrivateError", "is either offline or you typed the name wrong.");
  555.             ServerMessages.Add("PrivateFrom", "PM from");
  556.             ServerMessages.Add("PrivateTo", "PM sent to");
  557.             ServerMessages.Add("PresidentError", "You need to be the King to do that!");
  558.             ServerMessages.Add("SettingNewTaxChest", "You are now setting the new tax chest. Hit a storage box to make that the tax chest.");
  559.             ServerMessages.Add("NotSettingNewTaxChest", "You are no longer setting the tax chest.");
  560.             ServerMessages.Add("SetNewTaxChest", "You have set the new tax chest.");
  561.             ServerMessages.Add("ClaimPresident", "Nobody! /imking to become King of the North!");
  562.             ServerMessages.Add("IsNowPresident", "is now King of the North! He can collect taxes. He can also die, and you can be King.");
  563.             ServerMessages.Add("InfoPresident", "King of the North");
  564.             ServerMessages.Add("InfoRealmName", "Realm Name");
  565.             ServerMessages.Add("InfoTaxLevel", "Tax level");
  566.             ServerMessages.Add("UpdateTaxMessage", "King of the North, {0} has set Tax to {1}%. Unhappy with the tax? Kill him and become King of the North!");
  567.  
  568.             lang.RegisterMessages(ServerMessages, this);
  569.         }
  570.     }
  571. }
Add Comment
Please, Sign In to add comment