Fix #2 and new feature, on going games list!

This commit is contained in:
spla 2020-11-19 14:13:23 +01:00
pare f8c76aaee0
commit d3de46a743

Veure arxiu

@ -101,7 +101,7 @@ def get_notification_data():
visibility_lst = []
search_text = ['fi','mou','nova']
search_text = ['fi','mou','nova','jocs']
conn = None
@ -115,7 +115,7 @@ def get_notification_data():
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 '60 minutes'"
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 += " 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)))
@ -225,6 +225,54 @@ def check_replies(status_id):
conn.close()
def current_games():
player1_name_lst = []
player2_name_lst = []
game_status_lst = []
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, chess_status from games where not finished")
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])
cur.close()
return (player1_name_lst, player2_name_lst, game_status_lst)
except (Exception, psycopg2.DatabaseError) as error:
sys.exit(error)
finally:
if conn is not None:
conn.close()
def check_games():
game_id = ''
@ -391,6 +439,8 @@ def close_game():
now = datetime.now()
waiting = False
finished = True
try:
@ -401,7 +451,7 @@ def close_game():
cur = conn.cursor()
cur.execute("update games set finished=(%s), updated_at=(%s) where game_id=(%s)", (finished, now, game_id))
cur.execute("update games set waiting=(%s), finished=(%s), updated_at=(%s) where game_id=(%s)", (waiting, finished, now, game_id))
cur.execute("update stats set winner=(%s), finished=(%s), updated_at=(%s) where game_id=(%s)", (username, finished, now, game_id))
@ -686,6 +736,10 @@ def replying():
reply = True
elif query_word[0:4] == 'jocs':
reply = True
else:
reply = False
@ -813,12 +867,18 @@ if __name__ == '__main__':
visibility = visibility_lst[i]
if query_word != "jocs":
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:
is_playing = True
if reply == True and is_playing == False:
if query_word == 'nova' and not game_waiting:
@ -879,11 +939,15 @@ if __name__ == '__main__':
update_replies(status_id, username, now)
else:
update_replies(status_id, username, now)
elif reply and is_playing:
if query_word == 'nova':
toot_text = "@"+username + ' ja estas jugant una partida!' + '\n'
toot_text = "@"+username + ' ja tenies iniciada una partida!' + '\n'
if black_user != '':
@ -891,7 +955,7 @@ if __name__ == '__main__':
else:
toot_text += "esperant a l'altre jugador" + '\n'
toot_text += "espera l'altre jugador" + '\n'
toot_text += '\n'
@ -1061,7 +1125,7 @@ if __name__ == '__main__':
else:
toot_text = "@"+username + " ha abandonat la partida en espera."
toot_text = "@"+username + " has abandonat la partida en espera."
mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility)
@ -1073,6 +1137,37 @@ if __name__ == '__main__':
continue
elif query_word == 'jocs':
player1_name_lst, player2_name_lst, game_status_lst = current_games()
if len(player1_name_lst) > 0:
toot_text = "@"+username + " partides iniciades:" + "\n"
i = 0
while i < len(player1_name_lst):
if game_status_lst[i] == 'waiting':
toot_text += "\n" + player1_name_lst[i] + " / " + player2_name_lst[i] + " (en espera...)" + "\n"
else:
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)
i += 1
else:
toot_text = "@"+username + " cap partida en joc" + "\n"
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
update_replies(status_id, username, now)
else:
if playing_user == None: