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

Save profile immediately upon death #54

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
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
42 changes: 41 additions & 1 deletion Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using Aki.Custom.Airdrops;
using Aki.Common.Http;
using Aki.Custom.Airdrops;
using Aki.Reflection.Utils;
using Aki.SinglePlayer.Models.Progression;
using Aki.SinglePlayer.Utils.Healing;
using Aki.SinglePlayer.Utils.Insurance;
using Aki.SinglePlayer.Utils.Progression;
using BepInEx.Logging;
using Comfort.Common;
using CommonAssets.Scripts.Game;
Expand Down Expand Up @@ -34,6 +39,7 @@
using HarmonyLib;
using JsonType;
using LiteNetLib.Utils;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -1510,6 +1516,11 @@ public void Extract(CoopPlayer player, ExfiltrationPoint point)

player.ActiveHealthController.DiedEvent -= MainPlayerDied;

if(FikaPlugin.Instance.ForceSaveOnDeath)
{
SavePlayer(coopPlayer, MyExitStatus, null);
}

if (FikaPlugin.AutoExtract.Value)
{
if (MatchmakerAcceptPatches.IsClient)
Expand Down Expand Up @@ -1581,6 +1592,11 @@ private void HealthController_DiedEvent(EDamageType obj)
PlayerOwner.vmethod_1();
MyExitStatus = ExitStatus.Killed;
MyExitLocation = null;

if(FikaPlugin.Instance.ForceSaveOnDeath)
{
SavePlayer((CoopPlayer)gparam_0.Player, MyExitStatus, null);
}
}

public override void Stop(string profileId, ExitStatus exitStatus, string exitName, float delay = 0f)
Expand Down Expand Up @@ -1714,6 +1730,30 @@ public override void Stop(string profileId, ExitStatus exitStatus, string exitNa
GClass549.Config.UseSpiritPlayer = false;
}

private void SavePlayer(CoopPlayer player, ExitStatus exitStatus, string exitName)
{
//Since we're bypassing saving on exiting, run this now.
player.Profile.EftStats.LastPlayerState = null;
player.StatisticsManager.EndStatisticsSession(exitStatus, base.PastTime);
player.CheckAndResetControllers(exitStatus, base.PastTime, base.Location_0.Id, exitName);

//Method taken directly from AKI, 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;

SaveProfileRequest SaveRequest = new SaveProfileRequest
{
Exit = exitStatus.ToString().ToLowerInvariant(),
Profile = player.Profile,
Health = HealthListener.Instance.CurrentHealth,
Insurance = InsuredItemManager.Instance.GetTrackedItems(),
IsPlayerScav = player.Side is EPlayerSide.Savage
};

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

private void StopFromError(string profileId, ExitStatus exitStatus)
{
Logger.LogInfo("CoopGame::StopFromError");
Expand Down
9 changes: 9 additions & 0 deletions Fika.Core/Coop/Players/CoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,15 @@ public virtual void SetupDogTag()
}
}

public void CheckAndResetControllers(ExitStatus exitStatus, float pastTime, string locationId, string exitName)
{
_questController?.CheckExitConditionCounters(exitStatus, pastTime, locationId, exitName, HealthController.BodyPartEffects, TriggerZones);
_questController?.ResetCurrentNullableCounters();

_achievementsController?.CheckExitConditionCounters(exitStatus, pastTime, locationId, exitName, HealthController.BodyPartEffects, TriggerZones);
_achievementsController?.ResetCurrentNullableCounters();
}

public virtual void SetInventory(EquipmentClass equipmentClass)
{
// Do nothing
Expand Down
7 changes: 7 additions & 0 deletions Fika.Core/FikaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public class FikaPlugin : BaseUnityPlugin
public bool DynamicVExfils;
public bool AllowFreeCam;
public bool AllowItemSending;
public bool ForceSaveOnDeath;
#endregion

protected void Awake()
Expand Down Expand Up @@ -228,6 +229,11 @@ protected void Awake()
new ItemContext_Patch().Enable();
}

if(ForceSaveOnDeath)
{
new OfflineSaveProfilePatch().Disable(); //Disable this as we've moved it forward immediately after extraction or death
}

BotDifficulties = FikaRequestHandler.GetBotDifficulties();
ConsoleScreen.Processor.RegisterCommandGroup<FikaCommands>();

Expand Down Expand Up @@ -258,6 +264,7 @@ private void GetClientConfig()
DynamicVExfils = clientConfig.DynamicVExfils;
AllowFreeCam = clientConfig.AllowFreeCam;
AllowItemSending = clientConfig.AllowItemSending;
ForceSaveOnDeath = clientConfig.ForceSaveOnDeath;

clientConfig.ToString();
}
Expand Down
6 changes: 5 additions & 1 deletion Fika.Core/Models/ClientConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ public struct ClientConfigModel
[DataMember(Name = "allowItemSending")]
public bool AllowItemSending;

public ClientConfigModel(bool useBTR, bool friendlyFire, bool dynamicVExfils, bool allowFreeCam, bool allowItemSending)
[DataMember(Name = "forceSaveOnDeath")]
public bool ForceSaveOnDeath;

public ClientConfigModel(bool useBTR, bool friendlyFire, bool dynamicVExfils, bool allowFreeCam, bool allowItemSending, bool forceSaveOnDeath)
{
UseBTR = useBTR;
FriendlyFire = friendlyFire;
DynamicVExfils = dynamicVExfils;
AllowFreeCam = allowFreeCam;
AllowItemSending = allowItemSending;
ForceSaveOnDeath = forceSaveOnDeath;
}

public new void ToString()
Expand Down
Loading