Style: Move tasks out of server.cs

This commit is contained in:
UnknownShadow200 2016-09-14 14:20:51 +10:00
parent ced7daffc6
commit d2b4cd6ae0
6 changed files with 217 additions and 185 deletions

View File

@ -607,7 +607,8 @@
<Compile Include="Server\Server.DB.cs" /> <Compile Include="Server\Server.DB.cs" />
<Compile Include="Server\Server.Fields.cs" /> <Compile Include="Server\Server.Fields.cs" />
<Compile Include="Server\Server.Init.cs" /> <Compile Include="Server\Server.Init.cs" />
<Compile Include="Server\Server.Tasks.cs" /> <Compile Include="Server\Tasks\InitTasks.cs" />
<Compile Include="Server\Tasks\ServerTasks.cs" />
<Compile Include="Server\Tasks\UpgradeTasks.cs" /> <Compile Include="Server\Tasks\UpgradeTasks.cs" />
<Compile Include="Server\ThreadSafeCache.cs" /> <Compile Include="Server\ThreadSafeCache.cs" />
<Compile Include="Server\Updater.cs" /> <Compile Include="Server\Updater.cs" />

View File

@ -28,7 +28,7 @@ using MCGalaxy.Tasks;
namespace MCGalaxy { namespace MCGalaxy {
public sealed partial class Server { public sealed partial class Server {
void LoadMainLevel() { void LoadMainLevel() {
try { try {
if (LevelInfo.ExistsOffline(level)) { if (LevelInfo.ExistsOffline(level)) {
@ -67,7 +67,7 @@ namespace MCGalaxy {
ErrorLog(e); ErrorLog(e);
} }
} }
void LoadPlayerLists() { void LoadPlayerLists() {
agreed = new PlayerList("ranks/agreed.txt"); agreed = new PlayerList("ranks/agreed.txt");
try { try {
@ -171,7 +171,7 @@ namespace MCGalaxy {
IRC = new IRCBot(); IRC = new IRCBot();
if (Server.irc) IRC.Connect(); if (Server.irc) IRC.Connect();
locationChecker = new Thread(DoLocationChecks); locationChecker = new Thread(ServerTasks.LocationChecks);
locationChecker.Name = "MCG_LocationCheck"; locationChecker.Name = "MCG_LocationCheck";
locationChecker.Start(); locationChecker.Start();
@ -192,25 +192,5 @@ namespace MCGalaxy {
oldMain.Unload(true, false); oldMain.Unload(true, false);
} catch (Exception e) { Server.ErrorLog(e); } } catch (Exception e) { Server.ErrorLog(e); }
} }
const string staffUrl = "https://raw.githubusercontent.com/Hetal728/MCGalaxy/master/Uploads/devs.txt";
void UpdateStaffListTask() {
try {
using (WebClient web = new WebClient()) {
string[] result = web.DownloadString(staffUrl).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in result) {
string type = line.Split(':')[0].ToLower();
List<string> staffList = type.Equals("devs") ? Devs : type.Equals("mods") ? Mods : null;
foreach (string name in line.Split(':')[1].Split())
staffList.Add(name.TrimEnd('+'));
}
}
} catch (Exception e) {
ErrorLog(e);
s.Log("Failed to update MCGalaxy staff list.");
Devs.Clear();
Mods.Clear();
}
}
} }
} }

View File

@ -116,16 +116,16 @@ namespace MCGalaxy {
Devs.Clear(); Devs.Clear();
Mods.Clear(); Mods.Clear();
Background.QueueOnce(UpdateStaffListTask); Background.QueueOnce(InitTasks.UpdateStaffList);
MainScheduler.QueueRepeat(TemprankExpiryTask, null, MainScheduler.QueueRepeat(ServerTasks.TemprankExpiry,
TimeSpan.FromMinutes(1)); null, TimeSpan.FromMinutes(1));
Background.QueueRepeat(AutoSaveTask, 1, Background.QueueRepeat(ServerTasks.AutoSave,
TimeSpan.FromSeconds(Server.backupInterval)); 1, TimeSpan.FromSeconds(Server.backupInterval));
Background.QueueRepeat(BlockUpdatesTask, null, Background.QueueRepeat(ServerTasks.BlockUpdates,
TimeSpan.FromSeconds(Server.blockInterval)); null, TimeSpan.FromSeconds(Server.blockInterval));
Background.QueueRepeat(ThreadSafeCache.CleanupTask, null, Background.QueueRepeat(ThreadSafeCache.CleanupTask,
TimeSpan.FromMinutes(5)); null, TimeSpan.FromMinutes(5));
} }
void EnsureFilesExist() { void EnsureFilesExist() {

51
Server/Tasks/InitTasks.cs Normal file
View File

@ -0,0 +1,51 @@
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/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.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.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using MCGalaxy.Commands.World;
using MCGalaxy.Games;
using MCGalaxy.Generator;
using MCGalaxy.Tasks;
namespace MCGalaxy.Tasks {
internal static class InitTasks {
const string staffUrl = "https://raw.githubusercontent.com/Hetal728/MCGalaxy/master/Uploads/devs.txt";
internal static void UpdateStaffList() {
try {
using (WebClient web = new WebClient()) {
string[] result = web.DownloadString(staffUrl).Split(new [] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in result) {
string type = line.Split(':')[0].ToLower();
List<string> list = (type == "devs") ? Server.Devs : (type == "mods") ? Server.Mods : null;
foreach (string name in line.Split(':')[1].Split())
list.Add(name.TrimEnd('+'));
}
}
} catch (Exception e) {
Server.ErrorLog(e);
Server.s.Log("Failed to update MCGalaxy staff list.");
Server.Devs.Clear();
Server.Mods.Clear();
}
}
}
}

View File

@ -1,152 +1,152 @@
/* /*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Copyright 2011 MCForge Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS" software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
namespace MCGalaxy { namespace MCGalaxy.Tasks {
public sealed partial class Server { internal static class ServerTasks {
void DoLocationChecks() { internal static void LocationChecks() {
while (true) { while (true) {
Player[] players = PlayerInfo.Online.Items; Player[] players = PlayerInfo.Online.Items;
Thread.Sleep(players.Length == 0 ? 20 : 10); Thread.Sleep(players.Length == 0 ? 20 : 10);
players = PlayerInfo.Online.Items; players = PlayerInfo.Online.Items;
for (int i = 0; i < players.Length; i++) { for (int i = 0; i < players.Length; i++) {
try { try {
Player p = players[i]; Player p = players[i];
if (p.frozen) { if (p.frozen) {
p.SendPos(0xFF, p.pos[0], p.pos[1], p.pos[2], p.rot[0], p.rot[1]); continue; p.SendPos(0xFF, p.pos[0], p.pos[1], p.pos[2], p.rot[0], p.rot[1]); continue;
} else if (p.following != "") { } else if (p.following != "") {
Player who = PlayerInfo.FindExact(p.following); Player who = PlayerInfo.FindExact(p.following);
if (who == null || who.level != p.level) { if (who == null || who.level != p.level) {
p.following = ""; p.following = "";
if (!p.canBuild) if (!p.canBuild)
p.canBuild = true; p.canBuild = true;
if (who != null && who.possess == p.name) if (who != null && who.possess == p.name)
who.possess = ""; who.possess = "";
continue; continue;
} }
if (p.canBuild) { if (p.canBuild) {
p.SendPos(0xFF, who.pos[0], (ushort)(who.pos[1] - 16), who.pos[2], who.rot[0], who.rot[1]); p.SendPos(0xFF, who.pos[0], (ushort)(who.pos[1] - 16), who.pos[2], who.rot[0], who.rot[1]);
} else { } else {
p.SendPos(0xFF, who.pos[0], who.pos[1], who.pos[2], who.rot[0], who.rot[1]); p.SendPos(0xFF, who.pos[0], who.pos[1], who.pos[2], who.rot[0], who.rot[1]);
} }
} else if (p.possess != "") { } else if (p.possess != "") {
Player who = PlayerInfo.FindExact(p.possess); Player who = PlayerInfo.FindExact(p.possess);
if (who == null || who.level != p.level) if (who == null || who.level != p.level)
p.possess = ""; p.possess = "";
} }
ushort x = (ushort)(p.pos[0] / 32); ushort x = (ushort)(p.pos[0] / 32);
ushort y = (ushort)(p.pos[1] / 32); ushort y = (ushort)(p.pos[1] / 32);
ushort z = (ushort)(p.pos[2] / 32); ushort z = (ushort)(p.pos[2] / 32);
if (p.level.Death) if (p.level.Death)
p.CheckSurvival(x, y, z); p.CheckSurvival(x, y, z);
p.CheckBlock(x, y, z); p.CheckBlock(x, y, z);
p.oldIndex = p.level.PosToInt(x, y, z); p.oldIndex = p.level.PosToInt(x, y, z);
} catch (Exception e) { } catch (Exception e) {
Server.ErrorLog(e); Server.ErrorLog(e);
} }
} }
} }
} }
void BlockUpdatesTask(SchedulerTask task) { internal static void BlockUpdates(SchedulerTask task) {
Level[] loaded = LevelInfo.Loaded.Items; Level[] loaded = LevelInfo.Loaded.Items;
foreach (Level lvl in loaded) { foreach (Level lvl in loaded) {
try { try {
lvl.saveChanges(); lvl.saveChanges();
} catch (Exception e) { } catch (Exception e) {
Server.ErrorLog(e); Server.ErrorLog(e);
} }
} }
} }
void AutoSaveTask(SchedulerTask task) { internal static void AutoSave(SchedulerTask task) {
int count = (int)task.State; int count = (int)task.State;
count--; count--;
Level[] levels = LevelInfo.Loaded.Items; Level[] levels = LevelInfo.Loaded.Items;
foreach (Level l in levels) { foreach (Level l in levels) {
try { try {
if (!l.changed || !l.ShouldSaveChanges()) continue; if (!l.changed || !l.ShouldSaveChanges()) continue;
l.Save(); l.Save();
if (count == 0) { if (count == 0) {
int backupNumber = l.Backup(); int backupNumber = l.Backup();
if (backupNumber != -1) { if (backupNumber != -1) {
l.ChatLevel("Backup " + backupNumber + " saved."); l.ChatLevel("Backup " + backupNumber + " saved.");
Server.s.Log("Backup " + backupNumber + " saved for " + l.name); Server.s.Log("Backup " + backupNumber + " saved for " + l.name);
} }
} }
} catch { } catch {
Server.s.Log("Backup for " + l.name + " has caused an error."); Server.s.Log("Backup for " + l.name + " has caused an error.");
} }
} }
if (count <= 0) count = 15; if (count <= 0) count = 15;
task.State = count; task.State = count;
Player[] players = PlayerInfo.Online.Items; Player[] players = PlayerInfo.Online.Items;
try { try {
foreach (Player p in players) p.save(); foreach (Player p in players) p.save();
} catch (Exception e) { } catch (Exception e) {
Server.ErrorLog(e); Server.ErrorLog(e);
} }
players = PlayerInfo.Online.Items; players = PlayerInfo.Online.Items;
if (players.Length <= 0) return; if (players.Length <= 0) return;
string all = players.Join(p => p.name); string all = players.Join(p => p.name);
if (all.Length > 0) Server.s.Log("!PLAYERS ONLINE: " + all, true); if (all.Length > 0) Server.s.Log("!PLAYERS ONLINE: " + all, true);
levels = LevelInfo.Loaded.Items; levels = LevelInfo.Loaded.Items;
all = levels.Join(l => l.name); all = levels.Join(l => l.name);
if (all.Length > 0) Server.s.Log("!LEVELS ONLINE: " + all, true); if (all.Length > 0) Server.s.Log("!LEVELS ONLINE: " + all, true);
} }
void TemprankExpiryTask(SchedulerTask task) { internal static void TemprankExpiry(SchedulerTask task) {
Player[] players = PlayerInfo.Online.Items; Player[] players = PlayerInfo.Online.Items;
foreach (string line in File.ReadAllLines("text/tempranks.txt")) foreach (string line in File.ReadAllLines("text/tempranks.txt"))
foreach (Player p in players) foreach (Player p in players)
{ {
if (!line.CaselessStarts(p.name)) continue; if (!line.CaselessStarts(p.name)) continue;
string[] args = line.Split(' '); string[] args = line.Split(' ');
int min = int.Parse(args[4]), hour = int.Parse(args[5]); int min = int.Parse(args[4]), hour = int.Parse(args[5]);
int day = int.Parse(args[6]), month = int.Parse(args[7]), year = int.Parse(args[8]); int day = int.Parse(args[6]), month = int.Parse(args[7]), year = int.Parse(args[8]);
int periodH = int.Parse(args[3]), periodM = 0; int periodH = int.Parse(args[3]), periodM = 0;
if (args.Length > 10) periodM = int.Parse(args[10]); if (args.Length > 10) periodM = int.Parse(args[10]);
DateTime expire = new DateTime(year, month, day, hour, min, 0) DateTime expire = new DateTime(year, month, day, hour, min, 0)
.AddHours(periodH).AddMinutes(periodM); .AddHours(periodH).AddMinutes(periodM);
if (DateTime.Now >= expire) if (DateTime.Now >= expire)
Command.all.Find("temprank").Use(null, p.name + " delete"); Command.all.Find("temprank").Use(null, p.name + " delete");
} }
DateTime now = DateTime.UtcNow; DateTime now = DateTime.UtcNow;
task.Delay = TimeSpan.FromSeconds(60 - now.Second); // TODO: down to seconds task.Delay = TimeSpan.FromSeconds(60 - now.Second); // TODO: down to seconds
} }
} }
} }

View File

@ -24,7 +24,7 @@ using MCGalaxy.Commands.World;
using MCGalaxy.Games; using MCGalaxy.Games;
using MCGalaxy.Generator; using MCGalaxy.Generator;
namespace MCGalaxy.Tasks { namespace MCGalaxy.Tasks {
internal static class UpgradeTasks { internal static class UpgradeTasks {
internal static void UpgradeOldBlacklist() { internal static void UpgradeOldBlacklist() {