2020-11-16 17:54:46 +01:00
import pdb
import sys
import os
import os . path
import re
import unidecode
from datetime import datetime , timedelta
from mastodon import Mastodon
2020-11-25 11:38:16 +01:00
from email . mime . multipart import MIMEMultipart
from email . mime . text import MIMEText
from email . mime . base import MIMEBase
from email import encoders
import smtplib
from smtplib import SMTPException , SMTPAuthenticationError , SMTPConnectError , SMTPRecipientsRefused
import socket
from socket import gaierror
2020-11-16 17:54:46 +01:00
import psycopg2
import chess
import chess . svg
from cairosvg import svg2png
def cleanhtml ( raw_html ) :
cleanr = re . compile ( ' <.*?> ' )
cleantext = re . sub ( cleanr , ' ' , raw_html )
return cleantext
def unescape ( s ) :
s = s . replace ( " ' " , " ' " )
return s
def get_bot_id ( ) :
###################################################################################################################################
# get bot_id from bot's username
try :
conn = None
conn = psycopg2 . connect ( database = mastodon_db , user = mastodon_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
cur . execute ( " select id from accounts where username = ( %s ) and domain is null " , ( bot_username , ) )
row = cur . fetchone ( )
if row != None :
bot_id = row [ 0 ]
cur . close ( )
return bot_id
except ( Exception , psycopg2 . DatabaseError ) as error :
print ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-17 17:59:03 +01:00
def get_user_domain ( account_id ) :
2020-11-16 17:54:46 +01:00
try :
conn = None
conn = psycopg2 . connect ( database = mastodon_db , user = mastodon_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
2020-11-17 17:59:03 +01:00
cur . execute ( " select username, domain from accounts where id=( %s ) " , ( account_id , ) )
2020-11-16 17:54:46 +01:00
row = cur . fetchone ( )
if row != None :
2020-11-17 17:59:03 +01:00
username = row [ 0 ]
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
domain = row [ 1 ]
2020-11-16 17:54:46 +01:00
cur . close ( )
2020-11-17 17:59:03 +01:00
return ( username , domain )
2020-11-16 17:54:46 +01:00
except ( Exception , psycopg2 . DatabaseError ) as error :
print ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-21 19:58:30 +01:00
def get_piece_name ( captured_piece ) :
if captured_piece == 1 :
2020-11-27 20:50:38 +01:00
piece_name = pawn_piece
2020-11-21 19:58:30 +01:00
if captured_piece == 2 :
2020-11-27 20:50:38 +01:00
piece_name = knight_piece
2020-11-21 19:58:30 +01:00
if captured_piece == 3 :
2020-11-27 20:50:38 +01:00
piece_name = bishop_piece
2020-11-21 19:58:30 +01:00
if captured_piece == 4 :
2020-11-27 20:50:38 +01:00
piece_name = rook_piece
2020-11-21 19:58:30 +01:00
if captured_piece == 5 :
2020-11-27 20:50:38 +01:00
piece_name = queen_piece
2020-11-21 19:58:30 +01:00
if captured_piece == 6 :
2020-11-27 20:50:38 +01:00
piece_name = king_piece
2020-11-21 19:58:30 +01:00
return piece_name
2020-11-23 18:22:08 +01:00
def get_moved_piece_name ( moved_piece ) :
if moved_piece == 1 :
2020-11-27 20:50:38 +01:00
moved_piece_name = pawn_piece_letter
2020-11-23 18:22:08 +01:00
if moved_piece == 2 :
2020-11-27 20:50:38 +01:00
moved_piece_name = knight_piece_letter
2020-11-23 18:22:08 +01:00
if moved_piece == 3 :
2020-11-27 20:50:38 +01:00
moved_piece_name = bishop_piece_letter
2020-11-23 18:22:08 +01:00
if moved_piece == 4 :
2020-11-27 20:50:38 +01:00
moved_piece_name = rook_piece_letter
2020-11-23 18:22:08 +01:00
if moved_piece == 5 :
2020-11-27 20:50:38 +01:00
moved_piece_name = queen_piece_letter
2020-11-23 18:22:08 +01:00
if moved_piece == 6 :
2020-11-27 20:50:38 +01:00
moved_piece_name = king_piece_letter
2020-11-23 18:22:08 +01:00
return moved_piece_name
2020-11-16 17:54:46 +01:00
def get_notification_data ( ) :
try :
2020-11-17 17:59:03 +01:00
account_id_lst = [ ]
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
status_id_lst = [ ]
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
text_lst = [ ]
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
visibility_lst = [ ]
2020-11-16 17:54:46 +01:00
2020-11-20 09:42:19 +01:00
url_lst = [ ]
2020-11-28 23:00:15 +01:00
search_text = [ search_end , search_move , search_new , search_games , search_send , search_help ]
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
conn = None
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
conn = psycopg2 . connect ( database = mastodon_db , user = mastodon_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
cur = conn . cursor ( )
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
i = 0
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
while i < len ( search_text ) :
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
like_text = " % " + search_text [ i ] + " % "
2020-11-16 17:54:46 +01:00
2020-11-20 09:42:19 +01:00
select_query = " select account_id, id, text, visibility, url, created_at from statuses where text like ( %s ) and created_at + interval ' 60 minutes ' > now() - interval ' 5 minutes ' "
2020-11-17 17:59:03 +01:00
select_query + = " and id=any (select status_id from mentions where account_id=( %s )) order by created_at asc "
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
cur . execute ( select_query , ( like_text , str ( bot_id ) ) )
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
rows = cur . fetchall ( )
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
for row in rows :
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
account_id_lst . append ( row [ 0 ] )
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
status_id_lst . append ( row [ 1 ] )
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
text_lst . append ( row [ 2 ] )
if row [ 3 ] == 0 :
visibility_lst . append ( ' public ' )
elif row [ 3 ] == 1 :
visibility_lst . append ( ' unlisted ' )
elif row [ 3 ] == 2 :
visibility_lst . append ( ' private ' )
elif row [ 3 ] == 3 :
visibility_lst . append ( ' direct ' )
2020-11-16 17:54:46 +01:00
2020-11-20 09:42:19 +01:00
url_lst . append ( row [ 4 ] )
2020-11-17 17:59:03 +01:00
i + = 1
2020-11-16 17:54:46 +01:00
cur . close ( )
2020-11-20 09:42:19 +01:00
return ( account_id_lst , status_id_lst , text_lst , visibility_lst , url_lst )
2020-11-16 17:54:46 +01:00
except ( Exception , psycopg2 . DatabaseError ) as error :
print ( error )
finally :
if conn is not None :
conn . close ( )
def update_replies ( status_id , username , now ) :
post_id = status_id
try :
conn = None
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
insert_sql = " insert into botreplies(status_id, query_user, status_created_at) values( %s , %s , %s ) ON CONFLICT DO NOTHING "
cur . execute ( insert_sql , ( post_id , username , now ) )
conn . commit ( )
cur . close ( )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
def check_replies ( status_id ) :
post_id = status_id
replied = False
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 status_id from botreplies where status_id=( %s ) " , ( post_id , ) )
row = cur . fetchone ( )
if row != None :
replied = True
else :
replied = False
cur . close ( )
return replied
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-19 14:13:23 +01:00
def current_games ( ) :
player1_name_lst = [ ]
player2_name_lst = [ ]
game_status_lst = [ ]
2020-11-20 09:42:19 +01:00
game_link_lst = [ ]
2020-11-20 15:06:14 +01:00
next_move_lst = [ ]
2020-11-19 14:13:23 +01:00
try :
conn = None
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
2020-11-20 15:06:14 +01:00
cur . execute ( " select white_user, black_user, chess_status, chess_link, next_move from games where not finished " )
2020-11-19 14:13:23 +01:00
rows = cur . fetchall ( )
for row in rows :
player1_name_lst . append ( row [ 0 ] )
if row [ 1 ] != None :
player2_name_lst . append ( row [ 1 ] )
else :
player2_name_lst . append ( " " )
game_status_lst . append ( row [ 2 ] )
2020-11-20 09:42:19 +01:00
game_link_lst . append ( row [ 3 ] )
2020-11-20 15:06:14 +01:00
next_move_lst . append ( row [ 4 ] )
2020-11-19 14:13:23 +01:00
cur . close ( )
2020-11-20 15:06:14 +01:00
return ( player1_name_lst , player2_name_lst , game_status_lst , game_link_lst , next_move_lst )
2020-11-19 14:13:23 +01:00
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-16 17:54:46 +01:00
def check_games ( ) :
game_id = ' '
white_user = ' '
black_user = ' '
on_going_game = ' '
waiting = False
playing_user = ' '
try :
conn = None
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
### check if there is an ongoing game
cur . execute ( " SELECT game_id, white_user, black_user, chess_game, waiting, next_move FROM games where not finished and white_user=( %s ) " , ( username , ) )
row = cur . fetchone ( )
if row == None :
cur . execute ( " SELECT game_id, white_user, black_user, chess_game, waiting, next_move FROM games where not finished and black_user=( %s ) " , ( username , ) )
row = cur . fetchone ( )
if row == None :
is_playing = False
else :
is_playing = True
game_id = row [ 0 ]
white_user = row [ 1 ]
if row [ 1 ] != None :
black_user = row [ 2 ]
else :
black_user = ' '
on_going_game = row [ 3 ]
waiting = row [ 4 ]
playing_user = row [ 5 ]
else :
is_playing = True
game_id = row [ 0 ]
white_user = row [ 1 ]
if row [ 2 ] != None :
black_user = row [ 2 ]
else :
black_user = ' '
on_going_game = row [ 3 ]
waiting = row [ 4 ]
playing_user = row [ 5 ]
cur . close ( )
return ( is_playing , game_id , white_user , black_user , on_going_game , waiting , playing_user )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-20 09:42:19 +01:00
def new_game ( toot_url ) :
2020-11-16 17:54:46 +01:00
try :
game_status = ' waiting '
waiting = True
board_game = board . fen ( )
conn = None
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
2020-11-20 09:42:19 +01:00
insert_query = ' INSERT INTO games(created_at, white_user, chess_game, chess_status, waiting, updated_at, chess_link) VALUES ( %s , %s , %s , %s , %s , %s , %s ) ON CONFLICT DO NOTHING '
2020-11-16 17:54:46 +01:00
2020-11-20 09:42:19 +01:00
cur . execute ( insert_query , ( now , username , board_game , game_status , waiting , now , toot_url ) )
2020-11-16 17:54:46 +01:00
insert_query = ' INSERT INTO stats(created_at, white_user) VALUES ( %s , %s ) ON CONFLICT DO NOTHING '
cur . execute ( insert_query , ( now , username , ) )
conn . commit ( )
cur . close ( )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-20 09:42:19 +01:00
def update_game ( board_game , toot_url ) :
2020-11-16 17:54:46 +01:00
try :
now = datetime . now ( )
conn = None
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
2020-11-20 09:42:19 +01:00
cur . execute ( " update games set chess_game=( %s ), chess_link=( %s ), updated_at=( %s ) where game_id=( %s ) " , ( board_game , toot_url , now , game_id , ) )
2020-11-16 17:54:46 +01:00
conn . commit ( )
cur . close ( )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-27 20:50:38 +01:00
def save_anotation ( moving ) :
2020-11-23 18:22:08 +01:00
2020-11-27 20:50:38 +01:00
if moving_piece != 1 :
2020-11-23 18:22:08 +01:00
2020-11-27 20:50:38 +01:00
square_index = chess . SQUARE_NAMES . index ( moving [ 2 : ] )
2020-11-23 18:22:08 +01:00
2020-11-27 20:50:38 +01:00
moved_piece = board . piece_type_at ( square_index )
moved_piece_name = get_moved_piece_name ( moved_piece )
else :
moved_piece_name = ' P '
if promoted == True :
moving = moving + ' q '
2020-11-23 18:22:08 +01:00
2020-11-25 11:38:16 +01:00
game_file = " anotations/ " + str ( game_id ) + " .txt "
2020-11-23 18:22:08 +01:00
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 + " + "
if bool ( board . turn == chess . BLACK ) == True :
if check != True and checkmate != True :
2020-11-27 20:50:38 +01:00
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 "
2020-11-23 18:22:08 +01:00
else :
line_data = str ( board . fullmove_number ) + " . " + moved_piece_name
else :
moved_piece_name = moved_piece_name . lower ( )
if check != True and checkmate != True :
2020-11-27 20:50:38 +01:00
if promoted != True :
2020-11-23 18:22:08 +01:00
2020-11-27 20:50:38 +01:00
line_data = " - " + moved_piece_name + moving [ 2 : ] + " \n "
else :
line_data = " - " + moved_piece_name + " =D "
2020-11-23 18:22:08 +01:00
else :
line_data = " - " + moved_piece_name + " \n "
if not os . path . isfile ( game_file ) :
2020-11-27 20:50:38 +01:00
file_header = game_name + ' : ' + str ( game_id ) + " \n " + white_user + " / " + black_user + " \n \n "
2020-11-23 18:22:08 +01:00
with open ( game_file , ' w+ ' ) as f :
f . write ( file_header )
with open ( game_file , ' a ' ) as f :
f . write ( line_data )
else :
with open ( game_file , ' a ' ) as f :
f . write ( line_data )
2020-11-25 11:38:16 +01:00
def get_email_address ( username ) :
try :
conn = None
conn = psycopg2 . connect ( database = mastodon_db , user = mastodon_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
cur . execute ( " select email from users where account_id = (select id from accounts where username = ( %s ) and domain is null) " , ( username , ) )
row = cur . fetchone ( )
2020-11-28 20:12:36 +01:00
if row != None :
username_email = row [ 0 ]
else :
username_email = None
2020-11-25 11:38:16 +01:00
cur . close ( )
return username_email
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
def send_anotation ( game_id ) :
emailed = False
game_found = False
username_email = get_email_address ( username )
2020-11-28 20:12:36 +01:00
if username_email == None :
return ( emailed , game_id , game_found )
2020-11-25 11:38:16 +01:00
# Create message object instance
msg = MIMEMultipart ( )
# Declare message elements
msg [ ' From ' ] = smtp_user_login
msg [ ' To ' ] = username_email
2020-11-27 20:50:38 +01:00
msg [ ' Subject ' ] = email_subject + game_id
2020-11-25 11:38:16 +01:00
# Attach the game anotation
file_to_attach = " anotations/ " + game_id + " .txt "
try :
attachment = open ( file_to_attach , ' rb ' )
game_found = True
except FileNotFoundError as not_found_error :
print ( not_found_error )
return ( emailed , game_id , game_found )
obj = MIMEBase ( ' application ' , ' octet-stream ' )
obj . set_payload ( attachment . read ( ) )
encoders . encode_base64 ( obj )
obj . add_header ( ' Content-Disposition ' , " attachment; filename= " + file_to_attach )
# Add the message body to the object instance
msg . attach ( obj )
try :
# Create the server connection
server = smtplib . SMTP ( smtp_host )
# Switch the connection over to TLS encryption
server . starttls ( )
# Authenticate with the server
server . login ( smtp_user_login , smtp_user_password )
# Send the message
server . sendmail ( msg [ ' From ' ] , msg [ ' To ' ] , msg . as_string ( ) )
# Disconnect
server . quit ( )
print ( " Successfully sent game anotations to %s " % msg [ ' To ' ] )
emailed = True
return ( emailed , game_id , game_found )
except SMTPAuthenticationError as auth_error :
print ( auth_error )
pass
return emailed
except socket . gaierror as socket_error :
print ( socket_error )
pass
return emailed
except SMTPRecipientsRefused as recip_error :
print ( recip_error )
pass
return emailed
2020-11-16 17:54:46 +01:00
def close_game ( ) :
now = datetime . now ( )
2020-11-19 14:13:23 +01:00
waiting = False
2020-11-16 17:54:46 +01:00
finished = True
try :
conn = None
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
2020-11-19 14:13:23 +01:00
cur . execute ( " update games set waiting=( %s ), finished=( %s ), updated_at=( %s ) where game_id=( %s ) " , ( waiting , finished , now , game_id ) )
2020-11-16 17:54:46 +01:00
cur . execute ( " update stats set winner=( %s ), finished=( %s ), updated_at=( %s ) where game_id=( %s ) " , ( username , finished , now , game_id ) )
conn . commit ( )
cur . close ( )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-18 16:25:13 +01:00
def get_stats ( player ) :
played_games = 0
wins = 0
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 count(*) from stats where white_user = ( %s ) or black_user = ( %s ) and finished " , ( player , player ) )
row = cur . fetchone ( )
if row != None :
played_games = row [ 0 ]
cur . execute ( " select count(*) from stats where winner = ( %s ) and finished " , ( player , ) )
row = cur . fetchone ( )
if row != None :
wins = row [ 0 ]
cur . close ( )
return ( played_games , wins )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-16 17:54:46 +01:00
def waiting_games ( ) :
try :
game_id = ' '
game_waiting = False
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 game_id from games where waiting order by game_id desc limit 1 " )
row = cur . fetchone ( )
if row != None :
game_id = row [ 0 ]
game_waiting = True
cur . close ( )
return ( game_id , game_waiting )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
def join_player ( ) :
try :
now = datetime . now ( )
game_status = ' waiting '
waiting = True
moves = 0
conn = None
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
cur . execute ( " update games set black_user=( %s ), chess_status= ' playing ' , waiting= ' f ' , updated_at=( %s ), moves=( %s ) where game_id=( %s ) " , ( username , now , moves , game_id , ) )
cur . execute ( " update stats set black_user=( %s ), updated_at=( %s ) where game_id=( %s ) " , ( username , now , game_id , ) )
conn . commit ( )
cur . execute ( " select white_user, chess_game from games where game_id=( %s ) " , ( game_id , ) )
row = cur . fetchone ( )
if row != None :
white_user = row [ 0 ]
chess_game = row [ 1 ]
cur . execute ( " update games set next_move=( %s ), updated_at=( %s ) where game_id=( %s ) " , ( white_user , now , game_id , ) )
conn . commit ( )
cur . close ( )
game_status = ' playing '
return ( game_status , white_user , chess_game )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
def update_moves ( username , game_moves ) :
try :
now = datetime . now ( )
conn = None
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
cur . execute ( " update games set next_move=( %s ), last_move=( %s ), moves=( %s ), updated_at=( %s ) where game_id=( %s ) " , ( playing_user , username , game_moves , now , game_id , ) )
conn . commit ( )
cur . close ( )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
def next_move ( playing_user ) :
try :
now = datetime . now ( )
waiting = True
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, last_move, moves from games where game_id=( %s ) " , ( game_id , ) )
row = cur . fetchone ( )
if row != None :
white_user = row [ 0 ]
black_user = row [ 1 ]
last_move = row [ 2 ]
moves = row [ 3 ]
if last_move != None :
if playing_user == white_user :
playing_user = black_user
elif playing_user == black_user :
playing_user = white_user
else :
last_move = white_user
cur . execute ( " update games set next_move=( %s ), updated_at=( %s ) where game_id=( %s ) " , ( playing_user , now , game_id , ) )
conn . commit ( )
cur . close ( )
return playing_user
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
2020-11-28 23:00:15 +01:00
def toot_help ( ) :
help_text = ' @ ' + username + ' ' + search_help + ' : \n '
help_text + = ' \n '
help_text + = ' @ ' + bot_username + ' ' + start_or_join_a_new_game + ' \n '
help_text + = ' \n '
help_text + = ' @ ' + bot_username + ' ' + move_a_piece + ' \n '
help_text + = ' \n '
help_text + = ' @ ' + bot_username + ' ' + leave_a_game + ' \n '
help_text + = ' \n '
help_text + = ' @ ' + bot_username + ' ' + list_games + ' \n '
help_text + = ' \n '
help_text + = ' @ ' + bot_username + ' ' + get_a_game_anotation + ' \n '
help_text + = ' \n '
help_text + = ' @ ' + bot_username + ' ' + show_help + ' \n '
return help_text
2020-11-16 17:54:46 +01:00
def replying ( ) :
2020-11-17 16:29:26 +01:00
reply = False
2020-11-16 17:54:46 +01:00
moving = ' '
content = cleanhtml ( text )
content = unescape ( content )
try :
start = content . index ( " @ " )
end = content . index ( " " )
if len ( content ) > end :
content = content [ 0 : start : ] + content [ end + 1 : : ]
neteja = content . count ( ' @ ' )
i = 0
while i < neteja :
start = content . rfind ( " @ " )
end = len ( content )
content = content [ 0 : start : ] + content [ end + 1 : : ]
i + = 1
question = content . lower ( )
query_word = question
query_word_length = len ( query_word )
if unidecode . unidecode ( question ) [ 0 : query_word_length ] == query_word :
2020-11-27 20:50:38 +01:00
if query_word == search_new :
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
reply = True
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
elif query_word [ : search_move_slicing ] == search_move :
2020-11-16 17:54:46 +01:00
2020-11-28 18:07:52 +01:00
moving = query_word [ moving_slicing : query_word_length ] . replace ( " " , " " )
2020-11-27 20:50:38 +01:00
reply = True
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
elif query_word == search_end :
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
reply = True
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
elif query_word == search_games :
2020-11-19 14:13:23 +01:00
2020-11-27 20:50:38 +01:00
reply = True
2020-11-19 14:13:23 +01:00
2020-11-27 20:50:38 +01:00
elif query_word [ : search_send_slicing ] == search_send :
2020-11-25 11:38:16 +01:00
2020-11-27 20:50:38 +01:00
reply = True
2020-11-25 11:38:16 +01:00
2020-11-28 23:00:15 +01:00
elif query_word == search_help :
reply = True
2020-11-27 20:50:38 +01:00
else :
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
reply = False
2020-11-16 17:54:46 +01:00
return ( reply , query_word , moving )
except ValueError as v_error :
print ( v_error )
2020-11-27 20:50:38 +01:00
def load_strings ( bot_lang ) :
search_end = get_parameter ( " search_end " , language_filepath )
search_move = get_parameter ( " search_move " , language_filepath )
search_new = get_parameter ( " search_new " , language_filepath )
search_games = get_parameter ( " search_games " , language_filepath )
search_send = get_parameter ( " search_send " , language_filepath )
2020-11-28 23:00:15 +01:00
search_help = get_parameter ( " search_help " , language_filepath )
2020-11-27 20:50:38 +01:00
new_game_started = get_parameter ( " new_game_started " , language_filepath )
playing_with = get_parameter ( " playing_with " , language_filepath )
your_turn = get_parameter ( " your_turn " , language_filepath )
game_name = get_parameter ( " game_name " , language_filepath )
chess_hashtag = get_parameter ( " chess_hashtag " , language_filepath )
send_error = get_parameter ( " send_error " , language_filepath )
2020-11-28 23:00:15 +01:00
return ( search_end , search_move , search_new , search_games , search_send , search_help , new_game_started , playing_with , your_turn , game_name , chess_hashtag , send_error )
2020-11-27 20:50:38 +01:00
def load_strings1 ( bot_lang ) :
game_number_anotations = get_parameter ( " game_number_anotations " , language_filepath )
anotations_sent = get_parameter ( " anotations_sent " , language_filepath )
game_no_exists = get_parameter ( " game_no_exists " , language_filepath )
it_not_exists = get_parameter ( " it_not_exists " , language_filepath )
game_already_started = get_parameter ( " game_already_started " , language_filepath )
2020-11-28 20:12:36 +01:00
cant_send_to_fediverse_account = get_parameter ( " cant_send_to_fediverse_account " , language_filepath )
2020-11-27 20:50:38 +01:00
wait_other_player = get_parameter ( " wait_other_player " , language_filepath )
is_not_legal_move = get_parameter ( " is_not_legal_move " , language_filepath )
check_done = get_parameter ( " check_done " , language_filepath )
check_mate = get_parameter ( " check_mate " , language_filepath )
2020-11-28 20:12:36 +01:00
return ( game_number_anotations , anotations_sent , game_no_exists , cant_send_to_fediverse_account , it_not_exists , game_already_started , wait_other_player , is_not_legal_move , check_done , check_mate )
2020-11-27 20:50:38 +01:00
def load_strings2 ( bot_lang ) :
check_mate_movements = get_parameter ( " check_mate_movements " , language_filepath )
the_winner_is = get_parameter ( " the_winner_is " , language_filepath )
well_done = get_parameter ( " well_done " , language_filepath )
winned_games = get_parameter ( " winned_games " , language_filepath )
wins_of_many = get_parameter ( " wins_of_many " , language_filepath )
lost_piece = get_parameter ( " lost_piece " , language_filepath )
not_legal_move_str = get_parameter ( " not_legal_move_str " , language_filepath )
player_leave_game = get_parameter ( " player_leave_game " , language_filepath )
return ( check_mate_movements , the_winner_is , well_done , winned_games , wins_of_many , lost_piece , not_legal_move_str , player_leave_game )
def load_strings3 ( bot_lang ) :
leave_waiting_game = get_parameter ( " leave_waiting_game " , language_filepath )
started_games = get_parameter ( " started_games " , language_filepath )
game_is_waiting = get_parameter ( " game_is_waiting " , language_filepath )
game_is_on_going = get_parameter ( " game_is_on_going " , language_filepath )
no_on_going_games = get_parameter ( " no_on_going_games " , language_filepath )
is_not_your_turn = get_parameter ( " is_not_your_turn " , language_filepath )
is_the_turn_of = get_parameter ( " is_the_turn_of " , language_filepath )
return ( leave_waiting_game , started_games , game_is_waiting , game_is_on_going , no_on_going_games , is_not_your_turn , is_the_turn_of )
def load_strings4 ( bot_lang ) :
pawn_piece = get_parameter ( " pawn_piece " , language_filepath )
knight_piece = get_parameter ( " knight_piece " , language_filepath )
bishop_piece = get_parameter ( " bishop_piece " , language_filepath )
rook_piece = get_parameter ( " rook_piece " , language_filepath )
queen_piece = get_parameter ( " queen_piece " , language_filepath )
king_piece = get_parameter ( " king_piece " , language_filepath )
return ( pawn_piece , knight_piece , bishop_piece , rook_piece , queen_piece , king_piece )
def load_strings5 ( bot_lang ) :
pawn_piece_letter = get_parameter ( " pawn_piece_letter " , language_filepath )
knight_piece_letter = get_parameter ( " knight_piece_letter " , language_filepath )
bishop_piece_letter = get_parameter ( " bishop_piece_letter " , language_filepath )
rook_piece_letter = get_parameter ( " rook_piece_letter " , language_filepath )
queen_piece_letter = get_parameter ( " queen_piece_letter " , language_filepath )
king_piece_letter = get_parameter ( " king_piece_letter " , language_filepath )
email_subject = get_parameter ( " email_subject " , language_filepath )
return ( pawn_piece_letter , knight_piece_letter , bishop_piece_letter , rook_piece_letter , queen_piece_letter , king_piece_letter , email_subject )
2020-11-28 23:00:15 +01:00
def load_strings6 ( bot_lang ) :
start_or_join_a_new_game = get_parameter ( " start_or_join_a_new_game " , language_filepath )
move_a_piece = get_parameter ( " move_a_piece " , language_filepath )
leave_a_game = get_parameter ( " leave_a_game " , language_filepath )
list_games = get_parameter ( " list_games " , language_filepath )
get_a_game_anotation = get_parameter ( " get_a_game_anotation " , language_filepath )
show_help = get_parameter ( " show_help " , language_filepath )
return ( start_or_join_a_new_game , move_a_piece , leave_a_game , list_games , get_a_game_anotation , show_help )
2020-11-16 17:54:46 +01:00
def mastodon ( ) :
# Load secrets from secrets file
secrets_filepath = " secrets/secrets.txt "
uc_client_id = get_parameter ( " uc_client_id " , secrets_filepath )
uc_client_secret = get_parameter ( " uc_client_secret " , secrets_filepath )
uc_access_token = get_parameter ( " uc_access_token " , secrets_filepath )
# Load configuration from config file
config_filepath = " config/config.txt "
mastodon_hostname = get_parameter ( " mastodon_hostname " , config_filepath )
bot_username = get_parameter ( " bot_username " , config_filepath )
# Initialise Mastodon API
mastodon = Mastodon (
client_id = uc_client_id ,
client_secret = uc_client_secret ,
access_token = uc_access_token ,
api_base_url = ' https:// ' + mastodon_hostname ,
)
# Initialise access headers
headers = { ' Authorization ' : ' Bearer %s ' % uc_access_token }
return ( mastodon , mastodon_hostname , bot_username )
def db_config ( ) :
# Load db configuration from config file
config_filepath = " config/db_config.txt "
mastodon_db = get_parameter ( " mastodon_db " , config_filepath )
mastodon_db_user = get_parameter ( " mastodon_db_user " , config_filepath )
chess_db = get_parameter ( " chess_db " , config_filepath )
chess_db_user = get_parameter ( " chess_db_user " , config_filepath )
return ( mastodon_db , mastodon_db_user , chess_db , chess_db_user )
2020-11-25 11:38:16 +01:00
def smtp_config ( ) :
smtp_filepath = " config/smtp_config.txt "
smtp_host = get_parameter ( " smtp_host " , smtp_filepath )
smtp_user_login = get_parameter ( " smtp_user_login " , smtp_filepath )
smtp_user_password = get_parameter ( " smtp_user_password " , smtp_filepath )
return ( smtp_host , smtp_user_login , smtp_user_password )
2020-11-16 17:54:46 +01:00
def get_parameter ( parameter , file_path ) :
if not os . path . isfile ( file_path ) :
print ( " File %s not found, exiting. " % file_path )
sys . exit ( 0 )
with open ( file_path ) as f :
for line in f :
if line . startswith ( parameter ) :
return line . replace ( parameter + " : " , " " ) . strip ( )
print ( file_path + " Missing parameter %s " % parameter )
sys . exit ( 0 )
def create_dir ( ) :
if not os . path . exists ( ' games ' ) :
os . makedirs ( ' games ' )
2020-11-23 18:22:08 +01:00
if not os . path . exists ( ' anotations ' ) :
os . makedirs ( ' anotations ' )
2020-11-16 17:54:46 +01:00
def usage ( ) :
2020-11-28 18:07:52 +01:00
print ( ' usage: python ' + sys . argv [ 0 ] + ' --play ' + ' --en ' )
2020-11-16 17:54:46 +01:00
###############################################################################
# main
if __name__ == ' __main__ ' :
# usage modes
if len ( sys . argv ) == 1 :
usage ( )
2020-11-27 20:50:38 +01:00
elif len ( sys . argv ) > = 2 :
2020-11-16 17:54:46 +01:00
if sys . argv [ 1 ] == ' --play ' :
2020-11-27 20:50:38 +01:00
if len ( sys . argv ) == 3 :
2020-11-28 18:07:52 +01:00
if sys . argv [ 2 ] == ' --en ' :
2020-11-27 20:50:38 +01:00
2020-11-28 18:07:52 +01:00
bot_lang = ' en '
elif sys . argv [ 2 ] == ' --es ' :
bot_lang = ' es '
2020-11-27 20:50:38 +01:00
else :
bot_lang = ' ca '
2020-11-28 18:07:52 +01:00
if bot_lang == ' ca ' :
2020-11-27 20:50:38 +01:00
2020-11-28 18:07:52 +01:00
language_filepath = ' locales/ca.txt '
2020-11-27 20:50:38 +01:00
2020-11-28 18:07:52 +01:00
elif bot_lang == ' en ' :
2020-11-27 20:50:38 +01:00
2020-11-28 18:07:52 +01:00
language_filepath = ' locales/en.txt '
2020-11-27 20:50:38 +01:00
2020-11-28 18:07:52 +01:00
elif bot_lang == ' es ' :
2020-11-27 20:50:38 +01:00
2020-11-28 18:07:52 +01:00
language_filepath = ' locales/es.txt '
2020-11-27 20:50:38 +01:00
2020-11-28 18:07:52 +01:00
else :
2020-11-27 20:50:38 +01:00
2020-11-28 18:07:52 +01:00
print ( " \n Only ' ca ' , ' es ' and ' en ' languages are supported. \n " )
sys . exit ( 0 )
2020-11-27 20:50:38 +01:00
if bot_lang == ' ca ' :
search_move_slicing = 3
2020-11-28 18:07:52 +01:00
moving_slicing = 3
2020-11-27 20:50:38 +01:00
search_send_slicing = 5
send_game_slicing = 6
2020-11-28 18:07:52 +01:00
elif bot_lang == ' en ' :
2020-11-27 20:50:38 +01:00
search_move_slicing = 4
2020-11-28 18:07:52 +01:00
moving_slicing = 4
2020-11-27 20:50:38 +01:00
search_send_slicing = 4
send_game_slicing = 5
2020-11-28 18:07:52 +01:00
elif bot_lang == ' es ' :
search_move_slicing = 5
moving_slicing = 5
search_send_slicing = 5
send_game_slicing = 6
2020-11-28 23:00:15 +01:00
search_end , search_move , search_new , search_games , search_send , search_help , new_game_started , playing_with , your_turn , game_name , chess_hashtag , send_error = load_strings ( bot_lang )
2020-11-27 20:50:38 +01:00
2020-11-28 20:12:36 +01:00
game_number_anotations , anotations_sent , game_no_exists , cant_send_to_fediverse_account , it_not_exists , game_already_started , wait_other_player , is_not_legal_move , check_done , check_mate = load_strings1 ( bot_lang )
2020-11-27 20:50:38 +01:00
check_mate_movements , the_winner_is , well_done , winned_games , wins_of_many , lost_piece , not_legal_move_str , player_leave_game = load_strings2 ( bot_lang )
leave_waiting_game , started_games , game_is_waiting , game_is_on_going , no_on_going_games , is_not_your_turn , is_the_turn_of = load_strings3 ( bot_lang )
pawn_piece , knight_piece , bishop_piece , rook_piece , queen_piece , king_piece = load_strings4 ( bot_lang )
pawn_piece_letter , knight_piece_letter , bishop_piece_letter , rook_piece_letter , queen_piece_letter , king_piece_letter , email_subject = load_strings5 ( bot_lang )
2020-11-28 23:00:15 +01:00
start_or_join_a_new_game , move_a_piece , leave_a_game , list_games , get_a_game_anotation , show_help = load_strings6 ( bot_lang )
2020-11-16 17:54:46 +01:00
mastodon , mastodon_hostname , bot_username = mastodon ( )
mastodon_db , mastodon_db_user , chess_db , chess_db_user = db_config ( )
2020-11-25 11:38:16 +01:00
smtp_host , smtp_user_login , smtp_user_password = smtp_config ( )
2020-11-16 17:54:46 +01:00
now = datetime . now ( )
create_dir ( )
bot_id = get_bot_id ( )
2020-11-20 09:42:19 +01:00
account_id_lst , status_id_lst , text_lst , visibility_lst , url_lst = get_notification_data ( )
2020-11-16 17:54:46 +01:00
i = 0
2020-11-17 17:59:03 +01:00
while i < len ( account_id_lst ) :
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
account_id = account_id_lst [ i ]
2020-11-16 17:54:46 +01:00
2020-11-17 17:59:03 +01:00
username , domain = get_user_domain ( account_id )
2020-11-16 17:54:46 +01:00
2020-11-28 20:12:36 +01:00
if domain != None :
username = username + ' @ ' + domain
2020-11-17 17:59:03 +01:00
status_id = status_id_lst [ i ]
2020-11-16 17:54:46 +01:00
replied = check_replies ( status_id )
2020-11-28 20:12:36 +01:00
if replied == True : #or domain != None:
2020-11-16 17:54:46 +01:00
i + = 1
continue
2020-11-28 20:12:36 +01:00
#if domain != None:
2020-11-16 17:54:46 +01:00
2020-11-28 20:12:36 +01:00
#update_replies(username, status_id)
2020-11-16 17:54:46 +01:00
2020-11-28 20:12:36 +01:00
#i += 1
2020-11-16 17:54:46 +01:00
# listen them or not
2020-11-17 17:59:03 +01:00
text = text_lst [ i ]
2020-11-16 17:54:46 +01:00
reply , query_word , moving = replying ( )
2020-11-17 17:59:03 +01:00
visibility = visibility_lst [ i ]
2020-11-20 09:42:19 +01:00
status_url = url_lst [ i ]
2020-11-27 20:50:38 +01:00
if query_word != search_games :
2020-11-16 17:54:46 +01:00
2020-11-19 14:13:23 +01:00
is_playing , game_id , white_user , black_user , on_going_game , waiting , playing_user = check_games ( )
if game_id == ' ' :
game_id , game_waiting = waiting_games ( )
else :
2020-11-16 17:54:46 +01:00
2020-11-19 14:13:23 +01:00
is_playing = True
2020-11-16 17:54:46 +01:00
if reply == True and is_playing == False :
2020-11-27 20:50:38 +01:00
if query_word == search_new and not game_waiting :
2020-11-16 17:54:46 +01:00
board = chess . Board ( )
svgfile = chess . svg . board ( board = board )
board_file = ' games/ ' + str ( game_id ) + ' _board.png '
svg2png ( bytestring = svgfile , write_to = board_file )
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + new_game_started + ' \n '
2020-11-16 17:54:46 +01:00
toot_text + = ' \n '
image_id = mastodon . media_post ( board_file , " image/png " ) . id
2020-11-20 09:42:19 +01:00
toot_id = mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility , media_ids = { image_id } )
2020-11-16 17:54:46 +01:00
2020-11-20 17:58:53 +01:00
toot_url = toot_id . uri
2020-11-20 09:42:19 +01:00
new_game ( toot_url )
2020-11-16 17:54:46 +01:00
update_replies ( status_id , username , now )
2020-11-27 20:50:38 +01:00
elif query_word == search_new and game_waiting :
2020-11-16 17:54:46 +01:00
game_status , white_user , chess_game = join_player ( )
playing_user = white_user
next_move ( username )
board = chess . Board ( chess_game )
svgfile = chess . svg . board ( board = board )
board_file = ' games/ ' + str ( game_id ) + ' _board.png '
svg2png ( bytestring = svgfile , write_to = board_file )
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + playing_with + ' ' + white_user + " \n "
2020-11-16 17:54:46 +01:00
toot_text + = ' \n '
2020-11-27 20:50:38 +01:00
toot_text + = ' @ ' + white_user + ' : ' + your_turn + " \n "
2020-11-16 17:54:46 +01:00
toot_text + = ' \n '
2020-11-27 20:50:38 +01:00
toot_text + = game_name + ' : ' + str ( game_id ) + ' ' + chess_hashtag + ' \n '
2020-11-16 17:54:46 +01:00
image_id = mastodon . media_post ( board_file , " image/png " ) . id
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility , media_ids = { image_id } )
game_moves = board . ply ( )
update_moves ( username , game_moves )
update_replies ( status_id , username , now )
2020-11-27 20:50:38 +01:00
elif query_word [ : search_send_slicing ] == search_send :
2020-11-25 11:38:16 +01:00
query_word_length = len ( query_word )
2020-11-27 20:50:38 +01:00
send_game = query_word [ send_game_slicing : query_word_length ] . replace ( ' ' , ' ' )
2020-11-25 11:38:16 +01:00
emailed , game_id , game_found = send_anotation ( send_game )
if emailed == False and game_found == True :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + send_error
2020-11-25 11:38:16 +01:00
elif emailed == True and game_found == True :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + game_number_anotations + str ( game_id ) + ' ' + anotations_sent
2020-11-25 11:38:16 +01:00
elif emailed == False and game_found == False :
2020-11-28 20:12:36 +01:00
if domain != None :
toot_text = ' @ ' + username + ' ' + cant_send_to_fediverse_account
else :
toot_text = ' @ ' + username + ' ' + game_no_exists + str ( game_id ) + ' ' + it_not_exists
2020-11-25 11:38:16 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
update_replies ( status_id , username , now )
2020-11-28 23:00:15 +01:00
elif query_word == search_help :
help_text = toot_help ( )
mastodon . status_post ( help_text , in_reply_to_id = status_id , visibility = visibility )
update_replies ( status_id , username , now )
2020-11-19 14:13:23 +01:00
else :
update_replies ( status_id , username , now )
2020-11-16 17:54:46 +01:00
elif reply and is_playing :
2020-11-27 20:50:38 +01:00
if query_word == search_new :
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + game_already_started + ' \n '
2020-11-16 17:54:46 +01:00
if black_user != ' ' :
toot_text + = ' @ ' + white_user + ' / ' + ' @ ' + black_user + ' \n '
else :
2020-11-27 20:50:38 +01:00
toot_text + = wait_other_player + ' \n '
2020-11-16 17:54:46 +01:00
toot_text + = ' \n '
2020-11-27 20:50:38 +01:00
toot_text + = game_name + ' : ' + str ( game_id ) + ' ' + chess_hashtag + ' \n '
2020-11-16 17:54:46 +01:00
board = chess . Board ( on_going_game )
svgfile = chess . svg . board ( board = board )
board_file = ' games/ ' + str ( game_id ) + ' _board.png '
svg2png ( bytestring = svgfile , write_to = board_file )
image_id = mastodon . media_post ( board_file , " image/png " ) . id
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility , media_ids = { image_id } )
update_replies ( status_id , username , now )
2020-11-27 20:50:38 +01:00
elif query_word [ : search_move_slicing ] == search_move and playing_user == username :
2020-11-16 17:54:46 +01:00
board = chess . Board ( on_going_game )
2020-11-27 20:50:38 +01:00
promoted = False
2020-11-16 17:54:46 +01:00
try :
2020-11-27 20:50:38 +01:00
piece_square_index = chess . SQUARE_NAMES . index ( moving [ : 2 ] )
moving_piece = board . piece_type_at ( piece_square_index )
if moving_piece == 1 :
square_index = chess . SQUARE_NAMES . index ( moving [ 2 : 4 ] )
if bool ( board . turn == chess . WHITE ) == True :
square_rank_trigger = 7
elif bool ( board . turn == chess . BLACK ) == True :
square_rank_trigger = 0
if chess . square_rank ( square_index ) == square_rank_trigger :
promoted = True
if len ( moving ) == 4 :
moving = moving + ' q '
not_legal_move = chess . Move . from_uci ( moving ) not in board . legal_moves
else :
not_legal_move = chess . Move . from_uci ( moving ) not in board . legal_moves
if not_legal_move :
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' : ' + moving + ' ' + is_not_legal_move + ' \n '
2020-11-16 17:54:46 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
update_replies ( status_id , username , now )
else :
2020-11-18 12:54:49 +01:00
check = False
2020-11-16 17:54:46 +01:00
playing_user = next_move ( username )
2020-11-21 19:58:30 +01:00
if bool ( board . is_capture ( chess . Move . from_uci ( moving ) ) ) :
capture = True
2020-11-27 20:50:38 +01:00
square_capture_index = chess . SQUARE_NAMES . index ( moving [ 2 : 4 ] )
2020-11-21 19:58:30 +01:00
captured_piece = board . piece_type_at ( square_capture_index )
piece_name = get_piece_name ( captured_piece )
else :
capture = False
2020-11-16 17:54:46 +01:00
board . push ( chess . Move . from_uci ( moving ) )
2020-11-23 11:42:04 +01:00
if bool ( board . is_check ( ) ) :
2020-11-16 17:54:46 +01:00
2020-11-18 12:54:49 +01:00
if username == white_user :
king_square = board . king ( chess . BLACK )
check = True
else :
king_square = board . king ( chess . WHITE )
check = True
2020-11-16 17:54:46 +01:00
if board . is_game_over ( ) == True :
2020-11-20 17:58:53 +01:00
game_moves = board . ply ( )
2020-11-23 11:42:04 +01:00
close_game ( )
checkmate = True
else :
checkmate = False
if check == True and checkmate == False :
2020-11-27 20:50:38 +01:00
toot_text = " @ " + playing_user + " " + username + ' ' + check_done + ' \n '
2020-11-23 11:42:04 +01:00
elif check == True and checkmate == True :
2020-11-27 20:50:38 +01:00
toot_text = ' \n ' + check_mate + ' ' + str ( game_moves ) + ' ' + check_mate_movements + ' \n \n ' + the_winner_is + ' ' + " @ " + username + ' \n '
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
toot_text + = " \n @ " + playing_user + ' : ' + well_done + " \n "
2020-11-16 17:54:46 +01:00
2020-11-27 20:50:38 +01:00
toot_text + = ' \n ' + winned_games + " \n "
2020-11-16 17:54:46 +01:00
2020-11-18 16:25:13 +01:00
played_games , wins = get_stats ( username )
2020-11-27 20:50:38 +01:00
toot_text + = username + ' : ' + str ( wins ) + ' ' + wins_of_many + ' ' + str ( played_games ) + " \n "
2020-11-18 16:25:13 +01:00
played_games , wins = get_stats ( playing_user )
2020-11-27 20:50:38 +01:00
toot_text + = playing_user + ' : ' + str ( wins ) + ' ' + wins_of_many + ' ' + str ( played_games ) + " \n "
2020-11-18 16:25:13 +01:00
2020-11-16 17:54:46 +01:00
else :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + playing_user + ' ' + your_turn + ' \n '
2020-11-16 17:54:46 +01:00
2020-11-23 11:42:04 +01:00
if capture == True and checkmate == False :
2020-11-21 19:58:30 +01:00
2020-11-27 20:50:38 +01:00
toot_text + = ' \n ' + lost_piece + ' ' + piece_name + ' ! \n '
2020-11-21 19:58:30 +01:00
2020-11-27 20:50:38 +01:00
toot_text + = ' \n ' + game_name + ' : ' + str ( game_id ) + ' ' + chess_hashtag + ' \n '
2020-11-16 17:54:46 +01:00
if username == white_user :
2020-11-18 12:54:49 +01:00
if check == True :
svgfile = chess . svg . board ( board = board , orientation = chess . BLACK , lastmove = chess . Move . from_uci ( moving ) , check = board . king ( chess . BLACK ) )
else :
svgfile = chess . svg . board ( board = board , orientation = chess . BLACK , lastmove = chess . Move . from_uci ( moving ) )
2020-11-16 17:54:46 +01:00
else :
2020-11-18 12:54:49 +01:00
if check == True :
svgfile = chess . svg . board ( board = board , orientation = chess . WHITE , lastmove = chess . Move . from_uci ( moving ) , check = board . king ( chess . WHITE ) )
else :
svgfile = chess . svg . board ( board = board , orientation = chess . WHITE , lastmove = chess . Move . from_uci ( moving ) )
2020-11-16 17:54:46 +01:00
board_file = ' games/ ' + str ( game_id ) + ' _board.png '
svg2png ( bytestring = svgfile , write_to = board_file )
image_id = mastodon . media_post ( board_file , " image/png " ) . id
2020-11-20 09:42:19 +01:00
toot_id = mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility , media_ids = { image_id } )
2020-11-20 17:58:53 +01:00
toot_url = toot_id . uri
2020-11-16 17:54:46 +01:00
board_game = board . fen ( )
2020-11-20 09:42:19 +01:00
update_game ( board_game , toot_url )
2020-11-16 17:54:46 +01:00
game_moves = board . ply ( )
2020-11-27 20:50:38 +01:00
save_anotation ( moving )
2020-11-23 18:22:08 +01:00
2020-11-16 17:54:46 +01:00
update_moves ( username , game_moves )
update_replies ( status_id , username , now )
except ValueError as v_error :
print ( v_error )
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + not_legal_move_str + moving + ' !?) \n '
2020-11-20 13:26:54 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
update_replies ( status_id , username , now )
2020-11-16 17:54:46 +01:00
pass
except AssertionError as a_error :
print ( a_error )
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + not_legal_move_str + moving + ' !?) \n '
2020-11-16 17:54:46 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
update_replies ( status_id , username , now )
pass
2020-11-27 20:50:38 +01:00
elif query_word == search_end :
2020-11-16 17:54:46 +01:00
if black_user != ' ' :
if username == white_user :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + player_leave_game + ' ' + ' @ ' + black_user
2020-11-16 17:54:46 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
close_game ( )
update_replies ( status_id , username , now )
i + = 1
continue
else :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + player_leave_game + ' ' + white_user
2020-11-16 17:54:46 +01:00
2020-11-24 22:33:26 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
2020-11-16 17:54:46 +01:00
close_game ( )
update_replies ( status_id , username , now )
i + = 1
continue
else :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + leave_waiting_game
2020-11-16 17:54:46 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
close_game ( )
update_replies ( status_id , username , now )
i + = 1
continue
2020-11-27 20:50:38 +01:00
elif query_word == search_games :
2020-11-19 14:13:23 +01:00
2020-11-20 15:06:14 +01:00
player1_name_lst , player2_name_lst , game_status_lst , game_link_lst , next_move_lst = current_games ( )
2020-11-19 14:13:23 +01:00
if len ( player1_name_lst ) > 0 :
2020-11-27 20:50:38 +01:00
toot_text = " @ " + username + ' ' + started_games + " \n "
2020-11-19 14:13:23 +01:00
i = 0
while i < len ( player1_name_lst ) :
if game_status_lst [ i ] == ' waiting ' :
2020-11-27 20:50:38 +01:00
toot_text + = ' \n ' + player1_name_lst [ i ] + ' / ' + player2_name_lst [ i ] + ' ' + game_is_waiting + " \n "
2020-11-19 14:13:23 +01:00
else :
2020-11-20 15:06:14 +01:00
if next_move_lst [ i ] == player1_name_lst [ i ] :
2020-11-27 20:50:38 +01:00
toot_text + = ' \n * ' + player1_name_lst [ i ] + ' / ' + player2_name_lst [ i ] + ' ' + game_is_on_going + ' \n '
2020-11-20 15:06:14 +01:00
else :
2020-11-27 20:50:38 +01:00
toot_text + = ' \n ' + player1_name_lst [ i ] + ' / * ' + player2_name_lst [ i ] + ' ' + game_is_on_going + ' \n '
2020-11-19 14:13:23 +01:00
2020-11-20 09:42:19 +01:00
if game_link_lst [ i ] != None :
toot_text + = str ( game_link_lst [ i ] ) + " \n "
2020-11-19 14:13:23 +01:00
i + = 1
2020-11-20 09:42:19 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
2020-11-19 14:13:23 +01:00
else :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + no_on_going_games + ' \n '
2020-11-19 14:13:23 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
update_replies ( status_id , username , now )
2020-11-27 20:50:38 +01:00
elif query_word [ : search_send_slicing ] == search_send :
2020-11-25 13:15:43 +01:00
query_word_length = len ( query_word )
2020-11-28 18:07:52 +01:00
send_game = query_word [ search_send_slicing : query_word_length ] . replace ( ' ' , ' ' )
2020-11-25 13:15:43 +01:00
emailed , game_id , game_found = send_anotation ( send_game )
if emailed == False and game_found == True :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + send_error
2020-11-25 13:15:43 +01:00
elif emailed == True and game_found == True :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + game_number_anotations + str ( game_id ) + ' ' + anotations_sent
2020-11-25 13:15:43 +01:00
elif emailed == False and game_found == False :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + game_no_exists + str ( game_id ) + ' ' + it_not_exists
2020-11-25 13:15:43 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
update_replies ( status_id , username , now )
2020-11-28 23:00:15 +01:00
elif query_word == search_help :
help_text = toot_help ( )
mastodon . status_post ( help_text , in_reply_to_id = status_id , visibility = visibility )
update_replies ( status_id , username , now )
2020-11-16 17:54:46 +01:00
else :
if playing_user == None :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + is_not_your_turn + ' \n '
2020-11-16 17:54:46 +01:00
else :
2020-11-27 20:50:38 +01:00
toot_text = ' @ ' + username + ' ' + is_the_turn_of + ' ' + playing_user + " \n "
2020-11-16 17:54:46 +01:00
toot_text + = ' \n '
2020-11-27 20:50:38 +01:00
toot_text + = game_name + ' : ' + str ( game_id ) + ' ' + chess_hashtag + ' \n '
2020-11-16 17:54:46 +01:00
board = chess . Board ( on_going_game )
if username == white_user :
svgfile = chess . svg . board ( board = board , orientation = chess . BLACK )
else :
svgfile = chess . svg . board ( board = board , orientation = chess . WHITE )
board_file = ' games/ ' + str ( game_id ) + ' _board.png '
svg2png ( bytestring = svgfile , write_to = board_file )
image_id = mastodon . media_post ( board_file , " image/png " ) . id
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility , media_ids = { image_id } )
update_replies ( status_id , username , now )
i + = 1
2020-11-28 18:07:52 +01:00
else :
usage ( )