From b28fbaf764b6ef5c55bf15b9b770c15e68917d24 Mon Sep 17 00:00:00 2001 From: spla Date: Tue, 1 Dec 2020 15:28:26 +0100 Subject: [PATCH] Fix #8 and Fix #9 (added stalemate detection) --- app/locales/ca.txt | 2 +- app/locales/en.txt | 2 +- app/locales/es.txt | 2 +- mastochess.py | 92 ++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 88 insertions(+), 10 deletions(-) diff --git a/app/locales/ca.txt b/app/locales/ca.txt index 0c1dae6..4903d2e 100644 --- a/app/locales/ca.txt +++ b/app/locales/ca.txt @@ -54,5 +54,5 @@ leave_a_game: fi (per a deixar la partida en qualsevol moment) list_games: jocs (mostra un llistat de partides actives) get_a_game_anotation: envia 1 (1 és el número de la partida. Envia les anotacions de la partida per correu electrònic, només usuaris del servidor local) show_help: ajuda (mostra aquesta ajuda i, per tant, és l'ajuda de l'ajuda) - +stalemate_str: taules! partida finalitzada. diff --git a/app/locales/en.txt b/app/locales/en.txt index f94a778..a6c6af7 100644 --- a/app/locales/en.txt +++ b/app/locales/en.txt @@ -54,5 +54,5 @@ leave_a_game: end (to leave the game any time) list_games: games (show an on going games list) get_a_game_anotation: send 1 (1 is the game number. It send the game's anotations by email but local users only) show_help: help (show this help so, it's the help of the help) - +stalemate_str: stalemate! game is over. diff --git a/app/locales/es.txt b/app/locales/es.txt index 0464870..008c827 100644 --- a/app/locales/es.txt +++ b/app/locales/es.txt @@ -54,5 +54,5 @@ leave_a_game: fin (para dejar la partida en cualquier momento) list_games: partidas (muestra un listado de partidas activas) get_a_game_anotation: envia 1 (1 es el número de la partida. Envia las anotaciones de la partida pedida por correo electrónico pero sólo a usuarios del servidor local) show_help: ayuda (muestra esta ayuda y, por tanto, es la ayuda de la ayuda) - +stalemate_str: Tablas! la partida ha terminado. diff --git a/mastochess.py b/mastochess.py index f5ac4d6..e66fce3 100644 --- a/mastochess.py +++ b/mastochess.py @@ -710,14 +710,64 @@ def send_anotation(game_id): pass return emailed -def close_game(): +def close_game(username): + + try: + + conn = None + + conn = psycopg2.connect(database = chess_db, user = chess_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute("select white_user, black_user from games where game_id=(%s)", (game_id,)) + + row = cur.fetchone() + + if row != None: + + white_player = row[0] + + black_player = row[1] + + cur.close() + + except (Exception, psycopg2.DatabaseError) as error: + + sys.exit(error) + + finally: + + if conn is not None: + + conn.close() now = datetime.now() + winner = '' + waiting = False finished = True + if stalemate == True: + + winner = "stalemate" + + if black_user == '': + + winner = 'none' + + else: + + if query_word == search_end and username == white_user and stalemate == False: + + winner = black_user + + elif query_word == search_end and username == black_user and stalemate == False: + + winner = white_user + try: conn = None @@ -728,7 +778,7 @@ def close_game(): cur.execute("update games set waiting=(%s), finished=(%s), updated_at=(%s) where game_id=(%s)", (waiting, finished, now, game_id)) - cur.execute("update stats set winner=(%s), finished=(%s), updated_at=(%s) where game_id=(%s)", (username, finished, now, game_id)) + cur.execute("update stats set winner=(%s), finished=(%s), updated_at=(%s) where game_id=(%s)", (winner, finished, now, game_id)) conn.commit() @@ -1483,6 +1533,10 @@ if __name__ == '__main__': promoted = False + stalemate = False + + checkmate = False + try: piece_square_index = chess.SQUARE_NAMES.index(moving[:2]) @@ -1557,13 +1611,19 @@ if __name__ == '__main__': king_square = board.king(chess.WHITE) check = True + if board.is_stalemate() == True: + + stalemate = True + if board.is_game_over() == True: game_moves = board.ply() - close_game() + close_game(username) - checkmate = True + if stalemate == False: + + checkmate = True else: @@ -1589,6 +1649,22 @@ if __name__ == '__main__': toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + elif check == False and stalemate == True: + + toot_text = stalemate_str + ' (' + str(game_moves) + ')' + '\n' + + toot_text += '\n@'+playing_user + ', ' + '@'+username + "\n" + + toot_text += '\n' + winned_games + "\n" + + played_games, wins = get_stats(username) + + toot_text += username + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + + played_games, wins = get_stats(playing_user) + + toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + else: toot_text = '@'+playing_user + ' ' + your_turn + '\n' @@ -1667,6 +1743,8 @@ if __name__ == '__main__': elif query_word == search_end: + stalemate = False + if black_user != '': if username == white_user: @@ -1675,7 +1753,7 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) - close_game() + close_game(username) update_replies(status_id, username, now) @@ -1689,7 +1767,7 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) - close_game() + close_game(username) update_replies(status_id, username, now) @@ -1703,7 +1781,7 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) - close_game() + close_game(username) update_replies(status_id, username, now)