Finish the Economy database rewrite.

This commit is contained in:
UnknownShadow200 2016-09-02 20:26:33 +10:00
parent a204527ebb
commit 96db138a35
9 changed files with 246 additions and 218 deletions

View File

@ -29,40 +29,42 @@ namespace MCGalaxy.Commands {
public override CommandEnable Enabled { get { return CommandEnable.Economy; } } public override CommandEnable Enabled { get { return CommandEnable.Economy; } }
public override void Use(Player p, string message) { public override void Use(Player p, string message) {
Economy.EcoStats ecos;
if (CheckSuper(p, message, "player name")) return; if (CheckSuper(p, message, "player name")) return;
if (!ValidName(p, message, "player")) return;
int matches = 1; int matches = 1;
Player who = message == "" ? p : PlayerInfo.FindMatches(p, message, out matches); Player who = message == "" ? p : PlayerInfo.FindMatches(p, message, out matches);
if (matches > 1) return; if (matches > 1) return;
if (matches == 0) {
string dbName = PlayerInfo.FindOfflineNameMatches(p, message);
if (dbName == null) return;
ecos = Economy.RetrieveEcoStats(dbName);
Player.Message(p, "%3===Economy stats for: %f" + ecos.playerName + "%7(offline)%3===");
} else {
ecos = Economy.RetrieveEcoStats(who.name);
Player.Message(p, "%3===Economy stats for: " + who.color + who.name + "%3===");
}
Player.Message(p, "Current balance: %f" + ecos.money + " %3" + Server.moneys); string target = null;
Player.Message(p, "Total spent: %f" + ecos.totalSpent + " %3" + Server.moneys); int money = 0;
if (!(String.IsNullOrEmpty(ecos.purchase) || ecos.purchase == "%cNone")) if (matches == 0) {
Player.Message(p, "Last purchase: " + ecos.purchase); target = Economy.FindMatches(p, message, out money);
if (!(String.IsNullOrEmpty(ecos.payment) || ecos.payment == "%cNone")) if (target == null) return;
Player.Message(p, "Last payment: " + ecos.payment); } else {
if (!(String.IsNullOrEmpty(ecos.salary) || ecos.salary == "%cNone")) target = who.name; money = who.money;
Player.Message(p, "Last receipt: " + ecos.salary); }
if (!(String.IsNullOrEmpty(ecos.fine) || ecos.fine == "%cNone"))
Player.Message(p, "Last fine: " + ecos.fine); string targetName = PlayerInfo.GetColoredName(p, target);
Chat.MessageAll("Economy stats for {0}%S:", targetName);
Player.Message(p, " Current balance: &f{0} &3{1}", money, Server.moneys);
Economy.EcoStats ecos = Economy.RetrieveStats(target);
Player.Message(p, " Total spent: &f" + ecos.TotalSpent + " &3" + Server.moneys);
if (!(String.IsNullOrEmpty(ecos.Purchase) || ecos.Purchase == "%cNone"))
Player.Message(p, " Last purchase: " + ecos.Purchase);
if (!(String.IsNullOrEmpty(ecos.Payment) || ecos.Payment == "%cNone"))
Player.Message(p, " Last payment: " + ecos.Payment);
if (!(String.IsNullOrEmpty(ecos.Salary) || ecos.Salary == "%cNone"))
Player.Message(p, " Last receipt: " + ecos.Salary);
if (!(String.IsNullOrEmpty(ecos.Fine) || ecos.Fine == "%cNone"))
Player.Message(p, " Last fine: " + ecos.Fine);
} }
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/balance <player>"); Player.Message(p, "%T/balance [player]");
Player.Message(p, "%HShows how much %3" + Server.moneys + " %H<player> has, " + Player.Message(p, "%HShows how much %3" + Server.moneys + " %H<player> has, " +
"plus their most recent transactions."); "plus their most recent transactions.");
Player.Message(p, "%HIf <player> is not given, shows your own balance."); Player.Message(p, "%HIf [player] is not given, shows your own balance.");
} }
} }
} }

View File

@ -15,36 +15,24 @@
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
namespace MCGalaxy.Commands { namespace MCGalaxy.Commands {
public sealed class CmdFakePay : MoneyCmd {
public sealed class CmdFakePay : Command {
public override string name { get { return "fakepay"; } } public override string name { get { return "fakepay"; } }
public override string shortcut { get { return "fpay"; } } public override string shortcut { get { return "fpay"; } }
public override string type { get { return CommandTypes.Economy; } } public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public override CommandEnable Enabled { get { return CommandEnable.Economy; } }
public override void Use(Player p, string message) { public override void Use(Player p, string message) {
if (message == "") { Help(p); return; } MoneyCmdData data;
if (!ParseArgs(p, message, false, "fakepay", out data)) return;
string[] args = message.Split(' '); Player who = PlayerInfo.FindMatches(p, data.Name);
Player who = PlayerInfo.FindMatches(p, args[0]); if (who == null) return;
if (who == null) return; if (data.Amount >= 16777215) { Player.Message(p, "You can only fakepay up to 16777215."); return; }
int amount = 0;
if (args.Length == 1 || !int.TryParse(args[1], out amount)) {
Player.Message(p, "You must specify an integer amount to fakepay."); return;
}
if (amount < 0) { Player.Message(p, "You can't fakepay a negative amount."); return; }
if (amount >= 16777215) { Player.Message(p, "You can only fakepay up to 16777215."); return; }
Chat.MessageAll("{0} %Swas given {1} {2}", who.ColoredName, amount, Server.moneys); Chat.MessageAll("{0} %Swas given {1} {2}", who.ColoredName, data.Amount, Server.moneys);
} }
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/fakepay <name> <amount>"); Player.Message(p, "%T/fakepay [name] [amount]");
Player.Message(p, "%HSends a fake give change message."); Player.Message(p, "%HSends a fake give change message.");
} }
} }

View File

@ -17,58 +17,45 @@
*/ */
using System; using System;
using System.Globalization; using System.Globalization;
namespace MCGalaxy.Commands namespace MCGalaxy.Commands {
{ public sealed class CmdGive : MoneyCmd {
public sealed class CmdGive : Command
{
public override string name { get { return "give"; } } public override string name { get { return "give"; } }
public override string shortcut { get { return "gib"; } } public override string shortcut { get { return "gib"; } }
public override string type { get { return CommandTypes.Economy; } } public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
public override CommandEnable Enabled { get { return CommandEnable.Economy; } }
public CmdGive() { } public CmdGive() { }
public override void Use(Player p, string message) { public override void Use(Player p, string message) {
string[] args = message.Split(' '); MoneyCmdData data;
if (args.Length != 2) { Help(p); return; } if (!ParseArgs(p, message, false, "give", out data)) return;
string giver = null, giverRaw = null;
if (p == null) { giverRaw = "(console)"; giver = "(console)"; }
else { giverRaw = p.color + p.name; giver = p.ColoredName; }
int amount;
if (!int.TryParse(args[1], out amount)) {
Player.Message(p, "Amount must be an integer."); return;
}
if (amount < 0) { Player.Message(p, "Cannot give negative %3" + Server.moneys); return; }
int matches = 1; int matches = 1;
Player who = PlayerInfo.FindMatches(p, args[0], out matches); Player who = PlayerInfo.FindMatches(p, data.Name, out matches);
if (matches > 1) return; if (matches > 1) return;
if (p != null && p == who) { Player.Message(p, "You cannot give yourself %3" + Server.moneys); return; } if (p != null && p == who) { Player.Message(p, "You cannot give yourself %3" + Server.moneys); return; }
Economy.EcoStats ecos;
string target = null;
int money = 0;
if (who == null) { if (who == null) {
string dbName = PlayerInfo.FindOfflineNameMatches(p, args[0]); target = Economy.FindMatches(p, data.Name, out money);
if (dbName == null) return; if (target == null) return;
ecos = Economy.RetrieveEcoStats(dbName); if (ReachedMax(p, money, data.Amount)) return;
if (ReachedMax(p, ecos.money, amount)) return; money += data.Amount;
Chat.MessageAll("{0} %Sgave &f{1}%S(offline) &f{2} &3{3}", Economy.UpdateMoney(target, money);
giver, ecos.playerName, amount, Server.moneys);
} else { } else {
if (ReachedMax(p, who.money, amount)) return; target = who.name; money = who.money;
ecos.money = who.money; if (ReachedMax(p, money, data.Amount)) return;
who.SetMoney(who.money + amount); who.SetMoney(who.money + data.Amount);
ecos = Economy.RetrieveEcoStats(who.name);
Chat.MessageAll("{0} %Sgave {1} &f{2} &3{3}",
giver, who.ColoredName, amount, Server.moneys);
} }
string targetName = PlayerInfo.GetColoredName(p, target);
Chat.MessageAll("{0} %Sgave {1} &f{2} &3{3}",
data.Source, targetName, data.Amount, Server.moneys);
ecos.money += amount; Economy.EcoStats stats = Economy.RetrieveStats(target);
ecos.salary = "%f" + amount + "%3 " + Server.moneys + " by " + stats.Salary = "%f" + data.Amount + "%3 " + Server.moneys + " by "
giverRaw + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture); + data.SourceRaw + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
Economy.UpdateEcoStats(ecos); Economy.UpdateStats(stats);
} }
static bool ReachedMax(Player p, int current, int amount) { static bool ReachedMax(Player p, int current, int amount) {

View File

@ -17,62 +17,52 @@
*/ */
using System; using System;
using System.Globalization; using System.Globalization;
namespace MCGalaxy.Commands
{ namespace MCGalaxy.Commands {
public sealed class CmdPay : Command public sealed class CmdPay : MoneyCmd {
{
public override string name { get { return "pay"; } } public override string name { get { return "pay"; } }
public override string shortcut { get { return ""; } } public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Economy; } } public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
public override CommandEnable Enabled { get { return CommandEnable.Economy; } }
public CmdPay() { } public CmdPay() { }
public override void Use(Player p, string message) { public override void Use(Player p, string message) {
string[] args = message.Split(' '); MoneyCmdData data;
if (args.Length != 2) { Help(p); return; } if (!ParseArgs(p, message, false, "pay", out data)) return;
int amount;
if (!int.TryParse(args[1], out amount)) { Player.Message(p, "Amount must be an integer."); return; }
if (amount < 0) { Player.Message(p, "Cannot pay negative %3" + Server.moneys); return; }
int matches = 1; int matches = 1;
Player who = PlayerInfo.FindMatches(p, args[0], out matches); Player who = PlayerInfo.FindMatches(p, data.Name, out matches);
if (matches > 1) return; if (matches > 1) return;
if (p != null && p == who) { Player.Message(p, "You cannot pay yourself %3" + Server.moneys); return; } if (p != null && p == who) { Player.Message(p, "You cannot pay yourself %3" + Server.moneys); return; }
string target = null; string target = null;
Economy.EcoStats payer, receiver; int money;
if (who == null) { if (who == null) {
string dbName = PlayerInfo.FindOfflineNameMatches(p, args[0]); target = Economy.FindMatches(p, data.Name, out money);
if (dbName == null) return; if (target == null) return;
payer = Economy.RetrieveEcoStats(p.name); if (!IsLegalPayment(p, p.money, money, data.Amount)) return;
receiver = Economy.RetrieveEcoStats(dbName); money += data.Amount;
if (!IsLegalPayment(p, payer.money, receiver.money, amount)) return; Economy.UpdateMoney(target, money);
target = receiver.playerName;
Chat.MessageAll("{0} %Spaid &f{1}%S(offline) &f{2} &3{3}",
p.ColoredName, receiver.playerName, amount, Server.moneys);
} else { } else {
payer = Economy.RetrieveEcoStats(p.name); target = who.name; money = who.money;
receiver = Economy.RetrieveEcoStats(who.name); if (!IsLegalPayment(p, p.money, money, data.Amount)) return;
if (!IsLegalPayment(p, payer.money, receiver.money, amount)) return; who.SetMoney(who.money + data.Amount);
receiver.money = who.money;
who.SetMoney(who.money + amount);
target = who.color + who.name;
Chat.MessageAll("{0} %Spaid {1} &f{2} &3{3}",
p.ColoredName, who.ColoredName, amount, Server.moneys);
} }
p.SetMoney(p.money - data.Amount);
string targetName = PlayerInfo.GetColoredName(p, target);
Chat.MessageAll("{0} %Spaid {1} &f{2} &3{3}",
data.Source, targetName, data.Amount, Server.moneys);
payer.payment = "%f" + amount + " %3" + Server.moneys + " to " + target + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture); Economy.EcoStats stats = Economy.RetrieveStats(p.name);
receiver.salary = "%f" + amount + " %3" + Server.moneys + " by " + p.color + p.name + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture); stats.Payment = "%f" + data.Amount + " %3" + Server.moneys + " to "
receiver.money += amount; + target + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
p.SetMoney(p.money - amount); Economy.UpdateStats(stats);
payer.money = p.money;
Economy.UpdateEcoStats(payer); stats = Economy.RetrieveStats(target);
Economy.UpdateEcoStats(receiver); stats.Salary = "%f" + data.Amount + " %3" + Server.moneys + " by "
+ p.color + p.name + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
Economy.UpdateStats(stats);
} }
bool IsLegalPayment(Player p, int payer, int receiver, int amount) { bool IsLegalPayment(Player p, int payer, int receiver, int amount) {
@ -83,7 +73,7 @@ namespace MCGalaxy.Commands
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/pay [player] [amount] "); Player.Message(p, "%T/pay [player] [amount] ");
Player.Message(p, "%HPays <amount> %3" + Server.moneys + " %Hto [player]"); Player.Message(p, "%HPays [amount] &3" + Server.moneys + " %Hto [player]");
} }
} }
} }

View File

@ -19,70 +19,58 @@ using System;
using System.Globalization; using System.Globalization;
namespace MCGalaxy.Commands { namespace MCGalaxy.Commands {
public sealed class CmdTake : Command { public sealed class CmdTake : MoneyCmd {
public override string name { get { return "take"; } } public override string name { get { return "take"; } }
public override string shortcut { get { return ""; } } public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Economy; } } public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
public override CommandEnable Enabled { get { return CommandEnable.Economy; } }
public CmdTake() { } public CmdTake() { }
public override void Use(Player p, string message) { public override void Use(Player p, string message) {
string[] args = message.Split(' '); MoneyCmdData data;
if (args.Length != 2) { Help(p); return; } if (!ParseArgs(p, message, true, "take", out data)) return;
string taker = null, takerRaw = null;
if (p == null) { takerRaw = "(console)"; taker = "(console)"; }
else { takerRaw = p.color + p.name; taker = p.ColoredName; }
int amount = 0;
bool all = args[1].CaselessEq("all");
if (!all && !int.TryParse(args[1], out amount)) {
Player.Message(p, "Amount must be an integer."); return;
}
if (amount < 0) { Player.Message(p, "%cYou can't take negative %3" + Server.moneys); return; }
int matches = 1; int matches = 1;
Player who = PlayerInfo.FindMatches(p, args[0], out matches); Player who = PlayerInfo.FindMatches(p, data.Name, out matches);
if (matches > 1) return; if (matches > 1) return;
if (p != null && p == who) { Player.Message(p, "%cYou can't take %3" + Server.moneys + "%c from yourself"); return; } if (p != null && p == who) { Player.Message(p, "%cYou can't take %3" + Server.moneys + "%c from yourself"); return; }
Economy.EcoStats ecos; string target = null;
int money = 0;
if (who == null) { if (who == null) {
string dbName = PlayerInfo.FindOfflineNameMatches(p, args[0]); target = Economy.FindMatches(p, data.Name, out money);
if (dbName == null) return; if (target == null) return;
Take(ref money, ref data);
ecos = Economy.RetrieveEcoStats(dbName); Economy.UpdateMoney(target, money);
Take(all, ref ecos, ref amount);
Chat.MessageAll("{0} %Stook &f{2} &3{3} %Sfrom &f{1}%S(offline)",
p.ColoredName, ecos.playerName, amount, Server.moneys);
} else { } else {
ecos = Economy.RetrieveEcoStats(who.name); target = who.name; money = who.money;
ecos.money = who.money; Take(ref money, ref data);
Take(all, ref ecos, ref amount); who.SetMoney(money);
who.SetMoney(ecos.money);
Chat.MessageAll("{0} %Stook &f{2} &3{3} %Sfrom {1}",
p.ColoredName, who.ColoredName, amount, Server.moneys);
} }
ecos.fine = "%f" + amount + " %3" + Server.moneys + " by " + takerRaw + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
Economy.UpdateEcoStats(ecos); string targetName = PlayerInfo.GetColoredName(p, target);
Chat.MessageAll("{0} %Stook &f{2} &3{3} %Sfrom {1}",
data.Source, targetName, data.Amount, Server.moneys);
Economy.EcoStats stats = Economy.RetrieveStats(target);
stats.Fine = "%f" + data.Amount + "%3 " + Server.moneys + " by "
+ data.SourceRaw + "%3 on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
Economy.UpdateStats(stats);
} }
static void Take(bool all, ref Economy.EcoStats ecos, ref int amount) { static void Take(ref int money, ref MoneyCmdData data) {
if (all || ecos.money < amount) { if (data.All || money < data.Amount) {
amount = ecos.money; data.Amount = money;
ecos.money = 0; money = 0;
} else { } else {
ecos.money -= amount; money -= data.Amount;
} }
} }
public override void Help(Player p){ public override void Help(Player p){
Player.Message(p, "%T/take [player] <amount>"); Player.Message(p, "%T/take [player] [amount[");
Player.Message(p, "%HTakes <amount> of " + Server.moneys + " from [player]"); Player.Message(p, "%HTakes [amount] of &3" + Server.moneys + " %Sfrom [player]");
Player.Message(p, "%T/take [player] all"); Player.Message(p, "%T/take [player] all");
Player.Message(p, "%HTakes all the " + Server.moneys + " from [player]"); Player.Message(p, "%HTakes all the &3" + Server.moneys + " %Sfrom [player]");
} }
} }
} }

