InventoryRecipe

This commit is contained in:
Garry Newman 2019-05-07 14:27:11 +01:00
parent 0ae14db7f8
commit 78c69b15a8
3 changed files with 127 additions and 1 deletions

View File

@ -70,6 +70,33 @@ public async Task GetAllItems()
}
}
}
[TestMethod]
public async Task GetExchanges()
{
var result = await SteamInventory.WaitForDefinitions( 5 );
Assert.IsTrue( result );
foreach ( var def in SteamInventory.Definitions )
{
var exchangelist = def.GetRecipes();
if ( exchangelist == null ) continue;
foreach ( var exchange in exchangelist )
{
Assert.AreEqual( exchange.Result, def );
Console.WriteLine( $"{def.Name}:" );
foreach ( var item in exchange.Ingredients )
{
Console.WriteLine( $" {item.Count} x {item.Definition.Name}" );
}
Console.WriteLine( $"" );
}
}
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Steamworks.Data;
namespace Steamworks
@ -48,7 +49,19 @@ public InventoryDef( InventoryDefId defId )
/// <summary>
/// Shortcut to call GetProperty( "exchange" )
/// </summary>
public string Exchange => GetProperty( "exchange" );
public string ExchangeSchema => GetProperty( "exchange" );
/// <summary>
/// Get a list of exchanges that are available to make this item
/// </summary>
public InventoryRecipe[] GetRecipes()
{
if ( string.IsNullOrEmpty( ExchangeSchema ) ) return null;
var parts = ExchangeSchema.Split( new[] { ';' }, StringSplitOptions.RemoveEmptyEntries );
return parts.Select( x => InventoryRecipe.FromString( x, this ) ).ToArray();
}
/// <summary>
/// Shortcut to call GetBoolProperty( "marketable" )

View File

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Steamworks.Data;
namespace Steamworks
{
/// <summary>
/// A structured description of an item exchange
/// </summary>
public struct InventoryRecipe
{
public struct Ingredient
{
/// <summary>
/// The definition ID of the ingredient.
/// </summary>
public int DefinitionId;
/// <summary>
/// If we don't know about this item definition this might be null.
/// In which case, DefinitionId should still hold the correct id.
/// </summary>
public InventoryDef Definition;
/// <summary>
/// The amount of this item needed. Generally this will be 1.
/// </summary>
public int Count;
internal static Ingredient FromString( string part )
{
var i = new Ingredient();
i.Count = 1;
try
{
if ( part.Contains( "x" ) )
{
var idx = part.IndexOf( 'x' );
int count = 0;
if ( int.TryParse( part.Substring( idx + 1 ), out count ) )
i.Count = count;
part = part.Substring( 0, idx );
}
i.DefinitionId = int.Parse( part );
i.Definition = SteamInventory.FindDefinition( i.DefinitionId );
}
catch ( System.Exception )
{
return i;
}
return i;
}
}
/// <summary>
/// The item that this will create.
/// </summary>
public InventoryDef Result;
/// <summary>
/// The items, with quantity required to create this item.
/// </summary>
public Ingredient[] Ingredients;
internal static InventoryRecipe FromString( string part, InventoryDef Result )
{
var r = new InventoryRecipe
{
Result = Result
};
var parts = part.Split( new[] { ',' }, StringSplitOptions.RemoveEmptyEntries );
r.Ingredients = parts.Select( x => Ingredient.FromString( x ) ).Where( x => x.DefinitionId != 0 ).ToArray();
return r;
}
}
}