Facepunch.Steamworks/Facepunch.Steamworks.Test/Client/InventoryTest.cs

295 lines
10 KiB
C#
Raw Normal View History

2016-12-01 14:17:03 +03:00
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
namespace Facepunch.Steamworks.Test
{
[DeploymentItem( "steam_api64.dll" )]
[TestClass]
public class Inventory
{
[TestMethod]
public void InventoryDefinitions()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
2018-03-21 14:27:18 +03:00
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
2016-12-01 14:17:03 +03:00
Assert.IsNotNull( client.Inventory.Definitions );
Assert.AreNotEqual( 0, client.Inventory.Definitions.Length );
2018-01-31 15:53:23 +03:00
foreach ( var i in client.Inventory.Definitions.Where( x => x.PriceCategory != "" ) )
2016-12-01 14:17:03 +03:00
{
Console.WriteLine( "{0}: {1} ({2})", i.Id, i.Name, i.Type );
Console.WriteLine( " itemshortname: {0}", i.GetStringProperty( "itemshortname" ) );
Console.WriteLine( " workshopdownload: {0}", i.GetStringProperty( "workshopdownload" ) );
Console.WriteLine( " IconUrl: {0}", i.IconUrl );
Console.WriteLine( " IconLargeUrl: {0}", i.IconLargeUrl );
2018-01-31 15:53:23 +03:00
Console.WriteLine( " PriceRaw: {0}", i.PriceCategory );
2016-12-01 14:17:03 +03:00
Console.WriteLine( " PriceDollars: {0}", i.PriceDollars );
}
}
}
[TestMethod]
public void InventoryDefinitionExchange()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
2018-03-21 14:27:18 +03:00
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
2016-12-01 14:17:03 +03:00
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 ) )
{
2018-03-21 14:27:18 +03:00
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
2016-12-01 14:17:03 +03:00
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()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
2018-03-21 14:27:18 +03:00
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
2016-12-01 14:17:03 +03:00
bool CallbackCalled = false;
// OnUpdate hsould be called when we receive a list of our items
client.Inventory.OnUpdate += () => { CallbackCalled = true; };
2016-12-01 14:17:03 +03:00
// tell steam to download the items
client.Inventory.Refresh();
// Wait for the items
var timeout = Stopwatch.StartNew();
while ( client.Inventory.Items == null )
{
client.Update();
System.Threading.Thread.Sleep( 1000 );
if ( timeout.Elapsed.TotalSeconds > 10 )
break;
}
// make sure callback was called
Assert.IsTrue( CallbackCalled );
// Make sure items are valid
foreach ( var item in client.Inventory.Items )
{
Assert.IsNotNull( item );
Assert.IsNotNull( item.Definition );
Console.WriteLine( item.Definition.Name + " - " + item.Id );
}
}
}
2016-12-01 15:26:53 +03:00
2018-01-31 16:30:51 +03:00
[TestMethod]
public void InventoryItemProperties()
{
using (var client = new Facepunch.Steamworks.Client(252490))
{
while ( true )
{
client.Update();
if (client.Inventory.Items == null) continue;
foreach (var item in client.Inventory.Items)
{
Console.WriteLine($"{item.Id} ({item.Definition.Name})");
foreach (var property in item.Properties)
{
Console.WriteLine($" {property.Key} = {property.Value}");
}
Console.WriteLine("");
}
return;
}
}
}
2016-12-01 15:26:53 +03:00
[TestMethod]
public void Deserialize()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
2018-03-21 14:27:18 +03:00
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
2016-12-01 15:26:53 +03:00
Assert.IsTrue( client.IsValid );
2018-01-31 15:53:23 +03:00
Assert.IsNotNull(client.Inventory.Definitions);
Assert.AreNotEqual(0, client.Inventory.Definitions.Length);
2016-12-01 15:26:53 +03:00
client.Inventory.Refresh();
2018-01-31 14:28:47 +03:00
var stopwatch = Stopwatch.StartNew();
2016-12-01 15:26:53 +03:00
//
// Block until we have the items
//
while ( client.Inventory.SerializedItems == null )
{
client.Update();
2018-01-31 14:28:47 +03:00
if (stopwatch.Elapsed.Seconds > 10)
throw new System.Exception("Getting SerializedItems took too long");
2016-12-01 15:26:53 +03:00
}
Assert.IsNotNull( client.Inventory.SerializedItems );
Assert.IsTrue( client.Inventory.SerializedItems.Length > 4 );
using ( var server = new Facepunch.Steamworks.Server( 252490, new ServerInit( "rust", "Rust" ) ) )
2016-12-01 15:26:53 +03:00
{
server.LogOnAnonymous();
Assert.IsTrue( server.IsValid );
var result = server.Inventory.Deserialize( client.Inventory.SerializedItems );
2018-01-31 14:28:47 +03:00
stopwatch = Stopwatch.StartNew();
while (result.IsPending)
{
server.Update();
if (stopwatch.Elapsed.Seconds > 10)
throw new System.Exception("result took too long");
}
2016-12-01 15:26:53 +03:00
Assert.IsFalse( result.IsPending );
Assert.IsNotNull( result.Items );
foreach ( var item in result.Items )
{
Console.WriteLine( "Item: {0} ({1})", item.Id, item.DefinitionId );
Console.WriteLine( "Item: {0} ({1})", item.Id, item.DefinitionId );
}
}
}
}
2018-01-31 15:53:23 +03:00
[TestMethod]
public void PurchaseItems()
{
using (var client = new Facepunch.Steamworks.Client(252490))
{
2018-03-21 14:27:18 +03:00
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
2018-01-31 15:53:23 +03:00
Assert.IsNotNull(client.Inventory.Definitions);
Assert.AreNotEqual(0, client.Inventory.Definitions.Length);
while ( client.Inventory.Currency == null )
{
client.Update();
}
var shoppingList = client.Inventory.DefinitionsWithPrices.Take(2).ToArray();
bool waitingForCallback = true;
if ( !client.Inventory.StartPurchase(shoppingList, ( order, tran ) =>
{
Console.WriteLine($"Order: {order}, Transaction {tran}");
waitingForCallback = false;
} ) )
{
throw new Exception("Couldn't Buy!");
}
while ( waitingForCallback )
{
client.Update();
}
}
}
[TestMethod]
public void ListPrices()
{
using (var client = new Facepunch.Steamworks.Client(252490))
{
2018-03-21 14:27:18 +03:00
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
2018-01-31 15:53:23 +03:00
Assert.IsNotNull(client.Inventory.Definitions);
Assert.AreNotEqual(0, client.Inventory.Definitions.Length);
while (client.Inventory.Currency == null)
{
client.Update();
}
foreach ( var i in client.Inventory.Definitions.Where( x => x.LocalPrice > 0 ) )
{
Console.WriteLine( $" {i.Name} - {i.LocalPriceFormatted} ({client.Inventory.Currency})" );
}
}
}
2016-12-01 14:17:03 +03:00
}
}