mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-22 12:05:51 -04:00
Simplify economy code a bit
This commit is contained in:
parent
9805380698
commit
9343f7c995
@ -138,8 +138,6 @@ namespace MCGalaxy.SQL {
|
|||||||
if (col.PrimaryKey) priKey = col.Column;
|
if (col.PrimaryKey) priKey = col.Column;
|
||||||
if (col.AutoIncrement) sql.Append(" AUTO_INCREMENT");
|
if (col.AutoIncrement) sql.Append(" AUTO_INCREMENT");
|
||||||
if (col.NotNull) sql.Append(" NOT NULL");
|
if (col.NotNull) sql.Append(" NOT NULL");
|
||||||
if (col.DefaultValue != null)
|
|
||||||
sql.Append(" DEFAULT ").Append(col.DefaultValue);
|
|
||||||
|
|
||||||
if (i < columns.Length - 1) {
|
if (i < columns.Length - 1) {
|
||||||
sql.Append(',');
|
sql.Append(',');
|
||||||
|
@ -124,8 +124,6 @@ namespace MCGalaxy.SQL {
|
|||||||
}
|
}
|
||||||
if (col.AutoIncrement) sql.Append(" AUTOINCREMENT");
|
if (col.AutoIncrement) sql.Append(" AUTOINCREMENT");
|
||||||
if (col.NotNull) sql.Append(" NOT NULL");
|
if (col.NotNull) sql.Append(" NOT NULL");
|
||||||
if (col.DefaultValue != null)
|
|
||||||
sql.Append(" DEFAULT ").Append(col.DefaultValue);
|
|
||||||
|
|
||||||
if (i < columns.Length - 1) {
|
if (i < columns.Length - 1) {
|
||||||
sql.Append(',');
|
sql.Append(',');
|
||||||
|
@ -28,23 +28,20 @@ namespace MCGalaxy.SQL {
|
|||||||
public readonly bool AutoIncrement;
|
public readonly bool AutoIncrement;
|
||||||
public readonly bool PrimaryKey;
|
public readonly bool PrimaryKey;
|
||||||
public readonly bool NotNull;
|
public readonly bool NotNull;
|
||||||
public readonly string DefaultValue;
|
|
||||||
|
|
||||||
public ColumnDesc(string col, ColumnType type)
|
public ColumnDesc(string col, ColumnType type)
|
||||||
: this(col, type, 0, false, false, false, null) { }
|
: this(col, type, 0, false, false, false) { }
|
||||||
public ColumnDesc(string col, ColumnType type, ushort maxLen = 0)
|
public ColumnDesc(string col, ColumnType type, ushort maxLen = 0)
|
||||||
: this(col, type, maxLen, false, false, false, null) { }
|
: this(col, type, maxLen, false, false, false) { }
|
||||||
|
|
||||||
public ColumnDesc(string col, ColumnType type, ushort maxLen = 0,
|
public ColumnDesc(string col, ColumnType type, ushort maxLen = 0,
|
||||||
bool autoInc = false, bool priKey = false,
|
bool autoInc = false, bool priKey = false, bool notNull = false) {
|
||||||
bool notNull = false, string def = null) {
|
|
||||||
Column = col;
|
Column = col;
|
||||||
Type = type;
|
Type = type;
|
||||||
MaxLength = maxLen;
|
MaxLength = maxLen;
|
||||||
AutoIncrement = autoInc;
|
AutoIncrement = autoInc;
|
||||||
PrimaryKey = priKey;
|
PrimaryKey = priKey;
|
||||||
NotNull = notNull;
|
NotNull = notNull;
|
||||||
DefaultValue = def;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string FormatType() {
|
public string FormatType() {
|
||||||
|
@ -26,13 +26,13 @@ namespace MCGalaxy.Eco {
|
|||||||
static ColumnDesc[] createEconomy = new ColumnDesc[] {
|
static ColumnDesc[] createEconomy = new ColumnDesc[] {
|
||||||
new ColumnDesc("player", ColumnType.VarChar, 20, priKey: true),
|
new ColumnDesc("player", ColumnType.VarChar, 20, priKey: true),
|
||||||
new ColumnDesc("money", ColumnType.Int32),
|
new ColumnDesc("money", ColumnType.Int32),
|
||||||
new ColumnDesc("total", ColumnType.Integer, notNull: true, def: "0"),
|
new ColumnDesc("total", ColumnType.Integer),
|
||||||
new ColumnDesc("purchase", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
|
new ColumnDesc("purchase", ColumnType.VarChar, 255),
|
||||||
new ColumnDesc("payment", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
|
new ColumnDesc("payment", ColumnType.VarChar, 255),
|
||||||
new ColumnDesc("salary", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
|
new ColumnDesc("salary", ColumnType.VarChar, 255),
|
||||||
new ColumnDesc("fine", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
|
new ColumnDesc("fine", ColumnType.VarChar, 255),
|
||||||
};
|
};
|
||||||
|
|
||||||
public static void LoadDatabase() {
|
public static void LoadDatabase() {
|
||||||
Database.Backend.CreateTable("Economy", createEconomy);
|
Database.Backend.CreateTable("Economy", createEconomy);
|
||||||
using (DataTable eco = Database.Backend.GetRows("Economy", "*"))
|
using (DataTable eco = Database.Backend.GetRows("Economy", "*"))
|
||||||
@ -41,62 +41,14 @@ namespace MCGalaxy.Eco {
|
|||||||
int money = PlayerData.ParseInt(row["money"].ToString());
|
int money = PlayerData.ParseInt(row["money"].ToString());
|
||||||
if (money == 0) continue;
|
if (money == 0) continue;
|
||||||
|
|
||||||
EcoStats stats;
|
EcoStats stats = default(EcoStats);
|
||||||
stats.Player = row["player"].ToString();
|
stats.Player = row["player"].ToString();
|
||||||
stats.Payment = row["payment"].ToString();
|
ParseRow(row, ref stats);
|
||||||
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);
|
UpdateMoney(stats.Player, money);
|
||||||
UpdateStats(stats);
|
UpdateStats(stats);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public struct EcoStats {
|
|
||||||
public string Player, Purchase, Payment, Salary, Fine;
|
|
||||||
public int TotalSpent;
|
|
||||||
|
|
||||||
public EcoStats(int tot, string player, string pur,
|
|
||||||
string pay, string sal, string fin) {
|
|
||||||
TotalSpent = tot;
|
|
||||||
Player = player;
|
|
||||||
Purchase = pur;
|
|
||||||
Payment = pay;
|
|
||||||
Salary = sal;
|
|
||||||
Fine = fin;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void UpdateStats(EcoStats stats) {
|
|
||||||
Database.Backend.AddOrReplaceRow("Economy", "player, money, total, purchase, payment, salary, fine",
|
|
||||||
stats.Player, 0, stats.TotalSpent, stats.Purchase,
|
|
||||||
stats.Payment, stats.Salary, stats.Fine);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static EcoStats RetrieveStats(string name) {
|
|
||||||
EcoStats stats = default(EcoStats);
|
|
||||||
stats.Player = name;
|
|
||||||
|
|
||||||
using (DataTable eco = Database.Backend.GetRows("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) {
|
public static string FindMatches(Player p, string name, out int money) {
|
||||||
return PlayerInfo.FindOfflineMoneyMatches(p, name, out money);
|
return PlayerInfo.FindOfflineMoneyMatches(p, name, out money);
|
||||||
@ -105,5 +57,44 @@ namespace MCGalaxy.Eco {
|
|||||||
public static void UpdateMoney(string name, int money) {
|
public static void UpdateMoney(string name, int money) {
|
||||||
Database.Backend.UpdateRows("Players", "Money = @1", "WHERE Name = @0", name, money);
|
Database.Backend.UpdateRows("Players", "Money = @1", "WHERE Name = @0", name, money);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public struct EcoStats {
|
||||||
|
public string Player, Purchase, Payment, Salary, Fine; public int TotalSpent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void UpdateStats(EcoStats stats) {
|
||||||
|
Database.Backend.AddOrReplaceRow("Economy", "player, money, total, purchase, payment, salary, fine",
|
||||||
|
stats.Player, 0, stats.TotalSpent, stats.Purchase,
|
||||||
|
stats.Payment, stats.Salary, stats.Fine);
|
||||||
|
}
|
||||||
|
|
||||||
|
static string ParseField(object raw) {
|
||||||
|
if (raw == null) return "%cNone";
|
||||||
|
string value = raw.ToString();
|
||||||
|
return (value.Length == 0 || value.CaselessEq("NULL")) ? "%cNone" : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ParseRow(DataRow row, ref EcoStats stats) {
|
||||||
|
stats.Payment = ParseField(row["payment"]);
|
||||||
|
stats.Purchase = ParseField(row["purchase"]);
|
||||||
|
stats.Salary = ParseField(row["salary"]);
|
||||||
|
stats.Fine = ParseField(row["fine"]);
|
||||||
|
stats.TotalSpent = PlayerData.ParseInt(row["total"].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static EcoStats RetrieveStats(string name) {
|
||||||
|
EcoStats stats = default(EcoStats);
|
||||||
|
stats.Player = name;
|
||||||
|
stats.Purchase = "%cNone";
|
||||||
|
stats.Payment = "%cNone";
|
||||||
|
stats.Salary = "%cNone";
|
||||||
|
stats.Fine = "%cNone";
|
||||||
|
|
||||||
|
using (DataTable eco = Database.Backend.GetRows("Economy", "*", "WHERE player=@0", name)) {
|
||||||
|
if (eco.Rows.Count > 0) ParseRow(eco.Rows[0], ref stats);
|
||||||
|
}
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user