#tf2 #steam

tf2-price

Utilities for Team Fortress 2 item pricing

17 releases (10 breaking)

0.11.0 Feb 18, 2023
0.9.0 Feb 1, 2023
0.8.0 Dec 31, 2022
0.7.2 Sep 23, 2022
0.5.3 Mar 18, 2022

#10 in #steam

Download history 1/week @ 2022-11-30 1/week @ 2022-12-07 15/week @ 2022-12-14 2/week @ 2022-12-21 27/week @ 2022-12-28 5/week @ 2023-01-04 29/week @ 2023-01-11 3/week @ 2023-01-18 8/week @ 2023-01-25 34/week @ 2023-02-01 29/week @ 2023-02-08 104/week @ 2023-02-15 26/week @ 2023-02-22 10/week @ 2023-03-01 3/week @ 2023-03-15

61 downloads per month

MIT license

74KB
2K SLoC

tf2-price

Utilities for Team Fortress 2 item pricing.

use tf2_price::{Currencies, ListingCurrencies, refined, scrap};

fn main() {
    let currencies = Currencies {
        keys: 5,
        metal: refined!(2) + scrap!(3),
    };
    
    // 2.33 refined - metal values are counted in weapons.
    assert_eq!(
        currencies.metal,
        42
    );
    
    // String conversions.
    assert_eq!(
        currencies.to_string(),
        "5 keys, 2.33 ref"
    );
    assert_eq!(
        Currencies::try_from("5 keys, 2.33 ref").unwrap(),
        *&currencies
    );
    
    // Serde deserialization.
    assert_eq!(
        serde_json::from_str::<Currencies>(r#"{"keys":5,"metal":2.33}"#).unwrap(),
        *&currencies
    );
    
    let golden_frying_pan = Currencies { keys: 3000, metal: 0 };
    
    // Arithmetic.
    assert_eq!(
        &golden_frying_pan * 2,
        Currencies { keys: 6000, metal: 0 },
    );
    assert_eq!(
        &golden_frying_pan * 2.5,
        Currencies { keys: 7500, metal: 0 },
    );
    assert_eq!(
        &golden_frying_pan + Currencies { keys: 0, metal: 2 },
        Currencies { keys: 3000, metal: 2 },
    );
    
    // There are also some helper methods for checking for integer overflow/underflow.
    assert_eq!(
        Currencies { keys: 2, metal: 0 }.checked_add(&Currencies { keys: i64::MAX, metal: 0 }),
        None,
    );
    assert_eq!(Currencies { keys: 2, metal: 0 }.checked_mul(i64::MAX), None);
    
    // For currencies which require floating point key values, use ListingCurrencies.
    let currencies = ListingCurrencies {
        keys: 1.5,
        metal: 0,
    };
    
    // Arithmetic with standard Currencies objects still works.
    assert_eq!(
        ListingCurrencies { keys: 1.5, metal: 0 } - Currencies { keys: 1, metal: 0 },
        ListingCurrencies { keys: 0.5, metal: 0 },
    );
    // Due to the lossy nature of converting floats to integers, arithmatic in an inverse
    // manner (Currencies - ListingCurrencies) is not supported.
    
    // Conversions to Currencies are supported.
    assert!(
        Currencies::try_from(ListingCurrencies { keys: 1.0, metal: 0 }).is_ok()
    );
    // Fails if the key value holds a fractional number.
    assert!(
        Currencies::try_from(ListingCurrencies { keys: 1.5, metal: 0 }).is_err()
    );
}

License

MIT

Dependencies

~0.8–1.3MB
~31K SLoC