Skip to content

Commit

Permalink
Add new presences
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Oct 15, 2024
1 parent 6d0b136 commit 81c9728
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 74 deletions.
24 changes: 24 additions & 0 deletions Fika.Core/Coop/Patches/HideoutPlayerOwner_SetPointOfView_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using EFT;
using Fika.Core.UI.Custom;
using SPT.Reflection.Patching;
using System.Reflection;

namespace Fika.Core.Coop.Patches
{
public class HideoutPlayerOwner_SetPointOfView_Patch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return typeof(HideoutPlayerOwner).GetMethod(nameof(HideoutPlayerOwner.SetPointOfView));
}

[PatchPostfix]
public static void Postfix(HideoutPlayerOwner __instance)
{
if (__instance.FirstPersonMode && MainMenuUIScript.Instance != null)
{
MainMenuUIScript.Instance.UpdatePresence(UI.FikaUIGlobals.EFikaPlayerPresence.IN_HIDEOUT);
}
}
}
}
3 changes: 3 additions & 0 deletions Fika.Core/FikaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ private static void EnableFikaPatches()
new PlayEndGameSound_Patch().Enable();
new MenuScreen_Awake_Patch().Enable();
new BotOwner_UpdateManual_Patch().Enable();
new GClass3421_ShowAction_Patch().Enable();
new MenuScreen_method_8_Patch().Enable();
new HideoutPlayerOwner_SetPointOfView_Patch().Enable();

#if DEBUG
TasksExtensions_HandleFinishedTask_Patches.Enable();
Expand Down
13 changes: 12 additions & 1 deletion Fika.Core/Networking/Http/FikaRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EFT;
using Fika.Core.Models;
using Fika.Core.Networking.Models.Presence;
using Fika.Core.UI.Models;
using Fuyu.Platform.Common.Http;
using Newtonsoft.Json;
Expand All @@ -15,7 +16,7 @@

namespace Fika.Core.Networking.Http
{
public static class FikaRequestHandler
public static class FikaRequestHandler
{
private static readonly FuyuClient _httpClient;

Expand Down Expand Up @@ -218,5 +219,15 @@ public static FikaPlayerPresence[] GetPlayerPresences()
{
return GetJson<FikaPlayerPresence[]>("/fika/presence/get");
}

public static void SetPresence(FikaSetPresence data)
{
PutJson("/fika/presence/set", data);
}

public static FikaPlayerPresence[] SetAndGetPresence(FikaSetPresence data)
{
return PostJson<FikaSetPresence, FikaPlayerPresence[]>("/fika/presence/setget", data);
}
}
}
45 changes: 0 additions & 45 deletions Fika.Core/Networking/Models/FikaPlayerPresenceResponse.cs

This file was deleted.

55 changes: 55 additions & 0 deletions Fika.Core/Networking/Models/Presence/FikaPlayerPresenceResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using EFT;
using JsonType;
using System.Runtime.Serialization;
using static Fika.Core.UI.FikaUIGlobals;

namespace Fika.Core.Networking.Models.Presence
{
[DataContract]
public struct FikaPlayerPresence
{
[DataMember(Name = "nickname")]
public string Nickname;

[DataMember(Name = "level")]
public int Level;

[DataMember(Name = "activity")]
public EFikaPlayerPresence Activity;

[DataMember(Name = "activityStartedTimestamp")]
public long ActivityStartedTimestamp;

[DataMember(Name = "raidInformation")]
public RaidInformation? RaidInformation;

public FikaPlayerPresence(string nickname, int level, EFikaPlayerPresence activity, long activityStartedTimestamp, RaidInformation? raidInformation)
{
Nickname = nickname;
Level = level;
Activity = activity;
ActivityStartedTimestamp = activityStartedTimestamp;
RaidInformation = raidInformation;
}
}

[DataContract]
public struct RaidInformation
{
[DataMember(Name = "location")]
public string Location;

[DataMember(Name = "side")]
public ESideType Side;

[DataMember(Name = "time")]
public EDateTime Time;

public RaidInformation(string location, ESideType side, EDateTime time)
{
Location = location;
Side = side;
Time = time;
}
}
}
26 changes: 26 additions & 0 deletions Fika.Core/Networking/Models/Presence/FikaSetPresence.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Runtime.Serialization;
using static Fika.Core.UI.FikaUIGlobals;

namespace Fika.Core.Networking.Models.Presence
{
[DataContract]
public struct FikaSetPresence
{
[DataMember(Name = "activity")]
public EFikaPlayerPresence Presence;

[DataMember(Name = "raidInformation")]
public RaidInformation? RaidInformation;

public FikaSetPresence(EFikaPlayerPresence presence)
{
Presence = presence;
}

public FikaSetPresence(EFikaPlayerPresence presence, RaidInformation? raidInformation)
{
Presence = presence;
RaidInformation = raidInformation;
}
}
}
16 changes: 16 additions & 0 deletions Fika.Core/UI/Custom/MainMenuUIPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using TMPro;
using UnityEngine;
using static Fika.Core.UI.FikaUIGlobals;

public class MainMenuUIPlayer : MonoBehaviour
{
Expand All @@ -16,4 +17,19 @@ public void SetStatus(string name, int level, bool inRaid)
PlayerLevel.text = $"({level})";
PlayerStatus.text = inRaid ? "In Raid" : "In Menu";
}

