|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Timers; |
| 7 | +using Sandbox; |
| 8 | +using Sandbox.Engine.Multiplayer; |
| 9 | +using Sandbox.Game.GameSystems.BankingAndCurrency; |
| 10 | +using Sandbox.Game.World; |
| 11 | +using Torch; |
| 12 | +using Torch.API.Managers; |
| 13 | +using Torch.Commands; |
| 14 | +using Torch.Commands.Permissions; |
| 15 | +using Torch.Managers.ChatManager; |
| 16 | +using Torch.Mod; |
| 17 | +using Torch.Mod.Messages; |
| 18 | +using VRage.Game.ModAPI; |
| 19 | +using VRageRender.Utils; |
| 20 | + |
| 21 | +namespace Essentials.Commands |
| 22 | +{ |
| 23 | + [Category("econ")] |
| 24 | + public class EcoModule : CommandModule { |
| 25 | + [Command("give", "Add a specified anount of credits into a users account. Use '*' to affect all players")] |
| 26 | + [Permission(MyPromoteLevel.Admin)] |
| 27 | + public void EcoGive(string Player, long amount) { |
| 28 | + if (Player != "*") { |
| 29 | + var p = Utilities.GetPlayerByNameOrId(Player); |
| 30 | + if (p == null) { |
| 31 | + Context.Respond("Player not found"); |
| 32 | + return; |
| 33 | + } |
| 34 | + p.TryGetBalanceInfo(out long balance); |
| 35 | + Context.Respond($"new bal will be {balance + amount}"); |
| 36 | + p.RequestChangeBalance(amount); |
| 37 | + ModCommunication.SendMessageTo(new NotificationMessage($"{amount} credits have been added to your virtual account", 10000, "Blue"), p.SteamUserId); |
| 38 | + |
| 39 | + } |
| 40 | + else { |
| 41 | + foreach (var p in MySession.Static.Players.GetAllPlayers()) { |
| 42 | + long IdentityID = MySession.Static.Players.TryGetIdentityId(p.SteamId); |
| 43 | + MyBankingSystem.ChangeBalance(IdentityID, amount); |
| 44 | + ModCommunication.SendMessageTo(new NotificationMessage($"{amount} credits have been added to your virtual account", 10000, "Blue"), p.SteamId); |
| 45 | + } |
| 46 | + } |
| 47 | + Context.Respond($"{amount} credits given to account(s)"); |
| 48 | + } |
| 49 | + |
| 50 | + [Command("take", "Take a specified anount of credits from a users account. Use '*' to affect all players")] |
| 51 | + [Permission(MyPromoteLevel.Admin)] |
| 52 | + public void EcoTake(string Player, long amount) { |
| 53 | + if (Player != "*") { |
| 54 | + var p = Utilities.GetPlayerByNameOrId(Player); |
| 55 | + if (p == null) { |
| 56 | + Context.Respond("Player not found"); |
| 57 | + return; |
| 58 | + } |
| 59 | + long changefactor = 0 - amount; |
| 60 | + p.RequestChangeBalance(changefactor); |
| 61 | + ModCommunication.SendMessageTo(new NotificationMessage($"{amount} credits have been taken to your virtual account", 10000, "Blue"), p.SteamUserId); |
| 62 | + } |
| 63 | + else { |
| 64 | + foreach (var p in MySession.Static.Players.GetAllPlayers()) { |
| 65 | + long IdentityID = MySession.Static.Players.TryGetIdentityId(p.SteamId); |
| 66 | + long balance = MyBankingSystem.GetBalance(IdentityID); |
| 67 | + MyBankingSystem.ChangeBalance(IdentityID, -amount); |
| 68 | + ModCommunication.SendMessageTo(new NotificationMessage($"{amount} credits have been taken to your virtual account", 10000, "Blue"), p.SteamId); |
| 69 | + } |
| 70 | + } |
| 71 | + Context.Respond($"{amount} credits taken from account(s)"); |
| 72 | + } |
| 73 | + |
| 74 | + [Command("set", "Set a users account to a specifed balance. Use '*' to affect all players")] |
| 75 | + [Permission(MyPromoteLevel.Admin)] |
| 76 | + public void EcoSet(string Player, long amount) { |
| 77 | + if (Player != "*") { |
| 78 | + var p = Utilities.GetPlayerByNameOrId(Player); |
| 79 | + if (p == null) { |
| 80 | + Context.Respond("Player not found"); |
| 81 | + return; |
| 82 | + } |
| 83 | + p.TryGetBalanceInfo(out long balance); |
| 84 | + long difference = (balance - amount); |
| 85 | + p.RequestChangeBalance(-difference); |
| 86 | + ModCommunication.SendMessageTo(new NotificationMessage($"Your balance has been set to {amount} credits!", 10000, "Blue"), p.SteamUserId); |
| 87 | + } |
| 88 | + else { |
| 89 | + foreach (var p in MySession.Static.Players.GetAllPlayers()) { |
| 90 | + long IdentityID = MySession.Static.Players.TryGetIdentityId(p.SteamId); |
| 91 | + long balance = MyBankingSystem.GetBalance(IdentityID); |
| 92 | + long difference = (balance - amount); |
| 93 | + MyBankingSystem.ChangeBalance(IdentityID, -difference); |
| 94 | + ModCommunication.SendMessageTo(new NotificationMessage($"Your balance has been set to {amount} credits!", 10000, "Blue"), p.SteamId); |
| 95 | + } |
| 96 | + } |
| 97 | + Context.Respond($"Balance(s) set to {amount}"); |
| 98 | + } |
| 99 | + |
| 100 | + [Command("reset", "Reset the credits in a users account to 10,000. Use '*' to affect all players")] |
| 101 | + [Permission(MyPromoteLevel.Admin)] |
| 102 | + public void EcoReset(string Player) { |
| 103 | + if (Player != "*") { |
| 104 | + var p = Utilities.GetPlayerByNameOrId(Player); |
| 105 | + if (p == null) { |
| 106 | + Context.Respond("Player not found"); |
| 107 | + return; |
| 108 | + } |
| 109 | + p.TryGetBalanceInfo(out long balance); |
| 110 | + long difference = (balance - 10000); |
| 111 | + p.RequestChangeBalance(-difference); |
| 112 | + ModCommunication.SendMessageTo(new NotificationMessage($"Your balance has been reset to 10,000 credits!", 10000, "Blue"), p.SteamUserId); |
| 113 | + } |
| 114 | + else { |
| 115 | + foreach (var p in MySession.Static.Players.GetAllPlayers()) { |
| 116 | + long IdentityID = MySession.Static.Players.TryGetIdentityId(p.SteamId); |
| 117 | + long balance = MyBankingSystem.GetBalance(IdentityID); |
| 118 | + long difference = (balance - 10000); |
| 119 | + MyBankingSystem.ChangeBalance(IdentityID, -difference); |
| 120 | + ModCommunication.SendMessageTo(new NotificationMessage($"Your balance has been reset to 10,000 credits!", 10000, "Blue"), p.SteamId); |
| 121 | + } |
| 122 | + } |
| 123 | + Context.Respond("Balance(s) reset to 10,000 credits"); |
| 124 | + } |
| 125 | + |
| 126 | + [Command("top", "Return a list of each players balance on the server sorted from highest to lowest")] |
| 127 | + [Permission(MyPromoteLevel.None)] |
| 128 | + public void EcoTop() { |
| 129 | + StringBuilder ecodata = new StringBuilder(); |
| 130 | + ecodata.AppendLine("Summary of balanaces accross the server"); |
| 131 | + Dictionary<ulong, long> balances = new Dictionary<ulong, long>(); |
| 132 | + foreach (var p in MySession.Static.Players.GetAllPlayers()) { |
| 133 | + long IdentityID = MySession.Static.Players.TryGetIdentityId(p.SteamId); |
| 134 | + long balance = MyBankingSystem.GetBalance(IdentityID); |
| 135 | + balances.Add(p.SteamId, balance); |
| 136 | + } |
| 137 | + var sorted = balances.OrderByDescending(x => x.Value).ThenBy(x => x.Key); |
| 138 | + foreach (var value in sorted) { |
| 139 | + var test = MySession.Static.Players.TryGetIdentityNameFromSteamId(value.Key); |
| 140 | + ecodata.AppendLine($"Player: {MySession.Static.Players.TryGetIdentityNameFromSteamId(value.Key).ToString()} - Balance: {value.Value.ToString()}"); |
| 141 | + } |
| 142 | + |
| 143 | + if (Context.Player == null) { |
| 144 | + Context.Respond(ecodata.ToString()); |
| 145 | + return; |
| 146 | + } |
| 147 | + ModCommunication.SendMessageTo(new DialogMessage("Public balance list", "List of players and their credit balances", ecodata.ToString()), Context.Player.SteamUserId); |
| 148 | + } |
| 149 | + |
| 150 | + [Command("check", "Check the balance of a specific player")] |
| 151 | + [Permission(MyPromoteLevel.None)] |
| 152 | + public void EcoCheck(string Player) { |
| 153 | + var p = Utilities.GetPlayerByNameOrId(Player); |
| 154 | + if (p == null) { |
| 155 | + Context.Respond("Player is not online or cannot be found!"); |
| 156 | + return; |
| 157 | + } |
| 158 | + long balance = MyBankingSystem.GetBalance(p.Identity.IdentityId); |
| 159 | + Context.Respond($"{p.DisplayName}'s balance is {balance} credits"); |
| 160 | + } |
| 161 | + |
| 162 | + [Command("pay")] |
| 163 | + [Permission(MyPromoteLevel.None)] |
| 164 | + public void EcoPay(string Player, long amount) { |
| 165 | + if (Context.Player == null) { |
| 166 | + Context.Respond("Console cannot execute this command"); |
| 167 | + return; |
| 168 | + } |
| 169 | + var p = Utilities.GetPlayerByNameOrId(Player); |
| 170 | + if (p == null) { |
| 171 | + Context.Respond("Player is not online or cannot be found!"); |
| 172 | + return; |
| 173 | + } |
| 174 | + MyBankingSystem.RequestTransfer(Context.Player.Identity.IdentityId, p.IdentityId, amount); |
| 175 | + ModCommunication.SendMessageTo(new NotificationMessage($"Your have recieved {amount} credits from {Context.Player}!", 10000, "Blue"),p.SteamUserId); |
| 176 | + ModCommunication.SendMessageTo(new NotificationMessage($"Your have sent {amount} credits to {p.DisplayName}!", 10000, "Blue"),Context.Player.SteamUserId); |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | +} |
0 commit comments