Skip to content

Commit f0c463e

Browse files
author
shinkuan
committed
overlay show top three
1 parent d7150a3 commit f0c463e

File tree

3 files changed

+402
-73
lines changed

3 files changed

+402
-73
lines changed

client.py

+66-34
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,22 @@ def update(self, mjai_msg, state):
6666
for i in range(3):
6767
self.consumes[i].update(TILE_2_UNICODE_ART_RICH["?"])
6868
self.weight.update("0.0")
69+
self.app.rpc_server.draw_top3([self.recommand_idx, "?", "?", "?", "?", 0.0])
6970
return
7071
recommand = mjai_msg['meta'][self.recommand_idx]
7172
for action_class in self.action.classes:
7273
if "action_" in action_class:
7374
self.action.remove_class(action_class)
7475
self.weight.update(f"{(recommand[1]*100):.2f}")
76+
weight_text = f"{(recommand[1]*100):.2f}%"
7577
if recommand[0] in TILE_LIST:
7678
self.action.label = recommand[0]
7779
self.action.add_class("action_"+recommand[0])
7880
self.pai.update(TILE_2_UNICODE_ART_RICH[recommand[0]])
7981
self.vertical_rule.update(EMPTY_VERTICAL_RULE)
8082
for i in range(3):
8183
self.consumes[i].update(TILE_2_UNICODE_ART_RICH["?"])
84+
self.app.rpc_server.draw_top3([self.recommand_idx, recommand[0], "?", "?", "?", weight_text])
8285
elif recommand[0] in ['chi_low', 'chi_mid', 'chi_high']:
8386
self.action.label = "chi"
8487
self.action.add_class("action_chi")
@@ -88,15 +91,18 @@ def update(self, mjai_msg, state):
8891
last_kawa_tile_idx = TILE_LIST.index(last_kawa_tile)
8992
match recommand[0]:
9093
case 'chi_low':
91-
self.consumes[0].update(TILE_2_UNICODE_ART_RICH[TILE_LIST[last_kawa_tile_idx-2]])
92-
self.consumes[1].update(TILE_2_UNICODE_ART_RICH[TILE_LIST[last_kawa_tile_idx-1]])
94+
c0 = TILE_LIST[last_kawa_tile_idx+1]
95+
c1 = TILE_LIST[last_kawa_tile_idx+2]
9396
case 'chi_mid':
94-
self.consumes[0].update(TILE_2_UNICODE_ART_RICH[TILE_LIST[last_kawa_tile_idx-1]])
95-
self.consumes[1].update(TILE_2_UNICODE_ART_RICH[TILE_LIST[last_kawa_tile_idx+1]])
97+
c0 = TILE_LIST[last_kawa_tile_idx-1]
98+
c1 = TILE_LIST[last_kawa_tile_idx+1]
9699
case 'chi_high':
97-
self.consumes[0].update(TILE_2_UNICODE_ART_RICH[TILE_LIST[last_kawa_tile_idx+1]])
98-
self.consumes[1].update(TILE_2_UNICODE_ART_RICH[TILE_LIST[last_kawa_tile_idx+2]])
100+
c0 = TILE_LIST[last_kawa_tile_idx-2]
101+
c1 = TILE_LIST[last_kawa_tile_idx-1]
102+
self.consumes[0].update(TILE_2_UNICODE_ART_RICH[c0])
103+
self.consumes[1].update(TILE_2_UNICODE_ART_RICH[c1])
99104
self.consumes[2].update(TILE_2_UNICODE_ART_RICH["?"])
105+
self.app.rpc_server.draw_top3([self.recommand_idx, "chi", last_kawa_tile, c0, c1, weight_text])
100106
elif recommand[0] in ['pon']:
101107
self.action.label = "pon"
102108
self.action.add_class("action_pon")
@@ -106,6 +112,7 @@ def update(self, mjai_msg, state):
106112
for i in range(2):
107113
self.consumes[i].update(TILE_2_UNICODE_ART_RICH[last_kawa_tile])
108114
self.consumes[2].update(TILE_2_UNICODE_ART_RICH["?"])
115+
self.app.rpc_server.draw_top3([self.recommand_idx, "pon", last_kawa_tile, last_kawa_tile, last_kawa_tile, weight_text])
109116
elif recommand[0] in ['kan_select']:
110117
# The recommandation only shows kan_select, but not ['daiminkan', 'ankan', 'kakan'],
111118
# this is due to the Mortal model structure limitations.
@@ -116,20 +123,23 @@ def update(self, mjai_msg, state):
116123
self.vertical_rule.update(EMPTY_VERTICAL_RULE)
117124
for i in range(3):
118125
self.consumes[i].update(TILE_2_UNICODE_ART_RICH["?"])
126+
self.app.rpc_server.draw_top3([self.recommand_idx, "kan", "?", "?", "?", weight_text])
119127
elif recommand[0] in ['reach', 'hora', 'ryukyoku', 'none']:
120128
self.action.label = recommand[0]
121129
self.action.add_class("action_"+recommand[0])
122130
self.pai.update(TILE_2_UNICODE_ART_RICH["?"])
123131
self.vertical_rule.update(EMPTY_VERTICAL_RULE)
124132
for i in range(3):
125133
self.consumes[i].update(TILE_2_UNICODE_ART_RICH["?"])
134+
self.app.rpc_server.draw_top3([self.recommand_idx, recommand[0], "?", "?", "?", weight_text])
126135
elif recommand[0] in ['nukidora']:
127136
self.action.label = "nukidora"
128137
self.action.add_class("action_nukidora")
129138
self.pai.update(TILE_2_UNICODE_ART_RICH["N"])
130139
self.vertical_rule.update(EMPTY_VERTICAL_RULE)
131140
for i in range(3):
132141
self.consumes[i].update(TILE_2_UNICODE_ART_RICH["?"])
142+
self.app.rpc_server.draw_top3([self.recommand_idx, "nukidora", "N", "?", "?", weight_text])
133143
pass
134144

