Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.

Commit df83948

Browse files
committed
Fix auth flow to reflect latest changes in tenhou protocol
1 parent 882fe44 commit df83948

File tree

4 files changed

+6
-79
lines changed

4 files changed

+6
-79
lines changed

project/game/ai/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
class MahjongAI:
28-
version = "0.5.0"
28+
version = "0.5.1"
2929

3030
agari = None
3131
shanten_calculator = None

project/tenhou/client.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,12 @@ def authenticate(self):
6262

6363
return True
6464

65-
auth_string, rating_string, new_rank_message = self.decoder.parse_hello_string(auth_message)
65+
rating_string, new_rank_message = self.decoder.parse_hello_string(auth_message)
6666
self._rating_string = rating_string
67-
if not auth_string:
68-
self.logger.info("We didn't obtain auth string")
69-
return False
7067

7168
if new_rank_message:
7269
self.logger.info("Achieved a new rank! \n {}".format(new_rank_message))
7370

74-
auth_token = self.decoder.generate_auth_token(auth_string)
75-
76-
self._send_message('<AUTH val="{}"/>'.format(auth_token))
7771
self._send_message(self._pxr_tag())
7872

7973
# sometimes tenhou send an empty tag after authentication (in tournament mode)

project/tenhou/decoder.py

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,15 @@ class TenhouDecoder:
3131

3232
def parse_hello_string(self, message):
3333
rating_string = ""
34-
auth_message = ""
3534
new_rank_message = ""
3635

37-
if "auth=" in message:
38-
auth_message = self.get_attribute_content(message, "auth")
39-
# for NoName we don't have rating attribute
40-
if "PF4=" in message:
41-
rating_string = self.get_attribute_content(message, "PF4")
36+
if "PF4=" in message:
37+
rating_string = self.get_attribute_content(message, "PF4")
4238

4339
if "nintei" in message:
4440
new_rank_message = unquote(self.get_attribute_content(message, "nintei"))
4541

46-
return auth_message, rating_string, new_rank_message
42+
return rating_string, new_rank_message
4743

4844
def parse_initial_values(self, message):
4945
"""
@@ -236,56 +232,6 @@ def parse_nuki(self, data, meld):
236232
meld.type = MeldPrint.NUKI
237233
meld.tiles = [data >> 8]
238234

239-
def generate_auth_token(self, auth_string):
240-
translation_table = [
241-
63006,
242-
9570,
243-
49216,
244-
45888,
245-
9822,
246-
23121,
247-
59830,
248-
51114,
249-
54831,
250-
4189,
251-
580,
252-
5203,
253-
42174,
254-
59972,
255-
55457,
256-
59009,
257-
59347,
258-
64456,
259-
8673,
260-
52710,
261-
49975,
262-
2006,
263-
62677,
264-
3463,
265-
17754,
266-
5357,
267-
]
268-
269-
parts = auth_string.split("-")
270-
if len(parts) != 2:
271-
return False
272-
273-
first_part = parts[0]
274-
second_part = parts[1]
275-
if len(first_part) != 8 or len(second_part) != 8:
276-
return False
277-
278-
table_index = int("2" + first_part[2:8]) % (12 - int(first_part[7:8])) * 2
279-
280-
a = translation_table[table_index] ^ int(second_part[0:4], 16)
281-
b = translation_table[table_index + 1] ^ int(second_part[4:8], 16)
282-
283-
postfix = format(a, "2x") + format(b, "2x")
284-
285-
result = first_part + "-" + postfix
286-
287-
return result
288-
289235
def get_attribute_content(self, message, attribute_name):
290236
result = re.findall(r'{}="([^"]*)"'.format(attribute_name), message)
291237
return result and result[0] or None

project/tenhou/test_tenhou_decoder.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,12 @@ def test_parse_log_link():
7676
def test_auth_message():
7777
decoder = TenhouDecoder()
7878
message = """<HELO uname="%4E%6F%4E%61%6D%65"
79-
auth="20160318-54ebe070" ratingscale=""
8079
PF4="9,45,1290.90,-5184.0,69,95,129,157,71,4303,831,830,33,1761"/>"""
8180

82-
auth_string, rating_string, _ = decoder.parse_hello_string(message)
83-
84-
assert auth_string == "20160318-54ebe070"
81+
rating_string, _ = decoder.parse_hello_string(message)
8582
assert rating_string == "9,45,1290.90,-5184.0,69,95,129,157,71,4303,831,830,33,1761"
8683

8784

88-
def test_generate_auth_token():
89-
client = TenhouDecoder()
90-
91-
string = "20160318-54ebe070"
92-
assert client.generate_auth_token(string) == "20160318-72b5ba21"
93-
94-
string = "20160319-5b859bb3"
95-
assert client.generate_auth_token(string) == "20160319-9bc528f3"
96-
97-
9885
def test_decode_new_dora_indicator():
9986
decoder = TenhouDecoder()
10087
message = '<DORA hai="125" />'

0 commit comments

Comments
 (0)