-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.py
More file actions
137 lines (119 loc) · 4.77 KB
/
Copy pathprogress.py
File metadata and controls
137 lines (119 loc) · 4.77 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
133
134
135
136
137
"""Progress, XP and streak tracking, persisted to progress.json next to the app."""
from __future__ import annotations
import datetime as dt
import json
import os
STORE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "progress.json")
DEFAULT = {
"language": "en",
"prog_language": "python",
"xp": 0,
"streak": 0,
"best_streak": 0,
"last_day": "",
"lessons_done": [],
"problems_solved": [],
"attempts": {}, # id -> attempts
"drill_solved": 0,
"drill_attempts": 0,
"topic_solved": {}, # topic -> count
"history": [], # [{"day": "2026-07-27", "solved": 3}]
"recent": [], # last solved ids
}
XP_FOR = {"Easy": 10, "Medium": 20, "Hard": 35}
class Progress:
def __init__(self, path: str = STORE):
self.path = path
self.data = dict(DEFAULT)
self.load()
# ------------------------------------------------------------------ io
def load(self) -> None:
if os.path.exists(self.path):
try:
with open(self.path, "r", encoding="utf-8") as fh:
saved = json.load(fh)
for key, value in DEFAULT.items():
self.data[key] = saved.get(key, value if not isinstance(value, (list, dict))
else type(value)())
except (OSError, ValueError):
self.data = dict(DEFAULT)
self.touch_day()
def save(self) -> None:
try:
with open(self.path, "w", encoding="utf-8") as fh:
json.dump(self.data, fh, indent=2)
except OSError:
pass
# --------------------------------------------------------------- streak
def touch_day(self) -> None:
today = dt.date.today().isoformat()
last = self.data.get("last_day", "")
if last == today:
return
if last:
gap = (dt.date.today() - dt.date.fromisoformat(last)).days
self.data["streak"] = self.data["streak"] + 1 if gap == 1 else 1
else:
self.data["streak"] = 1
self.data["best_streak"] = max(self.data["best_streak"], self.data["streak"])
self.data["last_day"] = today
self.save()
# --------------------------------------------------------------- events
def attempt(self, task_id: str) -> None:
self.data["attempts"][task_id] = self.data["attempts"].get(task_id, 0) + 1
self.save()
def solved(self, task_id: str, difficulty: str = "Easy", topic: str = "General",
kind: str = "interview") -> int:
"""Record a solve. Returns XP gained (0 if it was already solved)."""
self.touch_day()
gained = 0
bucket = "lessons_done" if kind == "lesson" else "problems_solved"
if kind == "drill":
self.data["drill_solved"] += 1
gained = XP_FOR.get(difficulty, 10)
elif task_id not in self.data[bucket]:
self.data[bucket].append(task_id)
gained = XP_FOR.get(difficulty, 10) if kind != "lesson" else 8
else:
gained = 2 # small reward for re-solving
self.data["xp"] += gained
self.data["topic_solved"][topic] = self.data["topic_solved"].get(topic, 0) + 1
today = dt.date.today().isoformat()
hist = self.data["history"]
if hist and hist[-1].get("day") == today:
hist[-1]["solved"] += 1
else:
hist.append({"day": today, "solved": 1})
del hist[:-60]
recent = self.data["recent"]
entry = f"{kind}:{task_id}"
if entry in recent:
recent.remove(entry)
recent.insert(0, entry)
del recent[12:]
self.save()
return gained
# ---------------------------------------------------------------- query
def is_solved(self, task_id: str, kind: str = "interview") -> bool:
bucket = "lessons_done" if kind == "lesson" else "problems_solved"
return task_id in self.data[bucket]
@property
def level(self) -> int:
return 1 + int((self.data["xp"] / 100) ** 0.75)
@property
def xp(self) -> int:
return self.data["xp"]
def level_progress(self) -> tuple[int, int]:
"""(xp into current level, xp needed for the level)."""
lvl = self.level
lo = int(((lvl - 1) ** (1 / 0.75)) * 100) if lvl > 1 else 0
hi = int((lvl ** (1 / 0.75)) * 100)
return max(0, self.xp - lo), max(1, hi - lo)
def reset(self) -> None:
# a reset is about XP and solved marks, not about preferences
keep = {key: self.data.get(key, DEFAULT[key])
for key in ("language", "prog_language")}
self.data = json.loads(json.dumps(DEFAULT))
self.data.update(keep)
self.touch_day()
self.save()