Removed blocking input
This commit is contained in:
50
rpi/main.py
50
rpi/main.py
@@ -4,6 +4,16 @@ from board_mate_client import ApiClient, Configuration
|
||||
from controllers.board_mate_controller import ApiController
|
||||
from models.clock import Clock
|
||||
from scripts.timer.grove_rgb_lcd import setRGB, setText
|
||||
import threading
|
||||
import queue
|
||||
import time
|
||||
|
||||
|
||||
def get_move(prompt, move_queue):
|
||||
while True:
|
||||
move = input(prompt)
|
||||
move_queue.put(move)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
config = Configuration(
|
||||
@@ -21,9 +31,8 @@ if __name__ == "__main__":
|
||||
black_clock = Clock(time_value, increment)
|
||||
|
||||
print("Creating the party...")
|
||||
|
||||
game_id = controller.create_party(white_name, black_name, time_value, increment)
|
||||
if game_id is None :
|
||||
if game_id is None:
|
||||
print("An error occurred while creating the party. Exiting...")
|
||||
exit()
|
||||
|
||||
@@ -31,17 +40,28 @@ if __name__ == "__main__":
|
||||
|
||||
currentPlayer = 0
|
||||
white_clock.start()
|
||||
while True:
|
||||
message = None
|
||||
if currentPlayer == 0 :
|
||||
message = "White to play"
|
||||
currentPlayer = 1
|
||||
white_clock.stop()
|
||||
else :
|
||||
message = "Black to play"
|
||||
currentPlayer = 0
|
||||
black_clock.start()
|
||||
|
||||
move = input(f"{message} : ")
|
||||
setText(f"W ${white_clock.clock_to_str()}\n B ${black_clock.clock_to_str()}")
|
||||
controller.add_move(game_id, move)
|
||||
move_queue = queue.Queue()
|
||||
input_thread = threading.Thread(target=get_move, args=("", move_queue), daemon=True)
|
||||
input_thread.start()
|
||||
|
||||
while True:
|
||||
setText(f"W {white_clock.clock_to_str()}\n B {black_clock.clock_to_str()}")
|
||||
|
||||
try:
|
||||
move = move_queue.get_nowait()
|
||||
message = "White to play" if currentPlayer == 0 else "Black to play"
|
||||
controller.add_move(game_id, move)
|
||||
|
||||
if currentPlayer == 0:
|
||||
currentPlayer = 1
|
||||
white_clock.start()
|
||||
black_clock.stop()
|
||||
else:
|
||||
currentPlayer = 0
|
||||
black_clock.start()
|
||||
white_clock.stop()
|
||||
except queue.Empty:
|
||||
pass
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
Reference in New Issue
Block a user