Item Prices

This commit is contained in:
Garry Newman 2019-04-27 17:07:39 +01:00
parent 5a25d750f2
commit aa666a3149
5 changed files with 225 additions and 4 deletions

View File

@ -13,9 +13,30 @@ namespace Steamworks
public class InventoryTest
{
[TestMethod]
public async Task GetDefinitions()
public async Task GetItemsWithPricesAsync()
{
var items = await SteamInventory.GetItemsWithPricesAsync();
foreach ( var item in items )
{
Console.WriteLine( $"[{item.LocalPrice}] {item.Name}" );
}
}
[TestMethod]
public async Task IutemDefs()
{
var items = await SteamInventory.GetItemsWithPricesAsync();
foreach ( var item in items )
{
Console.WriteLine( $"{item.Id}" );
foreach ( var prop in item.Properties )
{
Console.WriteLine( $" {prop.Key}: {prop.Value}" );
}
Console.WriteLine( $"" );
}
}
}

View File

@ -387,13 +387,13 @@ internal uint GetNumItemsWithPrices()
#region FunctionMeta
[UnmanagedFunctionPointer( CallingConvention.ThisCall )]
[return: MarshalAs( UnmanagedType.I1 )]
private delegate bool FGetItemsWithPrices( IntPtr self, [In,Out] SteamItemDef_t[] pArrayItemDefs, ref ulong pCurrentPrices, ref ulong pBasePrices, uint unArrayLength );
private delegate bool FGetItemsWithPrices( IntPtr self, [In,Out] SteamItemDef_t[] pArrayItemDefs, [In,Out] ulong[] pCurrentPrices, [In,Out] ulong[] pBasePrices, uint unArrayLength );
private FGetItemsWithPrices _GetItemsWithPrices;
#endregion
internal bool GetItemsWithPrices( [In,Out] SteamItemDef_t[] pArrayItemDefs, ref ulong pCurrentPrices, ref ulong pBasePrices, uint unArrayLength )
internal bool GetItemsWithPrices( [In,Out] SteamItemDef_t[] pArrayItemDefs, [In,Out] ulong[] pCurrentPrices, [In,Out] ulong[] pBasePrices, uint unArrayLength )
{
return _GetItemsWithPrices( Self, pArrayItemDefs, ref pCurrentPrices, ref pBasePrices, unArrayLength );
return _GetItemsWithPrices( Self, pArrayItemDefs, pCurrentPrices, pBasePrices, unArrayLength );
}
#region FunctionMeta

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
@ -28,10 +29,37 @@ internal static void InstallEvents()
{
new Event<SteamInventoryFullUpdate_t>( x => OnInventoryUpdated?.Invoke() );
new Event<SteamInventoryDefinitionUpdate_t>( x => OnDefinitionsUpdated?.Invoke() );
Internal.LoadItemDefinitions();
}
public static event Action OnInventoryUpdated;
public static event Action OnDefinitionsUpdated;
public static async Task<SteamItemDef[]> GetItemsWithPricesAsync()
{
var priceRequest = await Internal.RequestPrices();
if ( !priceRequest.HasValue || priceRequest.Value.Result != Result.OK )
return null;
Console.WriteLine( $"Currency: {priceRequest?.Currency}" );
var num = Internal.GetNumItemsWithPrices();
Console.WriteLine( $"num: {num}" );
if ( num <= 0 )
return null;
var defs = new SteamItemDef_t[num];
var currentPrices = new ulong[num];
var baseprices = new ulong[num];
var gotPrices = Internal.GetItemsWithPrices( defs, currentPrices, baseprices, num );
if ( !gotPrices )
return null;
return defs.Select( x => new SteamItemDef { _id = x } ).ToArray();
}
}
}

View File

