File size: 1,840 Bytes
fd586d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const ProgressStore = {
  _key: 'math4-progress',

  _defaults() {
    return {
      points: 0,
      streak: 0,
      bestStreak: 0,
      totalCorrect: 0,
      totalAttempted: 0,
      topicStats: {},
      quizHistory: []
    };
  },

  load() {
    try {
      const raw = localStorage.getItem(this._key);
      if (raw) {
        const data = JSON.parse(raw);
        return { ...this._defaults(), ...data };
      }
    } catch (e) {}
    return this._defaults();
  },

  save(data) {
    try {
      localStorage.setItem(this._key, JSON.stringify(data));
    } catch (e) {}
  },

  getTopicStats(topic) {
    const data = this.load();
    if (!data.topicStats[topic]) {
      data.topicStats[topic] = { correct: 0, attempted: 0 };
    }
    return data.topicStats[topic];
  },

  recordAnswer(topic, correct) {
    const data = this.load();
    if (!data.topicStats[topic]) {
      data.topicStats[topic] = { correct: 0, attempted: 0 };
    }
    data.topicStats[topic].attempted++;
    data.totalAttempted++;
    if (correct) {
      data.topicStats[topic].correct++;
      data.totalCorrect++;
      data.streak++;
      if (data.streak > data.bestStreak) data.bestStreak = data.streak;
      data.points += 10;
    } else {
      data.streak = 0;
    }
    this.save(data);
    return data;
  },

  recordQuiz(topic, score, total, difficulty) {
    const data = this.load();
    data.quizHistory.push({
      topic,
      score,
      total,
      difficulty,
      date: new Date().toISOString()
    });
    if (data.quizHistory.length > 50) data.quizHistory.shift();
    const pct = score / total;
    if (pct >= 1) data.points += 50;
    else if (pct >= 0.8) data.points += 30;
    else if (pct >= 0.6) data.points += 15;
    this.save(data);
    return data;
  },

  reset() {
    localStorage.removeItem(this._key);
  }
};