View File

@ -0,0 +1,58 @@
/*
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
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.Globalization;
namespace MCGalaxy.Commands {
public abstract class MoneyCmd : Command {
public override string type { get { return CommandTypes.Economy; } }
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);
string[] args = message.Split(' ');
if (args.Length != 2) { Help(p); return false; }
data.Name = args[0];
if (p == null) {
data.SourceRaw = "(console)"; data.Source = "(console)";
} else {
data.SourceRaw = p.color + p.name; data.Source = p.ColoredName;
}
int amount = 0;
data.All = canAll && args[1].CaselessEq("all");
if (!data.All && !int.TryParse(args[1], out amount)) {
Player.Message(p, "Amount to {0} must be an integer.", action); return false;
}
if (amount < 0) {
Player.Message(p, "You cannot {0} negative &3" + Server.moneys, action); return false;
}
data.Amount = amount;
return true;
}
protected struct MoneyCmdData {
public string Source, SourceRaw, Name;
public int Amount;
public bool All;
}
}
}

View File

@ -41,28 +41,37 @@ PRIMARY KEY(player)
);"; );";
public struct EcoStats { public struct EcoStats {
public string playerName, purchase, payment, salary, fine; public string Player, Purchase, Payment, Salary, Fine;
public int money, totalSpent; public int TotalSpent;
public EcoStats(string name, int mon, int tot, string pur, string pay, string sal, string fin) {
playerName = name; public EcoStats(int tot, string player, string pur,
money = mon; string pay, string sal, string fin) {
totalSpent = tot; TotalSpent = tot;
purchase = pur; Player = player;
payment = pay; Purchase = pur;
salary = sal; Payment = pay;
fine = fin; Salary = sal;
Fine = fin;
} }
} }
public static void LoadDatabase() { public static void LoadDatabase() {
Database.Execute(createTable); Database.Execute(createTable);
DataTable eco = Database.Fill("SELECT (player, money) FROM Economy"); DataTable eco = Database.Fill("SELECT * FROM Economy");
foreach (DataRow row in eco) { foreach (DataRow row in eco.Rows) {
int money = PlayerData.ParseInt(row["money"].ToString()); int money = PlayerData.ParseInt(row["money"].ToString());
if (money == 0) continue; if (money == 0) continue;
UpdateMoney(row["player"].ToString(), money); EcoStats stats;
// TODO: remove zero money in Economy table stats.Player = row["player"].ToString();
stats.Payment = row["payment"].ToString();
stats.Purchase = row["purchase"].ToString();
stats.Salary = row["salary"].ToString();
stats.Fine = row["fine"].ToString();
stats.TotalSpent = PlayerData.ParseInt(row["total"].ToString());
UpdateMoney(stats.Player, money);
UpdateStats(stats);
} }
} }
@ -110,38 +119,43 @@ PRIMARY KEY(player)
} }
} }
} }
public static EcoStats RetrieveEcoStats(string playername) { public static void UpdateStats(EcoStats stats) {
EcoStats es = default(EcoStats);
es.playerName = playername;
using (DataTable eco = Database.Fill("SELECT * FROM Economy WHERE player=@0", playername)) {
if (eco.Rows.Count >= 1) {
es.money = int.Parse(eco.Rows[0]["money"].ToString());
es.totalSpent = int.Parse(eco.Rows[0]["total"].ToString());
es.purchase = eco.Rows[0]["purchase"].ToString();
es.payment = eco.Rows[0]["payment"].ToString();
es.salary = eco.Rows[0]["salary"].ToString();
es.fine = eco.Rows[0]["fine"].ToString();
} else {
es.purchase = "%cNone";
es.payment = "%cNone";
es.salary = "%cNone";
es.fine = "%cNone";
}
}
return es;
}
public static void UpdateEcoStats(EcoStats es) {
string type = Server.useMySQL ? "REPLACE INTO" : "INSERT OR REPLACE INTO"; string type = Server.useMySQL ? "REPLACE INTO" : "INSERT OR REPLACE INTO";
Database.Execute(type + " Economy (player, money, total, purchase, payment, salary, fine) " + Database.Execute(type + " Economy (player, money, total, purchase, payment, salary, fine) " +
"VALUES (@0, @1, @2, @3, @4, @5, @6)", es.playerName, es.money, es.totalSpent, "VALUES (@0, @1, @2, @3, @4, @5, @6)", stats.Player, 0, stats.TotalSpent,
es.purchase, es.payment, es.salary, es.fine); stats.Purchase, stats.Payment, stats.Salary, stats.Fine);
UpdateMoney(es.playerName, es.money); }
public static EcoStats RetrieveStats(string name) {
EcoStats stats = default(EcoStats);
stats.Player = name;
using (DataTable eco = Database.Fill("SELECT * FROM Economy WHERE player=@0", name)) {
if (eco.Rows.Count > 0) {
stats.TotalSpent = int.Parse(eco.Rows[0]["total"].ToString());
stats.Purchase = eco.Rows[0]["purchase"].ToString();
stats.Payment = eco.Rows[0]["payment"].ToString();
stats.Salary = eco.Rows[0]["salary"].ToString();
stats.Fine = eco.Rows[0]["fine"].ToString();
} else {
stats.Purchase = "%cNone";
stats.Payment = "%cNone";
stats.Salary = "%cNone";
stats.Fine = "%cNone";
}
}
return stats;
}
public static string FindMatches(Player p, string name, out int money) {
DataRow row = PlayerInfo.QueryMulti(p, name, "Name, Money");
money = row == null ? 0 : PlayerData.ParseInt(row["Money"].ToString());
return row == null ? null : row["Name"].ToString();
} }
public static void UpdateMoney(string name, int money) { public static void UpdateMoney(string name, int money) {
Database.Execute("UPDATE Players SET Money=@0 WHERE Name=@1", name, money); Database.Execute("UPDATE Players SET Money=@0 WHERE Name=@1", money, name);
} }
public static Item[] Items = { new ColorItem(), new TitleColorItem(), public static Item[] Items = { new ColorItem(), new TitleColorItem(),
@ -172,15 +186,15 @@ PRIMARY KEY(player)
public static RankItem Ranks { get { return (RankItem)Items[3]; } } public static RankItem Ranks { get { return (RankItem)Items[3]; } }
public static LevelItem Levels { get { return (LevelItem)Items[4]; } } public static LevelItem Levels { get { return (LevelItem)Items[4]; } }
public static void MakePurchase(Player p, int cost, string item) { public static void MakePurchase(Player p, int cost, string item) {
Economy.EcoStats ecos = RetrieveEcoStats(p.name);
p.SetMoney(p.money - cost); p.SetMoney(p.money - cost);
ecos.money = p.money; Player.Message(p, "Your balance is now &f{0} &3{1}", p.money, Server.moneys);
ecos.totalSpent += cost;
ecos.purchase = item + "%3 - Price: %f" + cost + " %3" + Server.moneys + Economy.EcoStats stats = RetrieveStats(p.name);
" - Date: %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture); stats.TotalSpent += cost;
UpdateEcoStats(ecos); stats.Purchase = item + "%3 for %f" + cost + " %3" + Server.moneys +
Player.Message(p, "%aYour balance is now %f" + p.money + " %3" + Server.moneys); " on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
Economy.UpdateStats(stats);
} }
} }
} }

View File

@ -216,6 +216,7 @@
<Compile Include="Commands\Economy\CmdPay.cs" /> <Compile Include="Commands\Economy\CmdPay.cs" />
<Compile Include="Commands\Economy\CmdStore.cs" /> <Compile Include="Commands\Economy\CmdStore.cs" />
<Compile Include="Commands\Economy\CmdTake.cs" /> <Compile Include="Commands\Economy\CmdTake.cs" />
<Compile Include="Commands\Economy\MoneyCmd.cs" />
<Compile Include="Commands\Fun\Cmd8Ball.cs" /> <Compile Include="Commands\Fun\Cmd8Ball.cs" />
<Compile Include="Commands\Fun\CmdAka.cs" /> <Compile Include="Commands\Fun\CmdAka.cs" />
<Compile Include="Commands\Fun\CmdCountdown.cs" /> <Compile Include="Commands\Fun\CmdCountdown.cs" />

View File

@ -180,14 +180,14 @@ namespace MCGalaxy {
} }
static DataTable Query(string name, string selector) { internal static DataTable Query(string name, string selector) {
string syntax = Server.useMySQL ? string syntax = Server.useMySQL ?
"SELECT " + selector + " FROM Players WHERE Name=@0 COLLATE utf8_general_ci" : "SELECT " + selector + " FROM Players WHERE Name=@0 COLLATE utf8_general_ci" :
"SELECT " + selector + " FROM Players WHERE Name=@0 COLLATE NOCASE"; "SELECT " + selector + " FROM Players WHERE Name=@0 COLLATE NOCASE";
return Database.Fill(syntax, name); return Database.Fill(syntax, name);
} }
static DataRow QueryMulti(Player p, string name, string selector) { internal static DataRow QueryMulti(Player p, string name, string selector) {
string syntax = Server.useMySQL ? string syntax = Server.useMySQL ?
"SELECT " + selector + " FROM Players WHERE Name LIKE @0 LIMIT 21" : "SELECT " + selector + " FROM Players WHERE Name LIKE @0 LIMIT 21" :
"SELECT " + selector + " FROM Players WHERE Name LIKE @0 LIMIT 21 COLLATE NOCASE"; "SELECT " + selector + " FROM Players WHERE Name LIKE @0 LIMIT 21 COLLATE NOCASE";