Przeglądaj źródła

statistics refactoring & negative dice fix

tBKwtWS 7 lat temu
rodzic
commit
99b33328e9
3 zmienionych plików z 83 dodań i 70 usunięć
  1. 12 10
      commands/common.py
  2. 6 6
      commands/games.py
  3. 65 54
      commands/statistics.py

+ 12 - 10
commands/common.py

@@ -35,6 +35,7 @@ class CommandHelpers():
     def ccc(self, command, rights=False, event=False):   # Commandlist colour coding and channel rights filter.
         if rights:
             show = False
+            
             if rights["homechan"] == "owner":
                 if self.channels[self.homechannel].is_owner(event.source.nick):
                     show = True
@@ -44,15 +45,16 @@ class CommandHelpers():
             if rights["homechan"] == "oper":
                 if self.channels[self.homechannel].is_owner(event.source.nick) or self.channels[self.homechannel].is_admin(event.source.nick) or self.channels[self.homechannel].is_oper(event.source.nick):
                     show = True
-            if rights["chan"] == "owner":
-                if self.channels[event.target].is_owner(event.source.nick):
-                    show = True
-            if rights["chan"] == "admin":
-                if self.channels[event.target].is_owner(event.source.nick) or self.channels[event.target].is_admin(event.source.nick):
-                    show = True
-            if rights["chan"] == "oper":
-                if self.channels[event.target].is_owner(event.source.nick) or self.channels[event.target].is_admin(event.source.nick) or self.channels[event.target].is_oper(event.source.nick):
-                    show = True
+            if not event.target == self.connection.get_nickname():   # Channel message.
+                if rights["chan"] == "owner":
+                    if self.channels[event.target].is_owner(event.source.nick):
+                        show = True
+                if rights["chan"] == "admin":
+                    if self.channels[event.target].is_owner(event.source.nick) or self.channels[event.target].is_admin(event.source.nick):
+                        show = True
+                if rights["chan"] == "oper":
+                    if self.channels[event.target].is_owner(event.source.nick) or self.channels[event.target].is_admin(event.source.nick) or self.channels[event.target].is_oper(event.source.nick):
+                        show = True
             if show:
                 return blue + self.cmdchar + command + reset + ", "
         else:
@@ -118,5 +120,5 @@ class GameHelpers():
     def roll_dice(amount, type):
         rolls = []
         for iterations in range(amount):
-            rolls.append(random.randint(1,  type))
+            rolls.append(random.randint(1, type))
         return rolls

+ 6 - 6
commands/games.py

@@ -15,8 +15,8 @@ def do_command(self, connection, event):
     cmdtype, trigger, command, replyto = CH.disect_command(self, event)
     if not command:
         return  # Do nothing if there is no command.
-    if not self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'"):
-        return  # Do noting if the games channel function is off.
+    if not self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'") and not event.target == connection.get_nickname():
+        return  # Do noting if the games channel function is off and it's a channel message.
     
     if command == "cmd" or command == "commands":
         if cmdtype == "cmd":
@@ -89,14 +89,14 @@ def do_command(self, connection, event):
                         except ValueError:  # "d" not followd by interger.
                             connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
                             return
-                        if dicetype == 0 :
+                        if dicetype < 1 :
                             connection.action(replyto, "can not create objects with less then one side.")
                         else:
                             connection.privmsg(replyto, str(GameHelpers.roll_dice(1,  dicetype)[0]))
                     else:   # Argument does not start with "d" and is not an integer.
                         connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
                     return
-                if diceamount == 0:
+                if diceamount < 1:
                     connection.privmsg(replyto, "Rolling " + bold + "no " + reset + "dice.")
                 elif diceamount > 10:
                     connection.action(replyto, "can not fit that many dice into it's robot hands.")
