-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_js.go
219 lines (196 loc) · 6.6 KB
/
main_js.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// +build tinygo
package main
import (
"syscall/js"
"github.com/marianogappa/cheesse/api"
)
var a = api.New()
func DefaultGame(this js.Value, p []js.Value) interface{} {
return js.ValueOf(convertOutputGame(a.DefaultGame()))
}
func ParseGame(this js.Value, p []js.Value) interface{} {
og, err := a.ParseGame(convertToInputGame(p[0]))
return js.ValueOf(map[string]interface{}{
"outputGame": convertOutputGame(og),
"error": convertError(err),
})
}
func DoAction(this js.Value, p []js.Value) interface{} {
og, oa, err := a.DoAction(convertToInputGame(p[0]), convertToInputAction(p[1]))
return js.ValueOf(map[string]interface{}{
"outputGame": convertOutputGame(og),
"outputAction": convertOutputAction(oa),
"error": convertError(err),
})
}
func ParseNotation(this js.Value, p []js.Value) interface{} {
og, ogs, err := a.ParseNotation(convertToInputGame(p[0]), p[1].String())
return js.ValueOf(map[string]interface{}{
"outputGame": convertOutputGame(og),
"outputGameSteps": convertOutputGameSteps(ogs),
"error": convertError(err),
})
}
func main() {
js.Global().Set("DefaultGame", js.FuncOf(DefaultGame))
js.Global().Set("ParseGame", js.FuncOf(ParseGame))
js.Global().Set("DoAction", js.FuncOf(DoAction))
js.Global().Set("ParseNotation", js.FuncOf(ParseNotation))
select {}
}
func convertToInputAction(v js.Value) api.InputAction {
return api.InputAction{
FromSquare: jsString(v.Get("fromSquare")),
ToSquare: jsString(v.Get("toSquare")),
PromotionPieceType: jsString(v.Get("promotionPieceType")),
}
}
func convertToInputGame(v js.Value) api.InputGame {
board := v.Get("board")
var (
innerBoard []string
outerBoard = api.Board{}
)
if board != js.Null() && board != js.Undefined() {
innerBoard = make([]string, board.Length())
for i := range innerBoard {
innerBoard[i] = jsString(board.Index(i))
}
outerBoard = api.Board{
Board: innerBoard,
CanWhiteKingsideCastle: jsBool(board.Get("canWhiteKingsideCastle")),
CanWhiteQueensideCastle: jsBool(board.Get("canWhiteQueensideCastle")),
CanBlackKingsideCastle: jsBool(board.Get("canBlackKingsideCastle")),
CanBlackQueensideCastle: jsBool(board.Get("canBlackQueensideCastle")),
HalfMoveClock: jsInt(board.Get("halfMoveClock")),
FullMoveNumber: jsInt(board.Get("fullMoveNumber")),
EnPassantTargetSquare: jsString(board.Get("enPassantTargetSquare")),
Turn: jsString(board.Get("turn")),
}
}
return api.InputGame{
DefaultGame: jsBool(v.Get("defaultGame")),
FENString: jsString(v.Get("fenString")),
Board: outerBoard,
}
}
func jsBool(j js.Value) bool {
if j == js.Undefined() || j == js.Null() {
return false
}
return j.Bool()
}
func jsInt(j js.Value) int {
if j == js.Undefined() || j == js.Null() {
return 0
}
return j.Int()
}
func jsString(j js.Value) string {
if j == js.Undefined() || j == js.Null() {
return ""
}
return j.String()
}
func convertError(err error) string {
if err == nil {
return ""
}
return err.Error()
}
func convertOutputGameSteps(ogs []api.OutputGameStep) []interface{} {
is := make([]interface{}, len(ogs))
for i := range ogs {
is[i] = convertOutputGameStep(ogs[i])
}
return is
}
func convertOutputGameStep(ogs api.OutputGameStep) map[string]interface{} {
return map[string]interface{}{
"game": convertOutputGame(ogs.Game),
"action": convertOutputAction(ogs.Action),
"actionString": ogs.ActionString,
}
}
func convertOutputGame(og api.OutputGame) map[string]interface{} {
return map[string]interface{}{
"fenString": og.FENString,
"board": convertBoard(og.Board),
"actions": convertOutputActions(og.Actions),
"canWhiteCastle": og.CanWhiteCastle,
"canWhiteKingsideCastle": og.CanWhiteKingsideCastle,
"canWhiteQueensideCastle": og.CanWhiteQueensideCastle,
"canBlackCastle": og.CanBlackCastle,
"canBlackKingsideCastle": og.CanBlackKingsideCastle,
"canBlackQueensideCastle": og.CanBlackQueensideCastle,
"halfMoveClock": og.HalfMoveClock,
"fullMoveNumber": og.FullMoveNumber,
"isLastMoveEnPassant": og.IsLastMoveEnPassant,
"enPassantTargetSquare": og.EnPassantTargetSquare,
"moveNumber": og.MoveNumber,
"blackPieces": convertMapStringToString(og.BlackPieces),
"whitePieces": convertMapStringToString(og.WhitePieces),
"blackKing": og.BlackKing,
"whiteKing": og.WhiteKing,
"isCheck": og.IsCheck,
"isCheckmate": og.IsCheckmate,
"isStalemate": og.IsStalemate,
"isDraw": og.IsDraw,
"isGameOver": og.IsGameOver,
"gameOverWinner": og.GameOverWinner,
"inCheckBy": convertStringArr(og.InCheckBy),
}
}
func convertStringArr(ss []string) []interface{} {
is := make([]interface{}, len(ss))
for i := 0; i < len(ss); i++ {
is[i] = ss[i]
}
return is
}
func convertMapStringToString(mss map[string]string) map[string]interface{} {
m := make(map[string]interface{}, len(mss))
for k, v := range mss {
m[k] = v
}
return m
}
func convertOutputActions(as []api.OutputAction) []interface{} {
is := make([]interface{}, len(as))
for i := range as {
is[i] = convertOutputAction(as[i])
}
return is
}
func convertOutputAction(a api.OutputAction) map[string]interface{} {
return map[string]interface{}{
"fromPieceOwner": a.FromPieceOwner,
"fromPieceType": a.FromPieceType,
"fromPieceSquare": a.FromPieceSquare,
"fromSquare": a.FromPieceSquare, // TODO alias so that it can be used as input action
"toSquare": a.ToSquare,
"isCapture": a.IsCapture,
"isResign": a.IsResign,
"isPromotion": a.IsPromotion,
"isEnPassant": a.IsEnPassant,
"isEnPassantCapture": a.IsEnPassantCapture,
"isCastle": a.IsCastle,
"isKingsideCastle": a.IsKingsideCastle,
"isQueensideCastle": a.IsQueensideCastle,
"promotionPieceType": a.PromotionPieceType,
"capturedPieceType": a.CapturedPieceType,
}
}
func convertBoard(b api.Board) map[string]interface{} {
return map[string]interface{}{
"board": convertStringArr(b.Board),
"canWhiteKingsideCastle": b.CanWhiteKingsideCastle,
"canWhiteQueensideCastle": b.CanWhiteQueensideCastle,
"canBlackKingsideCastle": b.CanBlackKingsideCastle,
"canBlackQueensideCastle": b.CanBlackQueensideCastle,
"halfMoveClock": b.HalfMoveClock,
"fullMoveNumber": b.FullMoveNumber,
"enPassantTargetSquare": b.EnPassantTargetSquare,
"turn": b.Turn,
}
}