Merge branch 'bloy-karma'
This commit is contained in:
commit
4013a93dc9
@ -36,9 +36,13 @@ class Karma(Module):
|
||||
pattern = "(?:(\S+)|\((.+)\))"
|
||||
karmapattern = pattern + '(\+\+|--|\+-|-\+)' + '(\s+|$)'
|
||||
querypattern = '^rank\s+(.*)'
|
||||
reportpattern = '^karma\s+report\s+(highest|lowest|positive|negative)'
|
||||
statpattern = '^karma\s+stat\s+(.*)'
|
||||
|
||||
self.karmare = re.compile(karmapattern)
|
||||
self.queryre = re.compile(querypattern)
|
||||
self.reportre = re.compile(reportpattern)
|
||||
self.statre = re.compile(statpattern)
|
||||
|
||||
def db_init(self):
|
||||
# need to init the database if karma tables don't already exist
|
||||
@ -67,6 +71,23 @@ class Karma(Module):
|
||||
sql = 'INSERT INTO drbotzo_modules VALUES (?,?)'
|
||||
conn.execute(sql, (self.__class__.__name__, 1))
|
||||
conn.commit()
|
||||
version = 1
|
||||
except sqlite3.Error as e:
|
||||
conn.rollback()
|
||||
print("sqlite error: " + str(e))
|
||||
raise
|
||||
if (version < 2):
|
||||
conn = self.get_db()
|
||||
try:
|
||||
conn.execute('''
|
||||
CREATE VIEW karma_users AS
|
||||
SELECT who, COUNT(NULLIF(delta, -1)) AS pos,
|
||||
COUNT(NULLIF(delta, 1)) AS neg
|
||||
FROM karma_log GROUP BY who''')
|
||||
sql = 'UPDATE drbotzo_modules SET version = ? WHERE module = ?'
|
||||
conn.execute(sql, (2, self.__class__.__name__))
|
||||
conn.commit()
|
||||
version = 2
|
||||
except sqlite3.Error as e:
|
||||
conn.rollback()
|
||||
print("sqlite error: " + str(e))
|
||||
@ -79,6 +100,10 @@ class Karma(Module):
|
||||
return self.handle_karma_change(connection, nick, userhost, replypath, what)
|
||||
elif (self.queryre.search(what)):
|
||||
return self.handle_karma_query(connection, nick, userhost, replypath, what)
|
||||
elif (self.statre.search(what)):
|
||||
return self.handle_stat_query(connection, nick, userhost, replypath, what)
|
||||
elif (self.reportre.search(what)):
|
||||
return self.handle_report_query(connection, nick, userhost, replypath, what)
|
||||
|
||||
def handle_karma_change(self, connection, nick, userhost, replypath, what):
|
||||
"""
|
||||
@ -88,17 +113,17 @@ class Karma(Module):
|
||||
key = match.group(1) if match.group(1) else match.group(2)
|
||||
value = match.group(3)
|
||||
if (value == '++'):
|
||||
return self.karma_modify(key, 1, nick, userhost)
|
||||
return self.karma_modify(key, 1, connection, nick, userhost, replypath)
|
||||
elif (value == '--'):
|
||||
return self.karma_modify(key, -1, nick, userhost)
|
||||
return self.karma_modify(key, -1, connection, nick, userhost, replypath)
|
||||
elif (value == '+-'):
|
||||
self.karma_modify(key, 1, nick, userhost)
|
||||
return self.karma_modify(key, -1, nick, userhost)
|
||||
self.karma_modify(key, 1, connection, nick, userhost, replypath)
|
||||
return self.karma_modify(key, -1, connection, nick, userhost, replypath)
|
||||
elif (value == '-+'):
|
||||
self.karma_modify(key, -1, nick, userhost)
|
||||
return self.karma_modify(key, 1, nick, userhost)
|
||||
self.karma_modify(key, -1, connection, nick, userhost, replypath)
|
||||
return self.karma_modify(key, 1, connection, nick, userhost, replypath)
|
||||
|
||||
def karma_modify(self, key, value, nick, userhost):
|
||||
def karma_modify(self, key, value, connection, nick, userhost, replypath):
|
||||
"""
|
||||
Go out to the database and update the karma value.
|
||||
"""
|
||||
@ -115,6 +140,66 @@ class Karma(Module):
|
||||
conn.rollback()
|
||||
return self.reply(connection, replypath, "sqlite error: " + str(e))
|
||||
|
||||
def handle_report_query(self, connection, nick, userhost, replypath, what):
|
||||
match = self.reportre.search(what)
|
||||
report = match.group(1)
|
||||
|
||||
message = '{nick}: the desired report is not yet implemented'.format(nick=nick)
|
||||
query = None
|
||||
header = None
|
||||
if (report == 'highest'):
|
||||
query = 'SELECT key, value FROM karma_values ORDER BY value DESC LIMIT 5'
|
||||
header = 'Top 5 karma recipients:'
|
||||
elif (report == 'lowest'):
|
||||
query = 'SELECT key, value FROM karma_values ORDER BY value ASC LIMIT 5'
|
||||
header = 'Bottom 5 karma recipients:'
|
||||
elif (report == 'positive'):
|
||||
query = 'SELECT who, pos FROM karma_users ORDER BY pos DESC LIMIT 5'
|
||||
header = 'Top 5 Optimists:'
|
||||
elif (report == 'negative'):
|
||||
query = 'SELECT who, neg FROM karma_users ORDER BY neg DESC LIMIT 5'
|
||||
header = 'Top 5 Pessimists"'
|
||||
|
||||
if (query != None):
|
||||
conn = self.get_db()
|
||||
list = []
|
||||
try:
|
||||
cursor = conn.execute(query)
|
||||
result = cursor.fetchone()
|
||||
while (result != None):
|
||||
list.append("{key} ({value})".format(key=result[0], value=result[1]))
|
||||
result = cursor.fetchone()
|
||||
list = ', '.join(list)
|
||||
message = '{header} {list}'.format(header=header, list=list)
|
||||
except sqlite3.Error as e:
|
||||
conn.rollback()
|
||||
return self.reply(connection, replypath, "sqlite error: " + str(e))
|
||||
return self.reply(connection, replypath, message);
|
||||
|
||||
def handle_stat_query(self, connection, nick, userhost, replypath, what):
|
||||
match = self.statre.search(what)
|
||||
statnick = match.group(1)
|
||||
|
||||
conn = self.get_db()
|
||||
reply = '{nick}: {statnick} has never given karma'.format(nick=nick, statnick=statnick)
|
||||
try:
|
||||
query = '''
|
||||
SELECT pos, neg
|
||||
FROM karma_users
|
||||
WHERE who = :who
|
||||
'''
|
||||
value = conn.execute(query, {'who': statnick}).fetchone()
|
||||
if (value != None):
|
||||
pos = value[0]
|
||||
neg = value[1]
|
||||
total = pos+neg;
|
||||
reply = '{nick}: {statnick} has given {pos} postive karma and {neg} negative karma, for a total of {total} karma'.format(nick=nick, statnick=statnick, pos=pos, neg=neg, total=total)
|
||||
|
||||
except sqlite3.Error as e:
|
||||
return self.reply(connection, replypath, "sqlite error: " + str(e))
|
||||
|
||||
return self.reply(connection, replypath, reply)
|
||||
|
||||
def handle_karma_query(self, connection, nick, userhost, replypath, what):
|
||||
match = self.queryre.search(what)
|
||||
key = match.group(1)
|
||||
|
Loading…
Reference in New Issue
Block a user