From 7b8f47fa6db42b36a352608c5b29e53b4f816e01 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 12 May 2017 23:02:32 +1000 Subject: [PATCH] Finished core to use EcoTransaction event. --- MCGalaxy/Commands/Economy/CmdGive.cs | 28 +++++----- MCGalaxy/Commands/Economy/CmdPay.cs | 35 ++++++------ MCGalaxy/Commands/Economy/CmdTake.cs | 37 +++++++------ MCGalaxy/Commands/Economy/MoneyCmd.cs | 65 ++++++----------------- MCGalaxy/CorePlugin/EcoHandlers.cs | 76 +++++++++++++++++++++++---- MCGalaxy/Economy/EcoTransaction.cs | 6 ++- MCGalaxy/Economy/Economy.cs | 2 +- 7 files changed, 135 insertions(+), 114 deletions(-) diff --git a/MCGalaxy/Commands/Economy/CmdGive.cs b/MCGalaxy/Commands/Economy/CmdGive.cs index 435f33e41..9fe003862 100644 --- a/MCGalaxy/Commands/Economy/CmdGive.cs +++ b/MCGalaxy/Commands/Economy/CmdGive.cs @@ -17,6 +17,7 @@ */ using System; using MCGalaxy.Eco; +using MCGalaxy.Events; namespace MCGalaxy.Commands.Eco { public sealed class CmdGive : MoneyCmd { @@ -26,33 +27,34 @@ namespace MCGalaxy.Commands.Eco { public CmdGive() { } public override void Use(Player p, string message) { - MoneyCmdData data; - if (!ParseArgs(p, message, false, "give", out data)) return; + EcoTransaction data; + bool all = false; + if (!ParseArgs(p, message, ref all, "give", out data)) return; int matches = 1; - Player who = PlayerInfo.FindMatches(p, data.Name, out matches); + Player who = PlayerInfo.FindMatches(p, data.TargetName, out matches); if (matches > 1) return; if (p != null && p == who) { Player.Message(p, "You cannot give yourself %3" + Server.moneys); return; } - - string target = null; int money = 0; + if (who == null) { - target = Economy.FindMatches(p, data.Name, out money); - if (target == null) return; + data.TargetName = Economy.FindMatches(p, data.TargetName, out money); + if (data.TargetName == null) return; if (ReachedMax(p, money, data.Amount)) return; money += data.Amount; - Economy.UpdateMoney(target, money); + Economy.UpdateMoney(data.TargetName, money); } else { - target = who.name; money = who.money; + data.TargetName = who.name; + money = who.money; + if (ReachedMax(p, money, data.Amount)) return; who.SetMoney(who.money + data.Amount); } - MessageAll(p, "{0} %Sgave {1} &f{2} &3{3}{4}", target, data); - Economy.EcoStats stats = Economy.RetrieveStats(target); - stats.Salary = Format(p, " by " + data.SourceRaw, data); - Economy.UpdateStats(stats); + data.TargetFormatted = PlayerInfo.GetColoredName(p, data.TargetName); + data.Type = EcoTransactionType.Give; + OnEcoTransactionEvent.Call(data); } static bool ReachedMax(Player p, int current, int amount) { diff --git a/MCGalaxy/Commands/Economy/CmdPay.cs b/MCGalaxy/Commands/Economy/CmdPay.cs index 69677f4a3..2ef65eceb 100644 --- a/MCGalaxy/Commands/Economy/CmdPay.cs +++ b/MCGalaxy/Commands/Economy/CmdPay.cs @@ -1,7 +1,7 @@ /* Copyright 2011 MCForge - Dual-licensed under the Educational Community License, Version 2.0 and + 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 @@ -17,6 +17,7 @@ */ using System; using MCGalaxy.Eco; +using MCGalaxy.Events; namespace MCGalaxy.Commands.Eco { public sealed class CmdPay : MoneyCmd { @@ -25,40 +26,34 @@ namespace MCGalaxy.Commands.Eco { public CmdPay() { } public override void Use(Player p, string message) { - MoneyCmdData data; - if (!ParseArgs(p, message, false, "pay", out data)) return; + EcoTransaction data; + bool all = false; + if (!ParseArgs(p, message, ref all, "pay", out data)) return; int matches = 1; - Player who = PlayerInfo.FindMatches(p, data.Name, out matches); + Player who = PlayerInfo.FindMatches(p, data.TargetName, out matches); if (matches > 1) return; if (p != null && p == who) { Player.Message(p, "You cannot pay yourself %3" + Server.moneys); return; } - string target = null; int money, srcMoney = Player.IsSuper(p) ? int.MaxValue : p.money; if (who == null) { - target = Economy.FindMatches(p, data.Name, out money); - if (target == null) return; + data.TargetName = Economy.FindMatches(p, data.TargetName, out money); + if (data.TargetName == null) return; if (!IsLegalPayment(p, srcMoney, money, data.Amount)) return; money += data.Amount; - Economy.UpdateMoney(target, money); + Economy.UpdateMoney(data.TargetName, money); } else { - target = who.name; money = who.money; + data.TargetName = who.name; + money = who.money; + if (!IsLegalPayment(p, srcMoney, money, data.Amount)) return; who.SetMoney(who.money + data.Amount); } - if (!Player.IsSuper(p)) p.SetMoney(p.money - data.Amount); - MessageAll(p, "{0} %Spaid {1} &f{2} &3{3}{4}", target, data); - - Economy.EcoStats stats = Economy.RetrieveStats(target); - stats.Salary = Format(p, " by " + data.SourceRaw, data); - Economy.UpdateStats(stats); - if (Player.IsSuper(p)) return; - stats = Economy.RetrieveStats(p.name); - string targetName = PlayerInfo.GetColoredName(p, target); - stats.Payment = Format(p, " to " + targetName, data); - Economy.UpdateStats(stats); + data.TargetFormatted = PlayerInfo.GetColoredName(p, data.TargetName); + data.Type = EcoTransactionType.Payment; + OnEcoTransactionEvent.Call(data); } static bool IsLegalPayment(Player p, int payer, int receiver, int amount) { diff --git a/MCGalaxy/Commands/Economy/CmdTake.cs b/MCGalaxy/Commands/Economy/CmdTake.cs index 224dcddb0..63d46ddd3 100644 --- a/MCGalaxy/Commands/Economy/CmdTake.cs +++ b/MCGalaxy/Commands/Economy/CmdTake.cs @@ -1,7 +1,7 @@ /* Copyright 2011 MCForge - Dual-licensed under the Educational Community License, Version 2.0 and + 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 @@ -17,6 +17,7 @@ */ using System; using MCGalaxy.Eco; +using MCGalaxy.Events; namespace MCGalaxy.Commands.Eco { public sealed class CmdTake : MoneyCmd { @@ -26,35 +27,37 @@ namespace MCGalaxy.Commands.Eco { public CmdTake() { } public override void Use(Player p, string message) { - MoneyCmdData data; - if (!ParseArgs(p, message, true, "take", out data)) return; + EcoTransaction data; + bool all = true; + if (!ParseArgs(p, message, ref all, "take", out data)) return; int matches = 1; - Player who = PlayerInfo.FindMatches(p, data.Name, out matches); + Player who = PlayerInfo.FindMatches(p, data.TargetName, out matches); if (matches > 1) return; if (p != null && p == who) { Player.Message(p, "%cYou can't take %3" + Server.moneys + "%c from yourself"); return; } - string target = null; int money = 0; if (who == null) { - target = Economy.FindMatches(p, data.Name, out money); - if (target == null) return; - Take(ref money, ref data); - Economy.UpdateMoney(target, money); + data.TargetName = Economy.FindMatches(p, data.TargetName, out money); + if (data.TargetName == null) return; + + Take(ref money, all, data); + Economy.UpdateMoney(data.TargetName, money); } else { - target = who.name; money = who.money; - Take(ref money, ref data); + data.TargetName = who.name; + money = who.money; + + Take(ref money, all, data); who.SetMoney(money); } - MessageAll(p, "{0} %Stook &f{2} &3{3} %Sfrom {1}{4}", target, data); - Economy.EcoStats stats = Economy.RetrieveStats(target); - stats.Fine = Format(p, " by " + data.SourceRaw, data); - Economy.UpdateStats(stats); + data.TargetFormatted = PlayerInfo.GetColoredName(p, data.TargetName); + data.Type = EcoTransactionType.Take; + OnEcoTransactionEvent.Call(data); } - static void Take(ref int money, ref MoneyCmdData data) { - if (data.All || money < data.Amount) { + static void Take(ref int money, bool all, EcoTransaction data) { + if (all || money < data.Amount) { data.Amount = money; money = 0; } else { diff --git a/MCGalaxy/Commands/Economy/MoneyCmd.cs b/MCGalaxy/Commands/Economy/MoneyCmd.cs index 0c4fcda88..dcedc5d91 100644 --- a/MCGalaxy/Commands/Economy/MoneyCmd.cs +++ b/MCGalaxy/Commands/Economy/MoneyCmd.cs @@ -1,7 +1,7 @@ /* Copyright 2011 MCForge - Dual-licensed under the Educational Community License, Version 2.0 and + 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 @@ -16,8 +16,7 @@ permissions and limitations under the Licenses. */ using System; -using System.Globalization; -using MCGalaxy.SQL; +using MCGalaxy.Eco; namespace MCGalaxy.Commands.Eco { public abstract class MoneyCmd : Command { @@ -25,60 +24,26 @@ namespace MCGalaxy.Commands.Eco { public override bool museumUsable { get { return true; } } public override CommandEnable Enabled { get { return CommandEnable.Economy; } } - protected bool ParseArgs(Player p, string message, bool canAll, - string action, out MoneyCmdData data) { - data = default(MoneyCmdData); + protected bool ParseArgs(Player p, string message, ref bool all, + string action, out EcoTransaction data) { + data = new EcoTransaction(); string[] args = message.SplitSpaces(3); if (args.Length < 2) { Help(p); return false; } - data.Name = args[0]; - data.Reason = args.Length > 2 ? args[2] : ""; + + data.TargetName = args[0]; + data.Reason = args.Length > 2 ? args[2] : null; + data.SourcePlayer = p; if (p == null) { - data.SourceRaw = "(console)"; data.Source = "(console)"; + data.SourceName = "(console)"; + data.SourceFormatted = "(console)"; } else { - data.SourceRaw = p.color + p.truename; data.Source = p.ColoredName; + data.SourceName = p.name; + data.SourceFormatted = p.ColoredName; } - int amount = 0; - data.All = canAll && args[1].CaselessEq("all"); - if (!data.All && !CommandParser.GetInt(p, args[1], "Amount", ref amount, 1)) return false; - - data.Amount = amount; - return true; - } - - protected struct MoneyCmdData { - public string Source, SourceRaw, Name, Reason; - public int Amount; - public bool All; - } - - protected static void MessageAll(Player p, string format, - string target, MoneyCmdData data) { - string targetName = PlayerInfo.GetColoredName(p, target); - string msgReason = data.Reason == "" ? "" : " %S(" + data.Reason + "%S)"; - - Chat.MessageGlobal(format, data.Source, targetName, - data.Amount, Server.moneys, msgReason); - } - - protected static string Format(Player p, string action, - MoneyCmdData data) { - string entry = "%f" + data.Amount + "%3 " + Server.moneys + action - + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture); - string reason = data.Reason; - - if (reason == "") return entry; - if (!Database.Backend.EnforcesTextLength) - return entry + " (" + reason + ")"; - - int totalLen = entry.Length + 3 + reason.Length; - if (totalLen >= 256) { - int truncatedLen = reason.Length - (totalLen - 255); - reason = reason.Substring(0, truncatedLen); - Player.Message(p, "Reason too long, truncating to: {0}", reason); - } - return entry + " (" + reason + ")"; + all = all && args[1].CaselessEq("all"); + return all || CommandParser.GetInt(p, args[1], "Amount", ref data.Amount, 1); } } } diff --git a/MCGalaxy/CorePlugin/EcoHandlers.cs b/MCGalaxy/CorePlugin/EcoHandlers.cs index f484932a8..490a1587d 100644 --- a/MCGalaxy/CorePlugin/EcoHandlers.cs +++ b/MCGalaxy/CorePlugin/EcoHandlers.cs @@ -16,13 +16,10 @@ 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; +using MCGalaxy.DB; +using MCGalaxy.SQL; +using MCGalaxy.Eco; namespace MCGalaxy.Core { internal static class EcoHandlers { @@ -31,18 +28,75 @@ namespace MCGalaxy.Core { switch (transaction.Type) { case EcoTransactionType.Purchase: HandlePurchase(transaction); break; + case EcoTransactionType.Take: + HandleTake(transaction); break; + case EcoTransactionType.Give: + HandleGive(transaction); break; + case EcoTransactionType.Payment: + HandlePayment(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 + static void HandlePurchase(EcoTransaction data) { + Economy.EcoStats stats = Economy.RetrieveStats(data.TargetName); + stats.TotalSpent += data.Amount; + stats.Purchase = data.ItemDescription + "%3 for %f" + data.Amount + " %3" + Server.moneys + " on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture); - Player p = PlayerInfo.FindExact(transaction.TargetName); + Player p = PlayerInfo.FindExact(data.TargetName); if (p != null) Player.Message(p, "Your balance is now &f{0} &3{1}", p.money, Server.moneys); Economy.UpdateStats(stats); } + + static void HandleTake(EcoTransaction data) { + MessageAll("{0} %Stook &f{2} &3{3} %Sfrom {1}{4}", data); + Economy.EcoStats stats = Economy.RetrieveStats(data.TargetName); + stats.Fine = Format(" by " + data.SourceName, data); + Economy.UpdateStats(stats); + } + + static void HandleGive(EcoTransaction data) { + MessageAll("{0} %Sgave {1} &f{2} &3{3}{4}", data); + Economy.EcoStats stats = Economy.RetrieveStats(data.TargetName); + stats.Salary = Format(" by " + data.SourceName, data); + Economy.UpdateStats(stats); + } + + static void HandlePayment(EcoTransaction data) { + MessageAll("{0} %Spaid {1} &f{2} &3{3}{4}", data); + Economy.EcoStats stats = Economy.RetrieveStats(data.TargetName); + stats.Salary = Format(" by " + data.SourceName, data); + Economy.UpdateStats(stats); + + if (Player.IsSuper(data.SourcePlayer)) return; + stats = Economy.RetrieveStats(data.SourceName); + stats.Payment = Format(" to " + data.TargetName, data); + Economy.UpdateStats(stats); + } + + + static void MessageAll(string format, EcoTransaction data) { + string reason = data.Reason == null ? "" : " %S(" + data.Reason + "%S)"; + Chat.MessageGlobal(format, data.SourceFormatted, data.TargetFormatted, + data.Amount, Server.moneys, reason); + } + + static string Format(string action, EcoTransaction data) { + string entry = "%f" + data.Amount + "%3 " + Server.moneys + action + + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture); + string reason = data.Reason; + + if (reason == null) return entry; + if (!Database.Backend.EnforcesTextLength) + return entry + " (" + reason + ")"; + + int totalLen = entry.Length + 3 + reason.Length; + if (totalLen >= 256) { + int truncatedLen = reason.Length - (totalLen - 255); + reason = reason.Substring(0, truncatedLen); + Player.Message(data.SourcePlayer, "Reason too long, truncating to: {0}", reason); + } + return entry + " (" + reason + ")"; + } } } diff --git a/MCGalaxy/Economy/EcoTransaction.cs b/MCGalaxy/Economy/EcoTransaction.cs index 7a20f9561..5bc3c7506 100644 --- a/MCGalaxy/Economy/EcoTransaction.cs +++ b/MCGalaxy/Economy/EcoTransaction.cs @@ -35,9 +35,11 @@ namespace MCGalaxy.Eco { public string TargetFormatted; + /// Source player who caused the transaction. Can be null. + public Player SourcePlayer; - /// Name of the item involved in the transaction. Can be null. - public string ItemName; + /// Description of the item involved in the transaction. Can be null. + public string ItemDescription; /// Reason provided for the transaction. Can be null. public string Reason; diff --git a/MCGalaxy/Economy/Economy.cs b/MCGalaxy/Economy/Economy.cs index 3380b8074..ce6414c56 100644 --- a/MCGalaxy/Economy/Economy.cs +++ b/MCGalaxy/Economy/Economy.cs @@ -116,7 +116,7 @@ namespace MCGalaxy.Eco { transaction.TargetFormatted = p.ColoredName; transaction.Amount = cost; transaction.Type = EcoTransactionType.Purchase; - transaction.ItemName = item; + transaction.ItemDescription = item; OnEcoTransactionEvent.Call(transaction); } }