Skip to content

Commit

Permalink
feat: move autoplay options to main GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
latorc committed Apr 18, 2024
1 parent 11997c2 commit 8512a09
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 87 deletions.
4 changes: 2 additions & 2 deletions common/lan_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class LanStr:
UPSTREAM_PROXY = "Upstream Proxy"
CLIENT_SIZE = "Client Size"
MAJSOUL_URL = "Majsoul URL"
LANGUAGE = "Language"
LANGUAGE = "Display Language"
MODEL_TYPE = "AI Model Type"
AI_MODEL_FILE = "Local Model File (4P)"
AI_MODEL_FILE_3P = "Local Model File (3P)"
Expand Down Expand Up @@ -200,7 +200,7 @@ class LanStrZHS(LanStr):
UPSTREAM_PROXY = "上游代理"
CLIENT_SIZE = "客户端大小"
MAJSOUL_URL = "雀魂网址"
LANGUAGE = "语言"
LANGUAGE = "显示语言"
MODEL_TYPE = "AI 模型类型"
AI_MODEL_FILE = "本地模型文件(四麻)"
AI_MODEL_FILE_3P = "本地模型文件(三麻)"
Expand Down
77 changes: 49 additions & 28 deletions gui/main_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from tkinter import ttk, messagebox

from bot_manager import BotManager, mjai_reaction_2_guide
from common.utils import RES_FOLDER, GameMode
from common.utils import RES_FOLDER, GameMode, GAME_MODES
from common.utils import UiState, sub_file, error_to_str
from common.log_helper import LOGGER, LogHelper
from common.settings import Settings
Expand Down Expand Up @@ -101,15 +101,28 @@ def _create_widgets(self):
self.tb2, self.st.lan().AUTOPLAY, tb_ht, font_size=sw_ft_sz, command=self._on_switch_autoplay_clicked)
self.switch_autoplay.pack(**pack_args)
# auto join
self.tb2.add_sep()
self.switch_autojoin = ToggleSwitch(
self.tb2, self.st.lan().AUTO_JOIN_GAME, tb_ht, font_size=sw_ft_sz, command=self._on_switch_autojoin_clicked)
self.switch_autojoin.pack(**pack_args)
# combo boxrd for auto join level and mode
_frame = tk.Frame(self.tb2)
_frame.pack(**pack_args)
self.auto_join_level_var = tk.StringVar(value=self.st.lan().GAME_LEVELS[self.st.auto_join_level])
options = self.st.lan().GAME_LEVELS
combo_autojoin_level = ttk.Combobox(_frame, textvariable=self.auto_join_level_var, values=options, state="readonly", width=8)
combo_autojoin_level.grid(row=0, column=0, padx=3, pady=3)
combo_autojoin_level.bind("<<ComboboxSelected>>", self._on_autojoin_level_selected)
mode_idx = GAME_MODES.index(self.st.auto_join_mode)
self.auto_join_mode_var = tk.StringVar(value=self.st.lan().GAME_MODES[mode_idx])
options = self.st.lan().GAME_MODES
combo_autojoin_mode = ttk.Combobox(_frame, textvariable=self.auto_join_mode_var, values=options, state="readonly", width=8)
combo_autojoin_mode.grid(row=1, column=0, padx=3, pady=3)
combo_autojoin_mode.bind("<<ComboboxSelected>>", self._on_autojoin_mode_selected)
# timer
self.timer = Timer(self.tb2, tb_ht, sw_ft_sz, self.st.lan().AUTO_JOIN_TIMER)
self.timer.set_callback(self.bot_manager.disable_autojoin) # stop autojoin when time is up
self.timer.pack(**pack_args)
self.tb2.add_sep()

self.tb2.add_sep()

# === AI guidance ===
cur_row += 1
Expand All @@ -118,14 +131,13 @@ def _create_widgets(self):
self.grid_frame.grid_rowconfigure(cur_row, weight=0)

cur_row += 1
self.text_ai_guide = tk.Text(
self.ai_guide_var = tk.StringVar()
self.text_ai_guide = tk.Label(
self.grid_frame,
state=tk.DISABLED,
textvariable=self.ai_guide_var,
font=GUI_STYLE.font_normal("Segoe UI Emoji",22),
height=5,
relief=tk.SUNKEN,
padx=5,
pady=5
height=5, anchor=tk.NW, justify=tk.LEFT,
relief=tk.SUNKEN, padx=5,pady=5,
)
self.text_ai_guide.grid(row=cur_row, **grid_args)
self.grid_frame.grid_rowconfigure(cur_row, weight=1)
Expand All @@ -136,13 +148,15 @@ def _create_widgets(self):
_label.grid(row=cur_row, **grid_args)
self.grid_frame.grid_rowconfigure(cur_row, weight=0)
cur_row += 1
self.text_state = tk.Text(
self.gameinfo_var = tk.StringVar()
self.text_gameinfo = tk.Label(
self.grid_frame,
state=tk.DISABLED,
height=2,
textvariable=self.gameinfo_var,
height=2, anchor=tk.W, justify=tk.LEFT,
font=GUI_STYLE.font_normal("Segoe UI Emoji",22),
relief=tk.SUNKEN, padx=5,pady=5,
)
self.text_state.grid(row=cur_row, **grid_args)
self.text_gameinfo.grid(row=cur_row, **grid_args)
self.grid_frame.grid_rowconfigure(cur_row, weight=1)

# === Model info ===
Expand All @@ -156,6 +170,16 @@ def _create_widgets(self):
self.status_bar = StatusBar(self.grid_frame, 3)
self.status_bar.grid(row=cur_row, column=0, sticky='ew', padx=1, pady=1)
self.grid_frame.grid_rowconfigure(cur_row, weight=0)

def _on_autojoin_level_selected(self, _event):
new_value = self.auto_join_level_var.get() # convert to index
self.st.auto_join_level = self.st.lan().GAME_LEVELS.index(new_value)

def _on_autojoin_mode_selected(self, _event):
new_mode = self.auto_join_mode_var.get() # convert to string
new_mode = self.st.lan().GAME_MODES.index(new_mode)
new_mode = GAME_MODES[new_mode]
self.st.auto_join_mode = new_mode

def _on_btn_start_browser_clicked(self):
self.btn_start_browser.config(state=tk.DISABLED)
Expand Down Expand Up @@ -213,7 +237,7 @@ def _on_exit(self):
try:
LOGGER.info("Exiting GUI and program. saving settings and stopping threads.")
self.st.save_json()
self.bot_manager.stop(False)
self.bot_manager.stop(False)
except: #pylint:disable=bare-except
pass
self.quit()
Expand Down Expand Up @@ -255,30 +279,28 @@ def _update_gui_info(self):
else:
sw.switch_off()

# Update Reaction
self.text_ai_guide.config(state=tk.NORMAL)
self.text_ai_guide.delete('1.0', tk.END)
# Update AI guide from Reaction
pending_reaction = self.bot_manager.get_pending_reaction()
# convert this reaction into string
if pending_reaction:
action_str, options = mjai_reaction_2_guide(pending_reaction, 3, self.st.lan())
self.text_ai_guide.insert(tk.END, f'{action_str}\n')
ai_guide_str, options = mjai_reaction_2_guide(pending_reaction, 3, self.st.lan())
ai_guide_str += '\n'
for tile_str, weight in options:
self.text_ai_guide.insert(tk.END, f' {tile_str} {weight*100:4.0f}%\n')
self.text_ai_guide.config(state=tk.DISABLED)
ai_guide_str += f" {tile_str:8} {weight*100:4.0f}%\n"
self.ai_guide_var.set(ai_guide_str)
else:
self.ai_guide_var.set("")

# update state: display tehai + tsumohai
gi:GameInfo = self.bot_manager.get_game_info()
self.text_state.config(state=tk.NORMAL)
self.text_state.delete('1.0', tk.END)
if gi and gi.my_tehai:
tehai = gi.my_tehai
tsumohai = gi.my_tsumohai
hand_str = ''.join(MJAI_TILE_2_UNICODE[t] for t in tehai)
if tsumohai:
hand_str += f" + {MJAI_TILE_2_UNICODE[tsumohai]}"
self.text_state.insert(tk.END, hand_str)
self.text_state.config(state=tk.DISABLED)
self.gameinfo_var.set(hand_str)
else:
self.gameinfo_var.set("")

# Model info
# bot/model
Expand All @@ -303,7 +325,6 @@ def _update_gui_info(self):
self.model_bar.update_column(0, text, self.icon_red)
self.model_bar.update_column(1, 'ℹ️ ')


### Status bar
# main thread
fps_disp = min([999, self.bot_manager.fps_counter.fps])
Expand Down
74 changes: 17 additions & 57 deletions gui/settings_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import tkinter as tk
from tkinter import ttk, messagebox


from common.utils import MODEL_FOLDER, GAME_MODES
from common.utils import MODEL_FOLDER
from common.utils import list_files
from common.log_helper import LOGGER
from common.settings import Settings
Expand Down Expand Up @@ -92,11 +91,9 @@ def create_widgets(self):
_frame.grid(row=cur_row, column=2, columnspan=2)
_label = ttk.Label(_frame, text=self.st.lan().UPSTREAM_PROXY)
_label.pack(side=tk.LEFT, **pad_args)
# _label.grid(row=cur_row, column=2, **args_label)
self.upstream_proxy_var = tk.StringVar(value=self.st.upstream_proxy)
_entry = ttk.Entry(_frame, textvariable=self.upstream_proxy_var, width=std_wid*2)
_entry.pack(side=tk.LEFT, **pad_args)
# _entry.grid(row=cur_row, column=3, **args_entry)
_entry.pack(side=tk.LEFT, **pad_args)

# Select language
cur_row += 1
Expand All @@ -107,7 +104,11 @@ def create_widgets(self):
select_menu = ttk.Combobox(main_frame, textvariable=self.language_var, values=options, state="readonly", width=std_wid)
select_menu.grid(row=cur_row, column=1, **args_entry)

# Select Model Type
# sep
cur_row += 1
sep = ttk.Separator(main_frame, orient=tk.HORIZONTAL)
sep.grid(row=cur_row, column=0, columnspan=4, sticky="ew", pady=5)
# Select Model Type
cur_row += 1
_label = ttk.Label(main_frame, text=self.st.lan().MODEL_TYPE)
_label.grid(row=cur_row, column=0, **args_label)
Expand All @@ -130,8 +131,7 @@ def create_widgets(self):
_label.grid(row=cur_row, column=0, **args_label)
self.model_file_3p_var = tk.StringVar(value=self.st.model_file_3p)
select_menu2 = ttk.Combobox(main_frame, textvariable=self.model_file_3p_var, values=model_files, state="readonly", width=std_wid*3)
select_menu2.grid(row=cur_row, column=1, columnspan=3, **args_entry)

select_menu2.grid(row=cur_row, column=1, columnspan=3, **args_entry)

# MJAPI url
cur_row += 1
Expand All @@ -150,8 +150,7 @@ def create_widgets(self):
string_entry.grid(row=cur_row, column=1, **args_entry)
# MJAPI usage
_label = ttk.Label(main_frame, text=f"{self.st.lan().MJAPI_USAGE}: {self.st.mjapi_usage}")
_label.grid(row=cur_row, column=2, **args_entry)

_label.grid(row=cur_row, column=2, **args_entry)

# MJAPI secret
cur_row += 1
Expand All @@ -172,23 +171,15 @@ def create_widgets(self):

_label = ttk.Label(main_frame, text=self.st.lan().LOGIN_TO_REFRESH)
_label.grid(row=cur_row, column=2, **args_entry)

# models_str = self.settings.lan().MODEL + ": " + ','.join(self.settings.mjapi_models)
# text = tk.Text(main_frame, wrap=tk.NONE)
# text.insert(tk.END, models_str)
# text.configure(state=tk.DISABLED, height=1, width=30)
# text.grid(row=cur_row, column=2, columnspan=2, **args_label)

# sep
cur_row += 1
sep = ttk.Separator(main_frame, orient=tk.HORIZONTAL)
sep.grid(row=cur_row, column=0, columnspan=4, sticky="ew", pady=5)
### Auto play settings
cur_row += 1
_label = ttk.Label(main_frame, text=self.st.lan().AUTO_PLAY_SETTINGS)
_label.grid(row=cur_row, column=0, **args_label)
# auto play
self.autoplay_var = tk.BooleanVar(value=self.st.enable_automation)
autoplay_entry = ttk.Checkbutton(main_frame, variable=self.autoplay_var, text=self.st.lan().AUTOPLAY, width=std_wid)
autoplay_entry.grid(row=cur_row, column=1, **args_entry)
_label.grid(row=cur_row, column=0, **args_label)
# random move
cur_row += 1
self.random_move_var = tk.BooleanVar(value=self.st.auto_random_move)
ran_moves_entry = ttk.Checkbutton(
main_frame, variable=self.random_move_var, text=self.st.lan().MOUSE_RANDOM_MOVE, width=std_wid)
Expand Down Expand Up @@ -232,37 +223,18 @@ def create_widgets(self):
delay_upper_entry = tk.Entry(main_frame, textvariable= self.delay_random_upper_var,width=std_wid)
delay_upper_entry.grid(row=cur_row, column=2, **args_entry)

# auto join settings
cur_row += 1
_label = ttk.Label(main_frame, text=self.st.lan().AUTO_JOIN_GAME)
_label.grid(row=cur_row, column=0, **args_label)
self.auto_join_var = tk.BooleanVar(value=self.st.auto_join_game)
auto_join_entry = ttk.Checkbutton(main_frame, variable=self.auto_join_var, text = self.st.lan().AUTO_JOIN_GAME, width=std_wid)
auto_join_entry.grid(row=cur_row, column=1, **args_entry)

self.auto_join_level_var = tk.StringVar(value=self.st.lan().GAME_LEVELS[self.st.auto_join_level])
options = self.st.lan().GAME_LEVELS
next_level = ttk.Combobox(main_frame, textvariable=self.auto_join_level_var, values=options, state="readonly", width=std_wid)
next_level.grid(row=cur_row, column=2, **args_entry)

mode_idx = GAME_MODES.index(self.st.auto_join_mode)
self.auto_join_mode_var = tk.StringVar(value=self.st.lan().GAME_MODES[mode_idx])
options = self.st.lan().GAME_MODES
next_mode = ttk.Combobox(main_frame, textvariable=self.auto_join_mode_var, values=options, state="readonly", width=std_wid)
next_mode.grid(row=cur_row, column=3, **args_entry)

# tips :Settings
cur_row += 1
label_settings = ttk.Label(main_frame, text=self.st.lan().SETTINGS_TIPS, width=40)
label_settings.grid(row=cur_row, column=1, columnspan=3, **args_entry)

# Buttons frame
button_frame = ttk.Frame(self)
button_frame.pack(side="bottom", fill="x")
button_frame.pack(side=tk.BOTTOM, fill=tk.X)
cancel_button = ttk.Button(button_frame, text=self.st.lan().CANCEL, command=self._on_cancel)
cancel_button.pack(side="left", padx=20, pady=10)
cancel_button.pack(side=tk.LEFT, padx=20, pady=20)
save_button = ttk.Button(button_frame, text=self.st.lan().SAVE, command=self._on_save)
save_button.pack(side="right", padx=20, pady=10)
save_button.pack(side=tk.RIGHT, padx=20, pady=20)

def _on_save(self):
# Get values from entry fields, validate, and save them
Expand Down Expand Up @@ -318,7 +290,6 @@ def _on_save(self):
self.model_updated = True

# auto play settings
autoplay_new = self.autoplay_var.get()
idle_move_new = self.auto_idle_move_var.get()
drag_dahai_new = self.auto_drag_dahai_var.get()
auto_random_move_new = self.random_move_var.get()
Expand All @@ -332,13 +303,6 @@ def _on_save(self):
return
delay_lower_new = max(0,delay_lower_new)
delay_upper_new = max(delay_lower_new, delay_upper_new)
auto_join_new = self.auto_join_var.get()
auto_join_level_new = self.auto_join_level_var.get() # convert to index
auto_join_level_new = self.st.lan().GAME_LEVELS.index(auto_join_level_new)
auto_join_mode_new = self.auto_join_mode_var.get() # convert to string
auto_join_mode_new = self.st.lan().GAME_MODES.index(auto_join_mode_new)
auto_join_mode_new = GAME_MODES[auto_join_mode_new]


# save settings
self.st.auto_launch_browser = auto_launch_new
Expand All @@ -357,17 +321,13 @@ def _on_save(self):
self.st.mjapi_secret = mjapi_secret_new
self.st.mjapi_model_select = mjapi_model_select_new

self.st.enable_automation = autoplay_new
self.st.auto_idle_move = idle_move_new
self.st.auto_dahai_drag = drag_dahai_new
self.st.auto_random_move = auto_random_move_new
self.st.ai_randomize_choice = randomized_choice_new
self.st.auto_reply_emoji_rate = reply_emoji_new
self.st.delay_random_lower = delay_lower_new
self.st.delay_random_upper = delay_upper_new
self.st.auto_join_game = auto_join_new
self.st.auto_join_level = auto_join_level_new
self.st.auto_join_mode = auto_join_mode_new

LOGGER.info("Saving Settings to file")
self.st.save_json()
Expand Down

0 comments on commit 8512a09

Please sign in to comment.