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.Fields.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\ThreadSafeCache.cs" />
<Compile Include="Server\Updater.cs" />

View File

@ -171,7 +171,7 @@ namespace MCGalaxy {
IRC = new IRCBot();
if (Server.irc) IRC.Connect();
locationChecker = new Thread(DoLocationChecks);
locationChecker = new Thread(ServerTasks.LocationChecks);
locationChecker.Name = "MCG_LocationCheck";
locationChecker.Start();
@ -192,25 +192,5 @@ namespace MCGalaxy {
oldMain.Unload(true, false);
} 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();
Mods.Clear();
Background.QueueOnce(UpdateStaffListTask);
Background.QueueOnce(InitTasks.UpdateStaffList);
MainScheduler.QueueRepeat(TemprankExpiryTask, null,
TimeSpan.FromMinutes(1));
Background.QueueRepeat(AutoSaveTask, 1,
TimeSpan.FromSeconds(Server.backupInterval));
Background.QueueRepeat(BlockUpdatesTask, null,
TimeSpan.FromSeconds(Server.blockInterval));
Background.QueueRepeat(ThreadSafeCache.CleanupTask, null,
TimeSpan.FromMinutes(5));
MainScheduler.QueueRepeat(ServerTasks.TemprankExpiry,
null, TimeSpan.FromMinutes(1));
Background.QueueRepeat(ServerTasks.AutoSave,
1, TimeSpan.FromSeconds(Server.backupInterval));
Background.QueueRepeat(ServerTasks.BlockUpdates,
null, TimeSpan.FromSeconds(Server.blockInterval));
Background.QueueRepeat(ThreadSafeCache.CleanupTask,
null, TimeSpan.FromMinutes(5));
}
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

@ -20,10 +20,10 @@ using System;
using System.IO;
using System.Threading;
namespace MCGalaxy {
public sealed partial class Server {
namespace MCGalaxy.Tasks {
internal static class ServerTasks {
void DoLocationChecks() {
internal static void LocationChecks() {
while (true) {
Player[] players = PlayerInfo.Online.Items;
Thread.Sleep(players.Length == 0 ? 20 : 10);
@ -72,7 +72,7 @@ namespace MCGalaxy {
}
}
void BlockUpdatesTask(SchedulerTask task) {
internal static void BlockUpdates(SchedulerTask task) {
Level[] loaded = LevelInfo.Loaded.Items;
foreach (Level lvl in loaded) {
try {
@ -83,7 +83,7 @@ namespace MCGalaxy {
}
}
void AutoSaveTask(SchedulerTask task) {
internal static void AutoSave(SchedulerTask task) {
int count = (int)task.State;
count--;
Level[] levels = LevelInfo.Loaded.Items;
@ -125,7 +125,7 @@ namespace MCGalaxy {
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;
foreach (string line in File.ReadAllLines("text/tempranks.txt"))