Fix #8 and Fix #9 (added stalemate detection)

This commit is contained in:
spla 2020-12-01 15:28:26 +01:00
pare 8e97d40526
commit b28fbaf764
S'han modificat 4 arxius amb 88 adicions i 10 eliminacions

Veure arxiu

@ -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) 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) 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) show_help: ajuda (mostra aquesta ajuda i, per tant, és l'ajuda de l'ajuda)
stalemate_str: taules! partida finalitzada.

Veure arxiu

@ -54,5 +54,5 @@ leave_a_game: end (to leave the game any time)
list_games: games (show an on going games list) 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) 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) show_help: help (show this help so, it's the help of the help)
stalemate_str: stalemate! game is over.

Veure arxiu

@ -54,5 +54,5 @@ leave_a_game: fin (para dejar la partida en cualquier momento)
list_games: partidas (muestra un listado de partidas activas) 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) 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) show_help: ayuda (muestra esta ayuda y, por tanto, es la ayuda de la ayuda)
stalemate_str: Tablas! la partida ha terminado.

Veure arxiu

@ -710,14 +710,64 @@ def send_anotation(game_id):
pass pass
return emailed 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() now = datetime.now()
winner = ''
waiting = False waiting = False
finished = True 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: try:
conn = None 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 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() conn.commit()
@ -1483,6 +1533,10 @@ if __name__ == '__main__':
promoted = False promoted = False
stalemate = False
checkmate = False
try: try:
piece_square_index = chess.SQUARE_NAMES.index(moving[:2]) piece_square_index = chess.SQUARE_NAMES.index(moving[:2])
@ -1557,13 +1611,19 @@ if __name__ == '__main__':
king_square = board.king(chess.WHITE) king_square = board.king(chess.WHITE)
check = True check = True
if board.is_stalemate() == True:
stalemate = True
if board.is_game_over() == True: if board.is_game_over() == True:
game_moves = board.ply() game_moves = board.ply()
close_game() close_game(username)
checkmate = True if stalemate == False:
checkmate = True
else: else:
@ -1589,6 +1649,22 @@ if __name__ == '__main__':
toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" 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: else:
toot_text = '@'+playing_user + ' ' + your_turn + '\n' toot_text = '@'+playing_user + ' ' + your_turn + '\n'
@ -1667,6 +1743,8 @@ if __name__ == '__main__':
elif query_word == search_end: elif query_word == search_end:
stalemate = False
if black_user != '': if black_user != '':
if username == white_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) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
close_game() close_game(username)
update_replies(status_id, username, now) 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) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
close_game() close_game(username)
update_replies(status_id, username, now) 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) mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility)
close_game() close_game(username)
update_replies(status_id, username, now) update_replies(status_id, username, now)