From: Georgios Atheridis Date: Fri, 30 Jun 2023 21:00:48 +0000 (+0100) Subject: fix X-Git-Url: https://git.atheridis.org/?a=commitdiff_plain;h=5366307650dc2da6fbbd773cc0aa4741886aaa0d;p=ihaspeks%2Faptbot-ihaspeks.git fix --- diff --git a/ihaspeks/main.py b/ihaspeks/main.py index 9c870fe..5bf0c8e 100644 --- a/ihaspeks/main.py +++ b/ihaspeks/main.py @@ -123,6 +123,7 @@ database_manager.update_auto_messages_in_database(auto_message_modules, auto_mes def start(bot: Bot, message: Message, stop_event: Event): i = 0 wait = 5 + scripts.subathon.start() scripts.subathon.donations(bot, message) while not stop_event.is_set(): i += wait @@ -140,6 +141,7 @@ def start(bot: Bot, message: Message, stop_event: Event): analyze_auto_message.do_auto_message(bot, message, auto_message_modules) i = 0 time.sleep(wait) + scripts.subathon.close() def main(bot: Bot, message: Message): diff --git a/ihaspeks/scripts/subathon.py b/ihaspeks/scripts/subathon.py index c0fd89c..ce7265c 100644 --- a/ihaspeks/scripts/subathon.py +++ b/ihaspeks/scripts/subathon.py @@ -7,7 +7,7 @@ import sqlite3 import socketio import tools.smart_privmsg from aptbot.bot import Bot, Commands, Message -from numpy import array, ceil, exp, sum +from numpy import ceil, exp from numpy.random import normal logger = logging.getLogger(__name__) @@ -15,7 +15,7 @@ logger = logging.getLogger(__name__) SCRIPTS_PATH = os.path.dirname(os.path.realpath(__file__)) PATH = os.path.join(SCRIPTS_PATH, "..") -socket_file = os.getenv("SUBATHON_SOCKET", "~/.cache/subathon.sock") +socket_file = os.getenv("SUBATHON_SOCKET", "/tmp/subathon.sock") conn = sqlite3.connect(os.path.join(PATH, "subathon.db")) c = conn.cursor() @@ -43,6 +43,8 @@ c.execute( conn.commit() c.close() +sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + def subathon(bot: Bot, message: Message): if message.command == Commands.USERNOTICE: @@ -135,14 +137,11 @@ def subathon(bot: Bot, message: Message): return total_timer = timer_add * multiplier - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client: - client.connect(socket_file) - data = { - "time": total_timer, - "name": display_name, - } - client.send(json.dumps(data).encode()) - client.close() + data = { + "time": total_timer, + "name": display_name, + } + sock.send(json.dumps(data).encode()) verb = "INCREASED ihaspeHappy" if total_timer > 0 else "DECREASED ihaspeAngi" msg = ( @@ -166,7 +165,7 @@ def donations(bot: Bot, message: Message): @io.on("event") def event(data): - if data["type"] == "donations": + if data["type"] == "donation": amount_cents = int(float(data["message"][0]["amount"]) * 100) div_count = (amount_cents // 400) * calculate_increase(10) @@ -177,14 +176,11 @@ def donations(bot: Bot, message: Message): display_name = data["message"][0]["from"] total_timer = timer_add - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client: - client.connect(socket_file) - data = { - "time": total_timer, - "name": display_name, - } - client.send(json.dumps(data).encode()) - client.close() + data = { + "time": total_timer, + "name": display_name, + } + sock.send(json.dumps(data).encode()) logger.info("%s donated %s", display_name, amount_cents) verb = ( @@ -203,3 +199,11 @@ def calculate_increase(sub_count: int) -> int: avg_time = ceil(30.0 * exp(-scale * sub_count) + 5.0) var_time = 35.0 / (1 + exp(-0.15 * (sub_count - 10))) - 5.0 return ceil(normal(avg_time, var_time) * 60.0) + + +def start() -> None: + sock.connect(socket_file) + + +def close() -> None: + sock.close()