Skip to content

Commit

Permalink
save/load moons from file
Browse files Browse the repository at this point in the history
  • Loading branch information
luludotdev authored and Sanae6 committed Jul 23, 2022
1 parent 8d90f50 commit f305c30
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Net;
using System.Numerics;
using System.Text;
using System.Text.Json;
using Server;
using Shared;
using Shared.Packet.Packets;
Expand All @@ -15,6 +16,47 @@
DiscordBot bot = new DiscordBot();
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, _) => {
if (Settings.Instance.BanList.Enabled
&& (Settings.Instance.BanList.Players.Contains(c.Id)
Expand Down Expand Up @@ -52,6 +94,7 @@ await client.Send(new ShinePacket {

async void SyncShineBag() {
try {
await PersistShines();
await Parallel.ForEachAsync(server.Clients.ToArray(), async (client, _) => await ClientSyncShineBag(client));
} catch {
// errors that can happen shines change will crash the server :)
Expand All @@ -78,6 +121,9 @@ async void SyncShineBag() {
c.Metadata["speedrun"] = true;
((ConcurrentBag<int>) (c.Metadata["shineSync"] ??= new ConcurrentBag<int>())).Clear();
shineBag.Clear();
Task.Run(async () => {
await PersistShines();
});
c.Logger.Info("Entered Cap on new save, preventing moon sync until Cascade");
break;
case "WaterfallWorldHomeStage":
Expand Down

0 comments on commit f305c30

Please sign in to comment.