-
Notifications
You must be signed in to change notification settings - Fork 46
/
action.py
333 lines (308 loc) · 14.1 KB
/
action.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import json
import time
import random
from PIL import Image, ImageGrab
from convert import MS_TILE_2_MJAI_TILE
from functools import cmp_to_key
from majsoul2mjai import compare_pai
from loguru import logger
from xmlrpc.client import ServerProxy
YAOJIU = ('1p','1m','1s','9p','9m','9s','E','S','W','N','P','F','C')
TWOEIGHT = ('2p','2m','2s','8p','8m','8s')
TFFSS = ('3p','3m','3s','4p','4m','4s','5p','5m','5s','6p','6m','6s','7p','7m','7s')
REDFIVE = ('5pr','5mr','5sr')
# Coordinates here is on the resolution of 16x9
LOCATION = {
"tiles": [
(2.23125 , 8.3625),
(3.021875 , 8.3625),
(3.8125 , 8.3625),
(4.603125 , 8.3625),
(5.39375 , 8.3625),
(6.184375 , 8.3625),
(6.975 , 8.3625),
(7.765625 , 8.3625),
(8.55625 , 8.3625),
(9.346875 , 8.3625),
(10.1375 , 8.3625),
(10.928125, 8.3625),
(11.71875 , 8.3625),
(12.509375, 8.3625),
],
"tsumo_space": 0.246875,
"actions": [
(10.875, 7), #none #
(8.6375, 7), # 5 4 3
(6.4 , 7), #
(10.875, 5.9), # 2 1 0
(8.6375, 5.9), #
(6.4 , 5.9),
(10.875, 4.8), # Not used
(8.6375, 4.8), # Not used
(6.4 , 4.8), # Not used
],
"candidates": [
(3.6625, 6.3), # (-(len/2)+idx+0.5)*2+5
(4.49625, 6.3),
(5.33 , 6.3),
(6.16375, 6.3),
(6.9975, 6.3),
(7.83125, 6.3), # 5 mid
(8.665, 6.3),
(9.49875, 6.3),
(10.3325, 6.3),
(11.16625,6.3),
(12, 6.3),
],
"candidates_kan": [
(4.325, 6.3), #
(5.4915, 6.3),
(6.6583, 6.3),
(7.825, 6.3), # 3 mid
(8.9917, 6.3),
(10.1583, 6.3),
(11.325, 6.3),
],
}
# Refer to majsoul2mjai.Operation
ACTION_PIORITY = [
0, # none #
99, # Discard # There is no discard button
4, # Chi # Opponent Discard
3, # Pon # Opponent Discard
3, # Ankan # Self Discard # If Ankan and Kakan are both available, use only kakan.
2, # Daiminkan # Opponent Discard
3, # Kakan # Self Discard
2, # Reach # Self Discard
1, # Zimo # Self Discard
1, # Rong # Opponent Discard
5, # Ryukyoku # Self Discard
4, # Nukidora # Self Discard
]
ACTION2TYPE = {
"none": 0,
"chi": 2,
"pon": 3,
"daiminkan": 5,
"hora": 9,
#^^^^^^^^^^^^^^^^Opponent Discard^^^^^^^^^^^^^^^^
"ryukyoku": 10,
"nukidora": 11,
"ankan": 4,
"kakan": 6,
"reach": 7,
"zimo": 8,
#^^^^^^^^^^^^^^^^Self Discard^^^^^^^^^^^^^^^^
}
class Action:
def __init__(self, rpc_server: ServerProxy):
self.isNewRound = True
self.reached = False
self.latest_operation_list = []
self.rpc_server = rpc_server
with open("settings.json", "r") as f:
settings = json.load(f)
self.new_min = settings['RandomTime']['new_min']
self.new_max = settings['RandomTime']['new_max']
self.min = settings['RandomTime']['min']
self.max = settings['RandomTime']['max']
self.moqiedelay = settings['RandomTime']['moqiedelay']
pass
def page_clicker(self, coord: tuple[float, float]):
self.rpc_server.page_clicker(coord)
def do_autohu(self):
self.rpc_server.do_autohu()
def decide_random_time(self):
if self.isNewRound:
return random.uniform(self.new_min, self.new_max)
return random.uniform(self.min, self.max)
def click_chiponkan(self, mjai_msg: dict | None, tehai: list[str], tsumohai: str | None):
latest_operation_list_temp = self.latest_operation_list.copy()
latest_operation_list_temp.append({'type': 0, 'combination': []})
can_ankan = False
can_kakan = False
ankan_combination = None
# if both Ankan (type 4) and Kakan (type 6) are available
for operation in self.latest_operation_list:
if operation['type'] == 4:
can_ankan = True
ankan_combination = operation['combination']
if operation['type'] == 6:
can_kakan = True
if can_ankan and can_kakan:
for idx, operation in enumerate(self.latest_operation_list):
if operation['type'] == 6:
latest_operation_list_temp[idx]['combination'] += ankan_combination
if operation['type'] == 4:
latest_operation_list_temp.remove(operation)
# Sort latest_operation_list by ACTION_PIORITY
# logger.debug(f"latest_operation_list_temp: {latest_operation_list_temp}")
latest_operation_list_temp.sort(key=lambda x: ACTION_PIORITY[x['type']])
if tsumohai != '?' and mjai_msg['type'] == 'hora':
mjai_msg['type'] = 'zimo'
for idx, operation in enumerate(latest_operation_list_temp):
if operation['type'] == ACTION2TYPE[mjai_msg['type']]:
self.page_clicker(LOCATION['actions'][idx])
self.do_autohu()
self.isNewRound = False
break
if mjai_msg['type'] == 'reach':
self.reached = True
time.sleep(0.5)
self.click_dahai(mjai_msg, tehai, tsumohai)
return
if mjai_msg['type'] in ['chi', 'pon', 'ankan', 'kakan']:
consumed_pais_mjai = mjai_msg['consumed']
consumed_pais_mjai = sorted(consumed_pais_mjai, key=cmp_to_key(compare_pai))
if mjai_msg['type'] == 'chi':
for operation in self.latest_operation_list:
if operation['type'] == 2:
combination_len = len(operation['combination'])
if combination_len == 1:
return # No need to click
for idx, combination in enumerate(operation['combination']):
consumed_pais_liqi = [MS_TILE_2_MJAI_TILE[pai] for pai in combination.split('|')]
consumed_pais_liqi = sorted(consumed_pais_liqi, key=cmp_to_key(compare_pai))
if consumed_pais_mjai == consumed_pais_liqi:
time.sleep(0.3)
candidate_idx = int((-(combination_len/2)+idx+0.5)*2+5)
self.page_clicker(LOCATION['candidates'][candidate_idx])
return
elif mjai_msg['type'] == 'pon':
for operation in self.latest_operation_list:
if operation['type'] == 3:
combination_len = len(operation['combination'])
if combination_len == 1:
return
for idx, combination in enumerate(operation['combination']):
consumed_pais_liqi = [MS_TILE_2_MJAI_TILE[pai] for pai in combination.split('|')]
consumed_pais_liqi = sorted(consumed_pais_liqi, key=cmp_to_key(compare_pai))
if consumed_pais_mjai == consumed_pais_liqi:
time.sleep(0.3)
candidate_idx = int((-(combination_len/2)+idx+0.5)*2+5)
self.page_clicker(LOCATION['candidates'][candidate_idx])
return
# If both Ankan (type 4) and Kakan (type 6) are available, only one kan button will be shown, and candidates = [kakan, ankan]
elif mjai_msg['type'] in ['ankan', 'kakan']:
if can_ankan and can_kakan:
for operation in latest_operation_list_temp:
if operation['type'] == 6:
combination_len = len(operation['combination'])
if combination_len == 1:
# impossible
return
for idx, combination in enumerate(operation['combination']):
consumed_pais_liqi = [MS_TILE_2_MJAI_TILE[pai] for pai in combination.split('|')]
consumed_pais_liqi = sorted(consumed_pais_liqi, key=cmp_to_key(compare_pai))
if consumed_pais_mjai == consumed_pais_liqi:
time.sleep(0.3)
candidate_idx = int((-(combination_len/2)+idx+0.5)*2+3)
self.page_clicker(LOCATION['candidates_kan'][candidate_idx])
return
elif mjai_msg['type'] == 'ankan':
for operation in self.latest_operation_list:
if operation['type'] == 4:
combination_len = len(operation['combination'])
if combination_len == 1:
return
for idx, combination in enumerate(operation['combination']):
consumed_pais_liqi = [MS_TILE_2_MJAI_TILE[pai] for pai in combination.split('|')]
consumed_pais_liqi = sorted(consumed_pais_liqi, key=cmp_to_key(compare_pai))
if consumed_pais_mjai == consumed_pais_liqi:
time.sleep(0.3)
candidate_idx = int((-(combination_len/2)+idx+0.5)*2+3)
self.page_clicker(LOCATION['candidates_kan'][candidate_idx])
return
elif mjai_msg['type'] == 'kakan':
for operation in self.latest_operation_list:
if operation['type'] == 6:
combination_len = len(operation['combination'])
if combination_len == 1:
return
for idx, combination in enumerate(operation['combination']):
consumed_pais_liqi = [MS_TILE_2_MJAI_TILE[pai] for pai in combination.split('|')]
consumed_pais_liqi = sorted(consumed_pais_liqi, key=cmp_to_key(compare_pai))
if consumed_pais_mjai == consumed_pais_liqi:
time.sleep(0.3)
candidate_idx = int((-(combination_len/2)+idx+0.5)*2+3)
self.page_clicker(LOCATION['candidates_kan'][candidate_idx])
return
def get_pai_coord(self, idx: int, tehais: list[str]):
tehai_count = 0
for tehai in tehais:
if tehai != '?':
tehai_count += 1
if tehai_count >= 14:
tehai_count = 13
if idx == 13:
pai_cord = (LOCATION['tiles'][tehai_count][0] + LOCATION['tsumo_space'], LOCATION['tiles'][tehai_count][1])
else:
pai_cord = LOCATION['tiles'][idx]
return pai_cord
def click_dahai(self, mjai_msg: dict | None, tehai: list[str], tsumohai: str | None):
dahai = mjai_msg['pai']
if self.isNewRound:
# In Majsoul, if you are the first dealer, there is no tsumohai, but 14 tehai.
# However, in MJAI, there is 13 tehai and 1 tsumohai.
temp_tehai = tehai.copy()
temp_tehai.append(tsumohai)
temp_tehai = sorted(temp_tehai, key=cmp_to_key(compare_pai))
for i in range(14):
if dahai == temp_tehai[i]:
pai_coord = self.get_pai_coord(i, temp_tehai)
self.page_clicker(pai_coord)
self.do_autohu()
self.isNewRound = False
return
if tsumohai != '?':
if dahai == tsumohai:
pai_coord = self.get_pai_coord(13, tehai)
self.page_clicker(pai_coord)
return
for i in range(13):
if dahai == tehai[i]:
pai_coord = self.get_pai_coord(i, tehai)
self.page_clicker(pai_coord)
break
def mjai2action(self, mjai_msg: dict | None, tehai: list[str], tsumohai: str | None, isliqi: bool | None, NoOver: bool | None):
dahai_delay = self.decide_random_time()
if mjai_msg is None:
return
if mjai_msg['type'] == 'dahai' and not self.reached:
if NoOver:
self.click_dahai(mjai_msg, tehai, tsumohai)
return
if self.moqiedelay:
if isliqi:
# if someone reached
dahai_delay = dahai_delay # Change value here
elif self.isNewRound:
dahai_delay = random.uniform(self.new_min, self.new_max)
elif not mjai_msg['tsumogiri']:
if mjai_msg['pai'] in YAOJIU:
dahai_delay = dahai_delay
elif mjai_msg['pai'] in TWOEIGHT:
dahai_delay = dahai_delay
elif mjai_msg['pai'] in TFFSS:
dahai_delay = dahai_delay
elif mjai_msg['pai'] in REDFIVE:
dahai_delay = dahai_delay
else:
# tsumogiri
dahai_delay = dahai_delay
else:
dahai_delay = dahai_delay
if "time" in mjai_msg:
dahai_delay -= mjai_msg["time"]
if dahai_delay < 0:
dahai_delay = 0
time.sleep(dahai_delay)
self.click_dahai(mjai_msg, tehai, tsumohai)
return
if mjai_msg['type'] in ['none', 'chi', 'pon', 'daiminkan', 'ankan', 'kakan', 'hora', 'reach', 'ryukyoku', 'nukidora']:
if NoOver:
self.click_chiponkan(mjai_msg, tehai, tsumohai)
return
time.sleep(2)
self.click_chiponkan(mjai_msg, tehai, tsumohai)
# kan can have multiple candidates too! ex: tehai=1111m 1111p 111s 11z, tsumohai=1s