2020-11-16 17:54:46 +01:00
import sys
import os
import os . path
import re
import unidecode
from datetime import datetime , timedelta
2022-03-17 12:53:30 +01:00
import time
2020-11-16 17:54:46 +01:00
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
2020-12-03 13:33:40 +01:00
import chess . pgn
2020-12-05 13:41:53 +01:00
from PIL import Image , ImageFont , ImageDraw
2020-12-21 10:23:04 +01:00
import math
2022-03-17 12:53:30 +01:00
import pdb
2020-11-16 17:54:46 +01:00
def cleanhtml ( raw_html ) :
cleanr = re . compile ( ' <.*?> ' )
cleantext = re . sub ( cleanr , ' ' , raw_html )
return cleantext
def unescape ( s ) :
s = s . replace ( " ' " , " ' " )
return s
2020-12-21 10:23:04 +01:00
# Function to calculate the Probability
def Probability ( rating1 , rating2 ) :
return 1.0 * 1.0 / ( 1 + 1.0 * math . pow ( 10 , 1.0 * ( rating1 - rating2 ) / 400 ) )
# Function to calculate Elo rating
# K is a constant.
# d determines whether
# Player A wins or Player B.
def EloRating ( Ra , Rb , K , d ) :
# To calculate the Winning
# Probability of Player B
Pb = Probability ( Ra , Rb )
# To calculate the Winning
# Probability of Player A
Pa = Probability ( Rb , Ra )
# Case -1 When Player A wins
# Updating the Elo Ratings
if ( d == 1 ) :
Ra = Ra + K * ( 1 - Pa )
Rb = Rb + K * ( 0 - Pb )
# Case -2 When Player B wins
# Updating the Elo Ratings
else :
Ra = Ra + K * ( 0 - Pa )
Rb = Rb + K * ( 1 - Pb )
Ra = round ( Ra , 6 )
Rb = round ( Rb , 6 )
print ( " Updated Ratings:- " )
print ( " Ra = " , Ra , " Rb = " , Rb )
return ( Ra , Rb )
# This code is contributed by
# Smitha Dinesh Semwal
def create_panel ( username , rating , played_games , wins ) :
2020-12-05 13:41:53 +01:00
2020-12-05 16:31:26 +01:00
if played_games > 0 and wins > 0 :
ratio = round ( ( wins * 100 ) / played_games , 2 )
else :
ratio = 0
2020-12-05 13:41:53 +01:00
x = 10
y = 10
fons = Image . open ( ' app/panel/fons.jpg ' )
print ( fons . size )
2020-12-05 17:58:32 +01:00
large , high = fons . size
2020-12-19 21:00:42 +01:00
panel_title_str = get_locale ( " panel_title_str " , player_lang )
2020-12-05 17:58:32 +01:00
title_length = len ( panel_title_str + ' ' + username )
2020-12-05 13:41:53 +01:00
# add chess icon
icon_path = ' app/panel/chess.png '
icon_img = Image . open ( icon_path )
2020-12-05 17:58:32 +01:00
fons . paste ( icon_img , ( y + 300 , x + 50 ) , icon_img )
2020-12-05 13:41:53 +01:00
logo_img = Image . open ( ' app/panel/logo.png ' )
fons . paste ( logo_img , ( 15 , 320 ) , logo_img )
fons . save ( ' app/panel/panel.png ' , " PNG " )
base = Image . open ( ' app/panel/panel.png ' ) . convert ( ' RGBA ' )
txt = Image . new ( ' RGBA ' , base . size , ( 255 , 255 , 255 , 0 ) )
fnt = ImageFont . truetype ( ' app/fonts/DroidSans.ttf ' , 40 , layout_engine = ImageFont . LAYOUT_BASIC )
# get a drawing context
draw = ImageDraw . Draw ( txt )
2020-12-05 17:58:32 +01:00
draw . text ( ( ( ( large / 2 ) - ( title_length * 2 ) ) , x + 20 ) , panel_title_str + ' ' + username , font = fnt , fill = ( 255 , 255 , 255 , 220 ) ) #fill=(255,255,255,255)) ## full opacity
2020-12-05 13:41:53 +01:00
2020-12-05 17:58:32 +01:00
fnt = ImageFont . truetype ( ' app/fonts/DroidSans.ttf ' , 35 , layout_engine = ImageFont . LAYOUT_BASIC )
2020-12-05 13:41:53 +01:00
2020-12-19 21:00:42 +01:00
panel_games_str = get_locale ( " panel_games_str " , player_lang )
panel_wins_str = get_locale ( " panel_wins_str " , player_lang )
panel_ratio_str = get_locale ( " panel_ratio_str " , player_lang )
2020-12-21 10:23:04 +01:00
panel_elo_rating_str = get_locale ( " panel_elo_rating_str " , player_lang )
2020-12-19 21:00:42 +01:00
2020-12-21 10:23:04 +01:00
draw . text ( ( y + 70 , x + 80 ) , panel_games_str + ' : ' + str ( played_games ) , font = fnt , fill = ( 255 , 255 , 255 , 220 ) ) #fill=(255,255,255,255)) ## full opacity
draw . text ( ( y + 70 , x + 130 ) , panel_wins_str + ' : ' + str ( wins ) , font = fnt , fill = ( 255 , 255 , 255 , 220 ) ) #fill=(255,255,255,255)) ## full opacity
draw . text ( ( y + 70 , x + 180 ) , panel_ratio_str + ' : ' + str ( ratio ) + ' % ' , font = fnt , fill = ( 255 , 255 , 255 , 220 ) )
draw . text ( ( y + 70 , x + 230 ) , panel_elo_rating_str + ' : ' + str ( round ( rating ) ) , font = fnt , fill = ( 255 , 255 , 255 , 220 ) )
2020-12-05 13:41:53 +01:00
fnt = ImageFont . truetype ( ' app/fonts/DroidSans.ttf ' , 15 , layout_engine = ImageFont . LAYOUT_BASIC )
draw . text ( ( 60 , 330 ) , bot_username + ' @ ' + mastodon_hostname + ' - 2020 ' , font = fnt , fill = ( 255 , 255 , 255 , 200 ) ) #fill=(255,255,255,255)) ## full opacity
out = Image . alpha_composite ( base , txt )
out . save ( ' app/panel/ ' + username + ' _panel.png ' )
2020-11-21 19:58:30 +01:00
def get_piece_name ( captured_piece ) :
if captured_piece == 1 :
2020-12-19 21:00:42 +01:00
pawn_piece = get_locale ( " pawn_piece " , player_lang )
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-12-19 21:00:42 +01:00
knight_piece = get_locale ( " knight_piece " , player_lang )
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-12-19 21:00:42 +01:00
bishop_piece = get_locale ( " bishop_piece " , player_lang )
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-12-19 21:00:42 +01:00
rook_piece = get_locale ( " rook_piece " , player_lang )
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-12-19 21:00:42 +01:00
queen_piece = get_locale ( " queen_piece " , player_lang )
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-12-19 21:00:42 +01:00
king_piece = get_locale ( " king_piece " , player_lang )
2020-11-27 20:50:38 +01:00
piece_name = king_piece
2020-11-21 19:58:30 +01:00
return piece_name
2022-03-17 12:53:30 +01:00
def get_player_langs ( account_id ) :
2020-12-19 21:00:42 +01:00
lang_changed = False
conn = None
try :
conn = psycopg2 . connect ( database = chess_db , user = chess_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-12-19 21:00:42 +01:00
select_query = " select lang from players where player_id = ( %s ) "
cur . execute ( select_query , ( str ( account_id ) , ) )
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
row = cur . fetchone ( )
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
if row != None :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
player_lang = row [ 0 ]
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
else :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
lang_changed , player_lang = set_lang ( account_id , username , bot_lang )
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
cur . close ( )
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
return ( lang_changed , player_lang )
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
except ( Exception , psycopg2 . DatabaseError ) as error :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
print ( error )
2020-11-17 17:59:03 +01:00
2020-12-19 21:00:42 +01:00
finally :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
if conn is not None :
2020-11-20 09:42:19 +01:00
2020-12-19 21:00:42 +01:00
conn . close ( )
def get_lang ( player ) :
conn = None
try :
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
select_query = " select lang from players where player_name = ( %s ) "
cur . execute ( select_query , ( player , ) )
row = cur . fetchone ( )
if row != None :
player_lang = row [ 0 ]
cur . close ( )
return ( player_lang )
except ( Exception , psycopg2 . DatabaseError ) as error :
print ( error )
finally :
if conn is not None :
conn . close ( )
def set_lang ( account_id , username , new_lang ) :
lang_changed = False
conn = None
try :
conn = psycopg2 . connect ( database = chess_db , user = chess_db_user , password = " " , host = " /var/run/postgresql " , port = " 5432 " )
cur = conn . cursor ( )
select_query = " select lang from players where player_id = ( %s ) "
cur . execute ( select_query , ( str ( account_id ) , ) )
row = cur . fetchone ( )
if row != None :
player_lang = row [ 0 ]
if new_lang not in [ ' ca ' , ' es ' , ' fr ' , ' en ' ] :
return lang_changed , player_lang
if row == None :
insert_sql = " insert into players(player_id, player_name, lang) values( %s , %s , %s ) ON CONFLICT DO NOTHING "
cur . execute ( insert_sql , ( account_id , username , new_lang ) )
lang_changed = True
else :
update_sql = " update players set lang=( %s ) where player_id=( %s ) "
cur . execute ( update_sql , ( new_lang , account_id ) )
lang_changed = True
conn . commit ( )
cur . close ( )
player_lang = new_lang
return ( lang_changed , player_lang )
except ( Exception , psycopg2 . DatabaseError ) as error :
print ( error )
finally :
if conn is not None :
conn . close ( )
def get_locale ( parameter , player_lang ) :
if player_lang not in [ ' ca ' , ' es ' , ' fr ' , ' en ' ] :
print ( " lang must be ' ca ' , ' es ' , ' fr ' or ' en ' " )
sys . exit ( 0 )
language_filepath = f " app/locales/ { player_lang } .txt "
if not os . path . isfile ( language_filepath ) :
print ( " File %s not found, exiting. " % language_filepath )
sys . exit ( 0 )
with open ( language_filepath ) as f :
for line in f :
if line . startswith ( parameter ) :
return line . replace ( parameter + " : " , " " ) . strip ( )
print ( language_filepath + " Missing parameter %s " % parameter )
sys . exit ( 0 )
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-12-03 13:33:40 +01:00
def write_result ( filename , old_string , new_string ) :
2020-11-23 18:22:08 +01:00
2020-12-03 13:33:40 +01:00
with open ( filename ) as f :
s = f . read ( )
if old_string not in s :
print ( ' " {old_string} " not found in {filename} . ' . format ( * * locals ( ) ) )
return
2020-11-23 18:22:08 +01:00
2020-12-03 13:33:40 +01:00
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 )
2020-11-23 18:22:08 +01:00
2020-12-03 13:33:40 +01:00
def save_anotation ( moving , san_move ) :
2020-11-27 20:50:38 +01:00
2020-12-03 13:33:40 +01:00
pgn_file = " app/anotations/ " + str ( game_id ) + " .pgn "
2020-11-23 18:22:08 +01:00
if bool ( board . turn == chess . BLACK ) == True :
2020-12-03 13:33:40 +01:00
line_data = str ( board . fullmove_number ) + " . " + san_move
2020-11-23 18:22:08 +01:00
else :
2020-12-03 13:33:40 +01:00
line_data = " " + san_move + " "
2020-11-27 20:50:38 +01:00
2020-12-03 13:33:40 +01:00
if checkmate or stalemate :
2020-12-02 12:58:02 +01:00
2020-12-03 13:33:40 +01:00
line_data = line_data + " " + board . result ( )
2020-11-23 18:22:08 +01:00
2020-12-03 13:33:40 +01:00
write_result ( pgn_file , ' [Result ] ' , ' [Result ' + board . result ( ) + ' ] ' )
2020-11-23 18:22:08 +01:00
2020-12-03 13:33:40 +01:00
if not os . path . isfile ( pgn_file ) :
2020-11-23 18:22:08 +01:00
2020-12-03 13:33:40 +01:00
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 '
2020-11-23 18:22:08 +01:00
2020-12-03 13:33:40 +01:00
with open ( pgn_file , ' w+ ' ) as f :
2020-11-23 18:22:08 +01:00
f . write ( file_header )
2020-12-03 13:33:40 +01:00
with open ( pgn_file , ' a ' ) as f :
2020-11-23 18:22:08 +01:00
f . write ( line_data )
else :
2020-12-03 13:33:40 +01:00
with open ( pgn_file , ' a ' ) as f :
2020-11-23 18:22:08 +01:00
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-12-19 21:00:42 +01:00
player_lang = get_lang ( username )
email_subject = get_locale ( " email_subject " , player_lang )
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
2020-12-03 13:33:40 +01:00
file_to_attach = " app/anotations/ " + game_id + " .pgn "
2020-11-25 11:38:16 +01:00
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
2020-12-03 13:33:40 +01:00
return ( emailed , game_id , game_found )
2020-11-25 11:38:16 +01:00
except socket . gaierror as socket_error :
print ( socket_error )
pass
2020-12-03 13:33:40 +01:00
return ( emailed , game_id , game_found )
2020-11-25 11:38:16 +01:00
except SMTPRecipientsRefused as recip_error :
print ( recip_error )
pass
2020-12-03 13:33:40 +01:00
return ( emailed , game_id , game_found )
2020-11-25 11:38:16 +01:00
2020-12-04 19:47:05 +01:00
def claim_draw ( 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 ]
if white_player == username :
2020-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
claim_draw_str = get_locale ( " claim_draw_str " , username_lang )
2020-12-04 19:47:05 +01:00
toot_text = ' @ ' + username + ' ' + claim_draw_str + ' @ ' + black_player + ' \n '
cur . execute ( " update games set white_stalemate = ' t ' where game_id=( %s ) " , ( game_id , ) )
else :
2020-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
claim_draw_str = get_locale ( " claim_draw_str " , username_lang )
toot_text = ' @ ' + username + ' ' + claim_draw_str + ' @ ' + white_player + ' \n '
2020-12-04 19:47:05 +01:00
cur . execute ( " update games set black_stalemate = ' t ' where game_id=( %s ) " , ( game_id , ) )
conn . commit ( )
cur . execute ( " select white_stalemate, black_stalemate from games where game_id=( %s ) " , ( game_id , ) )
row = cur . fetchone ( )
if row != None :
white_stalemate = row [ 0 ]
black_stalemate = row [ 1 ]
cur . close ( )
if white_stalemate == True and black_stalemate == True :
stalemate = True
else :
stalemate = False
return ( white_player , black_player , toot_text , stalemate )
except ( Exception , psycopg2 . DatabaseError ) as error :
sys . exit ( error )
finally :
if conn is not None :
conn . close ( )
2020-12-03 19:20:48 +01:00
def close_game ( username , checkmate ) :
2020-12-01 15:28:26 +01:00
2020-12-21 10:23:04 +01:00
d = 0
2020-12-01 15:28:26 +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 ( )
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 ]
2020-12-21 10:23:04 +01:00
cur . execute ( " select elo_rating from players where player_name = ( %s ) " , ( white_player , ) )
row = cur . fetchone ( )
2021-01-01 18:59:21 +01:00
if row != None :
if row [ 0 ] != None :
white_rating = row [ 0 ]
else :
2020-12-21 10:23:04 +01:00
2021-01-01 18:59:21 +01:00
white_rating = 1500
2020-12-21 10:23:04 +01:00
else :
white_rating = 1500
cur . execute ( " select elo_rating from players where player_name = ( %s ) " , ( black_player , ) )
row = cur . fetchone ( )
2021-01-01 18:59:21 +01:00
if row != None :
if row [ 0 ] != None :
black_rating = row [ 0 ]
else :
2020-12-21 10:23:04 +01:00
2021-01-01 18:59:21 +01:00
black_rating = 1500
2020-12-21 10:23:04 +01:00
else :
black_rating = 1500
2020-12-01 15:28:26 +01:00
cur . close ( )
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
now = datetime . now ( )
2020-12-01 15:28:26 +01:00
winner = ' '
2020-11-19 14:13:23 +01:00
waiting = False
2020-11-16 17:54:46 +01:00
finished = True
2020-12-01 15:28:26 +01:00
if stalemate == True :
winner = " stalemate "
if black_user == ' ' :
winner = ' none '
2020-12-03 19:20:48 +01:00
if checkmate :
winner = username
2020-12-21 10:23:04 +01:00
if winner == white_player :
d = 1
2020-12-01 15:28:26 +01:00
else :
if query_word == search_end and username == white_user and stalemate == False :
winner = black_user
2020-12-21 10:23:04 +01:00
d = 2
2020-12-01 15:28:26 +01:00
elif query_word == search_end and username == black_user and stalemate == False :
winner = white_user
2020-12-21 10:23:04 +01:00
d = 1
K = 30
new_white_rating , new_black_rating = EloRating ( white_rating , black_rating , K , d )
2020-11-16 17:54:46 +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-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
2020-12-01 15:28:26 +01:00
cur . execute ( " update stats set winner=( %s ), finished=( %s ), updated_at=( %s ) where game_id=( %s ) " , ( winner , finished , now , game_id ) )
2020-11-16 17:54:46 +01:00
2020-12-21 10:23:04 +01:00
cur . execute ( " update players set elo_rating=( %s ) where player_name=( %s ) " , ( new_white_rating , white_user ) )
cur . execute ( " update players set elo_rating=( %s ) where player_name=( %s ) " , ( new_black_rating , black_user ) )
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-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 ( )
2020-12-05 16:06:55 +01:00
cur . execute ( " select count(*) from games where (white_user = ( %s ) or black_user = ( %s )) and finished and moves > 0 " , ( player , player ) )
2020-11-18 16:25:13 +01:00
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 ]
2020-12-21 10:23:04 +01:00
cur . execute ( " select elo_rating from players where player_name = ( %s ) " , ( player , ) )
row = cur . fetchone ( )
if row [ 0 ] != None :
rating = row [ 0 ]
else :
rating = 1500
2020-11-18 16:25:13 +01:00
cur . close ( )
2020-12-21 10:23:04 +01:00
return ( rating , played_games , wins )
2020-11-18 16:25:13 +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 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 ( ) :
2020-12-05 17:36:04 +01:00
x = 10
y = 10
fons = Image . open ( ' app/panel/fons.jpg ' )
print ( fons . size )
logo_img = Image . open ( ' app/panel/logo.png ' )
fons . paste ( logo_img , ( 15 , 320 ) , logo_img )
fons . save ( ' app/panel/panel.png ' , " PNG " )
base = Image . open ( ' app/panel/panel.png ' ) . convert ( ' RGBA ' )
txt = Image . new ( ' RGBA ' , base . size , ( 255 , 255 , 255 , 0 ) )
fnt = ImageFont . truetype ( ' app/fonts/DroidSans.ttf ' , 40 , layout_engine = ImageFont . LAYOUT_BASIC )
# get a drawing context
draw = ImageDraw . Draw ( txt )
2020-12-19 21:00:42 +01:00
draw . text ( ( y + 270 , x + 10 ) , search_help , font = fnt , fill = ( 255 , 255 , 255 , 220 ) ) #fill=(255,255,255,255)) ## full opacity
2020-12-05 17:36:04 +01:00
fnt = ImageFont . truetype ( ' app/fonts/DroidSans.ttf ' , 18 , layout_engine = ImageFont . LAYOUT_BASIC )
2020-12-19 21:00:42 +01:00
start_or_join_a_new_game = get_locale ( " start_or_join_a_new_game " , player_lang )
move_a_piece = get_locale ( " move_a_piece " , player_lang )
leave_a_game = get_locale ( " leave_a_game " , player_lang )
list_games = get_locale ( " list_games " , player_lang )
get_a_game_anotation = get_locale ( " get_a_game_anotation " , player_lang )
claim_a_draw = get_locale ( " claim_a_draw " , player_lang )
post_my_panel_str = get_locale ( " post_my_panel_str " , player_lang )
change_lang_str = get_locale ( " change_lang_str " , player_lang )
draw . text ( ( y + 80 , x + 70 ) , ' @ ' + bot_username + ' ' + start_or_join_a_new_game , font = fnt , fill = ( 255 , 255 , 255 , 220 ) ) #fill=(255,255,255,255)) ## full opacity
draw . text ( ( y + 80 , x + 100 ) , ' @ ' + bot_username + ' ' + move_a_piece , font = fnt , fill = ( 255 , 255 , 255 , 220 ) ) #fill=(255,255,255,255)) ## full opacity
draw . text ( ( y + 80 , x + 130 ) , ' @ ' + bot_username + ' ' + leave_a_game , font = fnt , fill = ( 255 , 255 , 255 , 220 ) )
draw . text ( ( y + 80 , x + 160 ) , ' @ ' + bot_username + ' ' + list_games , font = fnt , fill = ( 255 , 255 , 255 , 220 ) )
draw . text ( ( y + 80 , x + 190 ) , ' @ ' + bot_username + ' ' + get_a_game_anotation , font = fnt , fill = ( 255 , 255 , 255 , 220 ) )
draw . text ( ( y + 80 , x + 220 ) , ' @ ' + bot_username + ' ' + claim_a_draw , font = fnt , fill = ( 255 , 255 , 255 , 220 ) )
draw . text ( ( y + 80 , x + 250 ) , ' @ ' + bot_username + ' ' + post_my_panel_str , font = fnt , fill = ( 255 , 255 , 255 , 220 ) )
draw . text ( ( y + 80 , x + 280 ) , ' @ ' + bot_username + ' ' + change_lang_str , font = fnt , fill = ( 255 , 255 , 255 , 220 ) )
2020-12-05 17:36:04 +01:00
fnt = ImageFont . truetype ( ' app/fonts/DroidSans.ttf ' , 15 , layout_engine = ImageFont . LAYOUT_BASIC )
draw . text ( ( 60 , 330 ) , bot_username + ' @ ' + mastodon_hostname + ' - 2020 ' , font = fnt , fill = ( 255 , 255 , 255 , 200 ) ) #fill=(255,255,255,255)) ## full opacity
out = Image . alpha_composite ( base , txt )
out . save ( ' app/panel/help_panel.png ' )
2020-11-28 23:00:15 +01:00
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 )
2020-12-19 21:00:42 +01:00
if query_word == search_new :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
reply = True
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
elif query_word [ : search_move_slicing ] == search_move :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
moving = query_word [ moving_slicing : query_word_length ] . replace ( " " , " " )
reply = True
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
elif query_word == search_end :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
reply = True
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
elif query_word == search_games :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
reply = True
2020-11-19 14:13:23 +01:00
2020-12-19 21:00:42 +01:00
elif query_word [ : search_send_slicing ] == search_send :
2020-11-19 14:13:23 +01:00
2020-12-19 21:00:42 +01:00
reply = True
2020-11-25 11:38:16 +01:00
2020-12-19 21:00:42 +01:00
elif query_word == search_help :
2020-11-25 11:38:16 +01:00
2020-12-19 21:00:42 +01:00
reply = True
2020-11-28 23:00:15 +01:00
2020-12-19 21:00:42 +01:00
elif query_word == search_draw :
2020-11-28 23:00:15 +01:00
2020-12-19 21:00:42 +01:00
reply = True
2020-12-04 19:47:05 +01:00
2020-12-19 21:00:42 +01:00
elif query_word == search_panel :
2020-12-04 19:47:05 +01:00
2020-12-19 21:00:42 +01:00
reply = True
2020-12-05 13:41:53 +01:00
2020-12-19 21:00:42 +01:00
elif query_word [ : 4 ] == search_config :
2020-12-05 13:41:53 +01:00
2020-12-19 21:00:42 +01:00
reply = True
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
else :
reply = False
2020-11-16 17:54:46 +01:00
return ( reply , query_word , moving )
except ValueError as v_error :
print ( v_error )
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 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-12-19 21:00:42 +01:00
bot_lang = ' '
2020-11-27 20:50:38 +01:00
if len ( sys . argv ) == 3 :
2020-12-19 21:00:42 +01:00
if sys . argv [ 2 ] == ' --ca ' :
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
bot_lang = ' ca '
2020-11-28 18:07:52 +01:00
2020-12-19 21:00:42 +01:00
if sys . argv [ 2 ] == ' --es ' :
2020-11-28 18:07:52 +01:00
bot_lang = ' es '
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
if sys . argv [ 2 ] == ' --fr ' :
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
bot_lang = ' fr '
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
elif sys . argv [ 2 ] == ' --en ' :
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
bot_lang = ' en '
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
elif len ( sys . argv ) == 2 :
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
bot_lang = ' ca '
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
if not bot_lang in [ ' ca ' , ' es ' , ' fr ' , ' en ' ] :
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
print ( " \n Only ' ca ' , ' es ' , ' fr ' and ' en ' languages are supported. \n " )
2020-11-28 18:07:52 +01:00
sys . exit ( 0 )
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
mastodon , mastodon_hostname , bot_username = mastodon ( )
mastodon_db , mastodon_db_user , chess_db , chess_db_user = db_config ( )
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
smtp_host , smtp_user_login , smtp_user_password = smtp_config ( )
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
now = datetime . now ( )
2020-11-27 20:50:38 +01:00
2022-03-17 12:53:30 +01:00
####################################################################
# get notifications
2020-11-27 20:50:38 +01:00
2022-03-17 12:53:30 +01:00
notifications = mastodon . notifications ( )
2020-11-28 18:07:52 +01:00
2022-03-17 12:53:30 +01:00
if len ( notifications ) == 0 :
2020-11-28 18:07:52 +01:00
2020-12-19 21:00:42 +01:00
print ( ' No mentions ' )
2022-03-17 12:53:30 +01:00
2020-12-19 21:00:42 +01:00
sys . exit ( 0 )
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
i = 0
2020-11-27 20:50:38 +01:00
2022-03-17 12:53:30 +01:00
while i < len ( notifications ) :
2020-11-27 20:50:38 +01:00
2022-03-17 12:53:30 +01:00
notification_id = notifications [ i ] . id
if notifications [ i ] . type != ' mention ' :
i + = 1
2020-11-27 20:50:38 +01:00
2022-03-17 12:53:30 +01:00
print ( f ' dismissing notification { notification_id } ' )
2020-11-27 20:50:38 +01:00
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
continue
account_id = notifications [ i ] . account . id
username = notifications [ i ] . account . acct
status_id = notifications [ i ] . status . id
text = notifications [ i ] . status . content
visibility = notifications [ i ] . status . visibility
url = notifications [ i ] . status . uri
lang_changed , player_lang = get_player_langs ( account_id )
2020-11-27 20:50:38 +01:00
2020-12-19 21:00:42 +01:00
if player_lang == ' ca ' :
2020-12-04 19:47:05 +01:00
2020-12-19 21:00:42 +01:00
search_move_slicing = 3
moving_slicing = 3
search_send_slicing = 5
send_game_slicing = 6
2020-11-28 23:00:15 +01:00
2020-12-19 21:00:42 +01:00
elif player_lang == ' en ' :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
search_move_slicing = 4
moving_slicing = 4
search_send_slicing = 4
send_game_slicing = 5
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
elif player_lang == ' es ' :
2020-11-25 11:38:16 +01:00
2020-12-19 21:00:42 +01:00
search_move_slicing = 5
moving_slicing = 5
search_send_slicing = 5
send_game_slicing = 6
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
elif player_lang == ' fr ' :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
search_move_slicing = 7
moving_slicing = 7
search_send_slicing = 7
send_game_slicing = 7
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
else :
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
sys . exit ( 0 )
2020-11-16 17:54:46 +01:00
# listen them or not
2020-12-19 21:00:42 +01:00
search_new = get_locale ( " search_new " , player_lang )
search_move = get_locale ( " search_move " , player_lang )
search_end = get_locale ( " search_end " , player_lang )
search_games = get_locale ( " search_games " , player_lang )
search_send = get_locale ( " search_send " , player_lang )
search_help = get_locale ( " search_help " , player_lang )
search_draw = get_locale ( " search_draw " , player_lang )
search_panel = get_locale ( " search_panel " , player_lang )
search_config = get_locale ( " search_config " , player_lang )
2020-11-17 17:59:03 +01:00
2020-11-16 17:54:46 +01:00
reply , query_word , moving = replying ( )
2020-12-19 21:00:42 +01:00
status_url = url
2020-11-20 09:42:19 +01:00
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 )
2020-11-30 09:50:08 +01:00
board_file = ' app/games/ ' + str ( game_id ) + ' _board.png '
2020-11-16 17:54:46 +01:00
svg2png ( bytestring = svgfile , write_to = board_file )
2020-12-19 21:00:42 +01:00
new_game_started = get_locale ( " new_game_started " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +01:00
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 )
2020-11-30 09:50:08 +01:00
board_file = ' app/games/ ' + str ( game_id ) + ' _board.png '
2020-11-16 17:54:46 +01:00
svg2png ( bytestring = svgfile , write_to = board_file )
2020-12-19 21:00:42 +01:00
player_lang1 = get_lang ( username )
playing_with = get_locale ( " playing_with " , player_lang1 )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { playing_with } { white_user } \n \n '
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
player_lang2 = get_lang ( white_user )
your_turn = get_locale ( " your_turn " , player_lang2 )
2022-03-17 12:53:30 +01:00
toot_text + = f ' @ { white_user } : { your_turn } \n \n '
2020-11-16 17:54:46 +01:00
2020-12-19 21:00:42 +01:00
game_name = get_locale ( " game_name " , player_lang2 )
chess_hashtag = get_locale ( " chess_hashtag " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text + = f " { 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 )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +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
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-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
send_error = get_locale ( " send_error " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { send_error } '
2020-11-25 11:38:16 +01:00
elif emailed == True and game_found == True :
2020-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
game_number_anotations = get_locale ( " game_number_anotations " , username_lang )
anotations_sent = get_locale ( " anotations_sent " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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 :
2020-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
cant_send_to_fediverse_account = get_locale ( " cant_send_to_fediverse_account " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { cant_send_to_fediverse_account } '
2020-11-28 20:12:36 +01:00
else :
2020-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
game_no_exists = get_locale ( " game_no_exists " , username_lang )
it_not_exists = get_locale ( " it_not_exists " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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 )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-25 11:38:16 +01:00
2020-12-05 13:41:53 +01:00
elif query_word == search_panel :
2020-12-21 10:23:04 +01:00
rating , played_games , wins = get_stats ( username )
2020-12-05 13:41:53 +01:00
2020-12-21 10:23:04 +01:00
create_panel ( username , rating , played_games , wins )
2020-12-05 13:41:53 +01:00
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } '
2020-12-05 13:41:53 +01:00
saved_panel = ' app/panel/ ' + username + ' _panel.png '
image_id = mastodon . media_post ( saved_panel , " image/png " ) . id
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility , media_ids = { image_id } )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-12-05 13:41:53 +01:00
2020-12-19 21:00:42 +01:00
elif query_word [ : 4 ] == search_config :
new_lang = query_word [ 5 : 7 ]
lang_changed , player_lang = set_lang ( account_id , username , new_lang )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-12-19 21:00:42 +01:00
if lang_changed :
locale_change_successfully = get_locale ( " locale_change_successfully " , new_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { locale_change_successfully } { new_lang } '
2020-12-19 21:00:42 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
else :
locale_not_changed = get_locale ( " locale_not_changed " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { new_lang } { locale_not_changed } '
2020-12-19 21:00:42 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
2020-11-28 23:00:15 +01:00
elif query_word == search_help :
2020-12-05 17:36:04 +01:00
toot_help ( )
2022-03-17 12:53:30 +01:00
help_text = f ' @ { username } '
2020-11-28 23:00:15 +01:00
2020-12-05 17:36:04 +01:00
help_panel = ' app/panel/help_panel.png '
2020-12-04 19:47:05 +01:00
2020-12-05 17:36:04 +01:00
image_id = mastodon . media_post ( help_panel , " image/png " ) . id
mastodon . status_post ( help_text , in_reply_to_id = status_id , visibility = visibility , media_ids = { image_id } )
2020-11-28 23:00:15 +01:00
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-28 23:00:15 +01:00
2020-11-19 14:13:23 +01:00
else :
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-19 14:13:23 +01:00
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-12-19 21:00:42 +01:00
player_lang1 = get_lang ( username )
game_already_started = get_locale ( " game_already_started " , player_lang1 )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { game_already_started } \n '
2020-11-16 17:54:46 +01:00
if black_user != ' ' :
2022-03-17 12:53:30 +01:00
toot_text + = f ' @ { white_user } / @ { black_user } \n '
2020-11-16 17:54:46 +01:00
else :
2020-12-19 21:00:42 +01:00
wait_other_player = get_locale ( " wait_other_player " , player_lang1 )
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-12-19 21:00:42 +01:00
game_name = get_locale ( " game_name " , player_lang1 )
chess_hashtag = get_locale ( " chess_hashtag " , player_lang1 )
2022-03-17 12:53:30 +01:00
toot_text + = f ' { 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 )
2020-11-30 09:50:08 +01:00
board_file = ' app/games/ ' + str ( game_id ) + ' _board.png '
2020-11-16 17:54:46 +01:00
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 } )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +01:00
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-12-01 15:28:26 +01:00
stalemate = False
checkmate = 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-12-19 21:00:42 +01:00
is_not_legal_move = get_locale ( " is_not_legal_move " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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 )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +01:00
else :
2020-12-03 13:33:40 +01:00
san_move = board . san ( chess . Move . from_uci ( moving ) )
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-12-01 15:28:26 +01:00
if board . is_stalemate ( ) == True :
stalemate = 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-12-01 15:28:26 +01:00
if stalemate == False :
2020-11-23 11:42:04 +01:00
2020-12-01 15:28:26 +01:00
checkmate = True
2020-11-23 11:42:04 +01:00
2020-12-03 19:20:48 +01:00
close_game ( username , checkmate )
2020-11-23 11:42:04 +01:00
else :
checkmate = False
if check == True and checkmate == False :
2020-12-19 21:00:42 +01:00
player_lang = get_lang ( playing_user )
check_done = get_locale ( " check_done " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f " @ { playing_user } { username } { check_done } \n "
2020-11-23 11:42:04 +01:00
elif check == True and checkmate == True :
2020-12-19 21:00:42 +01:00
player_lang1 = get_lang ( username )
check_mate = get_locale ( " check_mate " , player_lang1 )
check_mate_movements = get_locale ( " check_mate_movements " , player_lang1 )
the_winner_is = get_locale ( " the_winner_is " , player_lang1 )
2022-03-17 12:53:30 +01:00
toot_text = f ' \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-12-19 21:00:42 +01:00
winned_games = get_locale ( " winned_games " , player_lang1 )
2020-11-16 17:54:46 +01:00
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n { winned_games } \n '
2020-11-16 17:54:46 +01:00
2020-12-21 10:23:04 +01:00
rating , played_games , wins = get_stats ( username )
2020-11-18 16:25:13 +01:00
2020-12-19 21:00:42 +01:00
wins_of_many = get_locale ( " wins_of_many " , player_lang1 )
2022-03-17 12:53:30 +01:00
toot_text + = f ' { username } : { str ( wins ) } { wins_of_many } { str ( played_games ) } (Elo: { str ( round ( rating ) ) } ) \n '
2020-11-18 16:25:13 +01:00
2020-12-19 21:00:42 +01:00
player_lang2 = get_lang ( playing_user )
well_done = get_locale ( " well_done " , player_lang2 )
2022-03-17 12:53:30 +01:00
toot_text + = f " \n @ { playing_user } : { well_done } \n "
2020-12-19 21:00:42 +01:00
2020-12-21 10:23:04 +01:00
rating , played_games , wins = get_stats ( playing_user )
2020-11-18 16:25:13 +01:00
2020-12-19 21:00:42 +01:00
winned_games = get_locale ( " winned_games " , player_lang2 )
wins_of_many = get_locale ( " wins_of_many " , player_lang2 )
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n \n { winned_games } \n '
2020-12-19 21:00:42 +01:00
2022-03-17 12:53:30 +01:00
toot_text + = f ' { playing_user } : { str ( wins ) } { wins_of_many } { str ( played_games ) } (Elo: { str ( round ( rating ) ) } ) \n '
2020-11-18 16:25:13 +01:00
2020-12-01 15:28:26 +01:00
elif check == False and stalemate == True :
toot_text = stalemate_str + ' ( ' + str ( game_moves ) + ' ) ' + ' \n '
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n @ { playing_user } , @ { username } \n '
2020-12-01 15:28:26 +01:00
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n { winned_games } \n '
2020-12-01 15:28:26 +01:00
2020-12-21 10:23:04 +01:00
rating , played_games , wins = get_stats ( username )
2020-12-01 15:28:26 +01:00
2022-03-17 12:53:30 +01:00
toot_text + = f ' { username } : { str ( wins ) } { wins_of_many } { str ( played_games ) } \n '
2020-12-01 15:28:26 +01:00
2020-12-21 10:23:04 +01:00
rating , played_games , wins = get_stats ( playing_user )
2020-12-01 15:28:26 +01:00
2022-03-17 12:53:30 +01:00
toot_text + = f ' { playing_user } : { str ( wins ) } { wins_of_many } { str ( played_games ) } \n '
2020-12-01 15:28:26 +01:00
2020-11-16 17:54:46 +01:00
else :
2020-12-19 21:00:42 +01:00
player_lang = get_lang ( playing_user )
your_turn = get_locale ( " your_turn " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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-12-19 21:00:42 +01:00
player_lang = get_lang ( playing_user )
lost_piece = get_locale ( " lost_piece " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n { lost_piece } { piece_name } ! \n '
2020-11-21 19:58:30 +01:00
2020-12-19 21:00:42 +01:00
game_name = get_locale ( " game_name " , player_lang )
chess_hashtag = get_locale ( " chess_hashtag " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text + = f ' \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
2020-11-30 09:50:08 +01:00
board_file = ' app/games/ ' + str ( game_id ) + ' _board.png '
2020-11-16 17:54:46 +01:00
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-12-03 13:33:40 +01:00
save_anotation ( moving , san_move )
2020-11-23 18:22:08 +01:00
2020-11-16 17:54:46 +01:00
update_moves ( username , game_moves )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +01:00
except ValueError as v_error :
print ( v_error )
2020-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
not_legal_move_str = get_locale ( " not_legal_move_str " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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 )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-20 13:26:54 +01:00
2020-11-16 17:54:46 +01:00
pass
except AssertionError as a_error :
print ( a_error )
2020-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
not_legal_move_str = get_locale ( " not_legal_move_str " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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 )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +01:00
pass
2020-11-27 20:50:38 +01:00
elif query_word == search_end :
2020-11-16 17:54:46 +01:00
2020-12-01 15:28:26 +01:00
stalemate = False
2020-12-03 19:20:48 +01:00
checkmate = False
2020-11-16 17:54:46 +01:00
if black_user != ' ' :
if username == white_user :
2020-12-19 21:00:42 +01:00
player_leave_game = get_locale ( " player_leave_game " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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 )
2020-12-03 19:20:48 +01:00
close_game ( username , checkmate )
2020-11-16 17:54:46 +01:00
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +01:00
i + = 1
continue
else :
2021-01-01 17:44:58 +01:00
player_leave_game = get_locale ( " player_leave_game " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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
2020-12-03 19:20:48 +01:00
close_game ( username , checkmate )
2020-11-16 17:54:46 +01:00
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +01:00
i + = 1
continue
else :
2020-12-19 21:00:42 +01:00
leave_waiting_game = get_locale ( " leave_waiting_game " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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 )
2020-12-03 19:20:48 +01:00
close_game ( username , checkmate )
2020-11-16 17:54:46 +01:00
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +01:00
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-12-19 21:00:42 +01:00
started_games = get_locale ( " started_games " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f " @ { 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-12-19 21:00:42 +01:00
game_is_waiting = get_locale ( " game_is_waiting " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n { player1_name_lst [ i ] } / { player2_name_lst [ i ] } { game_is_waiting } \n '
2020-11-19 14:13:23 +01:00
else :
2020-12-19 21:00:42 +01:00
game_is_on_going = get_locale ( " game_is_on_going " , player_lang )
2020-11-20 15:06:14 +01:00
if next_move_lst [ i ] == player1_name_lst [ i ] :
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n * { player1_name_lst [ i ] } / { player2_name_lst [ i ] } { game_is_on_going } \n '
2020-11-20 15:06:14 +01:00
else :
2022-03-17 12:53:30 +01:00
toot_text + = f ' \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 :
2022-03-17 12:53:30 +01:00
toot_text + = f ' { 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-12-19 21:00:42 +01:00
no_on_going_games = get_locale ( " no_on_going_games " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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 )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
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 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 )
2020-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
2020-11-25 13:15:43 +01:00
if emailed == False and game_found == True :
2020-12-19 21:00:42 +01:00
send_error = get_locale ( " send_error " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { send_error } '
2020-11-25 13:15:43 +01:00
elif emailed == True and game_found == True :
2020-12-19 21:00:42 +01:00
game_number_anotations = get_locale ( " game_number_anotations " , username_lang )
anotations_sent = get_locale ( " anotations_sent " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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-12-19 21:00:42 +01:00
game_no_exists = get_locale ( " game_no_exists " , username_lang )
it_not_exists = get_locale ( " it_not_exists " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { 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 )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-25 13:15:43 +01:00
2020-12-04 19:47:05 +01:00
elif query_word == search_draw :
white_player , black_player , toot_text , stalemate = claim_draw ( username )
if stalemate == True :
checkmate = False
close_game ( username , checkmate )
2020-12-19 21:00:42 +01:00
player_lang1 = get_lang ( white_player )
draw_and_str = get_locale ( " draw_and_str " , player_lang1 )
agreed_draw_str = get_locale ( " agreed_draw_str " , player_lang1 )
winned_games = get_locale ( " winned_games " , player_lang1 )
wins_of_many = get_locale ( " wins_of_many " , player_lang1 )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { white_player } { draw_and_str } @ { black_player } { agreed_draw_str } \n \n '
2020-12-04 19:47:05 +01:00
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n { winned_games } \n '
2020-12-04 19:47:05 +01:00
2020-12-21 10:23:04 +01:00
rating , played_games , wins = get_stats ( white_player )
2020-12-04 19:47:05 +01:00
2022-03-17 12:53:30 +01:00
toot_text + = f ' { white_player } : { str ( wins ) } { wins_of_many } { str ( played_games ) } \n '
2020-12-04 19:47:05 +01:00
2020-12-19 21:00:42 +01:00
player_lang2 = get_lang ( black_player )
draw_and_str = get_locale ( " draw_and_str " , player_lang2 )
agreed_draw_str = get_locale ( " agreed_draw_str " , player_lang2 )
winned_games = get_locale ( " winned_games " , player_lang2 )
wins_of_many = get_locale ( " wins_of_many " , player_lang2 )
if player_lang1 != player_lang2 :
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n @ { white_player } { draw_and_str } @ { black_player } { agreed_draw_str } \n \n '
2020-12-19 21:00:42 +01:00
2022-03-17 12:53:30 +01:00
toot_text + = f ' \n { winned_games } \n '
2020-12-19 21:00:42 +01:00
2020-12-21 10:23:04 +01:00
rating , played_games , wins = get_stats ( black_player )
2020-12-04 19:47:05 +01:00
2022-03-17 12:53:30 +01:00
toot_text + = f ' { black_player } : { str ( wins ) } { wins_of_many } { str ( played_games ) } \n '
2020-12-04 19:47:05 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
else :
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-12-04 19:47:05 +01:00
2020-12-05 13:41:53 +01:00
elif query_word == search_panel :
2020-12-21 10:23:04 +01:00
rating , played_games , wins = get_stats ( username )
2020-12-05 13:41:53 +01:00
2020-12-21 10:23:04 +01:00
create_panel ( username , rating , played_games , wins )
2020-12-05 13:41:53 +01:00
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } '
2020-12-05 13:41:53 +01:00
saved_panel = ' app/panel/ ' + username + ' _panel.png '
image_id = mastodon . media_post ( saved_panel , " image/png " ) . id
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility , media_ids = { image_id } )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-12-05 13:41:53 +01:00
2020-12-19 21:00:42 +01:00
elif query_word [ : 4 ] == search_config :
new_lang = query_word [ 5 : 7 ]
lang_changed , player_lang = set_lang ( account_id , username , new_lang )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-12-19 21:00:42 +01:00
if lang_changed :
locale_change_successfully = get_locale ( " locale_change_successfully " , new_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { locale_change_successfully } { new_lang } '
2020-12-19 21:00:42 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
else :
locale_not_changed = get_locale ( " locale_not_changed " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { new_lang } { locale_not_changed } '
2020-12-19 21:00:42 +01:00
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
2020-11-28 23:00:15 +01:00
elif query_word == search_help :
2020-12-05 17:36:04 +01:00
toot_help ( )
2022-03-17 12:53:30 +01:00
help_text = f ' @ { username } '
2020-12-05 17:36:04 +01:00
help_panel = ' app/panel/help_panel.png '
image_id = mastodon . media_post ( help_panel , " image/png " ) . id
2020-11-28 23:00:15 +01:00
2020-12-05 17:36:04 +01:00
mastodon . status_post ( help_text , in_reply_to_id = status_id , visibility = visibility , media_ids = { image_id } )
2020-11-28 23:00:15 +01:00
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-28 23:00:15 +01:00
2020-11-16 17:54:46 +01:00
else :
if playing_user == None :
2020-12-19 21:00:42 +01:00
username_lang = get_lang ( username )
is_not_your_turn = get_locale ( " is_not_your_turn " , username_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { is_not_your_turn } \n '
2020-11-16 17:54:46 +01:00
else :
2020-12-19 21:00:42 +01:00
is_the_turn_of = get_locale ( " is_the_turn_of " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text = f ' @ { username } { is_the_turn_of } { playing_user } \n '
2020-11-16 17:54:46 +01:00
toot_text + = ' \n '
2020-12-19 21:00:42 +01:00
game_name = get_locale ( " game_name " , player_lang )
chess_hashtag = get_locale ( " chess_hashtag " , player_lang )
2022-03-17 12:53:30 +01:00
toot_text + = f ' { 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 )
2020-11-30 09:50:08 +01:00
board_file = ' app/games/ ' + str ( game_id ) + ' _board.png '
2020-11-16 17:54:46 +01:00
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 } )
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
else :
2020-11-16 17:54:46 +01:00
2022-03-17 12:53:30 +01:00
mastodon . notifications_dismiss ( notification_id )
2020-11-16 17:54:46 +01:00
i + = 1
2020-11-28 18:07:52 +01:00
else :
usage ( )