/// <summary> /// Saves the report to be read by the owner with /reports /// </summary> /// <param name="report">Report being saved</param> public static void SaveReport(Report report) { try { String[] reportData = {report.Sender, report.Datesent.ToBinary().ToString(), report.Message }; if (!Directory.Exists("./Reports")) { Directory.CreateDirectory("./Reports"); } File.WriteAllLines("./Reports/" + report.Id + "-" + report.Sender + ".txt", reportData); } catch (Exception ex) { Player.Console.Message("Report Saver Has Crashed: {0}", ex); } }
private static void reportHandler(Player player, CommandReader cmd) { if (player.DetectChatSpam()) return; string message = cmd.NextAll(); Report rCreate = new Report(); if (cmd.IsConfirmed) { rCreate.addReport(getNewReportId(), player.Name, DateTime.Now, message); player.Message("Report sent!"); foreach (Player p in Server.Players.Where(q => q.Info.Rank == RankManager.HighestRank)) { if (p.Supports(CpeExtension.MessageType)) { p.Send(Packet.Message((byte)MessageType.Announcement, String.Format("Player {0} has sent in a report!", player.Name))); } p.Message("Player {0} has sent in a report!", player.Name); } return; } if (message.Length < 1) { CdReport.PrintUsage(player); } else { player.Confirm(cmd, "&sYour message will show up like this: &n" + "&s[&1Report&s]&n" + " &sFrom:&f {0}&n" + " &sDate: &7{1} at {2}&n" + " &sMessage:&f {3}", player.Name, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString(), message); } }
/// <summary> Starts the server: /// Creates Console pseudoplayer, loads the world list, starts listening for incoming connections, /// sets up scheduled tasks and starts the scheduler, starts the heartbeat, and connects to IRC. /// Raises Server.Starting and Server.Started events. /// May throw an exception on hard failure. </summary> /// <returns> True if server started normally, false on soft failure. </returns> /// <exception cref="System.InvalidOperationException"> Server is already running, or server/library have not been initailized. </exception> public static bool StartServer() { if( IsRunning ) { throw new InvalidOperationException( "Server is already running" ); } if( !libraryInitialized || !serverInitialized ) { throw new InvalidOperationException( "Server.InitLibrary and Server.InitServer must be called before Server.StartServer" ); } StartTime = DateTime.UtcNow; cpuUsageStartingOffset = Process.GetCurrentProcess().TotalProcessorTime; Players = new Player[0]; RaiseEvent( Starting ); if( ConfigKey.BackupDataOnStartup.Enabled() ) { BackupData(); } Player.Console = new Player( ConfigKey.ConsoleName.GetString() ); Player.AutoRank = new Player( "(AutoRank)" ); // Back up server data (PlayerDB, worlds, bans, config) if( ConfigKey.BlockDBEnabled.Enabled() ) { BlockDB.Init(); } // Load the world list if( !WorldManager.LoadWorldList() ) return false; WorldManager.SaveWorldList(); // Back up all worlds (if needed) if( ConfigKey.BackupOnStartup.Enabled() ) { foreach( World world in WorldManager.Worlds ) { string backupFileName = String.Format( World.TimedBackupFormat, world.Name, DateTime.Now ); // localized world.SaveBackup( Path.Combine( Paths.BackupPath, backupFileName ) ); } } // open the port Port = ConfigKey.Port.GetInt(); InternalIP = IPAddress.Parse( ConfigKey.IP.GetString() ); try { listener = new TcpListener( InternalIP, Port ); listener.Start(); } catch( Exception ex ) { // if the port is unavailable Logger.Log( LogType.Error, "Could not start listening on port {0}, stopping. ({1})", Port, ex.Message ); if( !ConfigKey.IP.IsDefault() ) { Logger.Log( LogType.Warning, "Do not use the \"Designated IP\" setting unless you have multiple NICs or IPs." ); } return false; } // Resolve internal and external IP addresses InternalIP = ( (IPEndPoint)listener.LocalEndpoint ).Address; ExternalIP = CheckExternalIP(); if( ExternalIP == null ) { Logger.Log( LogType.SystemActivity, "&3Server.Run: now accepting connections on port {0}", Port ); } else { Logger.Log( LogType.SystemActivity, "&3Server.Run: now accepting connections at {0}:{1}", ExternalIP, Port ); } // list loaded worlds WorldManager.UpdateWorldList(); Logger.Log( LogType.SystemActivity, "&3All available worlds: {0}", WorldManager.Worlds.JoinToString( ", ", w => w.ClassyName ) ); Logger.Log( LogType.SystemActivity, "&3Main world: {0}&3; default rank: {1}", WorldManager.MainWorld.ClassyName, RankManager.DefaultRank.ClassyName ); // Check for incoming connections (every 250ms) checkConnectionsTask = Scheduler.NewTask( CheckConnections ).RunForever( CheckConnectionsInterval ); // Check for idles (every 1s) checkIdlesTask = Scheduler.NewTask( CheckIdles ).RunForever( CheckIdlesInterval );// Check for idles (every 30s) // Monitor CPU usage (every 30s) try { MonitorProcessorUsage( null ); Scheduler.NewTask( MonitorProcessorUsage ).RunForever( MonitorProcessorUsageInterval, MonitorProcessorUsageInterval ); } catch( Exception ex ) { Logger.Log( LogType.Error, "Server.StartServer: Could not start monitoring CPU use: {0}", ex ); } // PlayerDB saving (every 90s) PlayerDB.StartSaveTask(); // Announcements if( ConfigKey.AnnouncementInterval.GetInt() > 0 ) { TimeSpan announcementInterval = TimeSpan.FromMinutes( ConfigKey.AnnouncementInterval.GetInt() ); Scheduler.NewTask( ShowRandomAnnouncement ).RunForever( announcementInterval ); Scheduler.NewTask( RemoveRandomAnnouncement ).RunForever( announcementInterval, new TimeSpan(0, 0, 5)); } #region LoadTimers try { //Load Timers. if (Directory.Exists("./Timers")) { string[] TimersFileList = Directory.GetFiles("./Timers"); foreach (string filename in TimersFileList) { if (Path.GetExtension("./Timers/" + filename) == ".txt") { string[] TimerData = File.ReadAllLines(filename); DateTime StartDate = DateTime.UtcNow; DateTime EndDate = StartDate; PlayerInfo CreatedBy = Player.Console.Info; string TimerMessage = null; foreach (string line in TimerData) { if (line.Contains("StartDate: ")) { string date = line.Remove(0, "StartDate: ".Length); if (DateTime.TryParse(date, out StartDate)) { StartDate = DateTime.Parse(date); } } else if (line.Contains("EndDate: ")) { string date = line.Remove(0, "EndDate: ".Length); if (DateTime.TryParse(date, out EndDate)) { EndDate = DateTime.Parse(date); } } else if (line.Contains("CreatedBy: ")) { string creator = line.Remove(0, "CreatedBy: ".Length); if (PlayerDB.FindPlayerInfoExact(creator) != null) { CreatedBy = PlayerDB.FindPlayerInfoExact(creator); } } else if (line.Contains("Message: ")) { TimerMessage = line.Remove(0, "Creator: ".Length); } } if (StartDate == null || EndDate == null || CreatedBy == null || TimerMessage == null) { Player.Console.Message("Error starting a Timer: {0}, {1}, {2}, {3}", StartDate.ToString(), EndDate.ToString(), CreatedBy.Name, TimerMessage); continue; } if (DateTime.Compare(EndDate, DateTime.UtcNow) <= 0) { Player.Console.Message("Timer Expired: {0}, {1}, {2}, {3} Time Now: {4}", StartDate.ToString(), EndDate.ToString(), CreatedBy.Name, TimerMessage, DateTime.UtcNow.ToString()); if (Directory.Exists("./Timers")) { Player.Console.Message(filename); if (File.Exists(filename)) { File.Delete(filename); } } continue; } if ((StartDate != EndDate) && (CreatedBy != null) && (TimerMessage != null)) { ChatTimer.Start((EndDate - DateTime.UtcNow), TimerMessage, CreatedBy.Name); continue; } } } if (TimersFileList.Length > 0) Player.Console.Message("All Timers Loaded. ({0})", TimersFileList.Length); else Player.Console.Message("No Timers Were Loaded."); } } catch (Exception ex) { Player.Console.Message("Timer Loader Has Crashed: {0}", ex); } #endregion #region LoadReports try { if (Directory.Exists("./Reports")) { string[] ReportFileList = Directory.GetFiles("./Reports"); int created = 0; foreach (string filename in ReportFileList) { Report rCreate = new Report(); if (Path.GetExtension("./Reports/" + filename) == ".txt") { string[] reportData = File.ReadAllLines(filename); string idString = filename.Replace("./Reports\\", "").Replace(".txt", "").Split('-')[0]; string sender = reportData[0]; DateTime dateSent = DateTime.MinValue; long dateSentBinary; if (long.TryParse(reportData[1], out dateSentBinary)) { dateSent = DateTime.FromBinary(dateSentBinary); } string message = reportData[2]; int id; if (int.TryParse(idString, out id)) { rCreate.addReport(id, sender, dateSent, message); created++; } } } if (created > 0) Player.Console.Message("All Reports Loaded. ({0})", created); else Player.Console.Message("No reports were loaded."); } } catch (Exception ex) { Player.Console.Message("Report Loader Has Crashed: {0}", ex); } #endregion #region LoadFilters try { if (Directory.Exists("./Filters")) { string[] FilterFileList = Directory.GetFiles("./Filters"); int created = 0; foreach (string filename in FilterFileList) { Filter filterCreate = new Filter(); if (Path.GetExtension("./Filters/" + filename) == ".txt") { string[] filterData = File.ReadAllLines(filename); string idString = filename.Replace("./Filters\\", "").Replace(".txt", ""); string wordString = filterData[0]; string replacementString = filterData[1]; int id; if (int.TryParse(idString, out id)) { filterCreate.addFilter(id, wordString, replacementString); created++; } } } if (created > 0) Player.Console.Message("All Filters Loaded. ({0})", created); else Player.Console.Message("No filters were loaded."); } } catch (Exception ex) { Player.Console.Message("Filter Loader Has Crashed: {0}", ex); } #endregion #region LoadEntities try { if (Directory.Exists("./Entities")) { string[] EntityFileList = Directory.GetFiles("./Entities"); foreach (string filename in EntityFileList) { Bot botCreate = new Bot(); if (Path.GetExtension("./Entities/" + filename) == ".txt") { string[] entityData = File.ReadAllLines(filename); sbyte idString; Position posString; string nameString = entityData[0]; string skinString = entityData[1]; string modelString = entityData[2]; if (!CpeCommands.validEntities.Contains(modelString)) { Block block; if (Map.GetBlockByName(modelString, false, out block)) { modelString = block.GetHashCode().ToString(); } else { modelString = "humanoid"; } } if (!sbyte.TryParse(entityData[3], out idString)) { } World worldString = WorldManager.FindWorldExact(entityData[4]) ?? WorldManager.FindMainWorld(RankManager.LowestRank); if (!short.TryParse(entityData[5], out posString.X)) { posString.X = worldString.map.Spawn.X; } if (!short.TryParse(entityData[6], out posString.Y)) { posString.Y = worldString.map.Spawn.Y; } if (!short.TryParse(entityData[7], out posString.Z)) { posString.Z = worldString.map.Spawn.Z; } if (!byte.TryParse(entityData[8], out posString.L)) { posString.L = worldString.map.Spawn.L; } if (!byte.TryParse(entityData[9], out posString.R)) { posString.R = worldString.map.Spawn.R; } botCreate.setBot(nameString, skinString ?? nameString, modelString, worldString, posString, idString); } } if (EntityFileList.Length > 0) Player.Console.Message("All Entities Loaded. ({0})", EntityFileList.Length); else Player.Console.Message("No Entities Were Loaded."); } } catch (Exception ex) { Player.Console.Message("Entity Loader Has Crashed: {0}", ex); } #endregion #region ResourceDownloads if (!Directory.Exists("./Bot")) { Directory.CreateDirectory("./Bot"); } if (!File.Exists("Bot/Funfacts.txt")) { (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Funfacts.txt", "Bot/Funfacts.txt"); } if (!File.Exists("Bot/Jokes.txt")) { (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Jokes.txt", "Bot/Jokes.txt"); } if (!File.Exists("Bot/Protips.txt")) { (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Protips.txt", "Bot/Protips.txt"); } if (!File.Exists("Bot/Adjectives.txt")) { (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Adjectives.txt", "Bot/Adjectives.txt"); } if (!File.Exists("Bot/Nouns.txt")) { (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/Nouns.txt", "Bot/Nouns.txt"); } if (!File.Exists("./MOTDList.txt")) { (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/MOTDList.txt", "MOTDList.txt"); } if (!Directory.Exists("./fonts")) { Directory.CreateDirectory("./fonts"); (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/comicsans.ttf", "fonts/comicsans.ttf"); (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/mcclassic.ttf", "fonts/mcclassic.ttf"); (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/microsoft.ttf", "fonts/microsoft.ttf"); (new WebClient()).DownloadFile("http://123dmwm.tk/ProCraft/resources/minecraft.ttf", "fonts/minecraft.ttf"); } #endregion PortalHandler.GetInstance(); PortalDB.Load(); BlockDefinition.LoadGlobalDefinitions(); // garbage collection (every 60s) gcTask = Scheduler.NewTask( DoGC ).RunForever( GCInterval, TimeSpan.FromSeconds( 45 ) ); Heartbeat.Start(); if( ConfigKey.IRCBotEnabled.Enabled() ) { IRC.Start(); } if( ConfigKey.AutoRankEnabled.Enabled() ) { Scheduler.NewTask( AutoRankManager.TaskCallback ).RunForever( AutoRankManager.TickInterval ); } if( ConfigKey.RestartInterval.GetInt() > 0 ) { TimeSpan restartIn = TimeSpan.FromSeconds( ConfigKey.RestartInterval.GetInt() ); Shutdown( new ShutdownParams( ShutdownReason.RestartTimer, restartIn, true ), false ); ChatTimer.Start( restartIn, "Automatic Server Restart", Player.Console.Name ); } // start the main loop - server is now connectible Scheduler.Start(); IsRunning = true; RaiseEvent( Started ); return true; }