Skip to content

Commit 13d1948

Browse files
committed
Add user tasks completed XP multiplier
1 parent dec3e3c commit 13d1948

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class User(db.Model):
2222
nullable=False) # user XP required
2323
total_xp = db.Column(db.Float, default=0, nullable=False) # user total XP
2424
level = db.Column(db.Integer, default=1, nullable=False) # user level
25+
tasks_completed = db.Column(
26+
db.Integer, default=0, nullable=False
27+
) # number of times tasks has completed
2528

2629
def add_xp(self, amount): # add XP
2730
self.xp += amount # add XP by amount
@@ -225,6 +228,7 @@ def complete_task(task_id): # complete task from task id
225228
* task.repeat_often
226229
* repeat_multiplier
227230
* (1 + math.log(max(task.times_completed, 1)))
231+
* (1 + math.log(max(user.tasks_completed, 1)))
228232
)
229233
) # add XP
230234
db.session.commit() # commit database changes
@@ -284,6 +288,14 @@ def init_db(): # initialize database
284288
today = datetime.now().strftime(
285289
"%Y-%m-%d"
286290
) # get today's date in YYYY-MM-DD format
291+
if "tasks_completed" not in [
292+
column["name"] for column in db.inspect(db.engine).get_columns("user")
293+
]: # check if tasks completed column is not in user table
294+
db.session.execute(
295+
text(
296+
"ALTER TABLE user ADD COLUMN tasks_completed INT NOT NULL DEFAULT 0"
297+
)
298+
) # create tasks completed column
287299
if User.query.count() == 0: # if there is no users
288300
new_user = User(username="Player") # create new user
289301
db.session.add(new_user) # add new user to database

0 commit comments

Comments
 (0)