Skip to content

Commit

Permalink
fix steam ids and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
offish committed Feb 26, 2024
1 parent 865d800 commit f7c0f0b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 28 deletions.
7 changes: 6 additions & 1 deletion src/tf2_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
__title__ = "tf2-utils"
__author__ = "offish"
__version__ = "2.1.0"
__version__ = "2.1.1"
__license__ = "MIT"

from .sku import (
get_sku,
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
Expand Down
37 changes: 36 additions & 1 deletion src/tf2_utils/sku.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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]


Expand All @@ -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

Expand Down
11 changes: 8 additions & 3 deletions src/tf2_utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import struct
import math
import json

Expand Down Expand Up @@ -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])
30 changes: 15 additions & 15 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,39 @@ 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()
is_painted = PAINTED_HAT.is_painted()
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()
has_spell = SPELLED_ITEM.has_spell()
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):
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions tests/test_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
34 changes: 34 additions & 0 deletions tests/test_sku.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

0 comments on commit f7c0f0b

Please sign in to comment.