Merge branch 'game_link' into 'master'

Add link to games in games list

See merge request spla/mastochess!1
This commit is contained in:
spla 2020-11-20 09:25:02 +00:00
commit 5747485f6d
S'han modificat 3 arxius amb 81 adicions i 18 eliminacions

Veure arxiu

@ -39,4 +39,6 @@ Within Python Virtual Environment:
4. Use your favourite scheduling method to set `python mastochess.py` to run regularly. 4. Use your favourite scheduling method to set `python mastochess.py` to run regularly.
![board](board.png) ![board](board.png)
20.11.2020 - New feature! added link to on going games in games list

Veure arxiu

@ -159,7 +159,8 @@ if __name__ == '__main__':
table = "games" table = "games"
sql = "create table "+table+" (created_at timestamptz, game_id serial, white_user varchar(40), black_user varchar(40), chess_game varchar(200), " sql = "create table "+table+" (created_at timestamptz, game_id serial, white_user varchar(40), black_user varchar(40), chess_game varchar(200), "
sql +=" chess_status varchar(12), waiting boolean, updated_at timestamptz, next_move varchar(40), last_move varchar(40), moves int, finished boolean default False, PRIMARY KEY(game_id))" sql +=" chess_status varchar(12), waiting boolean, updated_at timestamptz, next_move varchar(40), last_move varchar(40), moves int, finished boolean default False, "
sql += "chess_link varchar(100), PRIMARY KEY(game_id))"
create_table(db, db_user, table, sql) create_table(db, db_user, table, sql)
table = "stats" table = "stats"

Veure arxiu

