mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-22 11:15:15 +00:00
save/load moons from file
This commit is contained in:
parent
8d90f50fa9
commit
f305c3001b
1 changed files with 47 additions and 1 deletions
|
@ -1,7 +1,8 @@
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using Server;
|
using Server;
|
||||||
using Shared;
|
using Shared;
|
||||||
using Shared.Packet.Packets;
|
using Shared.Packet.Packets;
|
||||||
|
@ -15,6 +16,47 @@ Logger consoleLogger = new Logger("Console");
|
||||||
DiscordBot bot = new DiscordBot();
|
DiscordBot bot = new DiscordBot();
|
||||||
await bot.Run();
|
await bot.Run();
|
||||||
|
|
||||||
|
async Task PersistShines()
|
||||||
|
{
|
||||||
|
if (!Settings.Instance.PersistShines.Enabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string shineJson = JsonSerializer.Serialize(shineBag);
|
||||||
|
await File.WriteAllTextAsync(Settings.Instance.PersistShines.Filename, shineJson);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
consoleLogger.Error(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task LoadShines()
|
||||||
|
{
|
||||||
|
if (!Settings.Instance.PersistShines.Enabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string shineJson = await File.ReadAllTextAsync(Settings.Instance.PersistShines.Filename);
|
||||||
|
var loadedShines = JsonSerializer.Deserialize<HashSet<int>>(shineJson);
|
||||||
|
|
||||||
|
if (loadedShines is not null) shineBag = loadedShines;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
consoleLogger.Error(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load shines table from file
|
||||||
|
await LoadShines();
|
||||||
|
|
||||||
server.ClientJoined += (c, _) => {
|
server.ClientJoined += (c, _) => {
|
||||||
if (Settings.Instance.BanList.Enabled
|
if (Settings.Instance.BanList.Enabled
|
||||||
&& (Settings.Instance.BanList.Players.Contains(c.Id)
|
&& (Settings.Instance.BanList.Players.Contains(c.Id)
|
||||||
|
@ -52,6 +94,7 @@ async Task ClientSyncShineBag(Client client) {
|
||||||
|
|
||||||
async void SyncShineBag() {
|
async void SyncShineBag() {
|
||||||
try {
|
try {
|
||||||
|
await PersistShines();
|
||||||
await Parallel.ForEachAsync(server.Clients.ToArray(), async (client, _) => await ClientSyncShineBag(client));
|
await Parallel.ForEachAsync(server.Clients.ToArray(), async (client, _) => await ClientSyncShineBag(client));
|
||||||
} catch {
|
} catch {
|
||||||
// errors that can happen shines change will crash the server :)
|
// errors that can happen shines change will crash the server :)
|
||||||
|
@ -78,6 +121,9 @@ server.PacketHandler = (c, p) => {
|
||||||
c.Metadata["speedrun"] = true;
|
c.Metadata["speedrun"] = true;
|
||||||
((ConcurrentBag<int>) (c.Metadata["shineSync"] ??= new ConcurrentBag<int>())).Clear();
|
((ConcurrentBag<int>) (c.Metadata["shineSync"] ??= new ConcurrentBag<int>())).Clear();
|
||||||
shineBag.Clear();
|
shineBag.Clear();
|
||||||
|
Task.Run(async () => {
|
||||||
|
await PersistShines();
|
||||||
|
});
|
||||||
c.Logger.Info("Entered Cap on new save, preventing moon sync until Cascade");
|
c.Logger.Info("Entered Cap on new save, preventing moon sync until Cascade");
|
||||||
break;
|
break;
|
||||||
case "WaterfallWorldHomeStage":
|
case "WaterfallWorldHomeStage":
|
||||||
|
|
Loading…
Reference in a new issue