From accb0ab9f6cfe928238a989d55e6bd8242ffa8c6 Mon Sep 17 00:00:00 2001 From: Merijn Hendriks Date: Sat, 20 Apr 2024 22:32:28 +0000 Subject: [PATCH] Reduce bot difficulty requests (!111) Requires server-side changes (will need your help @TheSparta for this!). This combined all the single bot difficulty requests into a single GET request (`/singleplayer/bot/difficulties`) with the following data structure: ```json [ { "role": "assault", "difficulty": "easy", "data": "assets/database/bots/types/assault.json difficulty easy contents" }, { "role": "pmcbot", "difficulty": "normal", "data": "assets/database/bots/types/pmcbot.json difficulty normal contents" } ] ``` The request expects all roles and all their respective difficulties to be in the response. Co-authored-by: Merijn Hendriks Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Modules/pulls/111 Reviewed-by: DrakiaXYZ Co-authored-by: Merijn Hendriks Co-committed-by: Merijn Hendriks --- project/Aki.Custom/Models/DifficultyInfo.cs | 24 +++++++++++ .../Aki.Custom/Patches/BotDifficultyPatch.cs | 4 +- .../Aki.Custom/Patches/CoreDifficultyPatch.cs | 6 +++ project/Aki.Custom/Utils/DifficultyManager.cs | 42 +++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 project/Aki.Custom/Models/DifficultyInfo.cs create mode 100644 project/Aki.Custom/Utils/DifficultyManager.cs diff --git a/project/Aki.Custom/Models/DifficultyInfo.cs b/project/Aki.Custom/Models/DifficultyInfo.cs new file mode 100644 index 0000000..7531773 --- /dev/null +++ b/project/Aki.Custom/Models/DifficultyInfo.cs @@ -0,0 +1,24 @@ +using System.Runtime.Serialization; + +namespace Aki.Custom.Models +{ + [DataContract] + public struct DifficultyInfo + { + [DataMember(Name = "role")] + public string Role; + + [DataMember(Name = "difficulty")] + public string Difficulty; + + [DataMember(Name = "data")] + public string Data; + + public DifficultyInfo(string role, string difficulty, string data) + { + Role = role; + Difficulty = difficulty; + Data = data; + } + } +} \ No newline at end of file diff --git a/project/Aki.Custom/Patches/BotDifficultyPatch.cs b/project/Aki.Custom/Patches/BotDifficultyPatch.cs index 2f5ebe3..ad9ee13 100644 --- a/project/Aki.Custom/Patches/BotDifficultyPatch.cs +++ b/project/Aki.Custom/Patches/BotDifficultyPatch.cs @@ -1,4 +1,4 @@ -using Aki.Common.Http; +using Aki.Custom.Utils; using Aki.Reflection.Patching; using Aki.Reflection.Utils; using EFT; @@ -21,7 +21,7 @@ namespace Aki.Custom.Patches [PatchPrefix] private static bool PatchPrefix(ref string __result, BotDifficulty botDifficulty, WildSpawnType role) { - __result = RequestHandler.GetJson($"/singleplayer/settings/bot/difficulty/{role}/{botDifficulty}"); + __result = DifficultyManager.Get(botDifficulty, role); var resultIsNullEmpty = string.IsNullOrWhiteSpace(__result); if (resultIsNullEmpty) { diff --git a/project/Aki.Custom/Patches/CoreDifficultyPatch.cs b/project/Aki.Custom/Patches/CoreDifficultyPatch.cs index 4dc2d70..26c8383 100644 --- a/project/Aki.Custom/Patches/CoreDifficultyPatch.cs +++ b/project/Aki.Custom/Patches/CoreDifficultyPatch.cs @@ -2,6 +2,7 @@ using Aki.Reflection.Patching; using Aki.Reflection.Utils; using Aki.Common.Http; using System.Reflection; +using Aki.Custom.Utils; namespace Aki.Custom.Patches { @@ -19,6 +20,11 @@ namespace Aki.Custom.Patches [PatchPrefix] private static bool PatchPrefix(ref string __result) { + // core difficulty is requested before the others + // so update the others now! + DifficultyManager.Update(); + + // update core difficulty __result = RequestHandler.GetJson("/singleplayer/settings/bot/difficulty/core/core"); return string.IsNullOrWhiteSpace(__result); } diff --git a/project/Aki.Custom/Utils/DifficultyManager.cs b/project/Aki.Custom/Utils/DifficultyManager.cs new file mode 100644 index 0000000..b9de3d9 --- /dev/null +++ b/project/Aki.Custom/Utils/DifficultyManager.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using EFT; +using Aki.Common.Http; +using Aki.Common.Utils; +using Aki.Custom.Models; + +namespace Aki.Custom.Utils +{ + public static class DifficultyManager + { + public static List Difficulties { get; private set; } + + static DifficultyManager() + { + Difficulties = new List(); + } + + public static void Update() + { + // remove existing list + Difficulties.Clear(); + + // get new difficulties + var json = RequestHandler.GetJson("/singleplayer/settings/bot/difficulties"); + Difficulties = Json.Deserialize>(json); + } + + public static string Get(BotDifficulty botDifficulty, WildSpawnType role) + { + foreach (var entry in Difficulties) + { + if (botDifficulty.ToString().ToLower() == entry.Difficulty + && role.ToString().ToLower() == entry.Role) + { + return entry.Data; + } + } + + return string.Empty; + } + } +}