From ca9ec58dd38b74767d234849ccc0e14942b5f501 Mon Sep 17 00:00:00 2001 From: spla Date: Sat, 19 Dec 2020 21:00:42 +0100 Subject: [PATCH 1/8] New feature! every player can configure bot's replies language! --- README.md | 31 +- app/locales/ca.txt | 4 + app/locales/en.txt | 4 + app/locales/es.txt | 4 + app/locales/fr.txt | 4 + db-setup.py | 4 + mastochess.py | 767 +++++++++++++++++++++++++++++++-------------- 7 files changed, 567 insertions(+), 251 deletions(-) diff --git a/README.md b/README.md index f514eda..69003f0 100644 --- a/README.md +++ b/README.md @@ -44,20 +44,25 @@ Don't use q for queen. Pawn is promoted to Queen by default. - To get your panel stats: -@your_bot_username panel +@your_bot_username panel + +- To change the bot's language: + +@your_bot_username conf en ### Commands table -| ca | en | es | ex. | Observ. | -|:-----:|:-----:|:--------:|:----:|:-----------:| -| nova | new | nueva | | | -| mou | move | mueve | e2e3 | | -| fi | end | fin | | | -| jocs | games | partidas | | | -| envia | send | envia | 1 | game number | -| taules| draw | tablas | | | -| ajuda | help | ayuda | | | -| panell| panel | panel | | | +| ca | en | es | fr | ex. | Observ. | +|:-----:|:-----:|:--------:|:--------:|:----:|:-----------:| +| nova | new | nueva | nouvelle | | | +| mou | move | mueve | déplace | e2e3 | | +| fi | end | fin | fin | | | +| jocs | games | partidas | parties | | | +| envia | send | envia | envoyer | 1 | game number | +| taules| draw | tablas | nulle | | | +| ajuda | help | ayuda | aide | | | +| panell| panel | panel | panneau | | | +| conf | conf | conf | conf | | ca,es,fr,en | ### Dependencies @@ -92,4 +97,6 @@ Within Python Virtual Environment: 28.11.2020 - New feature! Added help 03.12.2020 - New feature! Added pgn save & send support 04.12.2020 - New feature! Now players can claim a draw. -05.12.2020 - New feature! Add panel stats. +05.12.2020 - New feature! Add panel stats. +19.12.2020 - New feature! Now you can configure bot's language! +19.12.2020 - New feature! Added french language! diff --git a/app/locales/ca.txt b/app/locales/ca.txt index e4862ca..e86bfe2 100644 --- a/app/locales/ca.txt +++ b/app/locales/ca.txt @@ -3,6 +3,7 @@ search_move: mou search_new: nova search_games: jocs search_send: envia +search_config: conf search_help: ajuda search_draw: taules new_game_started: partida iniciada! Esperant jugador... @@ -67,3 +68,6 @@ panel_games_str: Partides panel_wins_str: Victòries panel_ratio_str: Ràtio post_my_panel_str: panell (publica les estadístiques) +locale_change_successfully: llengua canviada amb èxit a +locale_not_changed: encara no és suportada :-( +change_lang_str: conf ca (per a configurar el bot en català) diff --git a/app/locales/en.txt b/app/locales/en.txt index 56532ea..70cb02d 100644 --- a/app/locales/en.txt +++ b/app/locales/en.txt @@ -3,6 +3,7 @@ search_move: move search_new: new search_games: games search_send: send +search_config: conf search_help: help search_draw: draw new_game_started: game started! Waiting for the second player... @@ -67,3 +68,6 @@ panel_games_str: Games panel_wins_str: Wins panel_ratio_str: Ratio post_my_panel_str: panel (post player stats) +locale_change_successfully: language sucessfully changed to +locale_not_changed: is not supported yet :-( +change_lang_str: conf en (to configure the bot in english) diff --git a/app/locales/es.txt b/app/locales/es.txt index 7341159..82e8b74 100644 --- a/app/locales/es.txt +++ b/app/locales/es.txt @@ -3,6 +3,7 @@ search_move: mueve search_new: nueva search_games: partidas search_send: envia +search_config: conf search_help: ayuda search_draw: tablas new_game_started: partida iniciada! Esperando jugador... @@ -67,3 +68,6 @@ panel_games_str: Partidas panel_wins_str: Victorias panel_ratio_str: Ratio post_my_panel_str: panel (publica el panel de datos) +locale_change_successfully: idioma cambiado con éxito a +locale_not_changed: no es soportado aún :-( +change_lang_str: conf es (para configurar el bot en castellano) diff --git a/app/locales/fr.txt b/app/locales/fr.txt index cde460e..56f027f 100644 --- a/app/locales/fr.txt +++ b/app/locales/fr.txt @@ -3,6 +3,7 @@ search_move: déplace search_new: nouvelle search_games: parties search_send: envoyer +search_config: conf search_help: aide search_draw: nulle new_game_started: partie initié! En attente d'un joueur... @@ -67,3 +68,6 @@ panel_games_str: Parties panel_wins_str: Victoires panel_ratio_str: Ratio post_my_panel_str: panneau (publie les statistiques) +locale_change_successfully: langue modifiée avec succès en +locale_not_changed: n'est pas encore pris en charge :-( +change_lang_str: conf fr (pour configurer le bot en français) diff --git a/db-setup.py b/db-setup.py index 14a42d4..bf8763c 100644 --- a/db-setup.py +++ b/db-setup.py @@ -168,6 +168,10 @@ if __name__ == '__main__': sql += "finished boolean default False, updated_at timestamptz, CONSTRAINT fk_game FOREIGN KEY(game_id) REFERENCES games(game_id) ON DELETE CASCADE ON UPDATE CASCADE)" create_table(db, db_user, table, sql) + table = "players" + sql = "create table "+table+" (player_id bigint PRIMARY KEY, player_name varchar(40), lang varchar(2))" + create_table(db, db_user, table, sql) + ############################################################ print("Done!") diff --git a/mastochess.py b/mastochess.py index 4ba7093..cc88d15 100644 --- a/mastochess.py +++ b/mastochess.py @@ -20,6 +20,7 @@ import chess.svg from cairosvg import svg2png import chess.pgn from PIL import Image, ImageFont, ImageDraw +import lichess.api def cleanhtml(raw_html): cleanr = re.compile('<.*?>') @@ -48,6 +49,8 @@ def create_panel(username, played_games, wins): large, high = fons.size + panel_title_str = get_locale("panel_title_str", player_lang) + title_length = len(panel_title_str + ' ' + username) # add chess icon @@ -71,6 +74,10 @@ def create_panel(username, played_games, wins): fnt = ImageFont.truetype('app/fonts/DroidSans.ttf', 35, layout_engine=ImageFont.LAYOUT_BASIC) + 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) + draw.text((y+70,x+120), 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+170), panel_wins_str + ': ' + str(wins), font=fnt, fill=(255,255,255,220)) #fill=(255,255,255,255)) ## full opacity draw.text((y+70,x+220), panel_ratio_str + ': ' + str(ratio) + '%', font=fnt, fill=(255,255,255,220)) @@ -155,89 +162,275 @@ def get_piece_name(captured_piece): if captured_piece == 1: + pawn_piece = get_locale("pawn_piece", player_lang) + piece_name = pawn_piece if captured_piece == 2: + knight_piece = get_locale("knight_piece", player_lang) + piece_name = knight_piece if captured_piece == 3: + bishop_piece = get_locale("bishop_piece", player_lang) + piece_name = bishop_piece if captured_piece == 4: + rook_piece = get_locale("rook_piece", player_lang) + piece_name = rook_piece if captured_piece == 5: + queen_piece = get_locale("queen_piece", player_lang) + piece_name = queen_piece if captured_piece == 6: + king_piece = get_locale("king_piece", player_lang) + piece_name = king_piece return piece_name -def get_notification_data(): +def get_mentions(): + + account_id_lst = [] + + status_id_lst = [] conn = None try: - account_id_lst = [] - - status_id_lst = [] - - text_lst = [] - - visibility_lst = [] - - url_lst = [] - - search_text = [search_end, search_move, search_new, search_games, search_send, search_help, search_draw, search_panel] - conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") cur = conn.cursor() - i=0 + select_query = "select account_id, id from statuses where created_at + interval '60 minutes' > now() - interval '5 minutes'" + select_query += " and id=any (select status_id from mentions where account_id=(%s)) order by created_at asc" - while i < len(search_text): + cur.execute(select_query, (str(bot_id),)) - like_text = "%"+search_text[i]+"%" + rows = cur.fetchall() - 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'" - select_query += " and id=any (select status_id from mentions where account_id=(%s)) order by created_at asc" + for row in rows: - cur.execute(select_query, (like_text, str(bot_id))) + replied = check_replies(row[1]) - rows = cur.fetchall() - - for row in rows: + if not replied: account_id_lst.append(row[0]) status_id_lst.append(row[1]) - text_lst.append(row[2]) + cur.close() - 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') + return (account_id_lst, status_id_lst) - url_lst.append(row[4]) + except (Exception, psycopg2.DatabaseError) as error: - i += 1 + print(error) + + finally: + + if conn is not None: + + conn.close() + + +def get_mention_langs(account_id): + + 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] + + else: + + lang_changed, player_lang = set_lang (account_id, username, bot_lang) cur.close() - return (account_id_lst, status_id_lst, text_lst, visibility_lst, url_lst) + return (lang_changed, player_lang) + + except (Exception, psycopg2.DatabaseError) as error: + + print(error) + + finally: + + if conn is not None: + + 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) + +def get_notification_data(status_id): + + conn = None + + try: + + conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + select_query = "select text, visibility, url from statuses where id=(%s)" + + cur.execute(select_query, (status_id,)) + + row = cur.fetchone() + + text = row[0] + + if row[1] == 0: + visibility = 'public' + elif row[1] == 1: + visibility = 'unlisted' + elif row[1] == 2: + visibility = 'private' + elif row[1] == 3: + visibility = 'direct' + + url = row[2] + + cur.close() + + return (text, visibility, url) except (Exception, psycopg2.DatabaseError) as error: @@ -647,6 +840,10 @@ def send_anotation(game_id): # Declare message elements msg['From'] = smtp_user_login msg['To'] = username_email + + player_lang = get_lang(username) + email_subject = get_locale("email_subject", player_lang) + msg['Subject'] = email_subject + game_id # Attach the game anotation @@ -728,13 +925,19 @@ def claim_draw(username): if white_player == username: + username_lang = get_lang(username) + claim_draw_str = get_locale("claim_draw_str", username_lang) + toot_text = '@'+username + ' ' + claim_draw_str + ' @'+black_player + '\n' cur.execute("update games set white_stalemate = 't' where game_id=(%s)", (game_id,)) else: - toot_text = '@'+username + ' ha proposat taules a ' + '@'+white_player + '\n' + username_lang = get_lang(username) + claim_draw_str = get_locale("claim_draw_str", username_lang) + + toot_text = '@'+username + ' ' + claim_draw_str + ' @'+white_player + '\n' cur.execute("update games set black_stalemate = 't' where game_id=(%s)", (game_id,)) @@ -1089,12 +1292,6 @@ def toot_help(): fons = Image.open('app/panel/fons.jpg') print(fons.size) - # add chess icon - #icon_path = 'app/panel/chess.png' - #icon_img = Image.open(icon_path) - - #fons.paste(icon_img, (y+350, x+50), icon_img) - logo_img = Image.open('app/panel/logo.png') fons.paste(logo_img, (15, 320), logo_img) @@ -1106,17 +1303,27 @@ def toot_help(): # get a drawing context draw = ImageDraw.Draw(txt) - draw.text((y+270,x+20), search_help, font=fnt, fill=(255,255,255,220)) #fill=(255,255,255,255)) ## full opacity + draw.text((y+270,x+10), search_help, font=fnt, fill=(255,255,255,220)) #fill=(255,255,255,255)) ## full opacity fnt = ImageFont.truetype('app/fonts/DroidSans.ttf', 18, layout_engine=ImageFont.LAYOUT_BASIC) - draw.text((y+80,x+80), '@'+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+110), '@'+bot_username + ' ' + move_a_piece, font=fnt, fill=(255,255,255,220)) #fill=(255,255,255,255)) ## full opacity - draw.text((y+80,x+140), '@'+bot_username + ' ' + leave_a_game, font=fnt, fill=(255,255,255,220)) - draw.text((y+80,x+170), '@'+bot_username + ' ' + list_games, font=fnt, fill=(255,255,255,220)) - draw.text((y+80,x+200), '@'+bot_username + ' ' + get_a_game_anotation, font=fnt, fill=(255,255,255,220)) - draw.text((y+80,x+230), '@'+bot_username + ' ' + claim_a_draw, font=fnt, fill=(255,255,255,220)) - draw.text((y+80,x+260), '@'+bot_username + ' ' + post_my_panel, font=fnt, fill=(255,255,255,220)) + 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)) fnt = ImageFont.truetype('app/fonts/DroidSans.ttf', 15, layout_engine=ImageFont.LAYOUT_BASIC) @@ -1157,44 +1364,46 @@ def replying(): query_word = question query_word_length = len(query_word) - if unidecode.unidecode(question)[0:query_word_length] == query_word: + if query_word == search_new: - if query_word == search_new: + reply = True - reply = True + elif query_word[:search_move_slicing] == search_move: - elif query_word[:search_move_slicing] == search_move: + moving = query_word[moving_slicing:query_word_length].replace(" ","") + reply = True - moving = query_word[moving_slicing:query_word_length].replace(" ","") - reply = True + elif query_word == search_end: - elif query_word == search_end: + reply = True - reply = True + elif query_word == search_games: - elif query_word == search_games: + reply = True - reply = True + elif query_word[:search_send_slicing] == search_send: - elif query_word[:search_send_slicing] == search_send: + reply = True - reply = True + elif query_word == search_help: - elif query_word == search_help: + reply = True - reply = True + elif query_word == search_draw: - elif query_word == search_draw: + reply = True - reply = True + elif query_word == search_panel: - elif query_word == search_panel: + reply = True - reply = True + elif query_word[:4] == search_config: - else: + reply = True - reply = False + else: + + reply = False return (reply, query_word, moving) @@ -1202,114 +1411,6 @@ def replying(): print(v_error) -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) - search_help = get_parameter("search_help", language_filepath) - 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) - - 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) - -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) - cant_send_to_fediverse_account = get_parameter("cant_send_to_fediverse_account", language_filepath) - 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) - - 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) - -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) - -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) - search_draw = get_parameter("search_draw", language_filepath) - ask_for_draw = get_parameter("ask_for_draw", language_filepath) - - return (start_or_join_a_new_game, move_a_piece, leave_a_game, list_games, get_a_game_anotation, show_help, search_draw, ask_for_draw) - -def load_strings7(bot_lang): - - claim_draw_str = get_parameter("claim_draw_str", language_filepath) - draw_and_str = get_parameter("draw_and_str", language_filepath) - agreed_draw_str = get_parameter("agreed_draw_str", language_filepath) - claim_a_draw = get_parameter("claim_a_draw", language_filepath) - search_panel = get_parameter("search_panel", language_filepath) - panel_title_str = get_parameter("panel_title_str", language_filepath) - panel_games_str = get_parameter("panel_games_str", language_filepath) - panel_wins_str = get_parameter("panel_wins_str", language_filepath) - panel_ratio_str = get_parameter("panel_ratio_str", language_filepath) - post_my_panel_str = get_parameter("post_my_panel_str", language_filepath) - - return (claim_draw_str, draw_and_str, agreed_draw_str, claim_a_draw, search_panel, panel_title_str, panel_games_str, panel_wins_str, panel_ratio_str, post_my_panel_str) - def mastodon(): # Load secrets from secrets file @@ -1389,73 +1490,35 @@ if __name__ == '__main__': if sys.argv[1] == '--play': + bot_lang = '' + if len(sys.argv) == 3: - if sys.argv[2] == '--en': + if sys.argv[2] == '--ca': + + bot_lang = 'ca' + + if sys.argv[2] == '--es': + + bot_lang = 'es' + + if sys.argv[2] == '--fr': + + bot_lang = 'fr' + + elif sys.argv[2] == '--en': bot_lang = 'en' - elif sys.argv[2] == '--es': - - bot_lang = 'es' - else: + elif len(sys.argv) == 2: bot_lang = 'ca' - if bot_lang == 'ca': + if not bot_lang in ['ca', 'es', 'fr', 'en']: - language_filepath = 'app/locales/ca.txt' - - elif bot_lang == 'en': - - language_filepath = 'app/locales/en.txt' - - elif bot_lang == 'es': - - language_filepath = 'app/locales/es.txt' - - else: - - print("\nOnly 'ca', 'es' and 'en' languages are supported.\n") + print("\nOnly 'ca', 'es', 'fr' and 'en' languages are supported.\n") sys.exit(0) - if bot_lang == 'ca': - - search_move_slicing = 3 - moving_slicing = 3 - search_send_slicing = 5 - send_game_slicing = 6 - - elif bot_lang == 'en': - - search_move_slicing = 4 - moving_slicing = 4 - search_send_slicing = 4 - send_game_slicing = 5 - - elif bot_lang == 'es': - - search_move_slicing = 5 - moving_slicing = 5 - search_send_slicing = 5 - send_game_slicing = 6 - - 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) - - 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) - - 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) - - start_or_join_a_new_game, move_a_piece, leave_a_game, list_games, get_a_game_anotation, show_help, search_draw, ask_for_draw = load_strings6(bot_lang) - - claim_draw_str, draw_and_str, agreed_draw_str, claim_a_draw, search_panel, panel_title_str, panel_games_str, panel_wins_str, panel_ratio_str, post_my_panel = load_strings7(bot_lang) - mastodon, mastodon_hostname, bot_username = mastodon() mastodon_db, mastodon_db_user, chess_db, chess_db_user = db_config() @@ -1466,15 +1529,59 @@ if __name__ == '__main__': bot_id = get_bot_id() - account_id_lst, status_id_lst, text_lst, visibility_lst, url_lst = get_notification_data() + account_id_lst, status_id_lst = get_mentions() + + if len(account_id_lst) == 0: + + print('No mentions') + sys.exit(0) i = 0 + while i < len(account_id_lst): account_id = account_id_lst[i] username, domain = get_user_domain(account_id) + lang_changed, player_lang = get_mention_langs(account_id) + + if player_lang == 'ca': + + search_move_slicing = 3 + moving_slicing = 3 + search_send_slicing = 5 + send_game_slicing = 6 + + elif player_lang == 'en': + + search_move_slicing = 4 + moving_slicing = 4 + search_send_slicing = 4 + send_game_slicing = 5 + + elif player_lang == 'es': + + search_move_slicing = 5 + moving_slicing = 5 + search_send_slicing = 5 + send_game_slicing = 6 + + elif player_lang == 'fr': + + search_move_slicing = 7 + moving_slicing = 7 + search_send_slicing = 7 + send_game_slicing = 7 + + else: + + sys.exit(0) + + status_id = status_id_lst[i] + + text, visibility, url = get_notification_data(status_id) + if domain != None: username = username + '@' + domain @@ -1491,13 +1598,19 @@ if __name__ == '__main__': # listen them or not - text = text_lst[i] + 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) reply, query_word, moving = replying() - visibility = visibility_lst[i] - - status_url = url_lst[i] + status_url = url if query_word != search_games: @@ -1523,6 +1636,8 @@ if __name__ == '__main__': svg2png(bytestring=svgfile,write_to=board_file) + new_game_started = get_locale("new_game_started", player_lang) + toot_text = '@'+username + ' ' + new_game_started + '\n' toot_text += '\n' @@ -1553,14 +1668,25 @@ if __name__ == '__main__': svg2png(bytestring=svgfile,write_to=board_file) + player_lang1 = get_lang(username) + + playing_with = get_locale("playing_with", player_lang1) + toot_text = '@'+username + ' ' + playing_with + ' ' + white_user + "\n" toot_text += '\n' + player_lang2 = get_lang(white_user) + + your_turn = get_locale("your_turn", player_lang2) + toot_text += '@'+white_user + ': ' + your_turn + "\n" toot_text += '\n' + game_name = get_locale("game_name", player_lang2) + chess_hashtag = get_locale("chess_hashtag", player_lang) + toot_text += game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' image_id = mastodon.media_post(board_file, "image/png").id @@ -1583,20 +1709,34 @@ if __name__ == '__main__': if emailed == False and game_found == True: + username_lang = get_lang(username) + send_error = get_locale("send_error", username_lang) + toot_text = '@'+username + ' ' + send_error elif emailed == True and game_found == True: + username_lang = get_lang(username) + game_number_anotations = get_locale("game_number_anotations", username_lang) + anotations_sent = get_locale("anotations_sent", username_lang) + toot_text = '@'+username + ' ' + game_number_anotations + str(game_id) + ' ' + anotations_sent elif emailed == False and game_found == False: if domain != None: + username_lang = get_lang(username) + cant_send_to_fediverse_account = get_locale("cant_send_to_fediverse_account", username_lang) + toot_text = '@'+username + ' ' + cant_send_to_fediverse_account else: + 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) + toot_text = '@'+username + ' ' + game_no_exists + str(game_id) + ' ' + it_not_exists mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) @@ -1619,6 +1759,30 @@ if __name__ == '__main__': update_replies(status_id, username, now) + elif query_word[:4] == search_config: + + new_lang = query_word[5:7] + + lang_changed, player_lang = set_lang(account_id, username, new_lang) + + update_replies(status_id, username, now) + + if lang_changed: + + locale_change_successfully = get_locale("locale_change_successfully", new_lang) + + toot_text = '@'+username + ' ' + locale_change_successfully + ' ' + new_lang + + mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) + + else: + + locale_not_changed = get_locale("locale_not_changed", player_lang) + + toot_text = '@'+username + ' ' + new_lang + ' ' + locale_not_changed + + mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) + elif query_word == search_help: toot_help() @@ -1641,6 +1805,10 @@ if __name__ == '__main__': if query_word == search_new: + player_lang1 = get_lang(username) + + game_already_started = get_locale("game_already_started", player_lang1) + toot_text = '@'+username + ' ' + game_already_started + '\n' if black_user != '': @@ -1649,10 +1817,15 @@ if __name__ == '__main__': else: + wait_other_player = get_locale("wait_other_player", player_lang1) + toot_text += wait_other_player + '\n' toot_text += '\n' + game_name = get_locale("game_name", player_lang1) + chess_hashtag = get_locale("chess_hashtag", player_lang1) + toot_text += game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' board = chess.Board(on_going_game) @@ -1713,6 +1886,8 @@ if __name__ == '__main__': if not_legal_move: + is_not_legal_move = get_locale("is_not_legal_move", player_lang) + toot_text = '@'+username + ': ' + moving + ' ' + is_not_legal_move + '\n' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) @@ -1775,22 +1950,45 @@ if __name__ == '__main__': if check == True and checkmate == False: + player_lang = get_lang(playing_user) + check_done = get_locale("check_done", player_lang) + toot_text = "@"+playing_user + " " + username + ' ' + check_done + '\n' elif check == True and checkmate == True: + 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) + toot_text = '\n' + check_mate + ' ' + str(game_moves) + ' ' + check_mate_movements + '\n\n' + the_winner_is + ' ' + "@"+username + '\n' - toot_text += "\n@"+playing_user + ': ' + well_done + "\n" + winned_games = get_locale("winned_games", player_lang1) toot_text += '\n' + winned_games + "\n" played_games, wins = get_stats(username) + wins_of_many = get_locale("wins_of_many", player_lang1) + toot_text += username + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + player_lang2 = get_lang(playing_user) + + well_done = get_locale("well_done", player_lang2) + + toot_text += "\n@"+playing_user + ': ' + well_done + "\n" + played_games, wins = get_stats(playing_user) + winned_games = get_locale("winned_games", player_lang2) + + wins_of_many = get_locale("wins_of_many", player_lang2) + + toot_text += '\n\n' + winned_games + "\n" + toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" elif check == False and stalemate == True: @@ -1811,12 +2009,24 @@ if __name__ == '__main__': else: + player_lang = get_lang(playing_user) + + your_turn = get_locale("your_turn", player_lang) + toot_text = '@'+playing_user + ' ' + your_turn + '\n' if capture == True and checkmate == False: + player_lang = get_lang(playing_user) + lost_piece = get_locale("lost_piece", player_lang) + piece_name = get_locale("piece_name", player_lang) + toot_text += '\n' + lost_piece + ' ' + piece_name + '!\n' + game_name = get_locale("game_name", player_lang) + + chess_hashtag = get_locale("chess_hashtag", player_lang) + toot_text += '\n' + game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' if username == white_user: @@ -1865,6 +2075,9 @@ if __name__ == '__main__': print(v_error) + username_lang = get_lang(username) + not_legal_move_str = get_locale("not_legal_move_str", username_lang) + toot_text = '@'+username + ' ' + not_legal_move_str + ' ' + moving + '!?)\n' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) @@ -1877,6 +2090,9 @@ if __name__ == '__main__': print(a_error) + username_lang = get_lang(username) + not_legal_move_str = get_locale("not_legal_move_str", username_lang) + toot_text = '@'+username + ' ' + not_legal_move_str + ' ' + moving + '!?)\n' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) @@ -1895,6 +2111,8 @@ if __name__ == '__main__': if username == white_user: + player_leave_game = get_locale("player_leave_game", player_lang) + toot_text = '@'+username + ' ' + player_leave_game + ' ' + '@'+black_user mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) @@ -1923,6 +2141,8 @@ if __name__ == '__main__': else: + leave_waiting_game = get_locale("leave_waiting_game", player_lang) + toot_text = '@'+username + ' ' + leave_waiting_game mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) @@ -1941,6 +2161,8 @@ if __name__ == '__main__': if len(player1_name_lst) > 0: + started_games = get_locale("started_games", player_lang) + toot_text = "@"+username + ' ' + started_games + "\n" i = 0 @@ -1948,10 +2170,14 @@ if __name__ == '__main__': if game_status_lst[i] == 'waiting': + game_is_waiting = get_locale("game_is_waiting", player_lang) + toot_text += '\n' + player1_name_lst[i] + ' / ' + player2_name_lst[i] + ' ' + game_is_waiting + "\n" else: + game_is_on_going = get_locale("game_is_on_going", player_lang) + if next_move_lst[i] == player1_name_lst[i]: toot_text += '\n*' + player1_name_lst[i] + ' / ' + player2_name_lst[i] + ' ' + game_is_on_going + '\n' @@ -1970,6 +2196,8 @@ if __name__ == '__main__': else: + no_on_going_games = get_locale("no_on_going_games", player_lang) + toot_text = '@'+username + ' ' + no_on_going_games + '\n' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) @@ -1984,16 +2212,26 @@ if __name__ == '__main__': emailed, game_id, game_found = send_anotation(send_game) + username_lang = get_lang(username) + if emailed == False and game_found == True: + send_error = get_locale("send_error", username_lang) + toot_text = '@'+username + ' ' + send_error elif emailed == True and game_found == True: + game_number_anotations = get_locale("game_number_anotations", username_lang) + anotations_sent = get_locale("anotations_sent", username_lang) + toot_text = '@'+username + ' ' + game_number_anotations + str(game_id) + ' ' + anotations_sent elif emailed == False and game_found == False: + game_no_exists = get_locale("game_no_exists", username_lang) + it_not_exists = get_locale("it_not_exists", username_lang) + toot_text = '@'+username + ' ' + game_no_exists + str(game_id) + ' ' + it_not_exists mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) @@ -2010,6 +2248,12 @@ if __name__ == '__main__': close_game(username, checkmate) + 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) + toot_text = '@'+white_player + ' ' + draw_and_str + ' ' + '@'+black_player + ' ' + agreed_draw_str + '\n\n' toot_text += '\n' + winned_games + "\n" @@ -2018,6 +2262,18 @@ if __name__ == '__main__': toot_text += white_player + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + 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: + + toot_text += '\n@'+white_player + ' ' + draw_and_str + ' ' + '@'+black_player + ' ' + agreed_draw_str + '\n\n' + + toot_text += '\n' + winned_games + "\n" + played_games, wins = get_stats(black_player) toot_text += black_player + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" @@ -2046,6 +2302,30 @@ if __name__ == '__main__': update_replies(status_id, username, now) + elif query_word[:4] == search_config: + + new_lang = query_word[5:7] + + lang_changed, player_lang = set_lang(account_id, username, new_lang) + + update_replies(status_id, username, now) + + if lang_changed: + + locale_change_successfully = get_locale("locale_change_successfully", new_lang) + + toot_text = '@'+username + ' ' + locale_change_successfully + ' ' + new_lang + + mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) + + else: + + locale_not_changed = get_locale("locale_not_changed", player_lang) + + toot_text = '@'+username + ' ' + new_lang + ' ' + locale_not_changed + + mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) + elif query_word == search_help: toot_help() @@ -2064,14 +2344,23 @@ if __name__ == '__main__': if playing_user == None: + username_lang = get_lang(username) + is_not_your_turn = get_locale("is_not_your_turn", username_lang) + toot_text = '@'+username + ' ' + is_not_your_turn + '\n' else: + is_the_turn_of = get_locale("is_the_turn_of", player_lang) + toot_text = '@'+username + ' ' + is_the_turn_of + ' ' + playing_user + "\n" toot_text += '\n' + game_name = get_locale("game_name", player_lang) + + chess_hashtag = get_locale("chess_hashtag", player_lang) + toot_text += game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' board = chess.Board(on_going_game) -- 2.34.1 From 13ddb3b25b0bb097a39979eb6e40c2bfde211571 Mon Sep 17 00:00:00 2001 From: spla Date: Mon, 21 Dec 2020 10:23:04 +0100 Subject: [PATCH 2/8] New feature! Added Elo rating system! --- README.md | 4 +- app/locales/ca.txt | 1 + app/locales/en.txt | 1 + app/locales/es.txt | 1 + app/locales/fr.txt | 1 + db-setup.py | 2 +- mastochess.py | 133 +++++++++++++++++++++++++++++++++++++++------ 7 files changed, 123 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 69003f0..936a9d8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Mastodon Chess Play with other fediverse users a Chess game! Mastodon Chess control games, players and boards and even it post, graphically, every move to both players! Mastodon Chess (mastochess) uses [python-chess](https://python-chess.readthedocs.io/en/latest/) library. +Mastodon Chess uses Elo rating system to calculate the relative skill levels of fediverse players! ### How to play: @@ -99,4 +100,5 @@ Within Python Virtual Environment: 04.12.2020 - New feature! Now players can claim a draw. 05.12.2020 - New feature! Add panel stats. 19.12.2020 - New feature! Now you can configure bot's language! -19.12.2020 - New feature! Added french language! +19.12.2020 - New feature! Added french language! +21.12.2020 - New feature! Added Elo rating system! diff --git a/app/locales/ca.txt b/app/locales/ca.txt index e86bfe2..36ecd28 100644 --- a/app/locales/ca.txt +++ b/app/locales/ca.txt @@ -71,3 +71,4 @@ post_my_panel_str: panell (publica les estadístiques) locale_change_successfully: llengua canviada amb èxit a locale_not_changed: encara no és suportada :-( change_lang_str: conf ca (per a configurar el bot en català) +panel_elo_rating_str: Elo diff --git a/app/locales/en.txt b/app/locales/en.txt index 70cb02d..8718dd1 100644 --- a/app/locales/en.txt +++ b/app/locales/en.txt @@ -71,3 +71,4 @@ post_my_panel_str: panel (post player stats) locale_change_successfully: language sucessfully changed to locale_not_changed: is not supported yet :-( change_lang_str: conf en (to configure the bot in english) +panel_elo_rating_str: Elo diff --git a/app/locales/es.txt b/app/locales/es.txt index 82e8b74..fea1af5 100644 --- a/app/locales/es.txt +++ b/app/locales/es.txt @@ -71,3 +71,4 @@ post_my_panel_str: panel (publica el panel de datos) locale_change_successfully: idioma cambiado con éxito a locale_not_changed: no es soportado aún :-( change_lang_str: conf es (para configurar el bot en castellano) +panel_elo_rating_str: Elo diff --git a/app/locales/fr.txt b/app/locales/fr.txt index 56f027f..de86e82 100644 --- a/app/locales/fr.txt +++ b/app/locales/fr.txt @@ -71,3 +71,4 @@ post_my_panel_str: panneau (publie les statistiques) locale_change_successfully: langue modifiée avec succès en locale_not_changed: n'est pas encore pris en charge :-( change_lang_str: conf fr (pour configurer le bot en français) +panel_elo_rating_str: Elo diff --git a/db-setup.py b/db-setup.py index bf8763c..10c03f0 100644 --- a/db-setup.py +++ b/db-setup.py @@ -169,7 +169,7 @@ if __name__ == '__main__': create_table(db, db_user, table, sql) table = "players" - sql = "create table "+table+" (player_id bigint PRIMARY KEY, player_name varchar(40), lang varchar(2))" + sql = "create table "+table+" (player_id bigint PRIMARY KEY, player_name varchar(40), lang varchar(2), elo_rating float)" create_table(db, db_user, table, sql) ############################################################ diff --git a/mastochess.py b/mastochess.py index cc88d15..b848417 100644 --- a/mastochess.py +++ b/mastochess.py @@ -20,7 +20,7 @@ import chess.svg from cairosvg import svg2png import chess.pgn from PIL import Image, ImageFont, ImageDraw -import lichess.api +import math def cleanhtml(raw_html): cleanr = re.compile('<.*?>') @@ -31,7 +31,48 @@ def unescape(s): s = s.replace("'", "'") return s -def create_panel(username, played_games, wins): +# 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): if played_games > 0 and wins > 0: @@ -77,10 +118,12 @@ def create_panel(username, played_games, wins): 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) + panel_elo_rating_str = get_locale("panel_elo_rating_str", player_lang) - draw.text((y+70,x+120), 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+170), panel_wins_str + ': ' + str(wins), font=fnt, fill=(255,255,255,220)) #fill=(255,255,255,255)) ## full opacity - draw.text((y+70,x+220), panel_ratio_str + ': ' + str(ratio) + '%', font=fnt, fill=(255,255,255,220)) + 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)) fnt = ImageFont.truetype('app/fonts/DroidSans.ttf', 15, layout_engine=ImageFont.LAYOUT_BASIC) @@ -978,6 +1021,8 @@ def claim_draw(username): def close_game(username, checkmate): + d = 0 + try: conn = None @@ -996,6 +1041,30 @@ def close_game(username, checkmate): black_player = row[1] + cur.execute("select elo_rating from players where player_name = (%s)", (white_player,)) + + row = cur.fetchone() + + if row[0] != None: + + white_rating = row[0] + + else: + + white_rating = 1500 + + cur.execute("select elo_rating from players where player_name = (%s)", (black_player,)) + + row = cur.fetchone() + + if row[0] != None: + + black_rating = row[0] + + else: + + black_rating = 1500 + cur.close() except (Exception, psycopg2.DatabaseError) as error: @@ -1028,16 +1097,28 @@ def close_game(username, checkmate): winner = username + if winner == white_player: + + d = 1 + else: if query_word == search_end and username == white_user and stalemate == False: winner = black_user + d = 2 + elif query_word == search_end and username == black_user and stalemate == False: winner = white_user + d = 1 + + K = 30 + + new_white_rating, new_black_rating = EloRating(white_rating, black_rating, K, d) + try: conn = None @@ -1050,6 +1131,10 @@ def close_game(username, checkmate): cur.execute("update stats set winner=(%s), finished=(%s), updated_at=(%s) where game_id=(%s)", (winner, finished, now, game_id)) + 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)) + conn.commit() cur.close() @@ -1094,9 +1179,21 @@ def get_stats(player): wins = row[0] + 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 + cur.close() - return (played_games, wins) + return (rating, played_games, wins) except (Exception, psycopg2.DatabaseError) as error: @@ -1745,9 +1842,9 @@ if __name__ == '__main__': elif query_word == search_panel: - played_games, wins = get_stats(username) + rating, played_games, wins = get_stats(username) - create_panel(username, played_games, wins) + create_panel(username, rating, played_games, wins) toot_text = '@'+username @@ -1969,11 +2066,11 @@ if __name__ == '__main__': toot_text += '\n' + winned_games + "\n" - played_games, wins = get_stats(username) + rating, played_games, wins = get_stats(username) wins_of_many = get_locale("wins_of_many", player_lang1) - toot_text += username + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + toot_text += username + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + ' (Elo: ' + str(round(rating)) + ')' + '\n' player_lang2 = get_lang(playing_user) @@ -1981,7 +2078,7 @@ if __name__ == '__main__': toot_text += "\n@"+playing_user + ': ' + well_done + "\n" - played_games, wins = get_stats(playing_user) + rating, played_games, wins = get_stats(playing_user) winned_games = get_locale("winned_games", player_lang2) @@ -1989,7 +2086,7 @@ if __name__ == '__main__': toot_text += '\n\n' + winned_games + "\n" - toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + ' (Elo: ' + str(round(rating)) + ')' + '\n' elif check == False and stalemate == True: @@ -1999,11 +2096,11 @@ if __name__ == '__main__': toot_text += '\n' + winned_games + "\n" - played_games, wins = get_stats(username) + rating, played_games, wins = get_stats(username) toot_text += username + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" - played_games, wins = get_stats(playing_user) + rating, played_games, wins = get_stats(playing_user) toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" @@ -2258,7 +2355,7 @@ if __name__ == '__main__': toot_text += '\n' + winned_games + "\n" - played_games, wins = get_stats(white_player) + rating, played_games, wins = get_stats(white_player) toot_text += white_player + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" @@ -2274,7 +2371,7 @@ if __name__ == '__main__': toot_text += '\n' + winned_games + "\n" - played_games, wins = get_stats(black_player) + rating, played_games, wins = get_stats(black_player) toot_text += black_player + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" @@ -2288,9 +2385,9 @@ if __name__ == '__main__': elif query_word == search_panel: - played_games, wins = get_stats(username) + rating, played_games, wins = get_stats(username) - create_panel(username, played_games, wins) + create_panel(username, rating, played_games, wins) toot_text = '@'+username -- 2.34.1 From 845746cf8fc197773946574d876f0a256989cccd Mon Sep 17 00:00:00 2001 From: spla Date: Mon, 28 Dec 2020 14:06:27 +0100 Subject: [PATCH 3/8] Fix #13. Removed unneeded line --- mastochess.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mastochess.py b/mastochess.py index b848417..ea34356 100644 --- a/mastochess.py +++ b/mastochess.py @@ -1,4 +1,3 @@ -import pdb import sys import os import os.path @@ -2116,7 +2115,6 @@ if __name__ == '__main__': player_lang = get_lang(playing_user) lost_piece = get_locale("lost_piece", player_lang) - piece_name = get_locale("piece_name", player_lang) toot_text += '\n' + lost_piece + ' ' + piece_name + '!\n' -- 2.34.1 From 67af0c8f7a8da93904a4f82be0e044c37377ebb0 Mon Sep 17 00:00:00 2001 From: spla Date: Fri, 1 Jan 2021 17:44:58 +0100 Subject: [PATCH 4/8] Fix #14 --- mastochess.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mastochess.py b/mastochess.py index ea34356..0107025 100644 --- a/mastochess.py +++ b/mastochess.py @@ -2222,6 +2222,8 @@ if __name__ == '__main__': else: + player_leave_game = get_locale("player_leave_game", player_lang) + toot_text = '@'+username + ' ' + player_leave_game + ' ' + white_user mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) -- 2.34.1 From 7d7ffe33702bbd3b1d8852412a745045a5559e3c Mon Sep 17 00:00:00 2001 From: spla Date: Fri, 1 Jan 2021 18:09:49 +0100 Subject: [PATCH 5/8] Mention two players when any leaves the game --- mastochess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mastochess.py b/mastochess.py index 0107025..8e90031 100644 --- a/mastochess.py +++ b/mastochess.py @@ -2224,7 +2224,7 @@ if __name__ == '__main__': player_leave_game = get_locale("player_leave_game", player_lang) - toot_text = '@'+username + ' ' + player_leave_game + ' ' + white_user + toot_text = '@'+username + ' ' + player_leave_game + ' ' + '@'+white_user mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) -- 2.34.1 From eb2df2d0a8ceb31cc9822a0faca34529f9ce1775 Mon Sep 17 00:00:00 2001 From: spla Date: Fri, 1 Jan 2021 18:59:21 +0100 Subject: [PATCH 6/8] Fix #15 --- mastochess.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/mastochess.py b/mastochess.py index 8e90031..91710d4 100644 --- a/mastochess.py +++ b/mastochess.py @@ -1044,9 +1044,15 @@ def close_game(username, checkmate): row = cur.fetchone() - if row[0] != None: + if row != None: - white_rating = row[0] + if row[0] != None: + + white_rating = row[0] + + else: + + white_rating = 1500 else: @@ -1056,9 +1062,15 @@ def close_game(username, checkmate): row = cur.fetchone() - if row[0] != None: + if row != None: - black_rating = row[0] + if row[0] != None: + + black_rating = row[0] + + else: + + black_rating = 1500 else: -- 2.34.1 From c3027ccd042127bf810a926f3b7fad15f811a1db Mon Sep 17 00:00:00 2001 From: spla Date: Mon, 3 May 2021 15:07:42 +0200 Subject: [PATCH 7/8] Detect if DST is in effect --- mastochess.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mastochess.py b/mastochess.py index 91710d4..bc7dd35 100644 --- a/mastochess.py +++ b/mastochess.py @@ -4,6 +4,7 @@ import os.path import re import unidecode from datetime import datetime, timedelta +import time from mastodon import Mastodon from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText @@ -241,11 +242,19 @@ def get_piece_name(captured_piece): return piece_name def get_mentions(): - + account_id_lst = [] status_id_lst = [] + if time.localtime().tm_isdst == 0: + + interval_time = '60 minutes' + + elif time.localtime().tm_isdst == 1: + + interval_time = '120 minutes' + conn = None try: @@ -254,7 +263,7 @@ def get_mentions(): cur = conn.cursor() - select_query = "select account_id, id from statuses where created_at + interval '60 minutes' > now() - interval '5 minutes'" + select_query = "select account_id, id from statuses where created_at + interval '" + interval_time + "' > now() - interval '5 minutes'" select_query += " and id=any (select status_id from mentions where account_id=(%s)) order by created_at asc" cur.execute(select_query, (str(bot_id),)) -- 2.34.1 From 6a5638cfb7706a7915219c6df950597f0de2d4bc Mon Sep 17 00:00:00 2001 From: spla Date: Thu, 17 Mar 2022 12:53:30 +0100 Subject: [PATCH 8/8] Changed notifications management to Mastodon API --- mastochess.py | 452 ++++++++++++----------------------------------- requirements.txt | 1 + 2 files changed, 115 insertions(+), 338 deletions(-) diff --git a/mastochess.py b/mastochess.py index 91710d4..8553c8b 100644 --- a/mastochess.py +++ b/mastochess.py @@ -4,6 +4,7 @@ import os.path import re import unidecode from datetime import datetime, timedelta +import time from mastodon import Mastodon from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText @@ -20,6 +21,7 @@ from cairosvg import svg2png import chess.pgn from PIL import Image, ImageFont, ImageDraw import math +import pdb def cleanhtml(raw_html): cleanr = re.compile('<.*?>') @@ -131,75 +133,6 @@ def create_panel(username, rating, played_games, wins): out = Image.alpha_composite(base, txt) out.save('app/panel/' + username + '_panel.png') -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() - -def get_user_domain(account_id): - - 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 username, domain from accounts where id=(%s)", (account_id,)) - - row = cur.fetchone() - - if row != None: - - username = row[0] - - domain = row[1] - - cur.close() - - return (username, domain) - - except (Exception, psycopg2.DatabaseError) as error: - - print(error) - - finally: - - if conn is not None: - - conn.close() - def get_piece_name(captured_piece): if captured_piece == 1: @@ -240,53 +173,7 @@ def get_piece_name(captured_piece): return piece_name -def get_mentions(): - - account_id_lst = [] - - status_id_lst = [] - - conn = None - - try: - - conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") - - cur = conn.cursor() - - select_query = "select account_id, id from statuses where created_at + interval '60 minutes' > now() - interval '5 minutes'" - select_query += " and id=any (select status_id from mentions where account_id=(%s)) order by created_at asc" - - cur.execute(select_query, (str(bot_id),)) - - rows = cur.fetchall() - - for row in rows: - - replied = check_replies(row[1]) - - if not replied: - - account_id_lst.append(row[0]) - - status_id_lst.append(row[1]) - - cur.close() - - return (account_id_lst, status_id_lst) - - except (Exception, psycopg2.DatabaseError) as error: - - print(error) - - finally: - - if conn is not None: - - conn.close() - - -def get_mention_langs(account_id): +def get_player_langs(account_id): lang_changed = False @@ -360,7 +247,6 @@ def get_lang(player): conn.close() - def set_lang(account_id, username, new_lang): lang_changed = False @@ -441,119 +327,6 @@ def get_locale( parameter, player_lang): print(language_filepath + " Missing parameter %s "%parameter) sys.exit(0) -def get_notification_data(status_id): - - conn = None - - try: - - conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") - - cur = conn.cursor() - - select_query = "select text, visibility, url from statuses where id=(%s)" - - cur.execute(select_query, (status_id,)) - - row = cur.fetchone() - - text = row[0] - - if row[1] == 0: - visibility = 'public' - elif row[1] == 1: - visibility = 'unlisted' - elif row[1] == 2: - visibility = 'private' - elif row[1] == 3: - visibility = 'direct' - - url = row[2] - - cur.close() - - return (text, visibility, url) - - 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() - def current_games(): player1_name_lst = [] @@ -1635,24 +1408,46 @@ if __name__ == '__main__': now = datetime.now() - bot_id = get_bot_id() + #################################################################### + # get notifications - account_id_lst, status_id_lst = get_mentions() + notifications = mastodon.notifications() - if len(account_id_lst) == 0: + if len(notifications) == 0: print('No mentions') + sys.exit(0) i = 0 - while i < len(account_id_lst): + while i < len(notifications): - account_id = account_id_lst[i] + notification_id = notifications[i].id - username, domain = get_user_domain(account_id) + if notifications[i].type != 'mention': - lang_changed, player_lang = get_mention_langs(account_id) + i += 1 + + print(f'dismissing notification {notification_id}') + + 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) if player_lang == 'ca': @@ -1686,24 +1481,6 @@ if __name__ == '__main__': sys.exit(0) - status_id = status_id_lst[i] - - text, visibility, url = get_notification_data(status_id) - - if domain != None: - - username = username + '@' + domain - - status_id = status_id_lst[i] - - replied = check_replies(status_id) - - if replied == True: - - i += 1 - - continue - # listen them or not search_new = get_locale("search_new", player_lang) @@ -1746,7 +1523,7 @@ if __name__ == '__main__': new_game_started = get_locale("new_game_started", player_lang) - toot_text = '@'+username + ' ' + new_game_started + '\n' + toot_text = f'@{username} {new_game_started} \n' toot_text += '\n' @@ -1758,7 +1535,7 @@ if __name__ == '__main__': new_game(toot_url) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif query_word == search_new and game_waiting: @@ -1780,22 +1557,18 @@ if __name__ == '__main__': playing_with = get_locale("playing_with", player_lang1) - toot_text = '@'+username + ' ' + playing_with + ' ' + white_user + "\n" - - toot_text += '\n' + toot_text = f'@{username} {playing_with} {white_user} \n\n' player_lang2 = get_lang(white_user) your_turn = get_locale("your_turn", player_lang2) - toot_text += '@'+white_user + ': ' + your_turn + "\n" - - toot_text += '\n' + toot_text += f'@{white_user}: {your_turn}\n\n' game_name = get_locale("game_name", player_lang2) chess_hashtag = get_locale("chess_hashtag", player_lang) - toot_text += game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' + toot_text += f"{game_name}: {str(game_id)} {chess_hashtag} \n" image_id = mastodon.media_post(board_file, "image/png").id @@ -1805,7 +1578,7 @@ if __name__ == '__main__': update_moves(username, game_moves) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif query_word[:search_send_slicing] == search_send: @@ -1820,7 +1593,7 @@ if __name__ == '__main__': username_lang = get_lang(username) send_error = get_locale("send_error", username_lang) - toot_text = '@'+username + ' ' + send_error + toot_text = f'@{username} {send_error}' elif emailed == True and game_found == True: @@ -1828,7 +1601,7 @@ if __name__ == '__main__': game_number_anotations = get_locale("game_number_anotations", username_lang) anotations_sent = get_locale("anotations_sent", username_lang) - toot_text = '@'+username + ' ' + game_number_anotations + str(game_id) + ' ' + anotations_sent + toot_text = f'@{username} {game_number_anotations} {str(game_id)} {anotations_sent}' elif emailed == False and game_found == False: @@ -1837,7 +1610,7 @@ if __name__ == '__main__': username_lang = get_lang(username) cant_send_to_fediverse_account = get_locale("cant_send_to_fediverse_account", username_lang) - toot_text = '@'+username + ' ' + cant_send_to_fediverse_account + toot_text = f'@{username} {cant_send_to_fediverse_account}' else: @@ -1845,11 +1618,11 @@ if __name__ == '__main__': game_no_exists = get_locale("game_no_exists", username_lang) it_not_exists = get_locale("it_not_exists", username_lang) - toot_text = '@'+username + ' ' + game_no_exists + str(game_id) + ' ' + it_not_exists + toot_text = f'@{username} {game_no_exists} {str(game_id)} {it_not_exists}' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif query_word == search_panel: @@ -1857,7 +1630,7 @@ if __name__ == '__main__': create_panel(username, rating, played_games, wins) - toot_text = '@'+username + toot_text = f'@{username}' saved_panel = 'app/panel/' + username + '_panel.png' @@ -1865,7 +1638,7 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility, media_ids={image_id}) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif query_word[:4] == search_config: @@ -1873,13 +1646,13 @@ if __name__ == '__main__': lang_changed, player_lang = set_lang(account_id, username, new_lang) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) if lang_changed: locale_change_successfully = get_locale("locale_change_successfully", new_lang) - toot_text = '@'+username + ' ' + locale_change_successfully + ' ' + new_lang + toot_text = f'@{username} {locale_change_successfully} {new_lang}' mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) @@ -1887,7 +1660,7 @@ if __name__ == '__main__': locale_not_changed = get_locale("locale_not_changed", player_lang) - toot_text = '@'+username + ' ' + new_lang + ' ' + locale_not_changed + toot_text = f'@{username} {new_lang} {locale_not_changed}' mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) @@ -1895,7 +1668,7 @@ if __name__ == '__main__': toot_help() - help_text = '@'+username + help_text = f'@{username}' help_panel = 'app/panel/help_panel.png' @@ -1903,11 +1676,11 @@ if __name__ == '__main__': mastodon.status_post(help_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) else: - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif reply and is_playing: @@ -1917,11 +1690,11 @@ if __name__ == '__main__': game_already_started = get_locale("game_already_started", player_lang1) - toot_text = '@'+username + ' ' + game_already_started + '\n' + toot_text = f'@{username} {game_already_started} \n' if black_user != '': - toot_text += '@'+white_user + ' / ' + '@'+black_user + '\n' + toot_text += f'@{white_user} / @{black_user}\n' else: @@ -1934,7 +1707,7 @@ if __name__ == '__main__': game_name = get_locale("game_name", player_lang1) chess_hashtag = get_locale("chess_hashtag", player_lang1) - toot_text += game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' + toot_text += f'{game_name}: {str(game_id)} {chess_hashtag} \n' board = chess.Board(on_going_game) @@ -1948,7 +1721,7 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif query_word[:search_move_slicing] == search_move and playing_user == username: @@ -1996,11 +1769,11 @@ if __name__ == '__main__': is_not_legal_move = get_locale("is_not_legal_move", player_lang) - toot_text = '@'+username + ': ' + moving + ' ' + is_not_legal_move + '\n' + toot_text = f'@{username}: {moving} {is_not_legal_move} \n' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) else: @@ -2061,7 +1834,7 @@ if __name__ == '__main__': player_lang = get_lang(playing_user) check_done = get_locale("check_done", player_lang) - toot_text = "@"+playing_user + " " + username + ' ' + check_done + '\n' + toot_text = f"@{playing_user} {username} {check_done}\n" elif check == True and checkmate == True: @@ -2071,23 +1844,23 @@ if __name__ == '__main__': check_mate_movements = get_locale("check_mate_movements", player_lang1) the_winner_is = get_locale("the_winner_is", player_lang1) - toot_text = '\n' + check_mate + ' ' + str(game_moves) + ' ' + check_mate_movements + '\n\n' + the_winner_is + ' ' + "@"+username + '\n' + toot_text = f'\n{check_mate} {str(game_moves)} {check_mate_movements}\n\n{the_winner_is} @{username}\n' winned_games = get_locale("winned_games", player_lang1) - toot_text += '\n' + winned_games + "\n" + toot_text += f'\n{winned_games}\n' rating, played_games, wins = get_stats(username) wins_of_many = get_locale("wins_of_many", player_lang1) - toot_text += username + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + ' (Elo: ' + str(round(rating)) + ')' + '\n' + toot_text += f'{username}: {str(wins)} {wins_of_many} {str(played_games)} (Elo: {str(round(rating))})\n' player_lang2 = get_lang(playing_user) well_done = get_locale("well_done", player_lang2) - toot_text += "\n@"+playing_user + ': ' + well_done + "\n" + toot_text += f"\n@{playing_user}: {well_done}\n" rating, played_games, wins = get_stats(playing_user) @@ -2095,25 +1868,25 @@ if __name__ == '__main__': wins_of_many = get_locale("wins_of_many", player_lang2) - toot_text += '\n\n' + winned_games + "\n" + toot_text += f'\n\n{winned_games}\n' - toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + ' (Elo: ' + str(round(rating)) + ')' + '\n' + toot_text += f'{playing_user}: {str(wins)} {wins_of_many} {str(played_games)} (Elo: {str(round(rating))})\n' elif check == False and stalemate == True: toot_text = stalemate_str + ' (' + str(game_moves) + ')' + '\n' - toot_text += '\n@'+playing_user + ', ' + '@'+username + "\n" + toot_text += f'\n@{playing_user}, @{username}\n' - toot_text += '\n' + winned_games + "\n" + toot_text += f'\n{winned_games}\n' rating, played_games, wins = get_stats(username) - toot_text += username + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + toot_text += f'{username}: {str(wins)} {wins_of_many} {str(played_games)}\n' rating, played_games, wins = get_stats(playing_user) - toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + toot_text += f'{playing_user}: {str(wins)} {wins_of_many} {str(played_games)}\n' else: @@ -2121,20 +1894,20 @@ if __name__ == '__main__': your_turn = get_locale("your_turn", player_lang) - toot_text = '@'+playing_user + ' ' + your_turn + '\n' + toot_text = f'@{playing_user} {your_turn}\n' if capture == True and checkmate == False: player_lang = get_lang(playing_user) lost_piece = get_locale("lost_piece", player_lang) - toot_text += '\n' + lost_piece + ' ' + piece_name + '!\n' + toot_text += f'\n{lost_piece} {piece_name}!\n' game_name = get_locale("game_name", player_lang) chess_hashtag = get_locale("chess_hashtag", player_lang) - toot_text += '\n' + game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' + toot_text += f'\n{game_name}: {str(game_id)} {chess_hashtag}\n' if username == white_user: @@ -2176,7 +1949,7 @@ if __name__ == '__main__': update_moves(username, game_moves) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) except ValueError as v_error: @@ -2185,11 +1958,11 @@ if __name__ == '__main__': username_lang = get_lang(username) not_legal_move_str = get_locale("not_legal_move_str", username_lang) - toot_text = '@'+username + ' ' + not_legal_move_str + ' ' + moving + '!?)\n' + toot_text = f'@{username} {not_legal_move_str} {moving}!?)\n' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) pass @@ -2200,11 +1973,11 @@ if __name__ == '__main__': username_lang = get_lang(username) not_legal_move_str = get_locale("not_legal_move_str", username_lang) - toot_text = '@'+username + ' ' + not_legal_move_str + ' ' + moving + '!?)\n' + toot_text = f'@{username} {not_legal_move_str} {moving}!?)\n' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) pass @@ -2220,13 +1993,13 @@ if __name__ == '__main__': player_leave_game = get_locale("player_leave_game", player_lang) - toot_text = '@'+username + ' ' + player_leave_game + ' ' + '@'+black_user + toot_text = f'@{username} {player_leave_game} @{black_user}' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) close_game(username, checkmate) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) i += 1 @@ -2236,13 +2009,13 @@ if __name__ == '__main__': player_leave_game = get_locale("player_leave_game", player_lang) - toot_text = '@'+username + ' ' + player_leave_game + ' ' + '@'+white_user + toot_text = f'@{username} {player_leave_game} @{white_user}' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) close_game(username, checkmate) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) i += 1 @@ -2252,13 +2025,13 @@ if __name__ == '__main__': leave_waiting_game = get_locale("leave_waiting_game", player_lang) - toot_text = '@'+username + ' ' + leave_waiting_game + toot_text = f'@{username} {leave_waiting_game}' mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) close_game(username, checkmate) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) i += 1 @@ -2272,7 +2045,7 @@ if __name__ == '__main__': started_games = get_locale("started_games", player_lang) - toot_text = "@"+username + ' ' + started_games + "\n" + toot_text = f"@{username} {started_games}\n" i = 0 while i < len(player1_name_lst): @@ -2281,7 +2054,7 @@ if __name__ == '__main__': game_is_waiting = get_locale("game_is_waiting", player_lang) - toot_text += '\n' + player1_name_lst[i] + ' / ' + player2_name_lst[i] + ' ' + game_is_waiting + "\n" + toot_text += f'\n{player1_name_lst[i]} / {player2_name_lst[i]} {game_is_waiting}\n' else: @@ -2289,15 +2062,15 @@ if __name__ == '__main__': if next_move_lst[i] == player1_name_lst[i]: - toot_text += '\n*' + player1_name_lst[i] + ' / ' + player2_name_lst[i] + ' ' + game_is_on_going + '\n' + toot_text += f'\n*{player1_name_lst[i]} / {player2_name_lst[i]} {game_is_on_going}\n' else: - toot_text += '\n' + player1_name_lst[i] + ' / *' + player2_name_lst[i] + ' ' + game_is_on_going + '\n' + toot_text += f'\n{player1_name_lst[i]} / *{player2_name_lst[i]} {game_is_on_going}\n' if game_link_lst[i] != None: - toot_text += str(game_link_lst[i]) + "\n" + toot_text += f'{str(game_link_lst[i])}\n' i += 1 @@ -2307,11 +2080,11 @@ if __name__ == '__main__': no_on_going_games = get_locale("no_on_going_games", player_lang) - toot_text = '@'+username + ' ' + no_on_going_games + '\n' + toot_text = f'@{username} {no_on_going_games}\n' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif query_word[:search_send_slicing] == search_send: @@ -2327,25 +2100,25 @@ if __name__ == '__main__': send_error = get_locale("send_error", username_lang) - toot_text = '@'+username + ' ' + send_error + toot_text = f'@{username} {send_error}' elif emailed == True and game_found == True: game_number_anotations = get_locale("game_number_anotations", username_lang) anotations_sent = get_locale("anotations_sent", username_lang) - toot_text = '@'+username + ' ' + game_number_anotations + str(game_id) + ' ' + anotations_sent + toot_text = f'@{username} {game_number_anotations} {str(game_id)} {anotations_sent}' elif emailed == False and game_found == False: game_no_exists = get_locale("game_no_exists", username_lang) it_not_exists = get_locale("it_not_exists", username_lang) - toot_text = '@'+username + ' ' + game_no_exists + str(game_id) + ' ' + it_not_exists + toot_text = f'@{username} {game_no_exists} {str(game_id)} {it_not_exists}' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif query_word == search_draw: @@ -2363,13 +2136,13 @@ if __name__ == '__main__': winned_games = get_locale("winned_games", player_lang1) wins_of_many = get_locale("wins_of_many", player_lang1) - toot_text = '@'+white_player + ' ' + draw_and_str + ' ' + '@'+black_player + ' ' + agreed_draw_str + '\n\n' + toot_text = f'@{white_player} {draw_and_str} @{black_player} {agreed_draw_str}\n\n' - toot_text += '\n' + winned_games + "\n" + toot_text += f'\n{winned_games}\n' rating, played_games, wins = get_stats(white_player) - toot_text += white_player + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + toot_text += f'{white_player}: {str(wins)} {wins_of_many} {str(played_games)}\n' player_lang2 = get_lang(black_player) draw_and_str = get_locale("draw_and_str", player_lang2) @@ -2379,13 +2152,13 @@ if __name__ == '__main__': if player_lang1 != player_lang2: - toot_text += '\n@'+white_player + ' ' + draw_and_str + ' ' + '@'+black_player + ' ' + agreed_draw_str + '\n\n' + toot_text += f'\n@{white_player} {draw_and_str} @{black_player} {agreed_draw_str}\n\n' - toot_text += '\n' + winned_games + "\n" + toot_text += f'\n{winned_games}\n' rating, played_games, wins = get_stats(black_player) - toot_text += black_player + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" + toot_text += f'{black_player}: {str(wins)} {wins_of_many} {str(played_games)}\n' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) @@ -2393,7 +2166,7 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif query_word == search_panel: @@ -2401,7 +2174,7 @@ if __name__ == '__main__': create_panel(username, rating, played_games, wins) - toot_text = '@'+username + toot_text = f'@{username}' saved_panel = 'app/panel/' + username + '_panel.png' @@ -2409,7 +2182,7 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility, media_ids={image_id}) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) elif query_word[:4] == search_config: @@ -2417,13 +2190,13 @@ if __name__ == '__main__': lang_changed, player_lang = set_lang(account_id, username, new_lang) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) if lang_changed: locale_change_successfully = get_locale("locale_change_successfully", new_lang) - toot_text = '@'+username + ' ' + locale_change_successfully + ' ' + new_lang + toot_text = f'@{username} {locale_change_successfully} {new_lang}' mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) @@ -2431,7 +2204,7 @@ if __name__ == '__main__': locale_not_changed = get_locale("locale_not_changed", player_lang) - toot_text = '@'+username + ' ' + new_lang + ' ' + locale_not_changed + toot_text = f'@{username} {new_lang} {locale_not_changed}' mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) @@ -2439,7 +2212,7 @@ if __name__ == '__main__': toot_help() - help_text = '@'+username + help_text = f'@{username}' help_panel = 'app/panel/help_panel.png' @@ -2447,7 +2220,7 @@ if __name__ == '__main__': mastodon.status_post(help_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) else: @@ -2456,13 +2229,13 @@ if __name__ == '__main__': username_lang = get_lang(username) is_not_your_turn = get_locale("is_not_your_turn", username_lang) - toot_text = '@'+username + ' ' + is_not_your_turn + '\n' + toot_text = f'@{username} {is_not_your_turn}\n' else: is_the_turn_of = get_locale("is_the_turn_of", player_lang) - toot_text = '@'+username + ' ' + is_the_turn_of + ' ' + playing_user + "\n" + toot_text = f'@{username} {is_the_turn_of} {playing_user}\n' toot_text += '\n' @@ -2470,7 +2243,7 @@ if __name__ == '__main__': chess_hashtag = get_locale("chess_hashtag", player_lang) - toot_text += game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' + toot_text += f'{game_name}: {str(game_id)} {chess_hashtag}\n' board = chess.Board(on_going_game) @@ -2490,8 +2263,11 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) - update_replies(status_id, username, now) + mastodon.notifications_dismiss(notification_id) + else: + + mastodon.notifications_dismiss(notification_id) i += 1 diff --git a/requirements.txt b/requirements.txt index 26a2fcf..245f95d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +wheel>=0.36.2a Mastodon.py>=1.5.1 chess>=1.3.0 psycopg2-binary>=2.8.6 -- 2.34.1