@ -0,0 +1,170 @@
using System;
using System.Collections.Generic;
using Steamworks.Data;
namespace Steamworks
{
public struct SteamItemDef
{
internal SteamItemDef_t _id;
public int Id => _id.Value;
/// <summary>
/// Shortcut to call GetProperty( "name" )
/// </summary>
public string Name => GetProperty( "name" );
/// <summary>
/// Shortcut to call GetProperty( "description" )
/// </summary>
public string Description => GetProperty( "description" );
/// <summary>
/// Shortcut to call GetProperty( "icon_url" )
/// </summary>
public string IconUrl => GetProperty( "icon_url" );
/// <summary>
/// Shortcut to call GetProperty( "icon_url_large" )
/// </summary>
public string IconUrlLarge => GetProperty( "icon_url_large" );
/// <summary>
/// Shortcut to call GetProperty( "price_category" )
/// </summary>
public string PriceCategory => GetProperty( "price_category" );
/// <summary>
/// Shortcut to call GetProperty( "type" )
/// </summary>
public string Type => GetProperty( "type" );
/// <summary>
/// Shortcut to call GetProperty( "exchange" )
/// </summary>
public string Exchange => GetProperty( "exchange" );
/// <summary>
/// Shortcut to call GetBoolProperty( "marketable" )
/// </summary>
public bool Marketable => GetBoolProperty( "marketable" );
/// <summary>
/// Shortcut to call GetBoolProperty( "tradable" )
/// </summary>
public bool Tradable => GetBoolProperty( "tradable" );
/// <summary>
/// Gets the property timestamp
/// </summary>
public DateTime Created => GetProperty<DateTime>( "timestamp" );
/// <summary>
/// Gets the property modified
/// </summary>
public DateTime Modified => GetProperty<DateTime>( "modified" );
/// <summary>
/// Get a specific property by name
/// </summary>
public string GetProperty( string name )
{
var sb = Helpers.TakeStringBuilder();
uint _ = (uint)sb.Capacity;
if ( !SteamInventory.Internal.GetItemDefinitionProperty( Id, name, sb, ref _ ) )
return null;
return sb.ToString();
}
/// <summary>
/// Read a raw property from the definition schema
/// </summary>
public bool GetBoolProperty( string name )
{
string val = GetProperty( name );
if ( val.Length == 0 ) return false;
if ( val[0] == '0' || val[0] == 'F' || val[0] == 'f' ) return false;
return true;
}
/// <summary>
/// Read a raw property from the definition schema
/// </summary>
public T GetProperty<T>( string name )
{
string val = GetProperty( name );
if ( string.IsNullOrEmpty( val ) )
return default( T );
try
{
return (T)Convert.ChangeType( val, typeof( T ) );
}
catch ( System.Exception )
{
return default( T );
}
}
/// <summary>
/// Gets a list of all properties on this item
/// </summary>
public IEnumerable<KeyValuePair<string, string>> Properties
{
get
{
var list = GetProperty( null );
var keys = list.Split( ',' );
foreach ( var key in keys )
{
yield return new KeyValuePair<string, string>( key, GetProperty( key ) );
}
}
}
/// <summary>
/// Returns the price of this item in the local currency (SteamInventory.Currency)
/// </summary>
public int LocalPrice
{
get
{
ulong curprice = 0;
ulong baseprice = 0;
if ( !SteamInventory.Internal.GetItemPrice( Id, ref curprice, ref baseprice ) )
return 0;
return (int) curprice;
}
}
/// <summary>
/// If the price has been discounted, LocalPrice will differ from LocalBasePrice
/// (assumed, this isn't documented)
/// </summary>
public int LocalBasePrice
{
get
{
ulong curprice = 0;
ulong baseprice = 0;
if ( !SteamInventory.Internal.GetItemPrice( Id, ref curprice, ref baseprice ) )
return 0;
return (int)baseprice;
}
}
public int CurrentPrice;
public int BasePrice;
}
}

View File

@ -66,6 +66,8 @@ public virtual bool IsVector
if ( VarName == "prgUsers" ) return true;
if ( VarName == "punArrayQuantity" ) return true;
if ( VarName == "pArrayItemDefs" ) return true;
if ( VarName == "pBasePrices" ) return true;
if ( VarName == "pCurrentPrices" ) return true;
if ( VarName == "pDetails" && Func == "GetDownloadedLeaderboardEntry" ) return true;
if ( VarName == "pData" && NativeType.EndsWith( "*" ) && Func.StartsWith( "GetGlobalStatHistory" ) ) return true;
if ( NativeType.EndsWith( "**" ) ) return true;