From ff0b29482e6dee008a10c5cfdeb7229491619d70 Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Wed, 3 Nov 2021 13:06:48 +0100 Subject: [PATCH 1/5] fixed a bug that happens if the game crashes --- damastodon.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/damastodon.py b/damastodon.py index 981ca2f..ea96be6 100755 --- a/damastodon.py +++ b/damastodon.py @@ -73,6 +73,7 @@ def check_message(notification): start = pickle.load(f) except FileNotFoundError: mastodon.status_post("Hello @"+account+" \n unfortunately, your savegame is corrupted or missing",visibility="direct") #The file has moved or corrupted + print(account+" file not found") return if start != "WAIT": mastodon.status_post("Hello @"+account+" \n your request is not valid",visibility="direct") #The user that challenged us is playing a game with someone else @@ -101,7 +102,12 @@ def check_message(notification): return else: #We are in a game, so movements are parsed and lobby commands are disabled with open(save_position+account,"rb") as f: - start = pickle.load(f) + try: + start = pickle.load(f) + except EOFError: # Something went very wrong, file is corrupt? + mastodon.status_post("Hello @"+account+" \n unfortunately, your savegame is corrupted or missing",visibility="direct") #The file has moved or corrupted + print(account+" file corrupted") + return if start: #The game is started, load other parameters black = pickle.load(f) white = pickle.load(f) @@ -155,7 +161,7 @@ def check_message(notification): if __name__ == "__main__": if api_url[:4] != "http": - print("Invalid address.") + print("Invalid address") quit() mastodon = login.login(api_url) while True: From 3d8ba0f5e8f142a573d50571dfaa1fb26952c44b Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Wed, 3 Nov 2021 13:17:44 +0100 Subject: [PATCH 2/5] fixed a bug that happens if the game crashes again --- damastodon.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/damastodon.py b/damastodon.py index ea96be6..351bcea 100755 --- a/damastodon.py +++ b/damastodon.py @@ -105,7 +105,8 @@ def check_message(notification): try: start = pickle.load(f) except EOFError: # Something went very wrong, file is corrupt? - mastodon.status_post("Hello @"+account+" \n unfortunately, your savegame is corrupted or missing",visibility="direct") #The file has moved or corrupted + mastodon.status_post("Hello @"+account+" \n unfortunately, your savegame is corrupted or missing",visibility="direct") #The file has been moved or is corrupted + os.remove(save_position+account) print(account+" file corrupted") return if start: #The game is started, load other parameters From f18617225425f1f9e7fea790a751fcbb89cd7119 Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Wed, 3 Nov 2021 13:25:44 +0100 Subject: [PATCH 3/5] added logging for exception management --- damastodon.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/damastodon.py b/damastodon.py index 351bcea..191f9a1 100755 --- a/damastodon.py +++ b/damastodon.py @@ -9,6 +9,7 @@ import os import time import sys import re +import logging #configuration server api_url = sys.argv[1] @@ -26,6 +27,8 @@ black_norm="◾ " black_knight="⚫ " empty="🟦 " +#logging config +logging.basicConfig(filename="/tmp/dama.log",encoding="utf-8",level=logging.DEBUG) def cleanHTML(raw): cleanText = re.sub(CLEANR, '',raw) @@ -73,7 +76,7 @@ def check_message(notification): start = pickle.load(f) except FileNotFoundError: mastodon.status_post("Hello @"+account+" \n unfortunately, your savegame is corrupted or missing",visibility="direct") #The file has moved or corrupted - print(account+" file not found") + logging.warning("%s file not found",account) return if start != "WAIT": mastodon.status_post("Hello @"+account+" \n your request is not valid",visibility="direct") #The user that challenged us is playing a game with someone else @@ -107,7 +110,7 @@ def check_message(notification): except EOFError: # Something went very wrong, file is corrupt? mastodon.status_post("Hello @"+account+" \n unfortunately, your savegame is corrupted or missing",visibility="direct") #The file has been moved or is corrupted os.remove(save_position+account) - print(account+" file corrupted") + logging.warning("%s file corrupted",account) return if start: #The game is started, load other parameters black = pickle.load(f) @@ -162,7 +165,7 @@ def check_message(notification): if __name__ == "__main__": if api_url[:4] != "http": - print("Invalid address") + logging.error("Invalid address") quit() mastodon = login.login(api_url) while True: From af52f887fd4f2b8bf779549e12ae43bafbe59ccc Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Fri, 12 Nov 2021 17:35:30 +0100 Subject: [PATCH 4/5] Apparently, logging doesn't support 'encoding' parameter --- damastodon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/damastodon.py b/damastodon.py index 191f9a1..f01642c 100755 --- a/damastodon.py +++ b/damastodon.py @@ -28,7 +28,7 @@ black_knight="⚫ " empty="🟦 " #logging config -logging.basicConfig(filename="/tmp/dama.log",encoding="utf-8",level=logging.DEBUG) +logging.basicConfig(filename="/tmp/dama.log",level=logging.DEBUG) def cleanHTML(raw): cleanText = re.sub(CLEANR, '',raw) From 009361894040a8c008d89f344aedfd3975185364 Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Fri, 12 Nov 2021 17:41:53 +0100 Subject: [PATCH 5/5] fixed login.py, now it can be called from another dir --- login.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/login.py b/login.py index b816ea6..1f857ac 100644 --- a/login.py +++ b/login.py @@ -1,8 +1,12 @@ from mastodon import Mastodon +import os + +fileDir = os.path.dirname(os.path.abspath(__file__)) +fileDir = fileDir +"/" def login(url): mastodon = Mastodon( - access_token = "DAMA.SECRET", + access_token = fileDir+"DAMA.SECRET", api_base_url = url ) return mastodon