forked from MahjongRepository/mahjong_soul_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcn_example.py
179 lines (131 loc) · 5.7 KB
/
cn_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import asyncio
import hashlib
import hmac
import logging
import random
import uuid
from optparse import OptionParser
import aiohttp
from ms.base import MSRPCChannel
from ms.rpc import Lobby
import ms.protocol_pb2 as pb
from google.protobuf.json_format import MessageToJson
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
MS_HOST = "https://game.maj-soul.com"
async def main():
"""
Login to the CN server with username and password and get latest 30 game logs.
"""
parser = OptionParser()
parser.add_option("-u", "--username", type="string", help="Your account name.")
parser.add_option("-p", "--password", type="string", help="Your account password.")
parser.add_option("-l", "--log", type="string", help="Your log UUID for load.")
opts, _ = parser.parse_args()
username = opts.username
password = opts.password
log_uuid = opts.log
if not username or not password:
parser.error("Username or password cant be empty")
lobby, channel, version_to_force = await connect()
await login(lobby, username, password, version_to_force)
if not log_uuid:
game_logs = await load_game_logs(lobby)
logging.info("Found {} records".format(len(game_logs)))
else:
game_log = await load_and_process_game_log(lobby, log_uuid, version_to_force)
logging.info("game {} result : \n{}".format(game_log.head.uuid, game_log.head.result))
await channel.close()
async def connect():
async with aiohttp.ClientSession() as session:
async with session.get("{}/1/version.json".format(MS_HOST)) as res:
version = await res.json()
logging.info(f"Version: {version}")
version = version["version"]
version_to_force = version.replace(".w", "")
async with session.get("{}/1/v{}/config.json".format(MS_HOST, version)) as res:
config = await res.json()
logging.info(f"Config: {config}")
url = config["ip"][0]["region_urls"][1]["url"]
async with session.get(url + "?service=ws-gateway&protocol=ws&ssl=true") as res:
servers = await res.json()
logging.info(f"Available servers: {servers}")
servers = servers["servers"]
server = random.choice(servers)
endpoint = "wss://{}/gateway".format(server)
logging.info(f"Chosen endpoint: {endpoint}")
channel = MSRPCChannel(endpoint)
lobby = Lobby(channel)
await channel.connect(MS_HOST)
logging.info("Connection was established")
return lobby, channel, version_to_force
async def login(lobby, username, password, version_to_force):
logging.info("Login with username and password")
uuid_key = str(uuid.uuid1())
req = pb.ReqLogin()
req.account = username
req.password = hmac.new(b"lailai", password.encode(), hashlib.sha256).hexdigest()
req.device.is_browser = True
req.random_key = uuid_key
req.gen_access_token = True
req.client_version_string = f"web-{version_to_force}"
req.currency_platforms.append(2)
res = await lobby.login(req)
token = res.access_token
if not token:
logging.error("Login Error:")
logging.error(res)
return False
return True
async def load_game_logs(lobby):
logging.info("Loading game logs")
records = []
current = 1
step = 30
req = pb.ReqGameRecordList()
req.start = current
req.count = step
res = await lobby.fetch_game_record_list(req)
records.extend([r.uuid for r in res.record_list])
return records
async def load_and_process_game_log(lobby, uuid, version_to_force):
logging.info("Loading game log")
req = pb.ReqGameRecord()
req.game_uuid = uuid
req.client_version_string = f"web-{version_to_force}"
res = await lobby.fetch_game_record(req)
record_wrapper = pb.Wrapper()
record_wrapper.ParseFromString(res.data)
game_details = pb.GameDetailRecords()
game_details.ParseFromString(record_wrapper.data)
game_records_count = len(game_details.records)
logging.info("Found {} game records".format(game_records_count))
round_record_wrapper = pb.Wrapper()
is_show_new_round_record = False
is_show_discard_tile = False
is_show_deal_tile = False
for i in range(0, game_records_count):
round_record_wrapper.ParseFromString(game_details.records[i])
if round_record_wrapper.name == ".lq.RecordNewRound" and not is_show_new_round_record:
logging.info("Found record type = {}".format(round_record_wrapper.name))
round_data = pb.RecordNewRound()
round_data.ParseFromString(round_record_wrapper.data)
print_data_as_json(round_data, "RecordNewRound")
is_show_new_round_record = True
if round_record_wrapper.name == ".lq.RecordDiscardTile" and not is_show_discard_tile:
logging.info("Found record type = {}".format(round_record_wrapper.name))
discard_tile = pb.RecordDiscardTile()
discard_tile.ParseFromString(round_record_wrapper.data)
print_data_as_json(discard_tile, "RecordDiscardTile")
is_show_discard_tile = True
if round_record_wrapper.name == ".lq.RecordDealTile" and not is_show_deal_tile:
logging.info("Found record type = {}".format(round_record_wrapper.name))
deal_tile = pb.RecordDealTile()
deal_tile.ParseFromString(round_record_wrapper.data)
print_data_as_json(deal_tile, "RecordDealTile")
is_show_deal_tile = True
return res
def print_data_as_json(data, type):
json = MessageToJson(data)
logging.info("{} json {}".format(type, json))
if __name__ == "__main__":
asyncio.run(main())