135145

@@ -161,17 +171,17 @@ def compose(self) -> ComposeResult:
161171
recommandations_container.border_title = "Recommandations"
162172
mjai_log_container.border_title = "Mjai"
163173
tehai_labels = [Label(TILE_2_UNICODE_ART_RICH["?"], id="tehai_"+str(i)) for i in range(13)]
164-
tehai_value_labels = [Label(HAI_VALUE[40], id="tehai_value_"+str(i)) for i in range(13)]
174+
# tehai_value_labels = [Label(HAI_VALUE[40], id="tehai_value_"+str(i)) for i in range(13)]
165175
tehai_rule = Label(VERTICAL_RULE, id="tehai_rule")
166176
tsumohai_label = Label(TILE_2_UNICODE_ART_RICH["?"], id="tsumohai")
167-
tsumohai_value_label = Label(HAI_VALUE[40], id="tsumohai_value")
177+
# tsumohai_value_label = Label(HAI_VALUE[40], id="tsumohai_value")
168178
tehai_container = Horizontal(id="tehai_container")
169179
for i in range(13):
170180
tehai_container.mount(tehai_labels[i])
171-
tehai_container.mount(tehai_value_labels[i])
181+
# tehai_container.mount(tehai_value_labels[i])
172182
tehai_container.mount(tehai_rule)
173183
tehai_container.mount(tsumohai_label)
174-
tehai_container.mount(tsumohai_value_label)
184+
# tehai_container.mount(tsumohai_value_label)
175185
tehai_container.border_title = "Tehai"
176186
akagi_action = Button("Akagi", id="akagi_action", variant="default")
177187
akagi_pai = Button("Pai", id="akagi_pai", variant="default")
@@ -184,9 +194,9 @@ def compose(self) -> ComposeResult:
184194
loading_indicator = LoadingIndicator(id="loading_indicator")
185195
loading_indicator.styles.height = "3"
186196
checkbox_autoplay = Checkbox("Autoplay", id="checkbox_autoplay", classes="short", value=AUTOPLAY)
197+
checkbox_overlay = Checkbox("Overlay ", id="checkbox_overlay", classes="short")
187198
checkbox_test_one = Checkbox("test_one", id="checkbox_test_one", classes="short")
188-
checkbox_test_two = Checkbox("test_two", id="checkbox_test_two", classes="short")
189-
checkbox_container = Vertical(checkbox_autoplay, checkbox_test_one, id="checkbox_container")
199+
checkbox_container = Vertical(checkbox_autoplay, checkbox_overlay, id="checkbox_container")
190200
checkbox_container.border_title = "Options"
191201
bottom_container = Horizontal(checkbox_container, akagi_container, id="bottom_container")
192202
yield Header()
@@ -211,10 +221,10 @@ def on_mount(self) -> None:
211221
self.recommandations_container = self.query_one("#recommandations_container")
212222
self.mjai_log_container = self.query_one("#mjai_log_container")
213223
self.tehai_labels = [self.query_one("#tehai_"+str(i)) for i in range(13)]
214-
self.tehai_value_labels = [self.query_one("#tehai_value_"+str(i)) for i in range(13)]
224+
# self.tehai_value_labels = [self.query_one("#tehai_value_"+str(i)) for i in range(13)]
215225
self.tehai_rule = self.query_one("#tehai_rule")
216226
self.tsumohai_label = self.query_one("#tsumohai")
217-
self.tsumohai_value_label = self.query_one("#tsumohai_value")
227+
# self.tsumohai_value_label = self.query_one("#tsumohai_value")
218228
self.tehai_container = self.query_one("#tehai_container")
219229
# self.liqi_log_container.scroll_end(animate=False)
220230
self.mjai_log_container.scroll_end(animate=False)
@@ -233,8 +243,9 @@ def on_mount(self) -> None:
233243
self.akagi_action.label = "Akagi"
234244

