Fix offer unlock quests, 1.5.1

This commit is contained in:
Tyfon
2024-05-26 02:51:18 -07:00
parent 0082fa1e3a
commit 2aacea56d1
2 changed files with 44 additions and 12 deletions

View File

@@ -1,8 +1,10 @@
using Aki.Reflection.Patching; using Aki.Reflection.Patching;
using EFT.InventoryLogic;
using EFT.Quests; using EFT.Quests;
using EFT.UI; using EFT.UI;
using EFT.UI.Ragfair; using EFT.UI.Ragfair;
using HarmonyLib; using HarmonyLib;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@@ -78,7 +80,7 @@ namespace UIFixes
public class OfferViewLockedQuestPatch : ModulePatch public class OfferViewLockedQuestPatch : ModulePatch
{ {
private static readonly Dictionary<string, RawQuestClass> QuestUnlocks = []; private static readonly Dictionary<string, string> QuestUnlocks = [];
protected override MethodBase GetTargetMethod() protected override MethodBase GetTargetMethod()
{ {
@@ -93,25 +95,55 @@ namespace UIFixes
return; return;
} }
string templateId = __instance.Offer_0.Item.TemplateId;
if (__instance.Offer_0.Locked) if (__instance.Offer_0.Locked)
{ {
RawQuestClass quest = null; string questName = null;
if (QuestUnlocks.ContainsKey(templateId)) if (QuestUnlocks.ContainsKey(__instance.Offer_0.Id))
{ {
quest = QuestUnlocks[templateId]; questName = QuestUnlocks[__instance.Offer_0.Id];
} }
else else
{ {
quest = R.QuestCache.Instance.GetAllQuestTemplates() // Filter by as much data available. There are some unlocks that are ambiguous without access to the server-side questassorts.json.
.FirstOrDefault(q => q.Rewards[EQuestStatus.Success] // Using a tuple of (quest, rewards) to avoid doing all the reward checks more than once
.Any(r => r.type == ERewardType.AssortmentUnlock && r.items.Any(i => i._tpl == templateId))); var questsAndRewards = R.QuestCache.Instance.GetAllQuestTemplates()
QuestUnlocks[templateId] = quest; .Select(q => (quest: q, rewards: q.Rewards[EQuestStatus.Success]
.Where(r => r.type == ERewardType.AssortmentUnlock &&
r.traderId == __instance.Offer_0.User.Id &&
r.loyaltyLevel == __instance.Offer_0.LoyaltyLevel &&
r.items.First(i => i._id == r.target)._tpl == __instance.Offer_0.Item.TemplateId)))
.Where(x => x.rewards.Any());
if (questsAndRewards.Count() > 1)
{
// Some of the ambiguous unlocks are weapons with full loadouts we can actually compare
List<Item> items = [];
Item.smethod_0(__instance.Offer_0.Item, items, (item, container) => true); // complete list of items, including the top level item
// Hashset.SetEquals compares lists, ignoring order (don't care) and duplicates (don't have any)
var allItemTemplateIds = new HashSet<string>(items.Select(i => i.TemplateId));
questsAndRewards = questsAndRewards.Where(x => x.rewards.Any(r => allItemTemplateIds.SetEquals(r.items.Select(i => i._tpl))));
} }
if (quest != null) if (questsAndRewards.Count() > 1)
{ {
____hoverTooltipArea.SetMessageText(____hoverTooltipArea.String_1 + " (" + quest.Name + ")", true); // Some quests are USEC/Bear versions with the same name
questsAndRewards = questsAndRewards.Distinct(x => x.quest.Name);
}
if (questsAndRewards.Count() == 1)
{
questName = questsAndRewards.First().quest.Name;
}
// If it's still not clear by now, it's either missing or too ambiguous (e.g. Zeus thermal scope) ¯\_(ツ)_/¯
// Cache the result, even if empty
QuestUnlocks.Add(__instance.Offer_0.Id, questName);
}
if (!String.IsNullOrEmpty(questName))
{
____hoverTooltipArea.SetMessageText(____hoverTooltipArea.String_1 + " (" + questName + ")", true);
} }
} }
} }

View File

@@ -4,7 +4,7 @@
<TargetFramework>net471</TargetFramework> <TargetFramework>net471</TargetFramework>
<AssemblyName>Tyfon.UIFixes</AssemblyName> <AssemblyName>Tyfon.UIFixes</AssemblyName>
<Description>SPT UI Fixes</Description> <Description>SPT UI Fixes</Description>
<Version>1.5.0</Version> <Version>1.5.1</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<Configurations>Debug;Release;Dist</Configurations> <Configurations>Debug;Release;Dist</Configurations>