added teams
authorGeorgios Atheridis <atheridis@tutamail.com>
Wed, 11 May 2022 00:21:09 +0000 (00:21 +0000)
committerGeorgios Atheridis <atheridis@tutamail.com>
Wed, 11 May 2022 00:21:09 +0000 (00:21 +0000)
skgyorugo/commands/newteams.py
skgyorugo/commands/queue.py
skgyorugo/commands/queuesize.py [deleted file]
skgyorugo/commands/teams.py [new file with mode: 0644]
skgyorugo/commands/teamsize.py [new file with mode: 0644]
skgyorugo/database_manager.py

index 413400652fdcc79e2cd5a116ed83fc833046e34d..6d5b0ffdbda185666e3a711a5a2380ba3f9c36d2 100644 (file)
@@ -36,7 +36,7 @@ def main(bot: Bot, message: Message):
         """
     )
     fetched = c.fetchall()
-    queue = [x[0] for x in fetched]
+    queue: list[str] = [x[0] for x in fetched]
     twitch = ttv_api.users.get_users(user_ids=queue)
     if not twitch:
         bot.send_privmsg(
@@ -46,11 +46,11 @@ def main(bot: Bot, message: Message):
         )
         conn.close()
         return
-    queue_names = []
+    queue_users: list[ttv_api.users.User] = []
     for twitch_id in queue:
         for twitch_user in twitch:
             if int(twitch_user.user_id) == int(twitch_id):
-                queue_names.append(twitch_user.display_name)
+                queue_users.append(twitch_user)
                 break
         else:
             bot.send_privmsg(
@@ -74,14 +74,24 @@ def main(bot: Bot, message: Message):
             reply=message.tags["id"],
         )
 
-    queue = queue_names[:queue_size]
-    random.shuffle(queue)
-    blue_team = queue[: queue_size // 2]
-    red_team = queue[queue_size // 2 :]
+    queue_users: list[ttv_api.users.User] = queue_users[:queue_size]
+    random.shuffle(queue_users)
+    blue_team: list[ttv_api.users.User] = queue_users[: queue_size // 2]
+    red_team: list[ttv_api.users.User] = queue_users[queue_size // 2 :]
+
+    c.execute("UPDATE lol_queue SET team = NULL")
+    sql = f"UPDATE lol_queue SET team = 0 WHERE twitch_id IN ({(', ?' * (queue_size // 2))[2:]})"
+    c.execute(sql, tuple(user.user_id for user in blue_team))
+    sql = f"UPDATE lol_queue SET team = 1 WHERE twitch_id IN ({(', ?' * (queue_size - queue_size // 2))[2:]})"
+    c.execute(sql, tuple(user.user_id for user in red_team))
+    conn.commit()
+
+    blue_team_users: list[str] = [user.display_name for user in blue_team]
+    red_team_users: list[str] = [user.display_name for user in red_team]
 
     bot.send_privmsg(
         message.channel,
-        [f"Blue team is: {blue_team}", f"Red team is: {red_team}"],
+        [f"Blue team is: {blue_team_users}", f"Red team is: {red_team_users}"],
         reply=message.tags["id"],
     )
 
index e313af9e4cf8d19fc7ecdd561f35421786418c66..1f5d2c5aa07ee86b0080b9527e43b1b8d002e71f 100644 (file)
@@ -77,7 +77,7 @@ def main(bot: Bot, message: Message):
     tools.smart_privmsg.send(
         bot,
         message,
-        f"These people are to play with {message.channel}: {queue_names[1:queue_size]} | and these people are waiting: {queue_names[queue_size:]}",
+        f"These people are playing with {message.channel}: {queue_names[1:queue_size]} | and these people are waiting: {queue_names[queue_size:]}",
         reply=message.tags["id"],
     )
 
diff --git a/skgyorugo/commands/queuesize.py b/skgyorugo/commands/queuesize.py
deleted file mode 100644 (file)
index cca3cf1..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-from aptbot.bot import Message, Commands, Bot
-import os
-import logging
-import sqlite3
-
-logger = logging.getLogger(__name__)
-logger.setLevel(logging.DEBUG)
-
-formatter = logging.Formatter("[%(levelname)s] %(asctime)s: %(name)s; %(message)s")
-
-file_handler = logging.FileHandler("/var/log/aptbot/logs.log")
-file_handler.setFormatter(formatter)
-
-logger.handlers = []
-logger.addHandler(file_handler)
-
-PERMISSION = 10
-PREFIX = "\\"
-DESCRIPTION = r"Change the queue size"
-USER_COOLDOWN = 0
-GLOBAL_COOLDOWN = 0
-
-PATH = os.path.dirname(os.path.realpath(__file__))
-PATH = os.path.join(PATH, "..")
-
-
-def main(bot: Bot, message: Message):
-    replied_message = message.tags.get("reply-parent-msg-body", None)
-    if replied_message:
-        queue_size = message.value.split(" ")[2]
-    else:
-        queue_size = message.value.split(" ")[1]
-    try:
-        queue_size = int(queue_size)
-    except ValueError:
-        bot.send_privmsg(
-            message.channel,
-            f"Please choose a number. {queue_size} is not a valid number.",
-            reply=message.tags["id"],
-        )
-        return
-
-    conn = sqlite3.connect(os.path.join(PATH, "lol_data.db"))
-    c = conn.cursor()
-
-    c.execute(
-        """
-        REPLACE INTO lol_queue_data (name, data) VALUES ('queuesize', ?)
-        """,
-        (queue_size,),
-    )
-    conn.commit()
-
-    bot.send_privmsg(
-        message.channel,
-        f"Successfully changed queue size to {queue_size}.",
-        reply=message.tags["id"],
-    )
-
-    conn.close()
diff --git a/skgyorugo/commands/teams.py b/skgyorugo/commands/teams.py
new file mode 100644 (file)
index 0000000..1e6f01b
--- /dev/null
@@ -0,0 +1,83 @@
+from aptbot.bot import Message, Commands, Bot
+import os
+import logging
+import ttv_api.users
+import sqlite3
+import tools.smart_privmsg
+
+logger = logging.getLogger(__name__)
+logger.setLevel(logging.DEBUG)
+
+formatter = logging.Formatter("[%(levelname)s] %(asctime)s: %(name)s; %(message)s")
+
+file_handler = logging.FileHandler("/var/log/aptbot/logs.log")
+file_handler.setFormatter(formatter)
+
+logger.handlers = []
+logger.addHandler(file_handler)
+
+PERMISSION = 99
+PREFIX = "?"
+DESCRIPTION = r"Check current teams"
+USER_COOLDOWN = 30
+GLOBAL_COOLDOWN = 15
+
+PATH = os.path.dirname(os.path.realpath(__file__))
+PATH = os.path.join(PATH, "..")
+
+
+def main(bot: Bot, message: Message):
+    conn = sqlite3.connect(os.path.join(PATH, "lol_data.db"))
+    c = conn.cursor()
+
+    c.execute(
+        """
+        SELECT twitch_id, team FROM lol_queue WHERE team in (0, 1);
+        """
+    )
+    fetched = c.fetchall()
+    blue_team = []
+    red_team = []
+    for user in fetched:
+        if user[1] == 0:
+            blue_team.append(user[0])
+        elif user[1] == 1:
+            red_team.append(user[0])
+        else:
+            bot.send_privmsg(
+                message.channel,
+                f"Something VERY WEIRD occured. The user with id: {user[0]} is on team {user[1]}, which is neither blue = 0 or red = 1.",
+                reply=message.tags["id"],
+            )
+
+    users = [x[0] for x in fetched]
+    twitch = ttv_api.users.get_users(user_ids=users)
+    if not twitch:
+        bot.send_privmsg(
+            message.channel,
+            "There was an issue fetching twitch data. Sadge",
+            reply=message.tags["id"],
+        )
+        conn.close()
+        return
+    blue_team_users = []
+    red_team_users = []
+    for twitch_user in twitch:
+        if twitch_user.user_id in blue_team:
+            blue_team_users.append(twitch_user.display_name)
+        elif twitch_user.user_id in red_team:
+            red_team_users.append(twitch_user.display_name)
+        else:
+            bot.send_privmsg(
+                message.channel,
+                f"Something VERY WEIRD occured. The user with id: {twitch_user.user_id} who has the name {twitch_user.display_name} is not on a team.",
+                reply=message.tags["id"],
+            )
+
+    bot.send_privmsg(
+        message.channel,
+        [f"Blue team is: {blue_team_users}", f"Red team is: {red_team_users}"],
+        reply=message.tags["id"],
+    )
+
+    conn.close()
diff --git a/skgyorugo/commands/teamsize.py b/skgyorugo/commands/teamsize.py
new file mode 100644 (file)
index 0000000..cca3cf1
--- /dev/null
@@ -0,0 +1,60 @@
+from aptbot.bot import Message, Commands, Bot
+import os
+import logging
+import sqlite3
+
+logger = logging.getLogger(__name__)
+logger.setLevel(logging.DEBUG)
+
+formatter = logging.Formatter("[%(levelname)s] %(asctime)s: %(name)s; %(message)s")
+
+file_handler = logging.FileHandler("/var/log/aptbot/logs.log")
+file_handler.setFormatter(formatter)
+
+logger.handlers = []
+logger.addHandler(file_handler)
+
+PERMISSION = 10
+PREFIX = "\\"
+DESCRIPTION = r"Change the queue size"
+USER_COOLDOWN = 0
+GLOBAL_COOLDOWN = 0
+
+PATH = os.path.dirname(os.path.realpath(__file__))
+PATH = os.path.join(PATH, "..")
+
+
+def main(bot: Bot, message: Message):
+    replied_message = message.tags.get("reply-parent-msg-body", None)
+    if replied_message:
+        queue_size = message.value.split(" ")[2]
+    else:
+        queue_size = message.value.split(" ")[1]
+    try:
+        queue_size = int(queue_size)
+    except ValueError:
+        bot.send_privmsg(
+            message.channel,
+            f"Please choose a number. {queue_size} is not a valid number.",
+            reply=message.tags["id"],
+        )
+        return
+
+    conn = sqlite3.connect(os.path.join(PATH, "lol_data.db"))
+    c = conn.cursor()
+
+    c.execute(
+        """
+        REPLACE INTO lol_queue_data (name, data) VALUES ('queuesize', ?)
+        """,
+        (queue_size,),
+    )
+    conn.commit()
+
+    bot.send_privmsg(
+        message.channel,
+        f"Successfully changed queue size to {queue_size}.",
+        reply=message.tags["id"],
+    )
+
+    conn.close()
index 9f6a95c26759a908142bfb23bcca3b6d0dc6da97..718c2743d7221ad87e2ae6194d8ab2fa0898b755 100644 (file)
@@ -98,6 +98,7 @@ def create_lol_database():
             available INTEGER NOT NULL,
             last_available INTEGER,
             time_remaining INTEGER NOT NULL,
+            team INTEGER,
             PRIMARY KEY (twitch_id)
         );
         """