235245
def refresh_log(self) -> None:
236-
# Yes I know this is stupid
237246
try:
247+
if self.flow_id not in self.app.liqi_msg_dict:
248+
self.action_quit()
238249
if self.liqi_msg_idx < len(self.app.liqi_msg_dict[self.flow_id]):
239250
# self.liqi_log.update(self.app.liqi_msg_dict[self.flow_id][-1])
240251
# self.liqi_log_container.scroll_end(animate=False)
@@ -275,25 +286,38 @@ def refresh_log(self) -> None:
275286
tehai, tsumohai = state_to_tehai(player_state)
276287
for idx, tehai_label in enumerate(self.tehai_labels):
277288
tehai_label.update(TILE_2_UNICODE_ART_RICH[tehai[idx]])
278-
action_list = [x[0] for x in latest_mjai_msg['meta']]
279-
for idx, tehai_value_label in enumerate(self.tehai_value_labels):
280-
# latest_mjai_msg['meta'] is list of (pai, value)
281-
try:
282-
pai_value = int(latest_mjai_msg['meta'][action_list.index(tehai[idx])][1] * 40)
283-
if pai_value == 40:
284-
pai_value = 39
285-
except ValueError:
286-
pai_value = 40
287-
tehai_value_label.update(HAI_VALUE[pai_value])
289+
# action_list = [x[0] for x in latest_mjai_msg['meta']]
290+
# for idx, tehai_value_label in enumerate(self.tehai_value_labels):
291+
# # latest_mjai_msg['meta'] is list of (pai, value)
292+
# try:
293+
# pai_value = int(latest_mjai_msg['meta'][action_list.index(tehai[idx])][1] * 40)
294+
# if pai_value == 40:
295+
# pai_value = 39
296+
# except ValueError:
297+
# pai_value = 40
298+
# tehai_value_label.update(HAI_VALUE[pai_value])
288299
self.tsumohai_label.update(TILE_2_UNICODE_ART_RICH[tsumohai])
289-
if tsumohai in action_list:
290-
try:
291-
pai_value = int(latest_mjai_msg['meta'][action_list.index(tsumohai)][1] * 40)
292-
if pai_value == 40:
293-
pai_value = 39
294-
except ValueError:
295-
pai_value = 40
296-
self.tsumohai_value_label.update(HAI_VALUE[pai_value])
300+
# if tsumohai in action_list:
301+
# try:
302+
# pai_value = int(latest_mjai_msg['meta'][action_list.index(tsumohai)][1] * 40)
303+
# if pai_value == 40:
304+
# pai_value = 39
305+
# except ValueError:
306+
# pai_value = 40
307+
# self.tsumohai_value_label.update(HAI_VALUE[pai_value])
308+
309+
self.app.rpc_server.clear_top3()
310+
311+
# 將weight轉換為字典形式以便快速查找
312+
weight_dict = dict(self.app.mjai_msg_dict[self.flow_id][-1]['meta'])
313+
314+
# 生成對應tile_list的權重列表
315+
tile_order_weight = [weight_dict[tile] if tile in weight_dict else -1.0 if tile == "?" else 0.0 for tile in tehai]
316+
tile_order_weight.append(weight_dict[tsumohai] if tsumohai in weight_dict else -1.0 if tsumohai == "?" else 0.0)
317+
# tile_order_weight is numpy.float64, need to convert to float
318+
tile_order_weight = [float(weight) for weight in tile_order_weight]
319+
self.app.rpc_server.draw_weight(tile_order_weight)
320+
297321
# mjai log
298322
self.mjai_log.update(self.app.mjai_msg_dict[self.flow_id][-3:])
299323
self.mjai_log_container.scroll_end(animate=False)
@@ -338,7 +362,6 @@ def refresh_log(self) -> None:
338362
logger.log("CLICK", latest_mjai_msg)
339363
self.app.set_timer(0.15, self.autoplay)
340364
# self.autoplay(tehai, tsumohai)
341-
342365

343366
except Exception as e:
344367
logger.error(e)
@@ -349,6 +372,14 @@ def checkbox_autoplay_changed(self, event: Checkbox.Changed) -> None:
349372
global AUTOPLAY
350373
AUTOPLAY = event.value
351374
pass
375+
376+
@on(Checkbox.Changed, "#checkbox_overlay")
377+
def checkbox_overlay_changed(self, event: Checkbox.Changed) -> None:
378+
if event.value:
379+
self.app.rpc_server.start_overlay_action()
380+
else:
381+
self.app.rpc_server.stop_overlay_action()
382+
pass
352383

353384
def redo_action(self) -> None:
354385
try:
@@ -370,6 +401,7 @@ def autoplay(self) -> None:
370401
pass
371402

372403
def action_quit(self) -> None:
404+
self.app.rpc_server.stop_overlay_action()
373405
self.app.set_timer(2, self.app.update_flow.resume)
374406
self.update_log.stop()
375407
self.app.pop_screen()

0 commit comments

Comments
 (0)