-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
132 lines (109 loc) · 3.5 KB
/
helpers.py
File metadata and controls
132 lines (109 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from datetime import datetime, timezone
from functools import wraps
from flask import session, redirect
from cs50 import SQL
# Create a single shared DB connection (important for performance)
db = SQL("sqlite:///tama.db")
# ---------------------------------------------------
# LOGIN REQUIRED DECORATOR
# ---------------------------------------------------
def login_required(f):
"""
Decorator that ensures the user is logged in
before accessing a route.
"""
@wraps(f)
def decorated(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated
# ---------------------------------------------------
# PET STAGE LOGIC
# ---------------------------------------------------
def compute_stage(pet):
"""
Determine the pet stage based on age in minutes.
If any stat reaches death conditions, return 'dead'.
Stages timeline:
- 0–1 min → egg
- 1–2 min → baby
- 2–4 min → child
- 4–6 min → teen
- 6–8 min → adult
- 8-10 min → elder
"""
# Important: birth stored as TEXT without timezone → parse cleanly
birth = datetime.fromisoformat(pet["birth"])
# Use timezone-aware datetime consistently
now = datetime.now(timezone.utc).replace(tzinfo=None)
age_minutes = (now - birth).total_seconds() / 60
# Death conditions
if (
age_minutes > 10 or
pet["health"] <= 0 or
pet["happiness"] <= 0 or
pet["hygiene"] <= 0 or
pet["hunger"] >= 100
):
return "dead"
if age_minutes < 1:
return "egg"
elif age_minutes < 2:
return "baby"
elif age_minutes < 4:
return "child"
elif age_minutes < 6:
return "teen"
elif age_minutes < 8:
return "adult"
else:
return "elder"
# ---------------------------------------------------
# NEEDS DECAY SYSTEM
# ---------------------------------------------------
def update_needs(pet):
"""
Applies automatic stat decay based on the number of minutes
passed since last_update.
This function:
- calculates minutes elapsed
- applies decay formulas
- updates the database
- returns updated pet dictionary
"""
last_update = datetime.fromisoformat(pet["last_update"])
now = datetime.now(timezone.utc).replace(tzinfo=None)
seconds = (now - last_update).total_seconds()
# No decay if less than 1 minute has passed
if seconds < 5:
return pet
stage = compute_stage(pet)
stage_factor = 1
if stage == "adult":
stage_factor = 2
elif stage == "elder":
stage_factor = 3
# --- DECAY RULES ---
# You can adjust these values to make the game harder/easier.
hunger = min(100, pet["hunger"] + seconds * 5)
happiness = max(0, pet["happiness"] - seconds * 2)
hygiene = max(0, pet["hygiene"] - seconds * 2)
health = max(0, pet["health"] - seconds * 1 * stage_factor)
# Update DB only once per request to avoid lag
db.execute("""
UPDATE pet
SET hunger = ?,
happiness = ?,
hygiene = ?,
health = ?,
last_update = CURRENT_TIMESTAMP
WHERE id = ?
""", hunger, happiness, hygiene, health, pet["id"])
# Update the local dictionary so the caller gets fresh values
pet["hunger"] = hunger
pet["happiness"] = happiness
pet["hygiene"] = hygiene
pet["health"] = health
pet["last_update"] = now.isoformat()
return pet