From f448646ca109d67905374c762c825ffc3955e6a1 Mon Sep 17 00:00:00 2001 From: Rossen Georgiev Date: Mon, 15 Feb 2021 00:25:41 +0000 Subject: [PATCH] SteamID: invite_code_to_tuple() to SteamID.from_invite_code() --- steam/steamid.py | 8 +++++--- tests/test_steamid.py | 46 +++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/steam/steamid.py b/steam/steamid.py index 5186ad0..319d877 100644 --- a/steam/steamid.py +++ b/steam/steamid.py @@ -458,7 +458,7 @@ def steam3_to_tuple(value): return (steam32, etype, universe, instance) -def invite_code_to_tuple(code, universe=EUniverse.Public): +def from_invite_code(code, universe=EUniverse.Public): """ Invites urls can be generated at https://steamcommunity.com/my/friends/add @@ -486,7 +486,9 @@ def invite_code_to_tuple(code, universe=EUniverse.Public): accountid = int(re.sub("["+_icode_custom+"]", repl_mapper, code), 16) if 0 < accountid < 2**32: - return (accountid, EType(1), EUniverse(universe), 1) + return SteamID(accountid, EType.Individual, EUniverse(universe), 1) + +SteamID.from_invite_code = staticmethod(from_invite_code) def from_csgo_friend_code(code, universe=EUniverse.Public): """ @@ -520,7 +522,7 @@ def from_csgo_friend_code(code, universe=EUniverse.Public): result = result >> 4 accountid = (accountid << 4) | id_nib - return SteamID(accountid, EType.Individual, EUniverse(universe)) + return SteamID(accountid, EType.Individual, EUniverse(universe), 1) SteamID.from_csgo_friend_code = staticmethod(from_csgo_friend_code) diff --git a/tests/test_steamid.py b/tests/test_steamid.py index 94b59e6..269a2cf 100644 --- a/tests/test_steamid.py +++ b/tests/test_steamid.py @@ -289,11 +289,13 @@ class SteamID_properties(unittest.TestCase): ) def test_as_invite_code(self): - self.assertEqual(SteamID(0 , EType.Individual, EUniverse.Public, instance=1).as_invite_code, None) - self.assertEqual(SteamID(123456, EType.Individual, EUniverse.Public, instance=1).as_invite_code, 'cv-dgb') - self.assertEqual(SteamID(123456, EType.Individual, EUniverse.Beta , instance=1).as_invite_code, 'cv-dgb') - self.assertEqual(SteamID(123456, EType.Invalid , EUniverse.Public, instance=1).as_invite_code, None) - self.assertEqual(SteamID(123456, EType.Clan , EUniverse.Public, instance=1).as_invite_code, None) + self.assertEqual(SteamID(0 , EType.Individual, EUniverse.Public, instance=1).as_invite_code, None) + self.assertEqual(SteamID(1 , EType.Invalid , EUniverse.Public, instance=1).as_invite_code, None) + self.assertEqual(SteamID(1 , EType.Clan , EUniverse.Public, instance=1).as_invite_code, None) + self.assertEqual(SteamID(1 , EType.Individual, EUniverse.Beta , instance=1).as_invite_code, 'c') + self.assertEqual(SteamID(1 , EType.Individual, EUniverse.Public, instance=1).as_invite_code, 'c') + self.assertEqual(SteamID(123456 , EType.Individual, EUniverse.Public, instance=1).as_invite_code, 'cv-dgb') + self.assertEqual(SteamID(4294967295, EType.Individual, EUniverse.Public, instance=1).as_invite_code, 'wwww-wwww') def test_as_csgo_friend_code(self): self.assertEqual(SteamID(0 , EType.Individual, EUniverse.Public, instance=1).as_csgo_friend_code, None) @@ -444,20 +446,26 @@ class steamid_functions(unittest.TestCase): (1234, EType.Chat, EUniverse.Public, EInstanceFlag.Clan) ) - def test_arg_invite_code(self): - self.assertIsNone(steamid.invite_code_to_tuple('invalid_format')) - self.assertIsNone(steamid.invite_code_to_tuple('https://steamcommunity.com/p/cv-dgb')) - self.assertIsNone(steamid.invite_code_to_tuple('b')) - self.assertIsNone(steamid.invite_code_to_tuple('aaaaaaaaaaaaaaaaaaaaaaaaa')) - - self.assertEqual(steamid.invite_code_to_tuple('cv-dgb'), - (123456, EType.Individual, EUniverse.Public, 1)) - self.assertEqual(steamid.invite_code_to_tuple('http://s.team/p/cv-dgb'), - (123456, EType.Individual, EUniverse.Public, 1)) - self.assertEqual(steamid.invite_code_to_tuple('https://s.team/p/cv-dgb'), - (123456, EType.Individual, EUniverse.Public, 1)) - self.assertEqual(steamid.invite_code_to_tuple('https://s.team/p/cv-dgb/ABCDE12354'), - (123456, EType.Individual, EUniverse.Public, 1)) + def test_from_invite_code(self): + self.assertIsNone(steamid.from_invite_code('invalid_format')) + self.assertIsNone(steamid.from_invite_code('https://steamcommunity.com/p/cv-dgb')) + self.assertIsNone(steamid.from_invite_code('b')) + self.assertIsNone(steamid.from_invite_code('aaaaaaaaaaaaaaaaaaaaaaaaa')) + + self.assertEqual(steamid.from_invite_code('c', EUniverse.Beta), + SteamID(1, EType.Individual, EUniverse.Beta, 1)) + self.assertEqual(steamid.from_invite_code('c'), + SteamID(1, EType.Individual, EUniverse.Public, 1)) + self.assertEqual(steamid.from_invite_code('http://s.team/p/c', EUniverse.Beta), + SteamID(1, EType.Individual, EUniverse.Beta, 1)) + self.assertEqual(steamid.from_invite_code('http://s.team/p/c'), + SteamID(1, EType.Individual, EUniverse.Public, 1)) + self.assertEqual(steamid.from_invite_code('https://s.team/p/cv-dgb'), + SteamID(123456, EType.Individual, EUniverse.Public, 1)) + self.assertEqual(steamid.from_invite_code('https://s.team/p/cv-dgb/ABCDE12354'), + SteamID(123456, EType.Individual, EUniverse.Public, 1)) + self.assertEqual(steamid.from_invite_code('http://s.team/p/wwww-wwww'), + SteamID(4294967295, EType.Individual, EUniverse.Public, 1)) def test_from_csgo_friend_code(self): self.assertIsNone(steamid.from_csgo_friend_code(''))