Skip to content

Commit 9e14b73

Browse files
make stats streak care if you played the right day or not
1 parent 23ab3ae commit 9e14b73

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

src/hooks/useRangleScores.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {SaveStateDay, ScoreState} from "@/hooks/useRangleState";
77
export const useRangleScores = () => {
88
const [scores, setScores] = useState<ScoreState | null>(null);
99

10+
// TODO: recompute scores if game finished
11+
1012
useEffect(() => {
1113
const migrate_from_state = () => {
1214
const raw_state = localStorage.getItem("rangle_state_v1");
@@ -29,8 +31,10 @@ export const useRangleScores = () => {
2931

3032
migrated_scores[date] = {
3133
attempts: game_data.attempts.length,
32-
updated: new Date().toISOString(),
33-
result
34+
result,
35+
36+
// give the benefit of doubt, set the updated time to when the puzzle was released
37+
updated: new Date(`${date}T12:00:00Z`).toISOString(),
3438
};
3539
}
3640

@@ -73,75 +77,74 @@ export const useRangleScores = () => {
7377
return null;
7478
}
7579

76-
// filter out in progress games and sort by date ascending
7780
const sorted_entries = Object.entries(scores)
7881
.filter(([_, score]) => score.result !== undefined)
7982
.sort(([dateA], [dateB]) => dateA.localeCompare(dateB));
8083

8184
let current_play_streak = 0;
8285
let longest_play_streak = 0;
83-
8486
let current_win_streak = 0;
8587
let longest_win_streak = 0;
86-
8788
let wins = 0;
8889
const score_distribution: Record<number, number> = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 };
8990

9091
let previous_date: Date | null = null;
92+
let previous_was_on_time = false;
9193

9294
for (const [date_str, score] of sorted_entries) {
93-
// sum wins and score distribution
95+
// did they play on the day the puzzle was released? (i.e. not archived play)
96+
const played_date_iso = new Date(score.updated).toLocaleDateString("en-CA", { timeZone: time_zone });
97+
const played_on_time = played_date_iso === date_str;
98+
99+
// regardless archived or not, count the win if they got it right
94100
if (score.result === true) {
95101
wins++;
96102
if (score.attempts) {
97103
score_distribution[score.attempts] = (score_distribution[score.attempts] || 0) + 1;
98104
}
99105
}
100106

101-
// utc date maths
107+
// utc maths
102108
const current_date = new Date(`${date_str}T00:00:00Z`);
103109

104110
if (previous_date) {
105111
const diff_time = current_date.getTime() - previous_date.getTime();
106112
const diff_days = Math.round(diff_time / (1000 * 60 * 60 * 24));
107113

108-
if (diff_days === 1) {
109-
// next day in a row, so play streak continues
114+
if (diff_days === 1 && played_on_time && previous_was_on_time) {
115+
// played on time and next day in a row, so play streak continues
110116
current_play_streak++;
111117

112118
// if they also won, win streak continues, otherwise resets
113-
if (score.result === true) {
114-
current_win_streak++;
115-
} else {
116-
current_win_streak = 0;
117-
}
119+
current_win_streak = score.result === true ? current_win_streak + 1 : 0;
118120
} else {
119121
// both streaks reset as missed at least one day
120-
current_play_streak = 1;
121-
current_win_streak = score.result === true ? 1 : 0;
122+
current_play_streak = played_on_time ? 1 : 0;
123+
current_win_streak = (played_on_time && score.result === true) ? 1 : 0;
122124
}
123125
} else {
124126
// first entry, so start play streak and win streak if they won
125-
current_play_streak = 1;
126-
current_win_streak = score.result === true ? 1 : 0;
127+
current_play_streak = played_on_time ? 1 : 0;
128+
current_win_streak = (played_on_time && score.result === true) ? 1 : 0;
127129
}
128130

129131
// compare against current longest streaks
130-
if (current_play_streak > longest_play_streak) {
132+
if (current_play_streak > longest_play_streak){
131133
longest_play_streak = current_play_streak;
132134
}
135+
133136
if (current_win_streak > longest_win_streak) {
134137
longest_win_streak = current_win_streak;
135138
}
136139

137140
previous_date = current_date;
141+
previous_was_on_time = played_on_time;
138142
}
139143

140144
// check if streaks died today
141145
if (previous_date) {
142146
const today_iso = new Date().toLocaleDateString("en-CA", { timeZone: time_zone });
143147
const today_utc = new Date(`${today_iso}T00:00:00Z`);
144-
145148
const days_since_last_play = Math.round((today_utc.getTime() - previous_date.getTime()) / (1000 * 60 * 60 * 24));
146149

147150
// if they haven't played today or yesterday, streaks are dead

0 commit comments

Comments
 (0)