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

224 lines
5.0 KiB
C#
Raw Normal View History

2019-04-27 17:56:11 +03:00
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Steamworks.Data;
namespace Steamworks
{
[TestClass]
[DeploymentItem( "steam_api64.dll" )]
2020-02-22 23:29:37 +03:00
[DeploymentItem( "steam_api.dll" )]
public class InventoryTest
2019-04-27 17:56:11 +03:00
{
[TestMethod]
2019-04-27 23:52:47 +03:00
public async Task LoadItemDefinitionsAsync()
{
var result = await SteamInventory.WaitForDefinitions( 5 );
Assert.IsTrue( result );
result = await SteamInventory.WaitForDefinitions( 5 );
Assert.IsTrue( result );
}
[TestMethod]
public async Task GetDefinitions()
2019-04-27 17:56:11 +03:00
{
2019-04-27 23:52:47 +03:00
await SteamInventory.WaitForDefinitions();
Assert.IsNotNull( SteamInventory.Definitions );
2019-04-27 17:56:11 +03:00
2019-04-27 23:52:47 +03:00
foreach ( var def in SteamInventory.Definitions )
2019-04-27 19:07:39 +03:00
{
2019-04-27 23:52:47 +03:00
Console.WriteLine( $"[{def.Id:0000000000}] {def.Name} [{def.Type}]" );
2019-04-27 19:07:39 +03:00
}
}
[TestMethod]
2019-04-27 23:52:47 +03:00
public async Task GetDefinitionsWithPrices()
2019-04-27 19:07:39 +03:00
{
2019-04-27 23:52:47 +03:00
var defs = await SteamInventory.GetDefinitionsWithPricesAsync();
2019-04-27 19:07:39 +03:00
2019-04-27 23:52:47 +03:00
foreach ( var def in defs )
2019-04-27 19:07:39 +03:00
{
2019-04-27 23:52:47 +03:00
Console.WriteLine( $"[{def.Id:0000000000}] {def.Name} [{def.LocalPriceFormatted}]" );
2019-04-27 19:07:39 +03:00
}
2019-04-27 17:56:11 +03:00
}
2019-04-29 13:05:55 +03:00
[TestMethod]
public async Task GetAllItems()
{
await SteamInventory.WaitForDefinitions();
2019-05-07 18:59:52 +03:00
var result = await SteamInventory.GetAllItemsAsync();
2019-04-29 13:05:55 +03:00
Assert.IsTrue( result.HasValue );
Assert.IsTrue( result.Value.ItemCount > 0 );
2019-04-29 13:05:55 +03:00
using ( result )
{
var items = result.Value.GetItems( true );
Assert.IsNotNull( items );
2019-04-29 13:05:55 +03:00
foreach ( var item in items )
{
Console.WriteLine( $"{item.Id} / {item.DefId} / {item.Quantity} / {item.Def?.Name} /[{item.IsNoTrade}|{item.IsRemoved}|{item.IsConsumed}] " );
2019-04-29 13:05:55 +03:00
foreach ( var prop in item.Properties )
{
Console.WriteLine( $" {prop.Key} : {prop.Value}" );
}
}
}
}
2019-05-07 16:27:11 +03:00
[TestMethod]
public async Task GetItemSpecialProperties()
{
await SteamInventory.WaitForDefinitions();
var result = await SteamInventory.GetAllItemsAsync();
Assert.IsTrue( result.HasValue );
Assert.IsTrue( result.Value.ItemCount > 0 );
using ( result )
{
var items = result.Value.GetItems( true );
Assert.IsNotNull( items );
foreach ( var item in items )
{
Console.WriteLine( $"{item.Id} / {item.DefId} / {item.Quantity} / {item.Def?.Name} " );
Console.WriteLine( $" Acquired: {item.Acquired}" );
Console.WriteLine( $" Origin: {item.Origin}" );
}
}
}
[TestMethod]
public async Task GetAllItemsMultipleTimes()
{
await SteamInventory.WaitForDefinitions();
var fresult = await SteamInventory.GetAllItemsAsync();
Assert.IsTrue( fresult.HasValue );
Assert.IsTrue( fresult.Value.ItemCount > 0 );
await Task.Delay( 1000 );
var result = await SteamInventory.GetAllItemsAsync();
Assert.IsTrue( result.HasValue );
Assert.IsTrue( result.Value.GetItems().Length == fresult.Value.ItemCount );
await Task.Delay( 1000 );
result = await SteamInventory.GetAllItemsAsync();
Assert.IsTrue( result.HasValue );
Assert.IsTrue( result.Value.ItemCount == fresult.Value.ItemCount );
}
[TestMethod]
public async Task Items()
{
SteamInventory.GetAllItems();
await SteamInventory.WaitForDefinitions();
while ( SteamInventory.Items == null )
{
await Task.Delay( 10 );
}
Assert.IsNotNull( SteamInventory.Items );
foreach ( var item in SteamInventory.Items )
{
Console.WriteLine( $"{item.Id} / {item.DefId} / {item.Quantity} / {item.Def.Name}" );
}
}
2019-05-07 16:27:11 +03:00
[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( $"" );
}
}
}
2019-05-13 22:05:10 +03:00
[TestMethod]
public async Task Serialize()
{
await SteamInventory.WaitForDefinitions();
var result = await SteamInventory.GetAllItemsAsync();
Assert.IsTrue( result.HasValue );
var data = result.Value.Serialize();
Assert.IsNotNull( data );
Console.WriteLine( string.Join( "", data.Select( x => x.ToString( "x" ) ) ) );
}
[TestMethod]
public async Task Deserialize()
{
await SteamInventory.WaitForDefinitions();
byte[] data = null;
int itemCount = 0;
// Serialize
{
var result = await SteamInventory.GetAllItemsAsync();
Assert.IsTrue( result.HasValue );
itemCount = result.Value.ItemCount;
data = result.Value.Serialize();
Assert.IsNotNull( data );
result.Value.Dispose();
}
await Task.Delay( 2000 );
// Deserialize
{
var result = await SteamInventory.DeserializeAsync( data );
Assert.IsTrue( result.HasValue );
Assert.AreEqual( itemCount, result.Value.ItemCount );
result.Value.Dispose();
}
}
2019-04-27 17:56:11 +03:00
}
}