From 5a9c13556056bc0d5fcfaf26416ad9d7dd37af6c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 11 Apr 2016 22:48:13 +1000 Subject: [PATCH] Move zombie game API into a separate file, splitup Server.Start into several logical functions. --- Commands/CmdOverseer.cs | 2 +- Games/ZombieSurvival/ZombieGame.Props.cs | 130 ++++++++++++++++ Games/ZombieSurvival/ZombieGame.cs | 103 ------------- MCGalaxy_.csproj | 1 + Server/Server.cs | 188 ++++++++++++----------- 5 files changed, 228 insertions(+), 196 deletions(-) create mode 100644 Games/ZombieSurvival/ZombieGame.Props.cs diff --git a/Commands/CmdOverseer.cs b/Commands/CmdOverseer.cs index e3f21a0ac..482c3aa06 100644 --- a/Commands/CmdOverseer.cs +++ b/Commands/CmdOverseer.cs @@ -241,7 +241,7 @@ namespace MCGalaxy.Commands Player.SendMessage(p, "/os map buildable -- Sets whether any blocks can be placed"); Player.SendMessage(p, "/os map deletable -- Sets whether any blocks can be deleted"); Player.SendMessage(p, " Textures: If your URL is too long, use the \"<\" symbol to continue it on another line."); - Player.SendMessage(p, " Map Types: Desert, flat, forest, island, mountians, ocean, pixel, empty and space"); + Player.SendMessage(p, " Map Types: Desert, flat, forest, island, mountains, ocean, pixel, empty and space"); Player.SendMessage(p, " Motd: If no message is provided, the default message will be used."); } } diff --git a/Games/ZombieSurvival/ZombieGame.Props.cs b/Games/ZombieSurvival/ZombieGame.Props.cs new file mode 100644 index 000000000..353185f9f --- /dev/null +++ b/Games/ZombieSurvival/ZombieGame.Props.cs @@ -0,0 +1,130 @@ +/* + Copyright 2010 MCLawl Team - + Created by Snowl (David D.) and Cazzar (Cayde D.) + + 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.Threading; + +namespace MCGalaxy.Games { + + public class BountyData { + public Player Origin; + public int Amount; + + public BountyData(Player origin, int amount) { + Origin = origin; Amount = amount; + } + } + + public enum ZombieGameStatus { NotStarted, InfiniteRounds, SingleRound, VariableRounds, LastRound } + + public sealed partial class ZombieGame { + + public const string InfectCol = "&infect"; + + /// The number of rounds that have been played in this game so far. + public int RoundsDone = 0; + + /// The maximum number of rounds that can be played before the game ends. + public int MaxRounds = 0; + + /// How precise collision detection is between alive and dead players. (Where 1 block = 32 units) + public int HitboxPrecision = 32; + + /// The maximum distance a player is allowed to move between movement packets. + public int MaxMoveDistance = 70; + + /// Current round status of the game. + public ZombieGameStatus Status = ZombieGameStatus.NotStarted; + + /// Gets whether zombie survival is currently running. + public bool Running { get { return Status != ZombieGameStatus.NotStarted; } } + + /// Whether a round is currently in progress. + public bool RoundInProgress = false; + + /// Time at which the next round is scheduled to start. + public DateTime RoundStart; + + /// Time at which the next round is scheduled to end. + public DateTime RoundEnd; + + public static System.Timers.Timer timer; + public bool initialChangeLevel = false; + + /// The name of the level that the last round of zombie survival was played on. + public string LastLevelName = ""; + + /// The name of the level that the current round of zombie survival is being played on. + public string CurLevelName = ""; + + /// The level that the current round of zombie survival is being played on. + public Level CurLevel = null; + + /// List of alive/human players. + public VolatileArray Alive = new VolatileArray(false); + + /// List of dead/infected players. + public VolatileArray Infected = new VolatileArray(false); + + /// Name of the player queued to be the first zombie in the next round. + public string QueuedZombie; + + /// Name of the level queued to be used for the next round. + public string QueuedLevel; + + /// Whether the server's main level should be set to the current level at the end of each round. + public bool SetMainLevel; + + /// Whether zombie survival should start upon server startup. + public bool StartImmediately; + + /// Whether changes made during a round of zombie survival should be permanently saved. + public bool SaveLevelBlockchanges; + + /// Whether maps with '+' in their name are ignored when choosing levels for the next round. + public bool IgnorePersonalWorlds = true; + + /// Whether the current level name should be shown in the heartbeats sent. + public bool IncludeMapInHeartbeat = false; + + static string[] messages = new string[] { "{0} WIKIWOO'D {1}", "{0} stuck their teeth into {1}", + "{0} licked {1}'s brain ", "{0} danubed {1}", "{0} made {1} meet their maker", "{0} tripped {1}", + "{0} made some zombie babies with {1}", "{0} made {1} see the dark side", "{0} tweeted {1}", + "{0} made {1} open source", "{0} infected {1}", "{0} iDotted {1}", "{1} got nommed on", + "{0} transplanted {1}'s living brain" }; + + internal bool noRespawn = true, noPillaring = true; + internal string ZombieName = "", ZombieModel = "zombie"; + internal bool ChangeLevels = true; + + /// List of levels that are randomly picked for zombie survival. + /// If this left blank, then all level files are picked from instead. + internal List LevelList = new List(); + + /// List of levels that are never picked for zombie survival. + internal List IgnoredLevelList = new List(); + + string lastLevel1 = "", lastLevel2 = ""; + int Level1Vote = 0, Level2Vote = 0, Level3Vote = 0; + + string lastPlayerToInfect = ""; + int infectCombo = 0; + public Dictionary Bounties = new Dictionary(); + } +} diff --git a/Games/ZombieSurvival/ZombieGame.cs b/Games/ZombieSurvival/ZombieGame.cs index da5f3227d..ae16ff241 100644 --- a/Games/ZombieSurvival/ZombieGame.cs +++ b/Games/ZombieSurvival/ZombieGame.cs @@ -22,110 +22,7 @@ using System.Threading; namespace MCGalaxy.Games { - public class BountyData { - public Player Origin; - public int Amount; - - public BountyData(Player origin, int amount) { - Origin = origin; Amount = amount; - } - } - - public enum ZombieGameStatus { NotStarted, InfiniteRounds, SingleRound, VariableRounds, LastRound } - public sealed partial class ZombieGame { - - public const string InfectCol = "&infect"; - - /// The number of rounds that have been played in this game so far. - public int RoundsDone = 0; - - /// The maximum number of rounds that can be played before the game ends. - public int MaxRounds = 0; - - /// How precise collision detection is between alive and dead players. (Where 1 block = 32 units) - public int HitboxPrecision = 32; - - /// The maximum distance a player is allowed to move between movement packets. - public int MaxMoveDistance = 70; - - /// Current round status of the game. - public ZombieGameStatus Status = ZombieGameStatus.NotStarted; - - /// Gets whether zombie survival is currently running. - public bool Running { get { return Status != ZombieGameStatus.NotStarted; } } - - /// Whether a round is currently in progress. - public bool RoundInProgress = false; - - /// Time at which the next round is scheduled to start. - public DateTime RoundStart; - - /// Time at which the next round is scheduled to end. - public DateTime RoundEnd; - - public static System.Timers.Timer timer; - public bool initialChangeLevel = false; - - /// The name of the level that the last round of zombie survival was played on. - public string LastLevelName = ""; - - /// The name of the level that the current round of zombie survival is being played on. - public string CurLevelName = ""; - - /// The level that the current round of zombie survival is being played on. - public Level CurLevel = null; - - /// List of alive/human players. - public VolatileArray Alive = new VolatileArray(false); - - /// List of dead/infected players. - public VolatileArray Infected = new VolatileArray(false); - - /// Name of the player queued to be the first zombie in the next round. - public string QueuedZombie; - - /// Name of the level queued to be used for the next round. - public string QueuedLevel; - - /// Whether the server's main level should be set to the current level at the end of each round. - public bool SetMainLevel; - - /// Whether zombie survival should start upon server startup. - public bool StartImmediately; - - /// Whether changes made during a round of zombie survival should be permanently saved. - public bool SaveLevelBlockchanges; - - /// Whether maps with '+' in their name are ignored when choosing levels for the next round. - public bool IgnorePersonalWorlds = true; - - /// Whether the current level name should be shown in the heartbeats sent. - public bool IncludeMapInHeartbeat = false; - - static string[] messages = new string[] { "{0} WIKIWOO'D {1}", "{0} stuck their teeth into {1}", - "{0} licked {1}'s brain ", "{0} danubed {1}", "{0} made {1} meet their maker", "{0} tripped {1}", - "{0} made some zombie babies with {1}", "{0} made {1} see the dark side", "{0} tweeted {1}", - "{0} made {1} open source", "{0} infected {1}", "{0} iDotted {1}", "{1} got nommed on", - "{0} transplanted {1}'s living brain" }; - - internal bool noRespawn = true, noPillaring = true; - internal string ZombieName = "", ZombieModel = "zombie"; - internal bool ChangeLevels = true; - - /// List of levels that are randomly picked for zombie survival. - /// If this left blank, then all level files are picked from instead. - internal List LevelList = new List(); - - /// List of levels that are never picked for zombie survival. - internal List IgnoredLevelList = new List(); - - string lastLevel1 = "", lastLevel2 = ""; - int Level1Vote = 0, Level2Vote = 0, Level3Vote = 0; - - string lastPlayerToInfect = ""; - int infectCombo = 0; - public Dictionary Bounties = new Dictionary(); public void Start(ZombieGameStatus status, int amount) { Status = status; diff --git a/MCGalaxy_.csproj b/MCGalaxy_.csproj index 76c3338d3..3ff8bb0a1 100644 --- a/MCGalaxy_.csproj +++ b/MCGalaxy_.csproj @@ -459,6 +459,7 @@ + PropertyWindow.cs diff --git a/Server/Server.cs b/Server/Server.cs index adf9cb3d5..d46e1b39e 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -354,6 +354,41 @@ namespace MCGalaxy CheckFile("Newtonsoft.Json.dll"); CheckFile("LibNoise.dll"); + EnsureFilesExist(); + MoveOutdatedFiles(); + Chat.LoadCustomTokens(); + + if (File.Exists("text/emotelist.txt")) { + foreach (string s in File.ReadAllLines("text/emotelist.txt")) + Player.emoteList.Add(s); + } else { + File.Create("text/emotelist.txt").Dispose(); + } + + lava = new LavaSurvival(); + zombie = new ZombieGame(); + Countdown = new CountdownGame(); + LoadAllSettings(); + + InitDatabase(); + Economy.LoadDatabase(); + + Level[] loaded = LevelInfo.Loaded.Items; + foreach (Level l in loaded) + l.Unload(); + ml.Queue(LoadMainLevel); + Plugin.Load(); + ml.Queue(LoadPlayerLists); + ml.Queue(LoadAutoloadCommands); + ml.Queue(LoadGCAccepted); + + ml.Queue(InitTimers); + ml.Queue(InitRest); + ml.Queue(InitHeartbeat); + UpdateStaffList(); + } + + void EnsureFilesExist() { if (!Directory.Exists("properties")) Directory.CreateDirectory("properties"); if (!Directory.Exists("levels")) Directory.CreateDirectory("levels"); if (!Directory.Exists("bots")) Directory.CreateDirectory("bots"); @@ -371,9 +406,10 @@ namespace MCGalaxy if (!Directory.Exists("extra/copyBackup/")) Directory.CreateDirectory("extra/copyBackup/"); if (!Directory.Exists("extra/Waypoints")) Directory.CreateDirectory("extra/Waypoints"); if (!Directory.Exists("blockdefs")) Directory.CreateDirectory("blockdefs"); - - try - { + } + + void MoveOutdatedFiles() { + try { if (File.Exists("blocks.json")) File.Move("blocks.json", "blockdefs/global.json"); if (File.Exists("server.properties")) File.Move("server.properties", "properties/server.properties"); if (File.Exists("rules.txt")) File.Move("rules.txt", "text/rules.txt"); @@ -385,100 +421,68 @@ namespace MCGalaxy if (useWhitelist && File.Exists("whitelist.txt")) File.Move("whitelist.txt", "ranks/whitelist.txt"); } catch { } - Chat.LoadCustomTokens(); - - if (File.Exists("text/emotelist.txt")) { - foreach (string s in File.ReadAllLines("text/emotelist.txt")) - Player.emoteList.Add(s); - } else { - File.Create("text/emotelist.txt").Dispose(); + } + + void InitDatabase() { + try { + if (Server.useMySQL) + Database.executeQuery("CREATE DATABASE if not exists `" + MySQLDatabaseName + "`", true); + } catch (Exception e) { + ErrorLog(e); + s.Log("MySQL settings have not been set! Please Setup using the properties window."); + //process.Kill(); + return; } - - - lava = new LavaSurvival(); - zombie = new ZombieGame(); - Countdown = new CountdownGame(); - - LoadAllSettings(); - - {//MYSQL stuff - try - { - if (Server.useMySQL) - Database.executeQuery("CREATE DATABASE if not exists `" + MySQLDatabaseName + "`", true); - } - //catch (MySql.Data.MySqlClient.MySqlException e) - //{ - // Server.s.Log("MySQL settings have not been set! Many features will not be available if MySQL is not enabled"); - // // Server.ErrorLog(e); - //} - catch (Exception e) - { - ErrorLog(e); - s.Log("MySQL settings have not been set! Please Setup using the properties window."); - //process.Kill(); - return; - } - Database.executeQuery(string.Format("CREATE TABLE if not exists Players (ID INTEGER {0}AUTO{1}INCREMENT NOT NULL, Name TEXT, IP CHAR(15), FirstLogin DATETIME, LastLogin DATETIME, totalLogin MEDIUMINT, Title CHAR(20), TotalDeaths SMALLINT, Money MEDIUMINT UNSIGNED, totalBlocks BIGINT, totalCuboided BIGINT, totalKicked MEDIUMINT, TimeSpent VARCHAR(20), color VARCHAR(6), title_color VARCHAR(6){2});", (useMySQL ? "" : "PRIMARY KEY "), (useMySQL ? "_" : ""), (Server.useMySQL ? ", PRIMARY KEY (ID)" : ""))); - Database.executeQuery(string.Format("CREATE TABLE if not exists Opstats (ID INTEGER {0}AUTO{1}INCREMENT NOT NULL, Time DATETIME, Name TEXT, Cmd VARCHAR(40), Cmdmsg VARCHAR(40){2});", (useMySQL ? "" : "PRIMARY KEY "), (useMySQL ? "_" : ""), (Server.useMySQL ? ", PRIMARY KEY (ID)" : ""))); - if (!File.Exists("extra/alter.txt") && Server.useMySQL) { - Database.executeQuery("ALTER TABLE Players MODIFY Name TEXT"); - Database.executeQuery("ALTER TABLE Opstats MODIFY Name TEXT"); - File.Create("extra/alter.txt"); - } - //since 5.5.11 we are cleaning up the table Playercmds - string query = Server.useMySQL ? "SHOW TABLES LIKE 'Playercmds'" : "SELECT name FROM sqlite_master WHERE type='table' AND name='Playercmds';"; - DataTable playercmds = Database.fillData(query); DataTable opstats = Database.fillData("SELECT * FROM Opstats"); - //if Playercmds exists copy-filter to Ostats and remove Playercmds - if (playercmds.Rows.Count != 0) { - foreach (string cmd in Server.Opstats) - Database.executeQuery(string.Format("INSERT INTO Opstats (Time, Name, Cmd, Cmdmsg) SELECT Time, Name, Cmd, Cmdmsg FROM Playercmds WHERE cmd = '{0}';", cmd)); - Database.executeQuery("INSERT INTO Opstats (Time, Name, Cmd, Cmdmsg) SELECT Time, Name, Cmd, Cmdmsg FROM Playercmds WHERE cmd = 'review' AND cmdmsg = 'next';"); - Database.fillData("DROP TABLE Playercmds"); - } - playercmds.Dispose(); opstats.Dispose(); - - // Here, since SQLite is a NEW thing from 5.3.0.0, we do not have to check for existing tables in SQLite. - if (useMySQL) - { - // Check if the color column exists. - DataTable colorExists = Database.fillData("SHOW COLUMNS FROM Players WHERE `Field`='color'"); - if (colorExists.Rows.Count == 0) - Database.executeQuery("ALTER TABLE Players ADD COLUMN color VARCHAR(6) AFTER totalKicked"); - colorExists.Dispose(); - - DataTable tcolorExists = Database.fillData("SHOW COLUMNS FROM Players WHERE `Field`='title_color'"); - if (tcolorExists.Rows.Count == 0) - Database.executeQuery("ALTER TABLE Players ADD COLUMN title_color VARCHAR(6) AFTER color"); - tcolorExists.Dispose(); - - DataTable timespent = Database.fillData("SHOW COLUMNS FROM Players WHERE `Field`='TimeSpent'"); - if (timespent.Rows.Count == 0) - Database.executeQuery("ALTER TABLE Players ADD COLUMN TimeSpent VARCHAR(20) AFTER totalKicked"); - timespent.Dispose(); - - DataTable totalCuboided = Database.fillData("SHOW COLUMNS FROM Players WHERE `Field`='totalCuboided'"); - if (totalCuboided.Rows.Count == 0) - Database.executeQuery("ALTER TABLE Players ADD COLUMN totalCuboided BIGINT AFTER totalBlocks"); - totalCuboided.Dispose(); - } + + string autoInc = useMySQL ? "AUTO_INCREMENT" : "AUTOINCREMENT"; + Database.executeQuery(string.Format("CREATE TABLE if not exists Players (ID INTEGER {0}" + autoInc + " NOT NULL, " + + "Name TEXT, IP CHAR(15), FirstLogin DATETIME, LastLogin DATETIME, totalLogin MEDIUMINT, " + + "Title CHAR(20), TotalDeaths SMALLINT, Money MEDIUMINT UNSIGNED, totalBlocks BIGINT, " + + "totalCuboided BIGINT, totalKicked MEDIUMINT, TimeSpent VARCHAR(20), color VARCHAR(6), " + + "title_color VARCHAR(6){1});", (useMySQL ? "" : "PRIMARY KEY "), (useMySQL ? ", PRIMARY KEY (ID)" : ""))); + Database.executeQuery(string.Format("CREATE TABLE if not exists Opstats (ID INTEGER {0}" + autoInc + " NOT NULL, " + + "Time DATETIME, Name TEXT, Cmd VARCHAR(40), Cmdmsg VARCHAR(40){1});", + (useMySQL ? "" : "PRIMARY KEY "), (useMySQL ? ", PRIMARY KEY (ID)" : ""))); + if (!File.Exists("extra/alter.txt") && useMySQL) { + Database.executeQuery("ALTER TABLE Players MODIFY Name TEXT"); + Database.executeQuery("ALTER TABLE Opstats MODIFY Name TEXT"); + File.Create("extra/alter.txt"); } + + //since 5.5.11 we are cleaning up the table Playercmds + string query = Server.useMySQL ? "SHOW TABLES LIKE 'Playercmds'" : "SELECT name FROM sqlite_master WHERE type='table' AND name='Playercmds';"; + DataTable playercmds = Database.fillData(query), opstats = Database.fillData("SELECT * FROM Opstats"); + //if Playercmds exists copy-filter to Ostats and remove Playercmds + if (playercmds.Rows.Count != 0) { + foreach (string cmd in Server.Opstats) + Database.executeQuery(string.Format("INSERT INTO Opstats (Time, Name, Cmd, Cmdmsg) SELECT Time, Name, Cmd, Cmdmsg FROM Playercmds WHERE cmd = '{0}';", cmd)); + Database.executeQuery("INSERT INTO Opstats (Time, Name, Cmd, Cmdmsg) SELECT Time, Name, Cmd, Cmdmsg FROM Playercmds WHERE cmd = 'review' AND cmdmsg = 'next';"); + Database.fillData("DROP TABLE Playercmds"); + } + playercmds.Dispose(); opstats.Dispose(); - Economy.LoadDatabase(); + // Here, since SQLite is a NEW thing from 5.3.0.0, we do not have to check for existing tables in SQLite. + if (!useMySQL) return; + // Check if the color column exists. + DataTable colorExists = Database.fillData("SHOW COLUMNS FROM Players WHERE `Field`='color'"); + if (colorExists.Rows.Count == 0) + Database.executeQuery("ALTER TABLE Players ADD COLUMN color VARCHAR(6) AFTER totalKicked"); + colorExists.Dispose(); - Level[] loaded = LevelInfo.Loaded.Items; - foreach (Level l in loaded) - l.Unload(); - ml.Queue(LoadMainLevel); - Plugin.Load(); - ml.Queue(LoadPlayerLists); - ml.Queue(LoadAutoloadCommands); - ml.Queue(LoadGCAccepted); + DataTable tcolorExists = Database.fillData("SHOW COLUMNS FROM Players WHERE `Field`='title_color'"); + if (tcolorExists.Rows.Count == 0) + Database.executeQuery("ALTER TABLE Players ADD COLUMN title_color VARCHAR(6) AFTER color"); + tcolorExists.Dispose(); - ml.Queue(InitTimers); - ml.Queue(InitRest); - ml.Queue(InitHeartbeat); - UpdateStaffList(); + DataTable timespent = Database.fillData("SHOW COLUMNS FROM Players WHERE `Field`='TimeSpent'"); + if (timespent.Rows.Count == 0) + Database.executeQuery("ALTER TABLE Players ADD COLUMN TimeSpent VARCHAR(20) AFTER totalKicked"); + timespent.Dispose(); + + DataTable totalCuboided = Database.fillData("SHOW COLUMNS FROM Players WHERE `Field`='totalCuboided'"); + if (totalCuboided.Rows.Count == 0) + Database.executeQuery("ALTER TABLE Players ADD COLUMN totalCuboided BIGINT AFTER totalBlocks"); + totalCuboided.Dispose(); } public static string SendResponse(HttpListenerRequest request)