Skip to content

Commit dbe4dd6

Browse files
committed
Add task streak feature with XP multiplier based on task streak
1 parent d116668 commit dbe4dd6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

app.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Task(db.Model):
8282
times_completed = db.Column(
8383
db.Integer, default=0, nullable=False
8484
) # number of times task has completed
85+
streak = db.Column(db.Integer, default=0, nullable=False) # task streak
8586
completed = db.Column(
8687
db.Boolean, default=False, nullable=False
8788
) # is task completed
@@ -192,6 +193,12 @@ def complete_task(task_id): # complete task from task id
192193
task.repeat_interval,
193194
task.repeat_often,
194195
) # calculate next task due date
196+
if (
197+
datetime.now().date() > task.due_date
198+
): # chech if task is overdue (current date is after task due date)
199+
task.streak = 0 # reset streak to 0
200+
else:
201+
task.streak += 1 # increase streak by 1
195202
if task.repeat_often == 1: # if the task repetition interval is daily
196203
if task.repeat_interval < 7: # 7 days is 1 week
197204
repeat_multiplier = 1 + (task.repeat_interval - 1) / (
@@ -279,6 +286,7 @@ def complete_task(task_id): # complete task from task id
279286
* (1 + user.daily_streak / 10)
280287
* (1 + user.daily_tasks_completed / 10)
281288
* (1 + math.log(max(user.days_completed, 1)))
289+
* (1 + task.streak / 10)
282290
)
283291
) # add XP
284292
db.session.commit() # commit database changes
@@ -430,6 +438,12 @@ def init_db(): # initialize database
430438
"ALTER TABLE task ADD COLUMN times_completed INT NOT NULL DEFAULT 0"
431439
)
432440
) # create times completed column
441+
if "streak" not in [
442+
column["name"] for column in db.inspect(db.engine).get_columns("task")
443+
]: # check if streak column is not in task table
444+
db.session.execute(
445+
text("ALTER TABLE task ADD COLUMN streak INT NOT NULL DEFAULT 0")
446+
) # create streak column
433447
tasks = Task.query.all() # get list of tasks
434448
for task in tasks: # repeat for each task
435449
if (

0 commit comments

Comments
 (0)