-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.py
More file actions
82 lines (73 loc) · 2.57 KB
/
tracker.py
File metadata and controls
82 lines (73 loc) · 2.57 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
"""
QuestTracker handles users, quests, and persistence.
"""
import json
from quest import Quest
class User:
"""Represents a user with quests."""
def __init__(self, username: str):
self.username = username
self.quests = []
self.points = 0
def add_quest(self, quest: Quest):
"""Add a quest to the user's list."""
self.quests.append(quest)
def complete_quest(self, quest_name: str):
"""Complete a quest by name and add points."""
for q in self.quests:
if q.name == quest_name and not q.completed:
q.complete()
self.points += q.points
break
class QuestTracker:
"""Manages multiple users and saves/loads data."""
def __init__(self, data_file: str = "data/users.json"):
self.users = {}
self.data_file = data_file
self.load_users()
def add_user(self, username: str):
"""Add a new user or switch to existing user."""
if username not in self.users:
self.users[username] = User(username)
return True
return False
def get_user(self, username: str):
"""Retrieve a user by username."""
return self.users.get(username)
def save_users(self):
"""Save all users and quests to JSON."""
data = {}
for uname, user in self.users.items():
data[uname] = {
"points": user.points,
"quests": [
{
"name": q.name,
"description": q.description,
"points": q.points,
"completed": q.completed
} for q in user.quests
]
}
with open(self.data_file, "w") as f:
json.dump(data, f, indent=2)
def load_users(self):
"""Load users and quests from JSON."""
try:
with open(self.data_file, "r") as f:
data = json.load(f)
for uname, udata in data.items():
user = User(uname)
user.points = udata.get("points", 0)
for qd in udata.get("quests", []):
q = Quest(qd["name"], qd["description"], qd["points"])
if qd.get("completed", False):
q.complete()
user.add_quest(q)
self.users[uname] = user
except FileNotFoundError:
# File not created yet
pass
except json.JSONDecodeError:
# Empty file or invalid JSON
pass