@ -101,6 +101,8 @@ def get_notification_data():
visibility_lst = [] visibility_lst = []
url_lst = []
search_text = ['fi','mou','nova','jocs'] search_text = ['fi','mou','nova','jocs']
conn = None conn = None
@ -115,7 +117,7 @@ def get_notification_data():
like_text = "%"+search_text[i]+"%" like_text = "%"+search_text[i]+"%"
select_query = "select account_id, id, text, visibility, created_at from statuses where text like (%s) and created_at + interval '60 minutes' > now() - interval '5 minutes'" 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" select_query += " and id=any (select status_id from mentions where account_id=(%s)) order by created_at asc"
cur.execute(select_query, (like_text, str(bot_id))) cur.execute(select_query, (like_text, str(bot_id)))
@ -139,11 +141,13 @@ def get_notification_data():
elif row[3] == 3: elif row[3] == 3:
visibility_lst.append('direct') visibility_lst.append('direct')
url_lst.append(row[4])
i += 1 i += 1
cur.close() cur.close()
return (account_id_lst, status_id_lst, text_lst, visibility_lst) return (account_id_lst, status_id_lst, text_lst, visibility_lst, url_lst)
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
@ -233,6 +237,8 @@ def current_games():
game_status_lst = [] game_status_lst = []
game_link_lst = []
try: try:
conn = None conn = None
@ -241,7 +247,7 @@ def current_games():
cur = conn.cursor() cur = conn.cursor()
cur.execute("select white_user, black_user, chess_status from games where not finished") cur.execute("select white_user, black_user, chess_status, chess_link from games where not finished")
rows = cur.fetchall() rows = cur.fetchall()
@ -259,9 +265,11 @@ def current_games():
game_status_lst.append(row[2]) game_status_lst.append(row[2])
game_link_lst.append(row[3])
cur.close() cur.close()
return (player1_name_lst, player2_name_lst, game_status_lst) return (player1_name_lst, player2_name_lst, game_status_lst, game_link_lst)
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
@ -369,7 +377,47 @@ def check_games():
conn.close() conn.close()
def new_game(): def get_status_url(toot_id):
toot_id = str(toot_id.id)
try:
conn = None
conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
select_query = "select uri from statuses where id=(%s)"
cur.execute(select_query, (toot_id,))
row = cur.fetchone()
if row != None:
toot_url = row[0]
else:
toot_url = ''
cur.close()
return toot_url
except (Exception, psycopg2.DatabaseError) as error:
sys.exit(error)
finally:
if conn is not None:
conn.close()
def new_game(toot_url):
try: try:
@ -385,9 +433,9 @@ def new_game():
cur = conn.cursor() cur = conn.cursor()
insert_query = 'INSERT INTO games(created_at, white_user, chess_game, chess_status, waiting, updated_at) VALUES (%s, %s, %s, %s, %s, %s) ON CONFLICT DO NOTHING' 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'
cur.execute(insert_query, (now, username, board_game, game_status, waiting, now)) cur.execute(insert_query, (now, username, board_game, game_status, waiting, now, toot_url))
insert_query = 'INSERT INTO stats(created_at, white_user) VALUES (%s,%s) ON CONFLICT DO NOTHING' insert_query = 'INSERT INTO stats(created_at, white_user) VALUES (%s,%s) ON CONFLICT DO NOTHING'
@ -407,7 +455,7 @@ def new_game():
conn.close() conn.close()
def update_game(board_game): def update_game(board_game, toot_url):
try: try:
@ -419,7 +467,7 @@ def update_game(board_game):
cur = conn.cursor() cur = conn.cursor()
cur.execute("update games set chess_game=(%s), updated_at=(%s) where game_id=(%s)", (board_game, now, game_id,)) 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,))
conn.commit() conn.commit()
@ -834,7 +882,7 @@ if __name__ == '__main__':
bot_id = get_bot_id() bot_id = get_bot_id()
account_id_lst, status_id_lst, text_lst, visibility_lst = get_notification_data() account_id_lst, status_id_lst, text_lst, visibility_lst, url_lst = get_notification_data()
i = 0 i = 0
while i < len(account_id_lst): while i < len(account_id_lst):
@ -867,6 +915,8 @@ if __name__ == '__main__':
visibility = visibility_lst[i] visibility = visibility_lst[i]
status_url = url_lst[i]
if query_word != "jocs": if query_word != "jocs":
is_playing, game_id, white_user, black_user, on_going_game, waiting, playing_user = check_games() is_playing, game_id, white_user, black_user, on_going_game, waiting, playing_user = check_games()
@ -897,9 +947,11 @@ if __name__ == '__main__':
image_id = mastodon.media_post(board_file, "image/png").id 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}) toot_id = mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id})
new_game() toot_url = get_status_url(toot_id)
new_game(toot_url)
update_replies(status_id, username, now) update_replies(status_id, username, now)
@ -1061,11 +1113,13 @@ if __name__ == '__main__':
image_id = mastodon.media_post(board_file, "image/png").id 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}) toot_id = mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id})
toot_url = get_status_url(toot_id)
board_game = board.fen() board_game = board.fen()
update_game(board_game) update_game(board_game, toot_url)
game_moves = board.ply() game_moves = board.ply()
@ -1139,7 +1193,7 @@ if __name__ == '__main__':
elif query_word == 'jocs': elif query_word == 'jocs':
player1_name_lst, player2_name_lst, game_status_lst = current_games() player1_name_lst, player2_name_lst, game_status_lst, game_link_lst = current_games()
if len(player1_name_lst) > 0: if len(player1_name_lst) > 0:
@ -1156,10 +1210,16 @@ if __name__ == '__main__':
toot_text += "\n" + player1_name_lst[i] + " / " + player2_name_lst[i] + " (en joc)" + "\n" toot_text += "\n" + player1_name_lst[i] + " / " + player2_name_lst[i] + " (en joc)" + "\n"
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) if game_link_lst[i] != None:
toot_text += str(game_link_lst[i]) + "\n"
i += 1 i += 1
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
update_replies(status_id, username, now)
else: else:
toot_text = "@"+username + " cap partida en joc" + "\n" toot_text = "@"+username + " cap partida en joc" + "\n"