JavaScript · 982 bytes Raw Blame History
1 // Game state singleton for dougk gamification
2 // Tracks currency (captured koi) and other persistent state
3
4 const STORAGE_KEY = 'dougk-koi'
5
6 const gameState = {
7 capturedKoi: parseInt(localStorage.getItem(STORAGE_KEY) || '0'),
8
9 addKoi(n = 1) {
10 this.capturedKoi += n
11 this.save()
12 this.notifyListeners()
13 },
14
15 spendKoi(n) {
16 if (this.capturedKoi >= n) {
17 this.capturedKoi -= n
18 this.save()
19 this.notifyListeners()
20 return true
21 }
22 return false
23 },
24
25 getKoi() {
26 return this.capturedKoi
27 },
28
29 save() {
30 localStorage.setItem(STORAGE_KEY, this.capturedKoi.toString())
31 },
32
33 // Listener system for UI updates
34 listeners: [],
35
36 addListener(callback) {
37 this.listeners.push(callback)
38 },
39
40 removeListener(callback) {
41 this.listeners = this.listeners.filter(l => l !== callback)
42 },
43
44 notifyListeners() {
45 for (const listener of this.listeners) {
46 listener(this.capturedKoi)
47 }
48 }
49 }
50
51 export default gameState