@@ -113,9 +113,9 @@ def do_command(self, connection, event):
                 except ValueError:  # Second argument not a dice type.
                     connection.privmsg(replyto, "Invalid type of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
                     return
-                if diceamount == 0:
+                if diceamount < 1:
                     connection.privmsg(replyto, "Rolling " + bold + "no " + reset + "dice.")
-                elif dicetype == 0 :
+                elif dicetype < 1 :
                     connection.action(replyto, "can not create objects with less then one side.")
                 elif diceamount > 10:
                     connection.action(replyto, "can not fit that many dice into it's robot hands.")

+ 65 - 54
commands/statistics.py

@@ -1,4 +1,3 @@
-from common import log
 from commands.common import CommandHelpers as CH
 
 bold = "\x02"
@@ -15,8 +14,8 @@ def do_command(self, connection, event):
     cmdtype, trigger, command, replyto = CH.disect_command(self, event)
     if not command: # Do nothing if there is no command.
         return
-    if not self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'"):
-        return  # Do noting if the games channel function is off.
+    if not self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'") and not event.target == connection.get_nickname():
+        return  # Do noting if the games channel function is off and it's a channel message.
 
     if command == "cmd" or command == "commands":
         if cmdtype == "help":    #Display help text.
@@ -37,6 +36,7 @@ def do_command(self, connection, event):
             
             # Parse user input
             user = event.source.nick
+            channel = None
             if not connection.get_nickname() == event.target:   # Channel message.
                 channel = event.target
             if len(command.split()) == 1:    # Command contains only !joins.
@@ -60,6 +60,47 @@ def do_command(self, connection, event):
             elif len(command.split()) < 5:  # To many arguments
                 connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
                 return
+            
+            if channel: # User and channel.
+                if command.split()[0] == "joins":
+                    userstat = str(sum(self.db.all("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")))
+                    userchannelstat = str(self.db.one("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'"))
+                    channelstat = str(sum(self.db.all("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
+                    if userchannelstat == "None":
+                        userchannelstat = "0"
+                    if userstat == "0": # No user joins on record.
+                        connection.action(replyto, "has no record of any joins for " + red + user + reset + ".")
+                    else:   # User joins on record.
+                        connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + ". Of which " + green + userchannelstat + reset + " have been in " + red + channel + reset + ", that has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
+                elif command.split()[0] == "kicks":
+                    try:
+                        givenkicks, receivedkicks = self.db.one("SELECT given, received FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
+                    except:
+                        givenkicks = 0
+                        receivedkicks = 0
+                    if givenkicks == 0 and receivedkicks == 0:  # No kicks on record.
+                        connection.action(replyto, "has no record of any kicks for " + red + user + reset + ".")
+                    else:   # Kciks on record.
+                        channelkicks = self.db.all("SELECT given FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
+                        connection.privmsg(replyto, red + user + reset + " has kicked " + green + str(givenkicks) + reset + " and been kicked " + green + str(receivedkicks) + reset + " times in " + red + channel + reset + ", where were " + green + str(sum(channelkicks)) + reset + " kicks in total.")
+            else:   # Only user.
+                if command.split()[0] == "joins":
+                    userstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")))
+                    if userstat == "0":    # No statistics on user.
+                        connection.action(replyto, "has no record of any " + command.split()[0] + " for " + red + user + reset + ".")
+                    else:   # Got statistics on user.
+                        connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.")
+                elif command.split()[0] == "kicks":
+                    userstat = self.db.all("SELECT given, received FROM kicks WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
+                    kicksgiven = 0
+                    kicksreceived = 0
+                    for record in userstat:
+                        kicksgiven += record[0]
+                        kicksreceived += record[1]
+                    if kicksgiven == 0 and kicksreceived == 0:
+                        connection.action(replyto, red + user + reset + " has no record of any kicks for " + red + user + reset + " in " + red + channel + reset + ".")
+                    else:
+                        connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks")
 #            userstatfields = ()
 #            userchannelstatfields = ()
 #            channelstatfields = ()
@@ -71,54 +112,24 @@ def do_command(self, connection, event):
 #            channelstatparameters = ()
             
             
-            try:
-                if user and channel:
-                    if command.split()[0] == "joins":
-                        userstat = str(sum(self.db.all("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")))
-                        userchannelstat = str(self.db.one("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'"))
-                        channelstat = str(sum(self.db.all("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
-                        if userchannelstat == "None":
-                            userchannelstat = "0"
-                        connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + ". Of which " + green + userchannelstat + reset + " have been in " + red + channel + reset + ", that has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
-                    elif command.split()[0] == "kicks":
-                        try:
-                            givenkicks, receivedkicks = self.db.one("SELECT given, received FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
-                        except:
-                            givenkicks = 0
-                            receivedkicks = 0
-                        channelkicks = self.db.all("SELECT given FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
-                        connection.privmsg(replyto, red + user + reset + " has kicked " + green + str(givenkicks) + reset + " and been kicked " + green + str(receivedkicks) + reset + " times in " + red + channel + reset + ", where were " + green + str(sum(channelkicks)) + reset + " kicks in total.")
-                    return
-            except:
-                pass
-            try:
-                if user:
-                    if command.split()[0] == "joins":
-                        userstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")))
-                        if userstat == "[]":
-                            userstat = "[0]"
-                        connection.privmsg(replyto,  red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.")
-                    elif command.split()[0] == "kicks":
-                        userstat = self.db.all("SELECT given, received FROM kicks WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
-                        kicksgiven = 0
-                        kicksreceived = 0
-                        for record in userstat:
-                            kicksgiven += record[0]
-                            kicksreceived += record[1]
-                            print(record)
-                        print(userstat)
-                        connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks")
-                return
-            except:
-                pass
-            try:
-                if channel: # This situation should not occur anymore.
-                    channelstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
-                    if command.split()[0] == "joins":
-                        connection.privmsg(replyto, red + channel + reset + " has been " + command.split()[0][:-1] + "ed " + green + channelstat + reset + " times.")
-                    elif command.split()[0] == "kicks":
-                        connection.privmsg(replyto, "There have been " + green + channelstat + reset + " kicks in " + red + command.split()[0] + reset + ".")
-                    return
-            except:
-                pass
-            log.error("No situation applied on statistics.py")
+#            try:
+#                if user and channel:
+#                    
+#                    return
+#            except:
+#                pass
+#            try:
+#                
+#                return
+#            except:
+#                pass
+#            try:
+#                if channel: # This situation should not occur anymore.
+#                    channelstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
+#                    if command.split()[0] == "joins":
+#                        connection.privmsg(replyto, red + channel + reset + " has been " + command.split()[0][:-1] + "ed " + green + channelstat + reset + " times.")
+#                    elif command.split()[0] == "kicks":
+#                        connection.privmsg(replyto, "There have been " + green + channelstat + reset + " kicks in " + red + command.split()[0] + reset + ".")
+#                    return
+#            except:
+#                pass