From 48e672a7b0813079f430652800242cc883aee645 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Tue, 26 Sep 2023 12:58:40 +0300 Subject: [PATCH] pingtools: add the ping-pong minigame --- modules/pingtools/index.py | 129 ++++++++++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 3 deletions(-) diff --git a/modules/pingtools/index.py b/modules/pingtools/index.py index d663d58..5f28867 100644 --- a/modules/pingtools/index.py +++ b/modules/pingtools/index.py @@ -1,4 +1,127 @@ -text = self.MESSAGE.text +if self.MESSAGE.text == "!ping": + current_time = time.time() + delay = current_time - 7200 - float(self.MESSAGE.date.strftime('+%s')) + self.RESPONSE = f"Pong in {delay} seconds" -if text == "!ping": - self.RESPONSE = f"Pong in {time.time() - 7200 - float(self.MESSAGE.date.strftime('+%s'))} seconds" + if not os.path.exists(self.path + "scoreboard/"): + os.mkdir(self.path + "scoreboard/") + + def append_score(path, name, data): + open(path + f"scoreboard/{name}", "a").write(data) + + user_id = self.MESSAGE.from_user.id + chat_id = self.MESSAGE.chat.id + first_name = self.MESSAGE.from_user.first_name + last_name = self.MESSAGE.from_user.last_name + username = self.MESSAGE.from_user.username + + append_score(self.path, self.MESSAGE.from_user.id, f"{current_time} {user_id} {chat_id} {first_name} {last_name} {username} {delay}\n") + +elif self.MESSAGE.text == "!pong": + current_time = time.time() + delay = current_time - 7200 - float(self.MESSAGE.date.strftime('+%s')) + self.RESPONSE = f"Ping in {delay} seconds" + + if not os.path.exists(self.path + "pong-scoreboard/"): + os.mkdir(self.path + "pong-scoreboard/") + + def append_score(path, name, data): + open(path + f"pong-scoreboard/{name}", "a").write(data) + + user_id = self.MESSAGE.from_user.id + chat_id = self.MESSAGE.chat.id + first_name = self.MESSAGE.from_user.first_name + last_name = self.MESSAGE.from_user.last_name + username = self.MESSAGE.from_user.username + + append_score(self.path, self.MESSAGE.from_user.id, f"{current_time} {user_id} {chat_id} {first_name} {last_name} {username} {delay}\n") + +elif self.MESSAGE.text == "!pingtop": + def read_score(path, name): + if os.path.exists(path + f"scoreboard/{name}"): + return open(path + f"scoreboard/{name}").read() + else: + return None + + if not os.path.exists(self.path + "scoreboard/"): + os.mkdir(self.path + "scoreboard/") + + results = [] + for name in os.listdir(self.path + "scoreboard/"): + data = [i for i in read_score(self.path, name).split("\n") if i != ""] + data.sort(key = lambda x: float(x.split()[6])) + results.append(data[0].split()) + + print(results) + + results.sort(key = lambda x: x[6]) + self.RESPONSE = "Ping top:\n" + "\n".join( [f"{i+1}. {v[3]} {v[4]} ({v[5]}) - {v[6]}" for i, v in enumerate(results[:10])] ) + self.FORMAT = "HTML" + +elif self.MESSAGE.text == "!pongtop": + def read_score(path, name): + if os.path.exists(path + f"pong-scoreboard/{name}"): + return open(path + f"pong-scoreboard/{name}").read() + else: + return None + + if not os.path.exists(self.path + "pong-scoreboard/"): + os.mkdir(self.path + "pong-scoreboard/") + + results = [] + for name in os.listdir(self.path + "pong-scoreboard/"): + data = [i for i in read_score(self.path, name).split("\n") if i != ""] + data.sort(key = lambda x: float(x.split()[6])) + results.append(data[0].split()) + + print(results) + + results.sort(key = lambda x: x[6]) + self.RESPONSE = "Pong top:\n" + "\n".join( [f"{i+1}. {v[3]} {v[4]} ({v[5]}) - {v[6]}" for i, v in enumerate(results[:10])] ) + self.FORMAT = "HTML" + +elif self.MESSAGE.text == "!pingantitop": + def read_score(path, name): + if os.path.exists(path + f"scoreboard/{name}"): + return open(path + f"scoreboard/{name}").read() + else: + return None + + if not os.path.exists(self.path + "scoreboard/"): + os.mkdir(self.path + "scoreboard/") + + results = [] + for name in os.listdir(self.path + "scoreboard/"): + data = [i for i in read_score(self.path, name).split("\n") if i != ""] + data.sort(key = lambda x: float(x.split()[6])) + results.append(data[-1].split()) + + print(results) + + results.sort(key = lambda x: x[6]) + results = results[::-1] + self.RESPONSE = "Ping antitop:\n" + "\n".join( [f"{i+1}. {v[3]} {v[4]} ({v[5]}) - {v[6]}" for i, v in enumerate(results[:10])] ) + self.FORMAT = "HTML" + +elif self.MESSAGE.text == "!pongantitop": + def read_score(path, name): + if os.path.exists(path + f"pong-scoreboard/{name}"): + return open(path + f"pong-scoreboard/{name}").read() + else: + return None + + if not os.path.exists(self.path + "pong-scoreboard/"): + os.mkdir(self.path + "pong-scoreboard/") + + results = [] + for name in os.listdir(self.path + "pong-scoreboard/"): + data = [i for i in read_score(self.path, name).split("\n") if i != ""] + data.sort(key = lambda x: float(x.split()[6])) + results.append(data[-1].split()) + + print(results) + + results.sort(key = lambda x: x[6]) + results = results[::-1] + self.RESPONSE = "Pong antitop:\n" + "\n".join( [f"{i+1}. {v[3]} {v[4]} ({v[5]}) - {v[6]}" for i, v in enumerate(results[:10])] ) + self.FORMAT = "HTML"