From 3bf774451329d875976abad30c3a909cc0f9f520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B2=E8=8F=AF?= <42814579+yunwah@users.noreply.github.com> Date: Wed, 5 May 2021 01:33:53 -0400 Subject: [PATCH] Clean up timer widget code and create state widgets more intelligently This commit corresponds to the most recent update of warpy and removes the need for state handlers in order to determine the string value of the open worlds state. This also further abstracts the code for the asynchronous state_widget function by taking the function and timer widget instead of specifying these explicitly in code. This removes the need to have three near identical timer state setting functions due to a small difference in function calls despite ingesting the same target information, just with different strings and values. --- src/overlay.py | 36 +++++++++++++----------------------- src/timers.py | 18 ++---------------- 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/src/overlay.py b/src/overlay.py index 35058af..a74485b 100644 --- a/src/overlay.py +++ b/src/overlay.py @@ -44,6 +44,7 @@ else: # Assume fullscreen x_pos, y_pos, width, height, fullscreen = 0, 0, win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1), True + if fullscreen: # root.wm_attributes("-fullscreen", True) print("Fullscreen is not supported") # Currently unable to display above fullscreen apps @@ -68,11 +69,12 @@ tk.Label(timers, text="Cetus", fg="#FFFFFF").grid(row=0, column=0) tk.Label(timers, text="Deimos", fg="#FFFFFF").grid(row=0, column=3) tk.Label(timers, text="Vallis", fg="#FFFFFF").grid(row=0, column=6) -# String state indicators +# Create new timer objects cetus_timer = Timer(tk.StringVar(timers), tk.StringVar(timers), root, loop) vallis_timer = Timer(tk.StringVar(timers), tk.StringVar(timers), root, loop) cambion_timer = Timer(tk.StringVar(timers), tk.StringVar(timers), root, loop) +# State Labels cetus_state_label = tk.Label(timers, textvariable=cetus_timer.state) vallis_state_label = tk.Label(timers, textvariable=vallis_timer.state) cambion_state_label = tk.Label(timers, textvariable=cambion_timer.state) @@ -81,6 +83,7 @@ cetus_state_label.grid(row=0, column=1) cambion_state_label.grid(row=0, column=4) vallis_state_label.grid(row=0, column=7) +# Timer Labels cetus_timer_label = tk.Label(timers, textvariable=cetus_timer.timer) vallis_timer_label = tk.Label(timers, textvariable=vallis_timer.timer) cambion_timer_label = tk.Label(timers, textvariable=cambion_timer.timer) @@ -91,28 +94,15 @@ vallis_timer_label.grid(row=0, column=8) # Asynchronous functions -async def cetus_state_widget(): - json = await warframe.cetus_status() - next_attempt, remaining_time = cetus_timer.set_state(json, cetus_timer.cetus_handler) - cetus_timer.update_timer(remaining_time) - root.after(next_attempt, lambda: loop.run_until_complete(cetus_state_widget())) +async def state_widget(f, _timer_widget): + json = await f() + next_attempt, remaining_time = _timer_widget.set_state(json) + _timer_widget.update_timer(remaining_time) + root.after(next_attempt, lambda: loop.run_until_complete(state_widget(f, _timer_widget))) + root.after(next_attempt, lambda: enable_clickthrough(root)) - -async def vallis_state_widget(): - json = await warframe.vallis_status() - next_attempt, remaining_time = vallis_timer.set_state(json, vallis_timer.vallis_handler) - vallis_timer.update_timer(remaining_time) - root.after(next_attempt, lambda: loop.run_until_complete(vallis_state_widget())) - - -async def cambion_state_widget(): - json = await warframe.cambion_status() - next_attempt, remaining_time = cambion_timer.set_state(json, cambion_timer.cambion_handler) - cambion_timer.update_timer(remaining_time) - root.after(next_attempt, lambda: loop.run_until_complete(cambion_state_widget())) - -loop.run_until_complete(cetus_state_widget()) -loop.run_until_complete(vallis_state_widget()) -loop.run_until_complete(cambion_state_widget()) +loop.run_until_complete(state_widget(warframe.cetus_status, cetus_timer)) +loop.run_until_complete(state_widget(warframe.vallis_status, vallis_timer)) +loop.run_until_complete(state_widget(warframe.cambion_status, cambion_timer)) root.mainloop() diff --git a/src/timers.py b/src/timers.py index 4fdf287..b93b428 100644 --- a/src/timers.py +++ b/src/timers.py @@ -17,7 +17,7 @@ class Timer: self._root = root self._loop = loop - def set_state(self, json, state_handler): + def set_state(self, json): expiration, remaining_time, remaining_seconds = _create_timer_values(json) if remaining_seconds < 0: self.timer.set(str('00:00:00')) @@ -25,22 +25,8 @@ class Timer: else: self.timer.set(str(remaining_time)) retry = remaining_seconds * 1000.0 + 120000 - state_handler(json) - return round(retry), remaining_time - - # Do these belong here? - def cetus_handler(self, json): self.state.set(json['state'].capitalize()) - - def vallis_handler(self, json): - if json["isWarm"]: - self.state.set("Warm") - else: - self.state.set("Cold") - - def cambion_handler(self, json): - self.state.set(json['active'].capitalize()) - # End ? + return round(retry), remaining_time def update_timer(self, remaining_time): remaining_time -= timedelta(seconds=1)