def karma(self, event): sql_session = get_session() before, string, operator = re.search(expression[0], event.arguments, re.UNICODE).groups() string = string.strip().lower() nick = getnick(event.source) # Find this string or add the row item = sql_session.query(Karma).\ filter_by(string=string, nick=nick).first() if not item: item = Karma(string=string, nick=nick) sql_session.add(item) # Count +/- and alter the score within the range of 5 +/- if operator[0] == '+': if item.score < 7 - len(operator): item.score += len(operator) - 1 else: item.score = 5 elif operator[0] == '-': if item.score > -7 + len(operator): item.score -= len(operator) - 1 else: item.score = -5 else: print "karma.py: Something odd has happened." sql_session.commit()
def get_counter(name, sql_session=None): if not sql_session: sql_session = get_session() counter = sql_session.query(Counter).filter_by(name=name).first() if not counter: counter = Counter(name=name) sql_session.add(counter) sql_session.commit() return counter
def bang(pipein, arguments, event): sql_session = backend.get_session() token = arguments.strip().lower() if not token: token = fishapi.getnick(event.source).strip().lower() arguments = token tokens = token.split() if tokens[0] == 'stats': results = sql_session.query("string", "score").\ from_statement(stats_sql).all() sql_session.commit() results = sorted(results, key=itemgetter(1)) if len(tokens) >= 3 and tokens[2].isdigit() and int(tokens[2]) <= 15: amount = int(tokens[2]) else: amount = 3 if len(tokens) >= 2: if tokens[1] == 'top': return ("Karma top %s -> %s" % (amount, ", ".join( [":".join([str(y) for y in x]) for x in results[-amount:]])), None) elif tokens[1] == 'bottom': return ("Karma bottom %s -> %s" % (amount, ", ".join( [":".join([str(y) for y in x]) for x in results[:amount]])), None) elif tokens[1] == 'middle': mid_point = int(len(results) / 2) return ("Karma middle %s -> %s" % (amount, ", ".join([ ":".join([str(y) for y in x]) for x in results[mid_point - int(amount / 2 + 1):mid_point + int(amount / 2 + 1)] ])), None) return ("Highest score: %s (%s) - Lowest score: %s (%s)" % (results[-1][0], results[-1][1], results[0][0], results[0][1]), None) else: things = sql_session.query(Karma).filter_by(string=token) sql_session.commit() score = calckarma(things) if score: return ("'%s' has a score of: %s" % (arguments, score), None) else: return ("'%s' has neutral karma." % (arguments), None)
def bang(pipein, arguments, event): sql_session = backend.get_session() token = arguments.strip().lower() if not token: token = fishapi.getnick(event.source).strip().lower() arguments = token tokens = token.split() if tokens[0] == 'stats': results = sql_session.query("string", "score").\ from_statement(stats_sql).all() sql_session.commit() results = sorted(results, key=itemgetter(1)) if len(tokens) >= 3 and tokens[2].isdigit() and int(tokens[2]) <= 15: amount = int(tokens[2]) else: amount = 3 if len(tokens) >= 2: if tokens[1] == 'top': return ("Karma top %s -> %s" % (amount, ", ".join([":".join([str(y) for y in x]) for x in results[-amount:]])), None) elif tokens[1] == 'bottom': return ("Karma bottom %s -> %s" % (amount, ", ".join([":".join([str(y) for y in x]) for x in results[:amount]])), None) elif tokens[1] == 'middle': mid_point = int(len(results) / 2) return ("Karma middle %s -> %s" % (amount, ", ".join([":".join([str(y) for y in x]) for x in results[mid_point - int(amount / 2 + 1):mid_point + int(amount / 2 + 1)]])), None) return ("Highest score: %s (%s) - Lowest score: %s (%s)" % (results[-1][0], results[-1][1], results[0][0], results[0][1]), None) else: things = sql_session.query(Karma).filter_by(string=token) sql_session.commit() score = calckarma(things) if score: return ("'%s' has a score of: %s" % (arguments, score), None) else: return ("'%s' has neutral karma." % (arguments), None)
#!/usr/bin/python """Please, stop with the beatings.""" from fishapi import get_counter from backend import get_session sql_session = get_session() smacks = get_counter("smacks", sql_session) def bang(pipein, arguments, event): smacks.count += 1 sql_session.commit() return (":(", None)