From f7c0f0b41fe80de9c42d3a8418f596dddf0ac8e7 Mon Sep 17 00:00:00 2001 From: offish Date: Mon, 26 Feb 2024 08:15:02 +0100 Subject: [PATCH] fix steam ids and update tests --- src/tf2_utils/__init__.py | 7 ++++++- src/tf2_utils/sku.py | 37 ++++++++++++++++++++++++++++++++++++- src/tf2_utils/utils.py | 11 ++++++++--- tests/test_item.py | 30 +++++++++++++++--------------- tests/test_offer.py | 16 ++++++++-------- tests/test_sku.py | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 28 deletions(-) diff --git a/src/tf2_utils/__init__.py b/src/tf2_utils/__init__.py index 83f4c3b..6b569e5 100644 --- a/src/tf2_utils/__init__.py +++ b/src/tf2_utils/__init__.py @@ -1,6 +1,6 @@ __title__ = "tf2-utils" __author__ = "offish" -__version__ = "2.1.0" +__version__ = "2.1.1" __license__ = "MIT" from .sku import ( @@ -8,7 +8,12 @@ get_sku_properties, sku_to_defindex, sku_to_quality, + sku_to_color, sku_is_uncraftable, + is_sku, + is_pure, + is_metal, + get_metal, ) from .item import Item from .offer import Offer diff --git a/src/tf2_utils/sku.py b/src/tf2_utils/sku.py index 3d1141c..c670047 100644 --- a/src/tf2_utils/sku.py +++ b/src/tf2_utils/sku.py @@ -1,6 +1,6 @@ from .item import Item -from tf2_data import EFFECTS +from tf2_data import EFFECTS, COLORS from tf2_sku import to_sku @@ -46,7 +46,38 @@ def get_sku_properties(item: Item | dict) -> dict: return sku_properties +def is_sku(item: str) -> bool: + return item.find(";") != -1 + + +def is_pure(sku: str) -> bool: + return sku in ["5000;6", "5001;6", "5002;6", "5021;6"] + + +def is_metal(sku: str) -> bool: + return sku in ["5000;6", "5001;6", "5002;6"] + + +def get_metal(sku: str) -> int: + assert is_metal(sku), f"sku {sku} is not metal" + + match sku: + # refined + case "5002;6": + return 9 + + # reclaimed + case "5001;6": + return 3 + + # scrap + case "5000;6": + return 1 + + def get_property(sku: str, index: int) -> str: + assert is_sku(sku), f"sku {sku} is not valid" + return sku.split(";")[index] @@ -58,6 +89,10 @@ def sku_to_quality(sku: str) -> int: return int(get_property(sku, 1)) +def sku_to_color(sku: str) -> str: + return COLORS[str(sku_to_quality(sku))] + + def sku_is_uncraftable(sku: str) -> bool: return sku.find(";uncraftable") != -1 diff --git a/src/tf2_utils/utils.py b/src/tf2_utils/utils.py index 8928984..19bb39d 100644 --- a/src/tf2_utils/utils.py +++ b/src/tf2_utils/utils.py @@ -1,3 +1,4 @@ +import struct import math import json @@ -43,9 +44,13 @@ def get_token_from_trade_url(trade_url: str) -> str: return trade_url[token_index:] -def account_id_to_steam_id(account_id: int | str) -> str: - return str(76561197960265728 + int(account_id)) +# implementation from bukson's steampy +def account_id_to_steam_id(account_id: str | int) -> str: + first_bytes = int(account_id).to_bytes(4, byteorder="big") + last_bytes = 0x1100001.to_bytes(4, byteorder="big") + return str(struct.unpack(">Q", last_bytes + first_bytes)[0]) +# implementation from bukson's steampy def steam_id_to_account_id(steam_id: str | int) -> str: - return str(int(steam_id) - 76561197960265728) + return str(struct.unpack(">L", int(steam_id).to_bytes(8, byteorder="big")[4:])[0]) diff --git a/tests/test_item.py b/tests/test_item.py index 56a017f..8c110e0 100644 --- a/tests/test_item.py +++ b/tests/test_item.py @@ -24,18 +24,18 @@ def test_craft_hat(self): is_special = ELLIS_CAP.is_special() is_painted = ELLIS_CAP.is_painted() - self.assertEqual(True, is_craft_hat) - self.assertEqual(False, is_special) - self.assertEqual(False, is_painted) + self.assertTrue(is_craft_hat) + self.assertFalse(is_special) + self.assertFalse(is_painted) def test_uncraftable_hat(self): is_craftable = UNCRAFTABLE_HAT.is_craftable() is_craft_hat = UNCRAFTABLE_HAT.is_craft_hat() is_uncraftable = UNCRAFTABLE_HAT.is_uncraftable() - self.assertEqual(False, is_craftable) - self.assertEqual(False, is_craft_hat) - self.assertEqual(True, is_uncraftable) + self.assertFalse(is_craftable) + self.assertFalse(is_craft_hat) + self.assertTrue(is_uncraftable) def test_painted(self): is_special = PAINTED_HAT.is_special() @@ -43,10 +43,10 @@ def test_painted(self): paint = PAINTED_HAT.get_paint() is_craft_hat = PAINTED_HAT.is_craft_hat() - self.assertEqual(True, is_special) - self.assertEqual(True, is_painted) + self.assertTrue(is_special) + self.assertTrue(is_painted) self.assertEqual("Australium Gold", paint) - self.assertEqual(True, is_craft_hat) + self.assertTrue(is_craft_hat) def test_spell(self): is_special = SPELLED_ITEM.is_special() @@ -54,9 +54,9 @@ def test_spell(self): is_strange = SPELLED_ITEM.is_strange() # spell=SPELLED_ITEM.get_spell() - self.assertEqual(True, is_special) - self.assertEqual(True, has_spell) - self.assertEqual(True, is_strange) + self.assertTrue(is_special) + self.assertTrue(has_spell) + self.assertTrue(is_strange) # self.assertEqual("Exorcism", spell) def test_unusual(self): @@ -66,11 +66,11 @@ def test_unusual(self): is_craft_hat = HONG_KONG_CONE.is_craft_hat() strange_in_name = HONG_KONG_CONE.has_strange_in_name() - self.assertEqual(True, is_unusual) + self.assertTrue(is_unusual) self.assertEqual("Neutron Star", effect) self.assertEqual("An Extraordinary Abundance of Tinge", paint) - self.assertEqual(False, is_craft_hat) - self.assertEqual(True, strange_in_name) + self.assertFalse(is_craft_hat) + self.assertTrue(strange_in_name) def test_sku_from_item_class(self): sku = get_sku(ELLIS_CAP) diff --git a/tests/test_offer.py b/tests/test_offer.py index b614f77..ccf564c 100644 --- a/tests/test_offer.py +++ b/tests/test_offer.py @@ -10,16 +10,16 @@ class TestUtils(TestCase): def test_offer_state(self): - self.assertEqual(False, offer.is_active()) - self.assertEqual(False, offer.is_declined()) - self.assertEqual(True, offer.is_accepted()) + self.assertFalse(offer.is_active()) + self.assertFalse(offer.is_declined()) + self.assertTrue(offer.is_accepted()) def test_offer_sides(self): - self.assertEqual(False, offer.is_our_offer()) - self.assertEqual(False, offer.is_scam()) - self.assertEqual(False, offer.is_gift()) - self.assertEqual(True, offer.is_two_sided()) + self.assertFalse(offer.is_our_offer()) + self.assertFalse(offer.is_scam()) + self.assertFalse(offer.is_gift()) + self.assertTrue(offer.is_two_sided()) def test_offer_partner(self): - self.assertEqual(False, offer.has_trade_hold()) + self.assertFalse(offer.has_trade_hold()) self.assertEqual("76561198253325712", offer.get_partner()) diff --git a/tests/test_sku.py b/tests/test_sku.py index 374616d..5c44af3 100644 --- a/tests/test_sku.py +++ b/tests/test_sku.py @@ -5,6 +5,11 @@ sku_to_defindex, sku_to_quality, sku_is_uncraftable, + is_sku, + sku_to_color, + is_pure, + is_metal, + get_metal, ) from unittest import TestCase @@ -70,3 +75,32 @@ def test_properties(self): self.assertEqual(734, sku_to_defindex(sku)) self.assertEqual(6, sku_to_quality(sku)) self.assertEqual(True, sku_is_uncraftable(sku)) + + def test_is_sku(self): + self.assertTrue(is_sku("734;6;uncraftable")) + self.assertTrue(is_sku("something;text")) + self.assertFalse(is_sku("734")) + + def test_is_metal(self): + self.assertTrue(is_metal("5000;6")) + self.assertTrue(is_metal("5001;6")) + self.assertTrue(is_metal("5002;6")) + self.assertFalse(is_metal("5021;6")) + + def test_is_pure(self): + self.assertTrue(is_pure("5000;6")) + self.assertTrue(is_pure("5001;6")) + self.assertTrue(is_pure("5002;6")) + self.assertTrue(is_pure("5021;6")) + self.assertFalse(is_pure("5021;7")) + + def test_get_metal(self): + self.assertEqual(9, get_metal("5002;6")) + self.assertEqual(3, get_metal("5001;6")) + self.assertEqual(1, get_metal("5000;6")) + self.assertRaises(AssertionError, get_metal, "5021;6") + + def test_sku_to_color(self): + self.assertEqual("7D6D00", sku_to_color("734;6;uncraftable")) + self.assertEqual("4D7455", sku_to_color("30469;1")) + self.assertRaises(AssertionError, sku_to_color, "notsku")