Fix up heart leaderboards; improve some of its heuristics for matching terms
This commit is contained in:
parent
a3feaa4020
commit
0f9ba2bf0a
1 changed files with 48 additions and 27 deletions
|
@ -5,15 +5,23 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os.path
|
import os.path
|
||||||
|
import random
|
||||||
import re
|
import re
|
||||||
|
|
||||||
HEARTS_FILE = 'hearts.json'
|
HEARTS_FILE = 'hearts.json'
|
||||||
|
|
||||||
PLUSES = ['++', '<3', '<3', ':heart:', ':yellow_heart:', ':green_heart:', ':blue_heart:', ':purple_heart:', '❤️', '💛', '💚', '💙', '💜']
|
PLUSES = ['++', '<3', '<3', ':heart:', ':yellow_heart:', ':green_heart:', ':blue_heart:', ':purple_heart:', '❤️', '💛', '💚', '💙', '💜']
|
||||||
MINUSES = ['--', '–', '—', '</3', '</3', ':broken_heart:', '💔']
|
MINUSES = ['--', '–', '—', '</3', '</3', ':broken_heart:', '💔']
|
||||||
|
SASS = ['r u srs rn', 'no', 'noooope']
|
||||||
|
|
||||||
LOGGER = logging.getLogger('hearts')
|
LOGGER = logging.getLogger('hearts')
|
||||||
|
|
||||||
|
LEADERS_RE = re.compile('!(top|bottom)(\d+)')
|
||||||
|
WHITESPACE_RE = re.compile('\s+')
|
||||||
|
LINK_RE = re.compile(r'<(?P<type>[@#])(?P<id>[A-Z0-9]+)(\|(?P<name>\w+))?>')
|
||||||
|
VAR_RE = re.compile(r'<!(?P<name>\w+)>')
|
||||||
|
EMOJI_RE = re.compile(r':\w+:')
|
||||||
|
|
||||||
outputs = []
|
outputs = []
|
||||||
|
|
||||||
def process_message(data):
|
def process_message(data):
|
||||||
|
@ -23,14 +31,20 @@ def process_message(data):
|
||||||
# TODO: Make this better.
|
# TODO: Make this better.
|
||||||
return
|
return
|
||||||
|
|
||||||
if text == '!top5':
|
leaders_m = LEADERS_RE.match(text)
|
||||||
scores = top5()
|
if leaders_m:
|
||||||
outputs.append([data['channel'], scores])
|
top = leaders_m.group(1) == 'top'
|
||||||
return
|
try:
|
||||||
|
n = int(leaders_m.group(2))
|
||||||
if text == '!bottom5':
|
except ValueError:
|
||||||
scores = bottom5()
|
outputs.append([data['channel'], random.choice(SASS)])
|
||||||
outputs.append([data['channel'], scores])
|
return
|
||||||
|
if n == 0:
|
||||||
|
outputs.append([data['channel'], random.choice(SASS)])
|
||||||
|
return
|
||||||
|
scores = leaders(n, top)
|
||||||
|
if scores:
|
||||||
|
outputs.append([data['channel'], scores])
|
||||||
return
|
return
|
||||||
|
|
||||||
if text.startswith('!erase'):
|
if text.startswith('!erase'):
|
||||||
|
@ -76,9 +90,23 @@ def calculate_score_and_find_operators(text):
|
||||||
|
|
||||||
did_change = original_text != text
|
did_change = original_text != text
|
||||||
if did_change:
|
if did_change:
|
||||||
# Strip the trailing :
|
# Strip off the trailing : if the message is a user or group
|
||||||
if text.endswith(':'):
|
for regex in [LINK_RE, VAR_RE]:
|
||||||
text = text[:-1]
|
m = regex.match(text)
|
||||||
|
if not m:
|
||||||
|
continue
|
||||||
|
# string is just a username with a colon at the end so strip off the colon
|
||||||
|
if (m.end() == len(text)-1) and text.endswith(':'):
|
||||||
|
text = text[:-1]
|
||||||
|
break
|
||||||
|
|
||||||
|
# If the remaining string is all emojis, ignore it
|
||||||
|
tokenized = WHITESPACE_RE.split(text)
|
||||||
|
emoji_matches = map(EMOJI_RE.match, tokenized)
|
||||||
|
all_emoji = all(emoji_matches)
|
||||||
|
if all_emoji:
|
||||||
|
return None, None
|
||||||
|
|
||||||
return score, text
|
return score, text
|
||||||
else:
|
else:
|
||||||
return None, None
|
return None, None
|
||||||
|
@ -98,30 +126,23 @@ def has_operator(text, operators):
|
||||||
return op, False
|
return op, False
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
def top5():
|
def leaders(n, top=True):
|
||||||
data = read_data()
|
if n == 0:
|
||||||
items = [(score, name) for name, score in data.items()]
|
return
|
||||||
items.sort(key=lambda item: item[0], reverse=True)
|
|
||||||
out = ''
|
|
||||||
for idx in range(5):
|
|
||||||
try:
|
|
||||||
item = items[idx]
|
|
||||||
out += '{}. _{}_ : {}\n'.format(idx+1, item[1], item[0])
|
|
||||||
except IndexError:
|
|
||||||
break
|
|
||||||
return out
|
|
||||||
|
|
||||||
def bottom5():
|
|
||||||
data = read_data()
|
data = read_data()
|
||||||
items = [(score, name) for name, score in data.items()]
|
items = [(score, name) for name, score in data.items()]
|
||||||
items.sort(key=lambda item: item[0])
|
items.sort(key=lambda item: item[0], reverse=top)
|
||||||
out = ''
|
out = ''
|
||||||
for idx in range(4, -1, -1):
|
|
||||||
|
for idx in range(n):
|
||||||
try:
|
try:
|
||||||
item = items[idx]
|
item = items[idx]
|
||||||
out += '{}. _{}_ : {}\n'.format(len(items)-idx, item[1], item[0])
|
rank = idx + 1 if top else len(items) - idx
|
||||||
|
out += '{}. _{}_ : {}\n'.format(rank, item[1], item[0])
|
||||||
except IndexError:
|
except IndexError:
|
||||||
break
|
break
|
||||||
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue