Skip to content

Commit aea9154

Browse files
committed
Add due date XP multiplier
1 parent a9916df commit aea9154

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

app.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import calendar
2-
from datetime import datetime, timedelta
2+
from datetime import datetime, timedelta, date
33
import math
44
import os
55
from flask import Flask, flash, render_template, request, redirect, url_for
@@ -28,7 +28,7 @@ class User(db.Model):
2828
db.Integer, default=0, nullable=False
2929
) # number of times tasks has completed
3030
last_completion_date = db.Column(
31-
db.Date, default=datetime.now().date(), nullable=False
31+
db.Date, default=date.today(), nullable=False
3232
) # user last task completion date
3333
daily_streak = db.Column(
3434
db.Integer, default=0, nullable=False
@@ -65,11 +65,10 @@ class Task(db.Model):
6565
unique=True, nullable=False) # task id
6666
name = db.Column(db.String(80), nullable=False) # task name
6767
original_due_date = db.Column(
68-
db.Date, default=datetime.now().date(), nullable=False
69-
) # task due date
70-
due_date = db.Column(
71-
db.Date, default=datetime.now().date(), nullable=False
68+
db.Date, default=date.today(), nullable=False
7269
) # task due date
70+
due_date = db.Column(db.Date, default=date.today(),
71+
nullable=False) # task due date
7372
priority = db.Column(db.Integer, default=1,
7473
nullable=False) # task priority
7574
difficulty = db.Column(db.Integer, default=1,
@@ -183,6 +182,7 @@ def add_task(): # add task to task list
183182
def complete_task(task_id): # complete task from task id
184183
task = Task.query.get(task_id) # get task by task id
185184
if task:
185+
due_multiplier = 1 # set default due multiplier to 1
186186
if task.repeat_often == 5: # if task is one-time task
187187
task.completed = True # complete the task
188188
else: # if task is repeatable
@@ -193,8 +193,28 @@ def complete_task(task_id): # complete task from task id
193193
task.repeat_interval,
194194
task.repeat_often,
195195
) # calculate next task due date
196+
days_to_due = (
197+
task.due_date - date.today()
198+
).days # calculate number of days until task is due
199+
if days_to_due > 0: # if task due date is after today
200+
due_multiplier = 1 + 1 / (
201+
days_to_due + 1
202+
) # set due multiplier that increases over time when task is closer to due date
203+
elif (
204+
days_to_due < 0
205+
): # if task is overdue (current date is after task due date)
206+
due_multiplier = -2 / (
207+
days_to_due - 1
208+
) # set due multiplier that decreases over time when task is overdue
209+
elif days_to_due == 0: # if task due date is today
210+
next_midnight = datetime.combine(
211+
datetime.now().date() + timedelta(days=1), datetime.min.time()
212+
)
213+
due_multiplier = 4 / (
214+
1 + (next_midnight - datetime.now()) / timedelta(days=1)
215+
) # set due multiplier to 2 and increases over time to 4 at midnight
196216
if (
197-
datetime.now().date() > task.due_date
217+
date.today() > task.due_date
198218
): # check if task is overdue (current date is after task due date)
199219
task.streak = 0 # reset streak to 0
200220
else:
@@ -287,6 +307,7 @@ def complete_task(task_id): # complete task from task id
287307
* (1 + user.daily_tasks_completed / 10)
288308
* (1 + math.log(max(user.days_completed, 1)))
289309
* (1 + task.streak / 10)
310+
* due_multiplier
290311
)
291312
) # add XP
292313
db.session.commit() # commit database changes
@@ -320,7 +341,7 @@ def calculate_next_recurring_event(
320341
new_year = original_date.year + (new_month - 1) // 12 # get new year
321342
new_month = (
322343
new_month - 1
323-
) % 12 # clamp month from 1 (January) to 12 (December)
344+
) % 12 + 1 # clamp month from 1 (January) to 12 (December)
324345
max_days_in_month = calendar.monthrange(new_year, new_month)[
325346
1
326347
] # get number of days in month
@@ -337,7 +358,9 @@ def calculate_next_recurring_event(
337358
original_date.day, max_days_in_month)
338359
) # add years in original date
339360
else:
340-
return None
361+
return datetime(
362+
original_date.year, original_date.month, original_date.day
363+
) # return original task due date
341364

342365

343366
def init_db(): # initialize database
@@ -450,12 +473,10 @@ def init_db(): # initialize database
450473
task.original_due_date is None
451474
): # check if task original due date is none
452475
task.original_due_date = (
453-
datetime.now().date()
476+
date.today()
454477
) # set task original due date to today's date
455478
if task.due_date is None: # check if task due date is none
456-
task.due_date = (
457-
datetime.now().date()
458-
) # set task due date to today's date
479+
task.due_date = date.today() # set task due date to today's date
459480
if task.priority is None: # check if task priority is none
460481
task.priority = 1 # set task priority to low
461482
if task.difficulty is None: # check if task difficulty is none

0 commit comments

Comments
 (0)