fix /rules [name] sending to yourself, comments

This commit is contained in:
UnknownShadow200 2017-05-04 22:41:38 +10:00
parent e2c869d3fa
commit 9fcdaa4031
14 changed files with 172 additions and 115 deletions

View File

@ -47,7 +47,7 @@ namespace MCGalaxy.Commands {
Player.Message(p, "%T/buy [item] [value] <map name>");
Player.Message(p, "%Hmap name is only used for %T/buy map%H.");
Player.Message(p, "%HUse %T/store [item] %Hto see more information for an item.");
Player.Message(p, "%H Available items: %S" + Economy.GetItemNames());
Player.Message(p, "%H Available items: %S" + Economy.EnabledItemNames());
}
}
}

View File

@ -53,7 +53,7 @@ namespace MCGalaxy.Commands {
Player.Message(p, "%HViews information about the specific item, such as its cost.");
Player.Message(p, "%T/store");
Player.Message(p, "%HViews information about all enabled items.");
Player.Message(p, "%H Available items: %S" + Economy.GetItemNames());
Player.Message(p, "%H Available items: %S" + Economy.EnabledItemNames());
}
}
}

View File

@ -49,7 +49,7 @@ namespace MCGalaxy.Commands {
if (who != null) who.hasreadrules = true;
Player.Message(who, "Server Rules:");
Player.MessageLines(p, rules);
Player.MessageLines(who, rules);
if (who != null && who.name != p.name) {
Player.Message(p, "Sent the rules to " + who.ColoredName + "%S.");

View File

@ -28,7 +28,8 @@ namespace MCGalaxy.Commands {
public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
public CmdTop() { }
public override CommandAlias[] Aliases {
get { return new [] { new CommandAlias("topten", null, "10"), new CommandAlias("topfive", null, "5") }; }
get { return new [] { new CommandAlias("topten", null, "10"), new CommandAlias("topfive", null, "5"),
new CommandAlias("top10", null, "10"), }; }
}
public override void Use(Player p, string message) {

View File

@ -22,14 +22,18 @@ using System.Data.Common;
namespace MCGalaxy.SQL {
/// <summary> Callback function invoked on a row returned from an SQL query. </summary>
public delegate void ReaderCallback(IDataReader reader);
/// <summary> Represents an SQL command or query, that takes named parameters/arguments. </summary>
public abstract class ParameterisedQuery {
protected Dictionary<string, object> parameters = new Dictionary<string, object>();
/// <summary> Adds a named parameter/argument to this query. </summary>
public void AddParam(string name, object param) { parameters.Add(name, param); }
/// <summary> Clears the cached named parameters/arguments. </summary>
public void ClearParams() { parameters.Clear(); }
public static ParameterisedQuery Create() {
@ -48,6 +52,7 @@ namespace MCGalaxy.SQL {
protected abstract IDbDataParameter CreateParameter();
/// <summary> Executes an SQL command that does not return any results. </summary>
public void Execute(string query, string connString, bool createDB = false) {
using (IDbConnection conn = CreateConnection(connString)) {
conn.Open();
@ -62,6 +67,7 @@ namespace MCGalaxy.SQL {
}
}
/// <summary> Excecutes an SQL query, returning all rows into a single DataTable. </summary>
public void Fill(string query, string connString, DataTable toReturn) {
using (IDbConnection conn = CreateConnection(connString)) {
conn.Open();
@ -76,7 +82,8 @@ namespace MCGalaxy.SQL {
conn.Close();
}
}
/// <summary> Excecutes an SQL query, invoking a callback on the returned rows one by one. </summary>
public void ExecuteReader(string query, string connString, ReaderCallback callback) {
using (IDbConnection conn = CreateConnection(connString)) {
conn.Open();

View File

@ -1,9 +1,27 @@
/*
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.osedu.org/licenses/ECL-2.0
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.Collections.Generic;
using System.IO;
namespace MCGalaxy {
/// <summary> Stores per-player persistent data. </summary>
public static class PlayerDB {
public static string LoginPath(string name) {
@ -41,6 +59,8 @@ namespace MCGalaxy {
sw.WriteLine("Nick = " + p.DisplayName);
}
/// <summary> Retrieves the login message set for the given player. </summary>
public static string GetLoginMessage(Player p) {
if (!Directory.Exists("text/login"))
Directory.CreateDirectory("text/login");
@ -51,7 +71,8 @@ namespace MCGalaxy {
path = "text/login/" + p.name + ".txt";
return File.Exists(path) ? File.ReadAllText(path) : "connected";
}
/// <summary> Retrieves the logout message set for the given player. </summary>
public static string GetLogoutMessage(Player p) {
if (p.name == null) return "disconnected";
if (!Directory.Exists("text/logout"))
@ -63,15 +84,8 @@ namespace MCGalaxy {
path = "text/logout/" + p.name + ".txt";
return File.Exists(path) ? File.ReadAllText(path) : "disconnected";
}
public static void SetLoginMessage(string name, string value) {
File.WriteAllText(LoginPath(name), value);
}
public static void SetLogoutMessage(string name, string value) {
File.WriteAllText(LogoutPath(name), value);
}
/// <summary> Retrieves the ZS infect messages list for the given player. </summary>
public static List<string> GetInfectMessages(Player p) {
if (p.name == null || !Directory.Exists("text/infect")) return null;
string path = InfectPath(p.name);
@ -81,13 +95,24 @@ namespace MCGalaxy {
return new List<string>(lines);
}
public static void AppendInfectMessage(string name, string value) {
/// <summary> Sets the login message for the given player. </summary>
public static void SetLoginMessage(string name, string loginMsg) {
File.WriteAllText(LoginPath(name), loginMsg);
}
/// <summary> Sets the logout message for the given player. </summary>
public static void SetLogoutMessage(string name, string logoutMsg) {
File.WriteAllText(LogoutPath(name), logoutMsg);
}
public static void AppendInfectMessage(string name, string infectMsg) {
if (!Directory.Exists("text/infect"))
Directory.CreateDirectory("text/infect");
string path = InfectPath(name);
using (StreamWriter w = new StreamWriter(path, true))
w.WriteLine(value);
w.WriteLine(infectMsg);
}
}
}

View File

@ -0,0 +1,108 @@
/*
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.Data;
using MCGalaxy.SQL;
namespace MCGalaxy {
public static partial class Economy {
static ColumnDesc[] createEconomy = new ColumnDesc[] {
new ColumnDesc("player", ColumnType.VarChar, 20, priKey: true),
new ColumnDesc("money", ColumnType.Int32),
new ColumnDesc("total", ColumnType.Integer, notNull: true, def: "0"),
new ColumnDesc("purchase", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
new ColumnDesc("payment", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
new ColumnDesc("salary", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
new ColumnDesc("fine", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
};
public static void LoadDatabase() {
Database.Backend.CreateTable("Economy", createEconomy);
using (DataTable eco = Database.Backend.GetRows("Economy", "*"))
foreach (DataRow row in eco.Rows)
{
int money = PlayerData.ParseInt(row["money"].ToString());
if (money == 0) continue;
EcoStats stats;
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);
}
}
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) {
return PlayerInfo.FindOfflineMoneyMatches(p, name, out money);
}
public static void UpdateMoney(string name, int money) {
Database.Backend.UpdateRows("Players", "Money = @1", "WHERE Name = @0", name, money);
}
}
}

View File

@ -17,62 +17,14 @@
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using MCGalaxy.Eco;
using MCGalaxy.SQL;
namespace MCGalaxy {
public static class Economy {
public static partial class Economy {
public static bool Enabled;
static ColumnDesc[] createEconomy = new ColumnDesc[] {
new ColumnDesc("player", ColumnType.VarChar, 20, priKey: true),
new ColumnDesc("money", ColumnType.Int32),
new ColumnDesc("total", ColumnType.Integer, notNull: true, def: "0"),
new ColumnDesc("purchase", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
new ColumnDesc("payment", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
new ColumnDesc("salary", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
new ColumnDesc("fine", ColumnType.VarChar, 255, notNull: true, def: "'%cNone'"),
};
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 LoadDatabase() {
Database.Backend.CreateTable("Economy", createEconomy);
using (DataTable eco = Database.Backend.GetRows("Economy", "*"))
foreach (DataRow row in eco.Rows)
{
int money = PlayerData.ParseInt(row["money"].ToString());
if (money == 0) continue;
EcoStats stats;
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);
}
}
public static void Load() {
if (!File.Exists(Paths.EconomyPropsFile)) {
@ -120,44 +72,7 @@ namespace MCGalaxy {
}
}
}
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) {
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) {
Database.Backend.UpdateRows("Players", "Money = @1",
"WHERE Name = @0", name, money);
}
public static List<Item> Items = new List<Item>() { new ColorItem(), new TitleColorItem(),
new TitleItem(), new RankItem(), new LevelItem(), new LoginMessageItem(),
@ -165,6 +80,7 @@ namespace MCGalaxy {
new InfectMessageItem(), new NickItem(), new ReviveItem(),
new HumanInvisibilityItem(), new ZombieInvisibilityItem() };
/// <summary> Finds the item whose name or one of its aliases caselessly matches the input. </summary>
public static Item GetItem(string name) {
foreach (Item item in Items) {
if (name.CaselessEq(item.Name)) return item;
@ -176,7 +92,8 @@ namespace MCGalaxy {
return null;
}
public static string GetItemNames() {
/// <summary> Gets comma separated list of enabled items. </summary>
public static string EnabledItemNames() {
string items = Items.Join(x => x.Enabled ? x.ShopName : null);
return items.Length == 0 ? "(no enabled items)" : items;
}

View File

@ -181,7 +181,7 @@ namespace MCGalaxy {
return p.Rank >= target.Rank;
}
/// <summary> Updates the model of an entity to all other players in same level. </summary>
/// <summary> Updates the model of an entity to all players in same level. </summary>
public static void UpdateModel(Entity entity, string model) {
Player[] players = PlayerInfo.Online.Items;
entity.Model = model;

View File

@ -456,6 +456,7 @@
<Compile Include="Drawing\Transform\SimpleTransforms.cs" />
<Compile Include="Drawing\Transform\Transform.cs" />
<Compile Include="Economy\Awards.cs" />
<Compile Include="Economy\Economy.DB.cs" />
<Compile Include="Economy\ReviveItem.cs" />
<Compile Include="Economy\ZombieItems.cs" />
<Compile Include="Economy\Economy.cs" />

View File

@ -19,6 +19,7 @@ using System;
namespace MCGalaxy.Network {
/// <summary> List of packet opcode bytes. (Packet identifiers) </summary>
public static class Opcode {
public const byte Handshake = 0;

View File

@ -649,18 +649,13 @@ namespace MCGalaxy {
string HandleJoker(string text) {
if (!joker) return text;
if (!File.Exists("text/joker.txt")) {
File.Create("text/joker.txt").Dispose(); return text;
if (!File.Exists(Paths.JokerFile)) {
File.Create(Paths.JokerFile).Dispose(); return text;
}
Server.s.Log("<JOKER>: " + name + ": " + text);
Chat.MessageOps("%S<&aJ&bO&cK&5E&9R%S>: " + ColoredName + ":&f " + text);
List<string> lines = new List<string>();
using (StreamReader r = new StreamReader("text/joker.txt")) {
string line = null;
while ((line = r.ReadLine()) != null)
lines.Add(line);
}
List<string> lines = Utils.ReadAllLinesList(Paths.JokerFile);
Random rnd = new Random();
return lines.Count > 0 ? lines[rnd.Next(lines.Count)] : text;
}

View File

@ -33,6 +33,7 @@ namespace MCGalaxy {
public const string AliasesFile = "text/aliases.txt";
public const string NewsFile = "text/news.txt";
public const string WelcomeFile = "text/welcome.txt";
public const string JokerFile = "text/joker.txt";
public const string BlockPermsFile = "properties/block.properties";
@ -42,6 +43,7 @@ namespace MCGalaxy {
public const string ServerPropsFile = "properties/server.properties";
public const string RankPropsFile = "properties/ranks.properties";
public const string BotsFile = "extra/bots.json";
}
}

View File

@ -99,7 +99,7 @@ namespace MCGalaxy {
}
public static List<string> ReadAllLinesList(String path) {
public static List<string> ReadAllLinesList(string path) {
List<string> lines = new List<string>();
using (StreamReader r = new StreamReader(path)) {
string item;