Skip to content

Commit

Permalink
Course: Fix meme credit
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorNelson committed Nov 22, 2024
1 parent 4944678 commit d6ce5ca
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions dojo_plugin/pages/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,29 @@ def query(module_id):

module_solves = {}

def thanks_count(dojo, discord_user, unique):
course_start = datetime.datetime.fromisoformat(dojo.course["start_date"])
thanks = (discord_user.thanks(start=course_start, end=course_start + datetime.timedelta(weeks=16))
.group_by(DiscordUserActivity.message_id))
if not unique:
thanks = thanks.group_by(DiscordUserActivity.source_user_id)
return thanks.count()

def weekly_memes(dojo, discord_user):
course_start = datetime.datetime.fromisoformat(dojo.course["start_date"])
memes = (discord_user.memes(start=course_start, end=course_start + datetime.timedelta(weeks=16))
.order_by(DiscordUserActivity.message_timestamp))
return set((meme.message_timestamp.astimezone(datetime.timezone.utc) - course_start).days // 7
for meme in memes)

def get_thanks_progress(dojo, user_id, unique):
discord_user = DiscordUsers.query.where(DiscordUsers.user_id == user_id).first()
if not discord_user:
return "Discord not linked"
if "start_date" not in dojo.course:
return "Error: unknown course start date"

course_start = datetime.datetime.fromisoformat(dojo.course["start_date"])
thanks = (discord_user.thanks(start=course_start, end=course_start + datetime.timedelta(weeks=16))
.group_by(DiscordUserActivity.message_id))
if not unique:
thanks = thanks.group_by(DiscordUserActivity.source_user_id)
return f"{thanks.count()} thanks"
return f"{thanks_count(dojo, discord_user, unique)} thanks"

def get_meme_progress(dojo, user_id):
discord_user = DiscordUsers.query.where(DiscordUsers.user_id == user_id).first()
Expand All @@ -135,19 +145,12 @@ def get_meme_progress(dojo, user_id):
if "start_date" not in dojo.course:
return "Error: unknown course start date"

course_start = datetime.datetime.fromisoformat(dojo.course["start_date"])
memes = (discord_user.memes(start=course_start, end=course_start + datetime.timedelta(weeks=16))
.order_by(DiscordUserActivity.message_timestamp))

weekly_memes = set((meme.message_timestamp.astimezone(datetime.timezone.utc) - course_start).days // 7
for meme in memes)

def week_string(week):
start = course_start + datetime.timedelta(weeks=week)
end = start + datetime.timedelta(days=6)
return f"{start.month:02d}/{start.day:02d}-{end.month:02d}/{end.day:02d}"

return " ".join(week_string(week) for week in weekly_memes)
return " ".join(week_string(week) for week in weekly_memes(dojo, discord_user))

def result(user_id):
assessment_grades = []
Expand All @@ -166,30 +169,22 @@ def get_thanks_credit(dojo, user_id, method, unique, max_credit):
discord_user = DiscordUsers.query.where(DiscordUsers.user_id == user_id).first()
if not discord_user or "start_date" not in dojo.course:
return 0
course_start = datetime.datetime.fromisoformat(dojo.course["start_date"])
thanks = (discord_user.thanks(start=course_start, end=course_start + datetime.timedelta(weeks=16))
.group_by(DiscordUserActivity.message_id))
if not unique:
thanks = thanks.group_by(DiscordUserActivity.source_user_id)
thanks_count = thanks.count()

if thanks_count == 0:
thanks = thanks_count(dojo, discord_user, unique)
if thanks == 0:
return 0.0

credit = 0.0
if method == "log50":
credit = max_credit * math.log(thanks_count, 50)
credit = max_credit * math.log(thanks, 50)
elif method == "1337log2":
credit = 1.337 ** math.log(thanks_count, 2) / 100
credit = 1.337 ** math.log(thanks, 2) / 100
return min(credit, max_credit)

@limit_extra
def get_meme_credit(dojo, user_id, value, max_credit):
discord_user = DiscordUsers.query.where(DiscordUsers.user_id == user_id).first()
if not discord_user or "start_date" not in dojo.course:
return 0.0
course_start = datetime.datetime.fromisoformat(dojo.course["start_date"])
meme_count = discord_user.memes(start=course_start, end=course_start + datetime.timedelta(weeks=16)).count()
meme_count = len(weekly_memes(dojo, discord_user))
return min(meme_count * value, max_credit)

@limit_extra
Expand Down

0 comments on commit d6ce5ca

Please sign in to comment.