Skip to content

Commit

Permalink
code cleanup:
Browse files Browse the repository at this point in the history
Made conform better with pep and removed unused imports
  • Loading branch information
nicvagn committed Sep 27, 2024
1 parent 6937186 commit e25655a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 76 deletions.
35 changes: 19 additions & 16 deletions nicsoft/lichess/lila.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
from chess_clock import ChessClock
from game import Game as LichessGame # game is already a class
from game_start import GameStart
# other Nic modules
from game_state import GameState, timedelta
# lila modularity
from game_state import GameState
# exceptions
from serial import SerialException

Expand All @@ -36,7 +36,7 @@
from niclink.nl_exceptions import (ExitNicLink, IllegalMove, NicLinkGameOver,
NicLinkHandlingGame, NoMove)

### command line ###
# === command line ===
# parsing command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--tokenfile")
Expand Down Expand Up @@ -77,7 +77,7 @@
# POLL_DELAY for checking for new games
POLL_DELAY = 10

### lichess token parsing ###
# === lichess token parsing ===
TOKEN_FILE = os.path.join(script_dir, "lichess_token/nrv773_token")
# TOKEN_FILE = os.path.join(script_dir, "lichess_token/dev_token")

Expand All @@ -90,7 +90,7 @@
if args.tokenfile is not None:
TOKEN_FILE = args.tokenfile

### logger stuff ###
# === logger stuff ===
logger = logging.getLogger("nl_lichess")

consoleHandler = logging.StreamHandler(sys.stdout)
Expand Down Expand Up @@ -246,9 +246,8 @@ def run(self) -> None:
# while checking for game over
while state_change_thread.is_alive():
if state_change_thread.is_alive():
logger.debug(
"trying to join state_change_thread, it is alive."
)
logger.debug("trying to join state_change_thread, \
it is alive.")
# check that the game is not over.
# Will call game_done if so.
self.check_for_game_over(self.game_state)
Expand Down Expand Up @@ -488,13 +487,16 @@ def update_tmp_chessboard(self, move_list: list[str]) -> chess.Board:
return tmp_chessboard

def move_made(self, game_state: GameState) -> None:
"""signal that the opponent moved, signal the external clock and NicLink"""
"""signal that the opponent moved, signal the external clock
and NicLink
@param: game_state: the gamestate containing the move
"""
logger.info(
"\nmove_made(self, game_state) entered with GameState: %s",
game_state,
)

# check to make sure the game state has moves befor trying to access them
# check to make sure the game state has moves first
if game_state.has_moves():
move = game_state.get_last_move()
# tell nl about the move
Expand All @@ -512,7 +514,8 @@ def move_made(self, game_state: GameState) -> None:

def handle_state_change(self, game_state: GameState) -> None:
"""Handle a state change in the lichess game.
@param: GameState - The lichess game state we are handlinng wrapped in a conviniance class
@param: GameState - The lichess game state we are handlinng wrapped
in a convenience class
"""
global nl_inst, logger

Expand Down Expand Up @@ -543,7 +546,7 @@ def handle_state_change(self, game_state: GameState) -> None:
result,
winner,
)
# stop the tread (this does some cleanup and throws an exception)
# stop the thread (this does some cleanup and throws an exception)
self.game_done(game_state=game_state)

# a move was made
Expand All @@ -553,7 +556,7 @@ def handle_state_change(self, game_state: GameState) -> None:
# signal move
self.chess_clock.move_made(game_state)

# tmp_chessboard.turn == True when white, false when black playing_white is same
# is it our turn?
if tmp_chessboard.turn == self.playing_white:
# get our move from chessboard
move = self.get_move_from_chessboard(tmp_chessboard)
Expand Down Expand Up @@ -592,7 +595,7 @@ def handle_chat_line(self, chat_line: str) -> None:
nl_inst.beep()


### helper functions ###
# === helper functions ===
def show_FEN_on_board(FEN) -> None:
"""show board FEN on an ascii chessboard
@param FEN - the fed to display on a board"""
Expand All @@ -607,11 +610,11 @@ def handle_game_start(game_start: GameStart,
@param game_start: Typed Dict containing the game start info
@param chess_clock: ase we using an external chess clock?
@global berserk_client: client made for ous session with lila
@global game: the Game class object, global bc has to be accessed everywhere
@global game: Game class object, global bc has to be accessed everywhere
"""
global berserk_client, logger, game, nl_inst

# check if game speed is correspondence, skip those if --correspondence argument is not set
# check if game speed is correspondence, skip if !--correspondence set
if not correspondence:
if game_start["game"]["speed"] == "correspondence":
logger.info(
Expand Down
Loading

0 comments on commit e25655a

Please sign in to comment.