mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-23 12:42:22 -04:00
Finished core to use EcoTransaction event.
This commit is contained in:
parent
4dcc4b7cdc
commit
7b8f47fa6d
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,9 +35,11 @@ namespace MCGalaxy.Eco {
|
||||
public string TargetFormatted;
|
||||
|
||||
|
||||
/// <summary> Source player who caused the transaction. Can be null. </summary>
|
||||
public Player SourcePlayer;
|
||||
|
||||
/// <summary> Name of the item involved in the transaction. Can be null. </summary>
|
||||
public string ItemName;
|
||||
/// <summary> Description of the item involved in the transaction. Can be null. </summary>
|
||||
public string ItemDescription;
|
||||
|
||||
/// <summary> Reason provided for the transaction. Can be null. </summary>
|
||||
public string Reason;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user