From 4dcc4b7cdc56ff88cb513ccfff054f0aab85eed9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 12 May 2017 22:19:13 +1000 Subject: [PATCH] Stage 1 of refactoring economy. --- MCGalaxy/Commands/Economy/CmdEconomy.cs | 2 +- MCGalaxy/Commands/Fun/CmdLottery.cs | 2 +- MCGalaxy/Commands/Information/CmdWhois.cs | 2 - MCGalaxy/CorePlugin/CorePlugin.cs | 12 ++++- MCGalaxy/CorePlugin/EcoHandlers.cs | 48 +++++++++++++++++++ MCGalaxy/CorePlugin/MiscHandlers.cs | 2 +- MCGalaxy/Economy/EcoTransaction.cs | 56 +++++++++++++++++++++++ MCGalaxy/Economy/Economy.cs | 20 +++++--- MCGalaxy/MCGalaxy_.csproj | 3 ++ MCGalaxy/Player/Player.Fields.cs | 6 +-- MCGalaxy/Player/Player.cs | 11 ++--- MCGalaxy/Plugins/Events/EconomyEvents.cs | 44 ++++++++++++++++++ 12 files changed, 183 insertions(+), 25 deletions(-) create mode 100644 MCGalaxy/CorePlugin/EcoHandlers.cs create mode 100644 MCGalaxy/Economy/EcoTransaction.cs create mode 100644 MCGalaxy/Plugins/Events/EconomyEvents.cs diff --git a/MCGalaxy/Commands/Economy/CmdEconomy.cs b/MCGalaxy/Commands/Economy/CmdEconomy.cs index 8931d32e4..399bfef36 100644 --- a/MCGalaxy/Commands/Economy/CmdEconomy.cs +++ b/MCGalaxy/Commands/Economy/CmdEconomy.cs @@ -1,5 +1,5 @@ /* - Copyright 2015 MCGalaxy + Copyright 2011 MCForge Dual-licensed under the Educational Community License, Version 2.0 and the GNU General Public License, Version 3 (the "Licenses"); you may diff --git a/MCGalaxy/Commands/Fun/CmdLottery.cs b/MCGalaxy/Commands/Fun/CmdLottery.cs index 1f8945e51..c640dede2 100644 --- a/MCGalaxy/Commands/Fun/CmdLottery.cs +++ b/MCGalaxy/Commands/Fun/CmdLottery.cs @@ -31,7 +31,7 @@ namespace MCGalaxy.Commands.Fun { public override void Use(Player p, string message) { if (Player.IsSuper(p)) { MessageInGameOnly(p); return; } - if (!p.EnoughMoney(10)) { + if (p.money < 10) { Player.Message(p, "You need &f10 " + Server.moneys + " %Sto enter the lottery."); return; } diff --git a/MCGalaxy/Commands/Information/CmdWhois.cs b/MCGalaxy/Commands/Information/CmdWhois.cs index 5b08dfd92..ac0ec7429 100644 --- a/MCGalaxy/Commands/Information/CmdWhois.cs +++ b/MCGalaxy/Commands/Information/CmdWhois.cs @@ -16,9 +16,7 @@ permissions and limitations under the Licenses. */ using System; -using System.Collections.Generic; using MCGalaxy.DB; -using MCGalaxy.Games; namespace MCGalaxy.Commands.Info { public sealed class CmdWhois : Command { diff --git a/MCGalaxy/CorePlugin/CorePlugin.cs b/MCGalaxy/CorePlugin/CorePlugin.cs index 074eff3f7..1c7c1d8f3 100644 --- a/MCGalaxy/CorePlugin/CorePlugin.cs +++ b/MCGalaxy/CorePlugin/CorePlugin.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; using System.IO; +using MCGalaxy.Eco; using MCGalaxy.Events; using MCGalaxy.Tasks; @@ -36,12 +37,15 @@ namespace MCGalaxy.Core { Priority.Critical, this); OnPlayerConnectingEvent.Register(ConnectingHandler.HandleConnecting, Priority.Critical, this); + OnJoinedLevelEvent.Register(MiscHandlers.HandleOnJoinedLevel, Priority.Critical, this); OnPlayerMoveEvent.Register(MiscHandlers.HandlePlayerMove, Priority.Critical, this); - OnPlayerClickEvent.Register(MiscHandlers.HandlePlayerClick, - Priority.Critical, this); + OnPlayerClickEvent.Register(MiscHandlers.HandlePlayerClick, + Priority.Critical, this); + OnEcoTransactionEvent.Register(EcoHandlers.HandleEcoTransaction, + Priority.Critical, this); clearTask = Server.Background.QueueRepeat(IPThrottler.CleanupTask, null, TimeSpan.FromMinutes(10)); @@ -51,8 +55,12 @@ namespace MCGalaxy.Core { OnPlayerConnectEvent.UnRegister(this); OnPlayerCommandEvent.UnRegister(this); OnPlayerConnectingEvent.UnRegister(this); + OnJoinedLevelEvent.UnRegister(this); OnPlayerMoveEvent.UnRegister(this); + OnPlayerClickEvent.UnRegister(this); + OnEcoTransactionEvent.UnRegister(this); + Server.Background.Cancel(clearTask); } } diff --git a/MCGalaxy/CorePlugin/EcoHandlers.cs b/MCGalaxy/CorePlugin/EcoHandlers.cs new file mode 100644 index 000000000..f484932a8 --- /dev/null +++ b/MCGalaxy/CorePlugin/EcoHandlers.cs @@ -0,0 +1,48 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.IO; +using MCGalaxy.Blocks.Extended; +using MCGalaxy.Events; +using MCGalaxy.Maths; +using MCGalaxy.Network; +using MCGalaxy.Eco; +using System.Globalization; + +namespace MCGalaxy.Core { + internal static class EcoHandlers { + + internal static void HandleEcoTransaction(EcoTransaction transaction) { + switch (transaction.Type) { + case EcoTransactionType.Purchase: + HandlePurchase(transaction); break; + } + } + + static void HandlePurchase(EcoTransaction transaction) { + Economy.EcoStats stats = Economy.RetrieveStats(transaction.TargetName); + stats.TotalSpent += transaction.Amount; + stats.Purchase = transaction.ItemName + "%3 for %f" + transaction.Amount + " %3" + Server.moneys + + " on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture); + + Player p = PlayerInfo.FindExact(transaction.TargetName); + if (p != null) Player.Message(p, "Your balance is now &f{0} &3{1}", p.money, Server.moneys); + Economy.UpdateStats(stats); + } + } +} diff --git a/MCGalaxy/CorePlugin/MiscHandlers.cs b/MCGalaxy/CorePlugin/MiscHandlers.cs index e2c45026c..5580558fd 100644 --- a/MCGalaxy/CorePlugin/MiscHandlers.cs +++ b/MCGalaxy/CorePlugin/MiscHandlers.cs @@ -19,8 +19,8 @@ using System; using System.IO; using MCGalaxy.Blocks.Extended; using MCGalaxy.Events; -using MCGalaxy.Network; using MCGalaxy.Maths; +using MCGalaxy.Network; namespace MCGalaxy.Core { internal static class MiscHandlers { diff --git a/MCGalaxy/Economy/EcoTransaction.cs b/MCGalaxy/Economy/EcoTransaction.cs new file mode 100644 index 000000000..7a20f9561 --- /dev/null +++ b/MCGalaxy/Economy/EcoTransaction.cs @@ -0,0 +1,56 @@ +/* + Copyright 2014 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; + +namespace MCGalaxy.Eco { + + /// Describes an economic transaction. + public sealed class EcoTransaction { + + /// Name of player who caused the transaction. Can be null. (e.g. person who used /give /pay /take) + public string SourceName; + + /// Formatted name of source player. Can be null. + public string SourceFormatted; + + /// Name of player who was target of the transaction. (e.g. person who received money from /pay) + public string TargetName; + + /// Formatted name of target player. + public string TargetFormatted; + + + + /// Name of the item involved in the transaction. Can be null. + public string ItemName; + + /// Reason provided for the transaction. Can be null. + public string Reason; + + + /// Amount involved in the transaction. Zero or positive. + public int Amount; + + /// Type of this transaction. + public EcoTransactionType Type; + } + + public enum EcoTransactionType { + Give, Payment, Take, Purchase, + } +} \ No newline at end of file diff --git a/MCGalaxy/Economy/Economy.cs b/MCGalaxy/Economy/Economy.cs index 71ac8e5b6..3380b8074 100644 --- a/MCGalaxy/Economy/Economy.cs +++ b/MCGalaxy/Economy/Economy.cs @@ -19,11 +19,17 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using MCGalaxy.Events; namespace MCGalaxy.Eco { + public static partial class Economy { public static bool Enabled; + + public delegate void OnMoneyChanged(Player p); + + public delegate void OnEcoTransaction(EcoTransaction transaction); public static void Load() { if (!File.Exists(Paths.EconomyPropsFile)) { @@ -105,13 +111,13 @@ namespace MCGalaxy.Eco { public static void MakePurchase(Player p, int cost, string item) { p.SetMoney(p.money - cost); - Player.Message(p, "Your balance is now &f{0} &3{1}", p.money, Server.moneys); - - Economy.EcoStats stats = RetrieveStats(p.name); - stats.TotalSpent += cost; - stats.Purchase = item + "%3 for %f" + cost + " %3" + Server.moneys + - " on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture); - Economy.UpdateStats(stats); + EcoTransaction transaction = new EcoTransaction(); + transaction.TargetName = p.name; + transaction.TargetFormatted = p.ColoredName; + transaction.Amount = cost; + transaction.Type = EcoTransactionType.Purchase; + transaction.ItemName = item; + OnEcoTransactionEvent.Call(transaction); } } } \ No newline at end of file diff --git a/MCGalaxy/MCGalaxy_.csproj b/MCGalaxy/MCGalaxy_.csproj index 024522e96..ef0d96688 100644 --- a/MCGalaxy/MCGalaxy_.csproj +++ b/MCGalaxy/MCGalaxy_.csproj @@ -395,6 +395,7 @@ + @@ -462,6 +463,7 @@ + @@ -586,6 +588,7 @@ + diff --git a/MCGalaxy/Player/Player.Fields.cs b/MCGalaxy/Player/Player.Fields.cs index 3d9ff92f5..dc7361894 100644 --- a/MCGalaxy/Player/Player.Fields.cs +++ b/MCGalaxy/Player/Player.Fields.cs @@ -21,6 +21,7 @@ using MCGalaxy.Drawing.Transforms; using MCGalaxy.Games; using MCGalaxy.Undo; using MCGalaxy.Maths; +using MCGalaxy.Events; namespace MCGalaxy { @@ -262,10 +263,5 @@ namespace MCGalaxy { /// Returns whether the given player is console or IRC. public static bool IsSuper(Player p) { return p == null || p.ircChannel != null || p.ircNick != null; } - - public void SetMoney(int amount) { - money = amount; - OnMoneyChanged(); - } } } diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index 27c8688ff..bdee5b2ef 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -479,14 +479,13 @@ namespace MCGalaxy { catch { } return false; } - - public bool EnoughMoney(int amount) { - return money >= amount; - } - - public void OnMoneyChanged() { + + public void SetMoney(int amount) { + money = amount; + if (Server.zombie.Running) Server.zombie.PlayerMoneyChanged(this); if (Server.lava.active) Server.lava.PlayerMoneyChanged(this); + OnMoneyChangedEvent.Call(this); } public void TntAtATime() { diff --git a/MCGalaxy/Plugins/Events/EconomyEvents.cs b/MCGalaxy/Plugins/Events/EconomyEvents.cs new file mode 100644 index 000000000..a1fa2b04f --- /dev/null +++ b/MCGalaxy/Plugins/Events/EconomyEvents.cs @@ -0,0 +1,44 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using MCGalaxy.Eco; + +namespace MCGalaxy.Events { + + /// Raised whenever a player's online money changes. + public sealed class OnMoneyChangedEvent : IPluginEvent { + internal OnMoneyChangedEvent(Economy.OnMoneyChanged method, Priority priority, Plugin plugin) + : base(method, priority, plugin) { } + + public static void Call(Player p) { + if (handlers.Count == 0) return; + CallImpl(pl => pl(p)); + } + } + + /// Raised whenever an economic transaction occurs. + public sealed class OnEcoTransactionEvent : IPluginEvent { + internal OnEcoTransactionEvent(Economy.OnEcoTransaction method, Priority priority, Plugin plugin) + : base(method, priority, plugin) { } + + public static void Call(EcoTransaction transaction) { + if (handlers.Count == 0) return; + CallImpl(pl => pl(transaction)); + } + } +}