Skip to content

Commit

Permalink
Fixes for gifting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarno458 committed Oct 30, 2023
1 parent bcb9c33 commit 1e29b61
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 24 deletions.
2 changes: 2 additions & 0 deletions TsRandomizer/Archipelago/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static class Client

public static LocationCheckHelper LocationCheckHelper => session.Locations;

public static ReceivedItemsHelper ItemsHelper => session.Items;

public static DataStorageHelper DataStorage => session.DataStorage;

public static PlayerHelper Players => session.Players;
Expand Down
24 changes: 22 additions & 2 deletions TsRandomizer/Archipelago/Gifting/GiftingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Archipelago.MultiClient.Net.Models;
using Newtonsoft.Json.Linq;
using Timespinner.GameAbstractions.Inventory;
using TsRandomizer.IntermediateObjects;
using TsRandomizer.Screens;
using APGiftingService = Archipelago.Gifting.Net.Service.GiftingService;

Expand Down Expand Up @@ -44,7 +45,7 @@ void OnOnValueChanged(JToken originalvalue, JToken newvalue)
{
try
{
NumberOfGifts = newvalue.ToObject<Dictionary<string, JToken>>().Count;
NumberOfGifts = newvalue.ToObject<Dictionary<string, JToken>>()?.Count ?? 0;
}
catch (Exception e)
{
Expand Down Expand Up @@ -100,7 +101,7 @@ public bool Send(InventoryItem item, AcceptedTraits playerInfo, int amount)
{
try
{
var giftItem = new GiftItem(item.Name, amount, 0);
var giftItem = new GiftItem(GetItemName(item), amount, 0);
var traits = TraitMapping.ValuesPerItem[item]
.Select(t => new GiftTrait(t.Key.ToString(), 1, t.Value))
.ToArray();
Expand All @@ -115,6 +116,25 @@ public bool Send(InventoryItem item, AcceptedTraits playerInfo, int amount)
return false;
}

string GetItemName(InventoryItem item)
{
ItemIdentifier identifier;

switch (item)
{
case InventoryUseItem useItem:
identifier = new ItemIdentifier(useItem.UseItemType);
break;
case InventoryEquipment equipment:
identifier = new ItemIdentifier(equipment.EquipmentType);
break;
default:
throw new ArgumentOutOfRangeException(nameof(item), "parameter should be either UseItem or Equipment");
}

return Client.ItemsHelper.GetItemName(ItemMap.GetItemId(identifier)) ?? item.Name;
}

public void AcceptGift(Gift gift, int acceptedAmount)
{
try
Expand Down
64 changes: 45 additions & 19 deletions TsRandomizer/Archipelago/Gifting/TraitMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Timespinner.GameAbstractions.Inventory;
using TsRandomizer.Extensions;
using TsRandomizer.IntermediateObjects;

namespace TsRandomizer.Archipelago.Gifting
{
Expand Down Expand Up @@ -141,23 +141,15 @@ public Dictionary<Trait, float> this[InventoryItem item] {
public bool TryGetValue(EInventoryUseItemType item, out Dictionary<Trait, float> traits) => ValuesPerUseItem.TryGetValue(item, out traits);
public bool TryGetValue(EInventoryEquipmentType item, out Dictionary<Trait, float> traits) => ValuesPerEquipmentItem.TryGetValue(item, out traits);

static readonly Dictionary<string, InventoryItem> ItemNameToInventoryItemCache = new Dictionary<string, InventoryItem>();

public static InventoryItem ParseItem(string name, Dictionary<Trait, float> traits, int amount)
{
if(Enum.IsDefined(typeof(EInventoryUseItemType), name))
{
var useItemType = (EInventoryUseItemType)typeof(EInventoryUseItemType).GetEnumValue(name);

if (ValuesPerUseItem.ContainsKey(useItemType))
return new InventoryUseItem(useItemType) { Count = amount };
}

if (Enum.IsDefined(typeof(EInventoryEquipmentType), name))
{
var armorType = (EInventoryEquipmentType)typeof(EInventoryEquipmentType).GetEnumValue(name);
if (!ItemNameToInventoryItemCache.Any())
InitializeItemNameToInventoryItemCache();

if (ValuesPerEquipmentItem.ContainsKey(armorType))
return new InventoryEquipment(armorType) { Count = amount };
}
if (ItemNameToInventoryItemCache.TryGetValue(name, out var item))
return AddAmountToItem(item, amount);

if (traits.ContainsKey(Trait.Speed))
return new InventoryUseItem(EInventoryUseItemType.WarpCard) { Count = amount };
Expand Down Expand Up @@ -186,10 +178,44 @@ public static InventoryItem ParseItem(string name, Dictionary<Trait, float> trai

return new InventoryUseItem(FindClosestMatch(traits)) { Count = amount };
}

static void InitializeItemNameToInventoryItemCache()
{
foreach (var itemType in ValuesPerUseItem.Keys)
{
var identifier = new ItemIdentifier(itemType);
var name = Client.ItemsHelper.GetItemName(ItemMap.GetItemId(identifier));

ItemNameToInventoryItemCache.Add(name, new InventoryUseItem(identifier.UseItem));
}

foreach (var itemType in ValuesPerEquipmentItem.Keys)
{
var identifier = new ItemIdentifier(itemType);
var name = Client.ItemsHelper.GetItemName(ItemMap.GetItemId(identifier));

ItemNameToInventoryItemCache.Add(name, new InventoryEquipment(identifier.Equipment));
}
}

static InventoryItem AddAmountToItem(InventoryItem item, int amount)
{
switch (item)
{
case InventoryUseItem useItem:
useItem.Count = amount;
return useItem;
case InventoryEquipment equipment:
equipment.Count = amount;
return equipment;
default:
throw new ArgumentOutOfRangeException(nameof(item), "parameter should be either UseItem or Equipment");
}
}

static EInventoryUseItemType FindClosestMatch(Dictionary<Trait, float> traits)
{
var itemTypesWithMatchCount = new Dictionary<int, EInventoryUseItemType>();
var itemTypesWithMatchCount = new Dictionary<EInventoryUseItemType, int>();
var mostMatches = 0;

foreach (var useItemTraitMapping in ValuesPerUseItem)
Expand All @@ -201,12 +227,12 @@ static EInventoryUseItemType FindClosestMatch(Dictionary<Trait, float> traits)
matches++;

if (matches >= mostMatches)
itemTypesWithMatchCount.Add(matches, useItemTraitMapping.Key);
itemTypesWithMatchCount.Add(useItemTraitMapping.Key, matches);
}

var mostMatchedItemTypes = itemTypesWithMatchCount
.Where(kvp => kvp.Key == mostMatches)
.Select(kvp => kvp.Value);
.Where(kvp => kvp.Value == mostMatches)
.Select(kvp => kvp.Key);

var closestMatch = 0f;
var closestItemType = EInventoryUseItemType.None;
Expand Down
4 changes: 2 additions & 2 deletions TsRandomizer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("694e46c5-fd46-4cc8-8b71-d381e154ccf7")]
[assembly: AssemblyVersion("1.26.2")]
[assembly: AssemblyFileVersion("1.26.2")]
[assembly: AssemblyVersion("1.26.3")]
[assembly: AssemblyFileVersion("1.26.3")]

[assembly: InternalsVisibleTo("TsRandomizer.Tests")]
[assembly: InternalsVisibleTo("TsRandomizerSeedGeneratah")]
2 changes: 1 addition & 1 deletion TsRandomizer/Screens/Gifting/GiftingReceiveScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ void RefreshGiftInfo(SpriteFont menuFont)
if (i == NumberOfTraitsToDisplay - 1)
{
if (traitNames.Length == NumberOfTraitsToDisplay)
traits.Add(traitNames[i - 1].ToString());
traits.Add(traitNames[i].ToString());
else if (traitNames.Length < NumberOfTraitsToDisplay)
traits.Add("");
else
Expand Down

0 comments on commit 1e29b61

Please sign in to comment.