Parse exchange field for Item Definitions, giving Recipe

This commit is contained in:
Garry Newman 2016-11-08 12:43:49 +00:00
parent 019d46580e
commit 41400f56e6
3 changed files with 143 additions and 4 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Facepunch.Steamworks.Test
@ -221,6 +222,50 @@ public void InventoryDefinitions()
}
}
[TestMethod]
public void InventoryDefinitionExchange()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsNotNull( client.Inventory.Definitions );
Assert.AreNotEqual( 0, client.Inventory.Definitions.Length );
foreach ( var i in client.Inventory.Definitions )
{
if ( i.Recipes == null ) continue;
Console.WriteLine( "Ways To Create " + i.Name );
foreach ( var r in i.Recipes )
{
Console.WriteLine( " " + string.Join( ", ", r.Ingredients.Select( x => x.Count + " x " + x.Definition.Name ) ) );
}
}
}
}
[TestMethod]
public void InventoryDefinitionIngredients()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
Assert.IsNotNull( client.Inventory.Definitions );
Assert.AreNotEqual( 0, client.Inventory.Definitions.Length );
foreach ( var i in client.Inventory.Definitions )
{
if ( i.IngredientFor == null ) continue;
Console.WriteLine( i.Name + " Can Be Used to Make" );
foreach ( var r in i.IngredientFor )
{
Console.WriteLine( " " + r.Result.Name );
}
}
}
}
[TestMethod]
public void InventoryItemList()
{

View File

@ -18,11 +18,27 @@ public class Definition
internal SteamNative.SteamInventory inventory;
public int Id { get; private set; }
public string Name;
public string Description;
public string Name { get; set; }
public string Description { get; set; }
public DateTime Created;
public DateTime Modified;
/// <summary>
/// If this item can be created using other items this string will contain a comma seperated
/// list of definition ids that can be used, ie "100,101;102x5;103x3,104x3"
/// </summary>
public string ExchangeSchema { get; set; }
/// <summary>
/// A list of recepies for creating this item. Can be null if none.
/// </summary>
public Recipe[] Recipes { get; set; }
/// <summary>
/// A list of recepies we're included in
/// </summary>
public Recipe[] IngredientFor { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
private Dictionary<string, string> customProperties;
@ -86,6 +102,7 @@ internal void SetupCommonProperties()
Description = GetStringProperty( "description" );
Created = GetProperty<DateTime>( "timestamp" );
Modified = GetProperty<DateTime>( "modified" );
ExchangeSchema = GetStringProperty( "exchange" );
}
/// <summary>
@ -100,6 +117,78 @@ public void TriggerItemDrop()
inventory.TriggerItemDrop( ref result, Id );
inventory.DestroyResult( result );
}
internal void Link( Definition[] definitions )
{
LinkExchange( definitions );
}
private void LinkExchange( Definition[] definitions )
{
if ( string.IsNullOrEmpty( ExchangeSchema ) ) return;
var parts = ExchangeSchema.Split( new[] { ';' }, StringSplitOptions.RemoveEmptyEntries );
Recipes = parts.Select( x => Recipe.FromString( x, definitions, this ) ).ToArray();
}
internal void InRecipe( Recipe r )
{
if ( IngredientFor == null )
IngredientFor = new Recipe[0];
var list = new List<Recipe>( IngredientFor );
list.Add( r );
IngredientFor = list.ToArray();
}
}
public struct Recipe
{
public struct Ingredient
{
public int DefinitionId;
public Definition Definition;
public int Count;
internal static Ingredient FromString( string part, Definition[] definitions )
{
var i = new Ingredient();
i.Count = 1;
if ( part.Contains( 'x' ) )
{
var idx = part.IndexOf( 'x' );
i.Count = int.Parse( part.Substring( idx ) );
part = part.Substring( 0, idx );
}
i.DefinitionId = int.Parse( part );
i.Definition = definitions.FirstOrDefault( x => x.Id == i.DefinitionId );
return i;
}
}
public Definition Result;
public Ingredient[] Ingredients;
internal static Recipe FromString( string part, Definition[] definitions, Definition Result )
{
var r = new Recipe();
r.Result = Result;
var parts = part.Split( new[] { ',' }, StringSplitOptions.RemoveEmptyEntries );
r.Ingredients = parts.Select( x => Ingredient.FromString( x, definitions ) ).ToArray();
foreach ( var i in r.Ingredients )
{
i.Definition.InRecipe( r );
}
return r;
}
}
}
}

View File

@ -118,6 +118,11 @@ internal void FetchItemDefinitions()
return CreateDefinition( x );
} ).ToArray();
foreach ( var def in Definitions )
{
def.Link( Definitions );
}
}
/// <summary>