public void SetActivity(string nickname, int level, EFikaPlayerPresence presence)
{
PlayerName.text = nickname;
PlayerLevel.text = $"({level})";
string status = presence switch
{
EFikaPlayerPresence.IN_MENU => "In Menu",
EFikaPlayerPresence.IN_RAID => "In Raid",
EFikaPlayerPresence.IN_STASH => "In Stash",
EFikaPlayerPresence.IN_HIDEOUT => "In Hideout",
_ => "In Menu",
};
PlayerStatus.text = status;
}
}
32 changes: 29 additions & 3 deletions Fika.Core/UI/Custom/MainMenuUIScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using EFT.UI;
using Fika.Core.Bundles;
using Fika.Core.Networking.Http;
using Fika.Core.Utils;
using Fika.Core.Networking.Models.Presence;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -13,16 +13,29 @@ namespace Fika.Core.UI.Custom
{
public class MainMenuUIScript : MonoBehaviour
{
public static MainMenuUIScript Instance
{
get
{
return instance;
}
}

private static MainMenuUIScript instance;

private Coroutine queryRoutine;
private MainMenuUI mainMenuUI;
private GameObject playerTemplate;
private List<GameObject> players;
private DateTime lastRefresh;
private DateTime lastSet;

private void Start()
{
instance = this;
players = [];
lastRefresh = DateTime.Now;
lastSet = DateTime.Now;
CreateMainMenuUI();
}

Expand Down Expand Up @@ -91,8 +104,8 @@ private void SetupPlayers(ref FikaPlayerPresence[] responses)
{
GameObject newPlayer = GameObject.Instantiate(playerTemplate, playerTemplate.transform.parent);
MainMenuUIPlayer mainMenuUIPlayer = newPlayer.GetComponent<MainMenuUIPlayer>();
mainMenuUIPlayer.SetStatus(presence.Nickname, presence.Level, presence.InRaid);
if (presence.InRaid && presence.RaidInformation.HasValue)
mainMenuUIPlayer.SetActivity(presence.Nickname, presence.Level, presence.Activity);
if (presence.Activity is EFikaPlayerPresence.IN_RAID && presence.RaidInformation.HasValue)
{
RaidInformation information = presence.RaidInformation.Value;
string side = information.Side == EFT.ESideType.Pmc ? "PMC" : "Scav";
Expand All @@ -104,5 +117,18 @@ private void SetupPlayers(ref FikaPlayerPresence[] responses)
players.Add(newPlayer);
}
}

public void UpdatePresence(EFikaPlayerPresence presence)
{
// Prevent spamming going back and forth to the main menu causing server lag for no reason
if ((DateTime.Now - lastSet).TotalSeconds < 5)
{
return;
}

lastSet = DateTime.Now;
FikaSetPresence data = new(presence);
FikaRequestHandler.SetPresence(data);
}
}
}
2 changes: 1 addition & 1 deletion Fika.Core/UI/Custom/MatchMakerUIScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ private void RefreshUI()
break;
}
}
}
}

public IEnumerator ServerQuery()
{
Expand Down
7 changes: 4 additions & 3 deletions Fika.Core/UI/FikaUIGlobals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Fika.Core.UI
{
internal static class FikaUIGlobals
public static class FikaUIGlobals
{
private static readonly Dictionary<EColor, string> keyValuePairs = new()
{
Expand Down Expand Up @@ -154,11 +154,12 @@ public static string ColorizeText(EColor color, string text)
return $"<color=#{GetHexByColor(color)}>{text}</color>";
}

public enum EFikaRaidPresence
public enum EFikaPlayerPresence
{
IN_MENU,
IN_RAID,
IN_STASH
IN_STASH,
IN_HIDEOUT
}

/// <summary>
Expand Down
25 changes: 25 additions & 0 deletions Fika.Core/UI/Patches/GClass3421_ShowAction_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Fika.Core.UI.Custom;
using SPT.Reflection.Patching;
using System.Reflection;
using static EFT.UI.InventoryScreen;
using static Fika.Core.UI.FikaUIGlobals;

namespace Fika.Core.UI.Patches
{
public class GClass3421_ShowAction_Patch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return typeof(GClass3421).GetMethod(nameof(GClass3421.ShowAction));
}

[PatchPostfix]
public static void Postfix(GClass3421 __instance)
{
if (!__instance.InRaid)
{
MainMenuUIScript.Instance.UpdatePresence(EFikaPlayerPresence.IN_STASH);
}
}
}
}
21 changes: 21 additions & 0 deletions Fika.Core/UI/Patches/MenuScreen/MenuScreen_Awake_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using EFT.UI;
using Fika.Core.UI.Custom;
using SPT.Reflection.Patching;
using System.Reflection;

namespace Fika.Core.UI.Patches
{
public class MenuScreen_Awake_Patch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return typeof(MenuScreen).GetMethod(nameof(MenuScreen.Awake));
}

[PatchPostfix]
public static void Postfix(MenuScreen __instance)
{
__instance.gameObject.AddComponent<MainMenuUIScript>();
}
}
}
24 changes: 24 additions & 0 deletions Fika.Core/UI/Patches/MenuScreen/MenuScreen_method_8_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using EFT.UI;
using Fika.Core.UI.Custom;
using SPT.Reflection.Patching;
using System.Reflection;

namespace Fika.Core.UI.Patches
{
public class MenuScreen_method_8_Patch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return typeof(MenuScreen).GetMethod(nameof(MenuScreen.method_8));
}

[PatchPostfix]
public static void Postfix(bool minimized)
{
if (!minimized && MainMenuUIScript.Instance != null)
{
MainMenuUIScript.Instance.UpdatePresence(FikaUIGlobals.EFikaPlayerPresence.IN_MENU);
}
}
}
}
Loading

0 comments on commit 81c9728

Please sign in to comment.