From bf291fa062d31ab953d140dd0f623a1def4e2958 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 5 Sep 2016 22:04:04 +1000 Subject: [PATCH] Use sleep(10) instead of sleep(3) to reduce CPU usage slightly, also add AddColumn to IDatabaseBackend. --- Commands/Economy/CmdBalance.cs | 2 +- Commands/Moderation/CmdInfoSwap.cs | 6 +++--- Commands/other/CmdRide.cs | 4 ++-- Database/IDatabaseBackend.cs | 5 +++++ Database/MySQL/MySQLBackend.cs | 9 +++++++++ Database/SQLite/SQLiteBackend.cs | 9 ++++++++- Server/Server.DB.cs | 8 ++++---- Server/Server.Tasks.cs | 2 +- 8 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Commands/Economy/CmdBalance.cs b/Commands/Economy/CmdBalance.cs index 3b48b2bb6..669b287f1 100644 --- a/Commands/Economy/CmdBalance.cs +++ b/Commands/Economy/CmdBalance.cs @@ -45,7 +45,7 @@ namespace MCGalaxy.Commands { } string targetName = PlayerInfo.GetColoredName(p, target); - Player.Message(p, "Economy stats for {0}%S:", targetName); + Player.Message(p, "Economy stats for {0}%S:", targetName); Player.Message(p, " Current balance: &f{0} &3{1}", money, Server.moneys); Economy.EcoStats ecos = Economy.RetrieveStats(target); diff --git a/Commands/Moderation/CmdInfoSwap.cs b/Commands/Moderation/CmdInfoSwap.cs index 661ee4b37..154f485fb 100644 --- a/Commands/Moderation/CmdInfoSwap.cs +++ b/Commands/Moderation/CmdInfoSwap.cs @@ -59,12 +59,12 @@ namespace MCGalaxy.Commands { Player.Message(p, "Cannot /infoswap for a player ranked equal or higher to yours."); return; } - Swap(src, dst); Swap(dst, src); + SetData(src, dst.Name); SetData(dst, src.Name); SwapGroups(src, dst, srcGroup, dstGroup); } const string format = "yyyy-MM-dd HH:mm:ss"; - void Swap(PlayerData src, PlayerData dst) { + void SetData(PlayerData src, string dstName) { string first = src.FirstLogin.ToString(format); string last = src.LastLogin.ToString(format); const string syntax = "UPDATE Players SET totalBlocks=@0, totalCuboided=@1" + @@ -75,7 +75,7 @@ namespace MCGalaxy.Commands { long cuboided = PlayerData.CuboidPacked(src.TotalDeleted, src.TotalDrawn); Database.Execute(syntax, blocks, cuboided, src.Color, src.Deaths, first, src.IP, src.Kicks, last, src.Logins, - src.Money, src.Title, src.TitleColor, src.TotalTime, dst.Name); + src.Money, src.Title, src.TitleColor, src.TotalTime, dstName); } void SwapGroups(PlayerData src, PlayerData dst, Group srcGroup, Group dstGroup) { diff --git a/Commands/other/CmdRide.cs b/Commands/other/CmdRide.cs index ddb8b4b5d..df4228d6b 100644 --- a/Commands/other/CmdRide.cs +++ b/Commands/other/CmdRide.cs @@ -37,7 +37,7 @@ namespace MCGalaxy.Commands { void DoRide(Player p) { while (p.onTrain) { - Thread.Sleep(3); + Thread.Sleep(10); ushort x = (ushort)(p.pos[0] / 32); ushort y = (ushort)(p.pos[1] / 32); ushort z = (ushort)(p.pos[2] / 32); @@ -74,7 +74,7 @@ namespace MCGalaxy.Commands { goto skip; } - Thread.Sleep(3); + Thread.Sleep(10); p.trainGrab = false; skip: ; diff --git a/Database/IDatabaseBackend.cs b/Database/IDatabaseBackend.cs index a700bd7d4..9d123a95e 100644 --- a/Database/IDatabaseBackend.cs +++ b/Database/IDatabaseBackend.cs @@ -53,6 +53,11 @@ namespace MCGalaxy.SQL { /// Removes all entries from the given table. public abstract void ClearTable(string table); + /// Adds a new coloumn to the given table. + /// Note colAfter is only a hint - some database backends ignore this. + public abstract void AddColumn(string table, string column, + string colype, string colAfter); + /// Inserts/Copies all the rows from the source table into the destination table. /// Note: This may work incorrectly if the tables have different schema. public virtual void CopyAllRows(string srcTable, string dstTable) { diff --git a/Database/MySQL/MySQLBackend.cs b/Database/MySQL/MySQLBackend.cs index c259d5af6..1a13279a8 100644 --- a/Database/MySQL/MySQLBackend.cs +++ b/Database/MySQL/MySQLBackend.cs @@ -63,5 +63,14 @@ namespace MCGalaxy.SQL { string syntax = "TRUNCATE TABLE `" + table + "`"; Database.Execute(syntax); } + + + public override void AddColumn(string table, string column, + string colType, string colAfter) { + string syntax = "ALTER TABLE `" + table + "` ADD COLUMN " + + column + " " + colType; + if (colAfter != "") syntax += " AFTER " + colAfter; + Database.Execute(syntax); + } } } diff --git a/Database/SQLite/SQLiteBackend.cs b/Database/SQLite/SQLiteBackend.cs index a1edfc217..fc651804e 100644 --- a/Database/SQLite/SQLiteBackend.cs +++ b/Database/SQLite/SQLiteBackend.cs @@ -47,7 +47,7 @@ namespace MCGalaxy.SQL { public override bool TableExists(string table) { using (DataTable results = GetRows("sqlite_master", "name", - "WHERE type='table' AND name=@0", table)) { + "WHERE type='table' AND name=@0", table)) { return results.Rows.Count > 0; } } @@ -61,5 +61,12 @@ namespace MCGalaxy.SQL { string syntax = "DELETE FROM `" + table + "`"; Database.Execute(syntax); } + + public override void AddColumn(string table, string column, + string colType, string colAfter) { + string syntax = "ALTER TABLE `" + table + "` ADD COLUMN " + + column + " " + colType; + Database.Execute(syntax); + } } } diff --git a/Server/Server.DB.cs b/Server/Server.DB.cs index 3e2ac18ba..2b60bbdd3 100644 --- a/Server/Server.DB.cs +++ b/Server/Server.DB.cs @@ -88,22 +88,22 @@ SELECT Time, Name, Cmd, Cmdmsg FROM Playercmds WHERE {0};"; // Check if the color column exists. DataTable colorExists = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='color'"); if (colorExists.Rows.Count == 0) - Database.Execute("ALTER TABLE Players ADD COLUMN color VARCHAR(6) AFTER totalKicked"); + Database.Backend.AddColumn("Players", "color", "VARCHAR(6)", "totalKicked"); colorExists.Dispose(); DataTable tcolorExists = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='title_color'"); if (tcolorExists.Rows.Count == 0) - Database.Execute("ALTER TABLE Players ADD COLUMN title_color VARCHAR(6) AFTER color"); + Database.Backend.AddColumn("Players", "title_color", "VARCHAR(6)", "color"); tcolorExists.Dispose(); DataTable timespent = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='TimeSpent'"); if (timespent.Rows.Count == 0) - Database.Execute("ALTER TABLE Players ADD COLUMN TimeSpent VARCHAR(20) AFTER totalKicked"); + Database.Backend.AddColumn("Players", "TimeSpent", "VARCHAR(20)", "totalKicked"); timespent.Dispose(); DataTable totalCuboided = Database.Fill("SHOW COLUMNS FROM Players WHERE `Field`='totalCuboided'"); if (totalCuboided.Rows.Count == 0) - Database.Execute("ALTER TABLE Players ADD COLUMN totalCuboided BIGINT AFTER totalBlocks"); + Database.Backend.AddColumn("Players", "totalCuboided", "BIGINT", "totalBlocks"); totalCuboided.Dispose(); } } diff --git a/Server/Server.Tasks.cs b/Server/Server.Tasks.cs index 9e5d01ba5..ce25d40c0 100644 --- a/Server/Server.Tasks.cs +++ b/Server/Server.Tasks.cs @@ -26,7 +26,7 @@ namespace MCGalaxy { void DoLocationChecks() { while (true) { Player[] players = PlayerInfo.Online.Items; - Thread.Sleep(players.Length == 0 ? 16 : 3); + Thread.Sleep(players.Length == 0 ? 20 : 10); players = PlayerInfo.Online.Items; for (int i = 0; i < players.Length; i++) {