New feature! Added pgn save & send support
This commit is contained in:
pare
83cddfca9d
commit
1892141b5d
S'han modificat 1 arxius amb 39 adicions i 67 eliminacions
106
mastochess.py
106
mastochess.py
|
@ -18,6 +18,7 @@ import psycopg2
|
|||
import chess
|
||||
import chess.svg
|
||||
from cairosvg import svg2png
|
||||
import chess.pgn
|
||||
|
||||
def cleanhtml(raw_html):
|
||||
cleanr = re.compile('<.*?>')
|
||||
|
@ -511,91 +512,59 @@ def update_game(board_game, toot_url):
|
|||
|
||||
conn.close()
|
||||
|
||||
def save_anotation(moving):
|
||||
def write_result(filename, old_string, new_string):
|
||||
|
||||
if moving_piece != 1:
|
||||
with open(filename) as f:
|
||||
s = f.read()
|
||||
if old_string not in s:
|
||||
print('"{old_string}" not found in {filename}.'.format(**locals()))
|
||||
return
|
||||
|
||||
square_index = chess.SQUARE_NAMES.index(moving[2:])
|
||||
with open(filename, 'w') as f:
|
||||
print('Changing "{old_string}" to "{new_string}" in {filename}'.format(**locals()))
|
||||
s = s.replace(old_string, new_string)
|
||||
f.write(s)
|
||||
|
||||
moved_piece = board.piece_type_at(square_index)
|
||||
def save_anotation(moving, san_move):
|
||||
|
||||
moved_piece_name = get_moved_piece_name(moved_piece)
|
||||
|
||||
else:
|
||||
|
||||
moved_piece_name = 'P'
|
||||
|
||||
if promoted == True:
|
||||
|
||||
moving = moving + 'q'
|
||||
|
||||
game_file = "app/anotations/" + str(game_id) + ".txt"
|
||||
|
||||
if moved_piece_name == 'P':
|
||||
|
||||
moved_piece_name = moved_piece_name.replace('P','')
|
||||
|
||||
if capture == True:
|
||||
|
||||
moved_piece_name = moved_piece_name + "X"
|
||||
|
||||
if check == True:
|
||||
|
||||
moved_piece_name = moved_piece_name + moving[2:] + "+"
|
||||
|
||||
if checkmate == True:
|
||||
|
||||
moved_piece_name = moved_piece_name + "+"
|
||||
pgn_file = "app/anotations/" + str(game_id) + ".pgn"
|
||||
|
||||
if bool(board.turn == chess.BLACK) == True:
|
||||
|
||||
if check != True and checkmate != True:
|
||||
|
||||
if promoted != True:
|
||||
|
||||
line_data = str(board.fullmove_number) + ". " + moved_piece_name + moving[2:]
|
||||
|
||||
else:
|
||||
|
||||
line_data = str(board.fullmove_number) + ". " + moved_piece_name + "=D"
|
||||
|
||||
else:
|
||||
|
||||
line_data = str(board.fullmove_number) + ". " + moved_piece_name
|
||||
line_data = str(board.fullmove_number) + ". " + san_move
|
||||
|
||||
else:
|
||||
|
||||
moved_piece_name = moved_piece_name.lower()
|
||||
line_data = " " + san_move + " "
|
||||
|
||||
if check != True and checkmate != True:
|
||||
if checkmate or stalemate:
|
||||
|
||||
if promoted != True:
|
||||
line_data = line_data + " " + board.result()
|
||||
|
||||
line_data = " - " + moved_piece_name + moving[2:] + "\n"
|
||||
write_result(pgn_file, '[Result ]', '[Result ' + board.result() + ' ]')
|
||||
|
||||
else:
|
||||
if not os.path.isfile(pgn_file):
|
||||
|
||||
line_data = " - " + moved_piece_name + "=D"
|
||||
file_header = '[Event ' + game_name + ': ' + str(game_id) + ']\n'
|
||||
file_header += '[Site ' + mastodon_hostname + ']' + '\n'
|
||||
file_header += '[Date ' + str(datetime.today().strftime('%d-%m-%Y')) + ']' + '\n'
|
||||
file_header += '[Round ' + str(game_id) + ']' + '\n'
|
||||
file_header += '[White ' + white_user + ' ]' + '\n'
|
||||
file_header += '[Black ' + black_user + ' ]' + '\n'
|
||||
file_header += '[Result ]' + '\n'
|
||||
file_header += '[Time ' + str(datetime.now().strftime('%H:%M:%S')) + ']' + '\n\n'
|
||||
|
||||
else:
|
||||
|
||||
line_data = " - " + moved_piece_name + "\n"
|
||||
|
||||
if not os.path.isfile(game_file):
|
||||
|
||||
file_header = game_name + ': ' + str(game_id) + "\n" + white_user + " / " + black_user + "\n\n"
|
||||
|
||||
with open(game_file, 'w+') as f:
|
||||
with open(pgn_file, 'w+') as f:
|
||||
|
||||
f.write(file_header)
|
||||
|
||||
with open(game_file, 'a') as f:
|
||||
with open(pgn_file, 'a') as f:
|
||||
|
||||
f.write(line_data)
|
||||
|
||||
else:
|
||||
|
||||
with open(game_file, 'a') as f:
|
||||
with open(pgn_file, 'a') as f:
|
||||
|
||||
f.write(line_data)
|
||||
|
||||
|
@ -656,7 +625,7 @@ def send_anotation(game_id):
|
|||
msg['Subject'] = email_subject + game_id
|
||||
|
||||
# Attach the game anotation
|
||||
file_to_attach = "app/anotations/" + game_id + ".txt"
|
||||
file_to_attach = "app/anotations/" + game_id + ".pgn"
|
||||
try:
|
||||
|
||||
attachment = open(file_to_attach, 'rb')
|
||||
|
@ -697,19 +666,20 @@ def send_anotation(game_id):
|
|||
|
||||
print(auth_error)
|
||||
pass
|
||||
return emailed
|
||||
return (emailed, game_id, game_found)
|
||||
|
||||
except socket.gaierror as socket_error:
|
||||
|
||||
print(socket_error)
|
||||
pass
|
||||
return emailed
|
||||
return (emailed, game_id, game_found)
|
||||
|
||||
except SMTPRecipientsRefused as recip_error:
|
||||
|
||||
print(recip_error)
|
||||
pass
|
||||
return emailed
|
||||
return (emailed, game_id, game_found)
|
||||
|
||||
|
||||
def close_game(username):
|
||||
|
||||
|
@ -1464,7 +1434,7 @@ if __name__ == '__main__':
|
|||
|
||||
if emailed == False and game_found == True:
|
||||
|
||||
toot_text = '@'+username + send_error
|
||||
toot_text = '@'+username + ' ' + send_error
|
||||
|
||||
elif emailed == True and game_found == True:
|
||||
|
||||
|
@ -1580,6 +1550,8 @@ if __name__ == '__main__':
|
|||
|
||||
else:
|
||||
|
||||
san_move = board.san(chess.Move.from_uci(moving))
|
||||
|
||||
check = False
|
||||
|
||||
playing_user = next_move(username)
|
||||
|
@ -1712,7 +1684,7 @@ if __name__ == '__main__':
|
|||
|
||||
game_moves = board.ply()
|
||||
|
||||
save_anotation(moving)
|
||||
save_anotation(moving, san_move)
|
||||
|
||||
update_moves(username, game_moves)
|
||||
|
||||
|
|
Loading…
Referencia en una nova incidència