-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
103 lines (86 loc) · 2.45 KB
/
main.ts
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
import "./style.css"
import { send, subscribe } from "./src/app"
import { audioControl, sfxControl } from "./src/audio/audioControl"
// @ts-ignore
import { registerSW } from "virtual:pwa-register"
// @ts-ignore
import Worker from "./src/worker?worker"
const root = document.documentElement
registerSW()
const analyzerWorker = new Worker()
// Music
subscribe("toggleMute", () => {
audioControl.toggleMute()
sfxControl.toggleMute()
})
subscribe("playThemeMusic", async () => {
if (audioControl.isSongPlaying("theme:0")) return
await audioControl.stop()
audioControl.selectSong("theme:0")
audioControl.fadeIn()
})
subscribe("playGameMusic", async () => {
await audioControl.stop()
audioControl.shuffleAlbum("inGame")
})
// SFX
subscribe("playEndGameSfx", (isHumanWinner) => {
const song = isHumanWinner ? "sfx:0" : "sfx:1"
audioControl.pause()
sfxControl.selectSong(song)
sfxControl.fadeIn()
const playAftergameMusic = setInterval(() => {
if (!sfxControl.isSongPlaying(song)) {
clearInterval(playAftergameMusic)
audioControl.shuffleAlbum("afterGame")
}
}, 1000)
})
// Save/Load game
subscribe("saveGame", (game: string) => {
localStorage.setItem("game", game)
})
subscribe("loadGame", () => {
const game = localStorage.getItem("game")
if (game) {
send("gameLoaded", game)
}
})
// Save/load lobby settings
subscribe("saveLobby", (settings: string) => {
localStorage.setItem("lobbySettings", settings)
})
subscribe("loadLobby", () => {
const settings = localStorage.getItem("lobbySettings")
if (settings) {
send("lobbyLoaded", settings)
}
})
// Analyzer
subscribe("analyze", (game: string) => analyzerWorker.postMessage(game))
analyzerWorker.onmessage = (e: any) => {
send("analyzed", e.data)
}
// Zooming in and out of the game board
function getCurrentZoom() {
return parseInt(
root.style.getPropertyValue("--cell-size")?.replace("px", ""),
10
)
}
function updateZoom(zoom: number) {
localStorage.setItem("zoom", zoom.toString())
root.style.setProperty("--cell-size", `${zoom}px`)
}
function zoomIn() {
const currentZoom = getCurrentZoom()
updateZoom(currentZoom + 5)
}
function zoomOut() {
const currentZoom = getCurrentZoom()
updateZoom(currentZoom - 5)
}
subscribe("zoomIn", zoomIn)
subscribe("zoomOut", zoomOut)
const zoom = parseInt(localStorage.getItem("zoom") || "", 10) || 85
updateZoom(zoom)