Hopefully fix removing users last point. Fix logging

This commit is contained in:
Watchful1
2020-12-16 18:53:01 -08:00
parent 236e40ac97
commit f3c527bb06
3 changed files with 64 additions and 38 deletions

View File

@@ -1,4 +1,5 @@
import logging import logging
from logging.handlers import RotatingFileHandler
import os import os
import os.path import os.path
import re import re
@@ -28,7 +29,7 @@ def run():
cfg = config.load() cfg = config.load()
file_handler = logging.FileHandler(cfg.log_path, 'w', 'utf-8') file_handler = RotatingFileHandler(cfg.log_path, mode='w', maxBytes=1024*1024, backupCount=3, encoding='utf-8')
console_handler = logging.StreamHandler(sys.stderr) console_handler = logging.StreamHandler(sys.stderr)
console_handler.setLevel(logging.INFO) console_handler.setLevel(logging.INFO)
@@ -129,7 +130,10 @@ def monitor_comments(reddit, subreddit, db, levels, cfg):
points = db.get_points(solver) points = db.get_points(solver)
logging.info('Total points for user "%s": %d', solver.name, points) logging.info('Total points for user "%s": %d', solver.name, points)
level_info = level.user_level_info(points, levels) if points > 0:
level_info = level.user_level_info(points, levels)
else:
level_info = None
# Reply to the comment marking the submission as solved # Reply to the comment marking the submission as solved
reply_body = reply.make(solver, reply_body = reply.make(solver,
@@ -154,8 +158,8 @@ def monitor_comments(reddit, subreddit, db, levels, cfg):
continue continue
# Check if (non-mod) user flair should be updated to new level # Check if (non-mod) user flair should be updated to new level
lvl = level_info.current if level_info and level_info.current and level_info.current.points == points:
if lvl and lvl.points == points: lvl = level_info.current
logging.info('User reached level: %s', lvl.name) logging.info('User reached level: %s', lvl.name)
if not subreddit.moderator(redditor=solver): if not subreddit.moderator(redditor=solver):
logging.info('User is not mod; setting flair') logging.info('User is not mod; setting flair')

View File

@@ -63,6 +63,16 @@ class Database:
self.cursor.execute(insert_stmt, {'id': redditor.id, 'name': redditor.name}) self.cursor.execute(insert_stmt, {'id': redditor.id, 'name': redditor.name})
return self.cursor.rowcount return self.cursor.rowcount
@transaction
def remove_redditor(self, redditor):
insert_stmt = '''
DELETE FROM redditor_points
WHERE id = :id
AND name = :name
'''
self.cursor.execute(insert_stmt, {'id': redditor.id, 'name': redditor.name})
return self.cursor.rowcount
def add_point(self, redditor): def add_point(self, redditor):
return self._update_points(redditor, 1) return self._update_points(redditor, 1)
@@ -73,18 +83,21 @@ class Database:
def _update_points(self, redditor, points_modifier): def _update_points(self, redditor, points_modifier):
"""points_modifier is positive to add points, negative to subtract.""" """points_modifier is positive to add points, negative to subtract."""
points = self.get_points(redditor, add_if_none=True) points = self.get_points(redditor, add_if_none=True)
params = { if points + points_modifier <= 0:
'id': redditor.id, return self.remove_redditor(redditor)
'name': redditor.name, else:
'points': points + points_modifier, params = {
} 'id': redditor.id,
update_stmt = ''' 'name': redditor.name,
UPDATE redditor_points 'points': points + points_modifier,
SET points = :points }
WHERE id = :id AND name = :name update_stmt = '''
''' UPDATE redditor_points
self.cursor.execute(update_stmt, params) SET points = :points
return self.cursor.rowcount WHERE id = :id AND name = :name
'''
self.cursor.execute(update_stmt, params)
return self.cursor.rowcount
@transaction @transaction
def get_points(self, redditor, add_if_none=False): def get_points(self, redditor, add_if_none=False):
@@ -101,6 +114,8 @@ class Database:
elif add_if_none: elif add_if_none:
points = 0 points = 0
self.add_redditor(redditor) self.add_redditor(redditor)
else:
points = 0
return points return points

View File

@@ -27,32 +27,35 @@ def make(redditor, points, level_info, feedback_url=None, scoreboard_url=None, i
else: else:
paras = [remove_header()] paras = [remove_header()]
if points <= 1: if level_info is None:
paras.append(first_greeting(redditor)) paras.append(no_points(redditor))
if level_info.current and points == level_info.current.points: else:
paras.append(level_up(redditor, if points <= 1:
level_info.current.name, paras.append(first_greeting(redditor))
tag_user=False)) if level_info.current and points == level_info.current.points:
elif points > 1: paras.append(level_up(redditor,
user_already_tagged = False level_info.current.name,
tag_user=False))
elif points > 1:
user_already_tagged = False
if level_info.current and points == level_info.current.points: if level_info.current and points == level_info.current.points:
paras.append(level_up(redditor, paras.append(level_up(redditor,
level_info.current.name, level_info.current.name,
tag_user=(not user_already_tagged))) tag_user=(not user_already_tagged)))
user_already_tagged = True user_already_tagged = True
if points % EXCESS_POINTS == 0: if points % EXCESS_POINTS == 0:
first_excess = (points == EXCESS_POINTS) first_excess = (points == EXCESS_POINTS)
paras.append(new_excess_symbol(redditor, paras.append(new_excess_symbol(redditor,
first_excess=first_excess, first_excess=first_excess,
tag_user=(not user_already_tagged))) tag_user=(not user_already_tagged)))
user_already_tagged = True user_already_tagged = True
if not user_already_tagged: if not user_already_tagged:
paras.append(normal_greeting(redditor)) paras.append(normal_greeting(redditor))
paras.append(points_status(redditor, points, level_info)) paras.append(points_status(redditor, points, level_info))
paras.append(divider()) paras.append(divider())
paras.append(footer(feedback_url=feedback_url, scoreboard_url=scoreboard_url)) paras.append(footer(feedback_url=feedback_url, scoreboard_url=scoreboard_url))
return '\n\n'.join(paras) return '\n\n'.join(paras)
@@ -69,6 +72,10 @@ def remove_header():
return 'Point removed.' return 'Point removed.'
def no_points(redditor):
return f'u/{redditor.name} now has no points'
def first_greeting(redditor): def first_greeting(redditor):
return (f'Congrats, u/{redditor.name}, you have received a point! Points ' return (f'Congrats, u/{redditor.name}, you have received a point! Points '
'help you "level up" to the next user flair!') 'help you "level up" to the next user flair!')