-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (65 loc) · 1.81 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"math/rand"
"syscall/js"
"time"
g "github.com/jamestrew/gowasm-sweeper/pkg/game"
)
var game *g.Game
func newGame(this js.Value, args []js.Value) interface{} {
gameParams := args[0]
difficulty := g.DifficultyLevel(gameParams.Get("difficulty").Int())
if difficulty == g.Custom {
g.CustomWidth = gameParams.Get("width").Int()
g.CustomHeight = gameParams.Get("height").Int()
g.CustomMineCount = gameParams.Get("mineCount").Int()
}
rand.Seed(time.Now().UnixNano())
game, _ = g.NewGame(difficulty)
return game.AsJson()
}
func openCell(this js.Value, args []js.Value) interface{} {
x, y := args[0].Int(), args[1].Int()
game.OpenCell(x, y)
return game.AsJson()
}
func flagCell(this js.Value, args []js.Value) interface{} {
x, y := args[0].Int(), args[1].Int()
game.ToggleFlag(x, y)
return game.AsJson()
}
func chordedOpen(this js.Value, args []js.Value) interface{} {
x, y := args[0].Int(), args[1].Int()
game.ChordedOpen(x, y)
return game.AsJson()
}
func cellNeighbors(this js.Value, args []js.Value) interface{} {
x, y := args[0].Int(), args[1].Int()
data, err := json.Marshal(game.CellNeighbors(x, y))
if err != nil {
panic(err)
}
return string(data)
}
func debugMines(this js.Value, args []js.Value) interface{} {
fmt.Println(game.Mines)
return nil
}
func getGame(this js.Value, args []js.Value) interface{} {
if game != nil {
return game.AsJson()
}
return nil
}
func main() {
js.Global().Set("newGame", js.FuncOf(newGame))
js.Global().Set("openCell", js.FuncOf(openCell))
js.Global().Set("flagCell", js.FuncOf(flagCell))
js.Global().Set("chordedOpen", js.FuncOf(chordedOpen))
js.Global().Set("cellNeighbors", js.FuncOf(cellNeighbors))
js.Global().Set("getGame", js.FuncOf(getGame))
js.Global().Set("debugMines", js.FuncOf(debugMines))
<-make(chan bool)
}