Skip to content

Commit 8bfbe22

Browse files
committed
Add number of tasks completed in a day and update XP gained calculation
1 parent c748d28 commit 8bfbe22

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

app.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ 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=datetime.now().date(), nullable=False
3232
) # user last task completion date
3333
daily_streak = db.Column(
3434
db.Integer, default=0, nullable=False
3535
) # user daily task streak
36+
daily_tasks_completed = db.Column(
37+
db.Integer, default=0, nullable=False
38+
) # user tasks completed in a day
3639

3740
def add_xp(self, amount): # add XP
3841
self.xp += amount # add XP by amount
3942
self.total_xp += amount # add total XP by amount
4043
flash(
4144
"Task completed! You gained " +
4245
short_numeric_filter(amount) + " XP!"
43-
) # display message with gained XP
46+
) # display message with amount of XP earned
4447
self.check_level_up() # check if user has leveled up
4548

4649
def check_level_up(self): # check if user has leveled up
@@ -180,12 +183,12 @@ def complete_task(task_id): # complete task from task id
180183
task.completed = True # complete the task
181184
else: # if task is repeatable
182185
task.times_completed += 1 # increase times task completed by 1
183-
task.due_date = calculate_next_recurring_event(
184-
task.original_due_date,
185-
task.times_completed,
186-
task.repeat_interval,
187-
task.repeat_often,
188-
) # calculate next task due date
186+
task.due_date = calculate_next_recurring_event(
187+
task.original_due_date,
188+
task.times_completed,
189+
task.repeat_interval,
190+
task.repeat_often,
191+
) # calculate next task due date
189192
if task.repeat_often == 1: # if the task repetition interval is daily
190193
if task.repeat_interval < 7: # 7 days is 1 week
191194
repeat_multiplier = 1 + (task.repeat_interval - 1) / (
@@ -244,9 +247,18 @@ def complete_task(task_id): # complete task from task id
244247
) # calculate difference in days
245248
if day_difference.days == 1: # if a new day has passed
246249
user.daily_streak += 1 # increase daily streak by 1
250+
user.daily_tasks_completed = (
251+
0 # reset number of tasks completed in a day to 0
252+
)
247253
elif day_difference.days > 1: # if more than a day has passed
248-
user.daily_streak = 0 # reset daily streak to 0
249-
254+
user.daily_streak = 1 # reset daily streak to 1
255+
user.daily_tasks_completed = (
256+
0 # reset number of tasks completed in a day to 0
257+
)
258+
else:
259+
user.daily_tasks_completed += (
260+
1 # increase number of tasks completed in a day by 1
261+
)
250262
user.last_completion_date = (
251263
datetime.now()
252264
) # set user last completion date to today
@@ -260,6 +272,7 @@ def complete_task(task_id): # complete task from task id
260272
* (1 + math.log(max(user.tasks_completed, 1)))
261273
* (1 + math.log(max(active_tasks, 1)))
262274
* (1 + user.daily_streak / 10)
275+
* (1 + user.daily_tasks_completed / 10)
263276
)
264277
) # add XP
265278
db.session.commit() # commit database changes
@@ -339,6 +352,14 @@ def init_db(): # initialize database
339352
db.session.execute(
340353
text("ALTER TABLE user ADD COLUMN daily_streak INT NOT NULL DEFAULT 0")
341354
) # create tasks completed column
355+
if "daily_tasks_completed" not in [
356+
column["name"] for column in db.inspect(db.engine).get_columns("user")
357+
]: # check if daily tasks completed column is not in user table
358+
db.session.execute(
359+
text(
360+
"ALTER TABLE user ADD COLUMN daily_tasks_completed INT NOT NULL DEFAULT 0"
361+
)
362+
) # create daily tasks completed column
342363
if User.query.count() == 0: # if there is no users
343364
new_user = User(username="Player") # create new user
344365
db.session.add(new_user) # add new user to database

0 commit comments

Comments
 (0)