Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SavePlayer async #75

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1757,16 +1757,7 @@ private void HealthController_DiedEvent(EDamageType obj)

if (FikaPlugin.Instance.ForceSaveOnDeath)
{
StartCoroutine(SaveOnDeathRoutine());
}
}

private IEnumerator SaveOnDeathRoutine()
{
Task saveTask = SavePlayer((CoopPlayer)gparam_0.Player, MyExitStatus, null, true);
while (!saveTask.IsCompleted)
{
yield return null;
Task.Run(() => SavePlayer((CoopPlayer)gparam_0.Player, MyExitStatus, null, true));
}
}

Expand Down Expand Up @@ -1900,11 +1891,11 @@ public override void Stop(string profileId, ExitStatus exitStatus, string exitNa
/// <param name="exitName"></param>
/// <param name="fromDeath"></param>
/// <returns></returns>
private Task SavePlayer(CoopPlayer player, ExitStatus exitStatus, string exitName, bool fromDeath)
private async Task SavePlayer(CoopPlayer player, ExitStatus exitStatus, string exitName, bool fromDeath)
{
if (hasSaved)
{
return Task.CompletedTask;
return;
}

if (fromDeath)
Expand All @@ -1918,9 +1909,10 @@ private Task SavePlayer(CoopPlayer player, ExitStatus exitStatus, string exitNam
//Method taken directly from SPT, can be found in the aki-singleplayer assembly as OfflineSaveProfilePatch
Type converterClass = typeof(AbstractGame).Assembly.GetTypes().First(t => t.GetField("Converters", BindingFlags.Static | BindingFlags.Public) != null);

JsonConverter[] Converters = Traverse.Create(converterClass).Field<JsonConverter[]>("Converters").Value;
JsonConverter[] converters = Traverse.Create(converterClass).Field<JsonConverter[]>("Converters").Value;
converters = [.. converters, new NotesJsonConverter()];

SaveProfileRequest SaveRequest = new()
SaveProfileRequest saveRequest = new()
{
Exit = exitStatus.ToString().ToLowerInvariant(),
Profile = player.Profile,
Expand All @@ -1929,10 +1921,8 @@ private Task SavePlayer(CoopPlayer player, ExitStatus exitStatus, string exitNam
IsPlayerScav = player.Side is EPlayerSide.Savage
};

RequestHandler.PutJson("/raid/profile/save", SaveRequest.ToJson(Converters.AddItem(new NotesJsonConverter()).ToArray()));

await RequestHandler.PutJsonAsync("/raid/profile/save", saveRequest.ToJson(converters));
hasSaved = true;
return Task.CompletedTask;
}

/// <summary>
Expand Down Expand Up @@ -2179,7 +2169,7 @@ private void FireCallback()
{
Callback<ExitStatus, TimeSpan, MetricsClass> endCallback = Traverse.Create(localGame).Field<Callback<ExitStatus, TimeSpan, MetricsClass>>("callback_0").Value;

localGame.SavePlayer(localPlayer, exitStatus, exitName, false);
Task.Run(() => localGame.SavePlayer(localPlayer, exitStatus, exitName, false));

endCallback(new Result<ExitStatus, TimeSpan, MetricsClass>(exitStatus, GClass1304.Now - localGame.dateTime_0, new MetricsClass()));
UIEventSystem.Instance.Enable();
Expand Down
Loading