-
Notifications
You must be signed in to change notification settings - Fork 131
/
main.go
258 lines (221 loc) · 6.94 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//go:build wasm
// +build wasm
package main
import (
"context"
"log"
"runtime/debug"
"syscall/js"
"github.com/wowsims/wotlk/sim"
"github.com/wowsims/wotlk/sim/core"
proto "github.com/wowsims/wotlk/sim/core/proto"
protojson "google.golang.org/protobuf/encoding/protojson"
googleProto "google.golang.org/protobuf/proto"
)
func init() {
sim.RegisterAll()
}
func main() {
c := make(chan struct{}, 0)
js.Global().Set("computeStats", js.FuncOf(computeStats))
js.Global().Set("computeStatsJson", js.FuncOf(computeStatsJson))
js.Global().Set("raidSim", js.FuncOf(raidSim))
js.Global().Set("raidSimJson", js.FuncOf(raidSimJson))
js.Global().Set("raidSimAsync", js.FuncOf(raidSimAsync))
js.Global().Set("statWeights", js.FuncOf(statWeights))
js.Global().Set("statWeightsAsync", js.FuncOf(statWeightsAsync))
js.Global().Set("bulkSimAsync", js.FuncOf(bulkSimAsync))
js.Global().Call("wasmready")
<-c
}
func computeStats(this js.Value, args []js.Value) (response interface{}) {
defer func() {
if err := recover(); err != nil {
errStr := ""
switch errt := err.(type) {
case string:
errStr = errt
case error:
errStr = errt.Error()
}
errStr += "\nStack Trace:\n" + string(debug.Stack())
result := &proto.ComputeStatsResult{
ErrorResult: errStr,
}
outbytes, err := googleProto.Marshal(result)
if err != nil {
log.Printf("[ERROR] Failed to marshal error (%s) result: %s", errStr, err.Error())
return
}
outArray := js.Global().Get("Uint8Array").New(len(outbytes))
js.CopyBytesToJS(outArray, outbytes)
response = outArray
}
}()
csr := &proto.ComputeStatsRequest{}
if err := googleProto.Unmarshal(getArgsBinary(args[0]), csr); err != nil {
log.Printf("Failed to parse request: %s", err)
return nil
}
result := core.ComputeStats(csr)
outbytes, err := googleProto.Marshal(result)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
return nil
}
outArray := js.Global().Get("Uint8Array").New(len(outbytes))
js.CopyBytesToJS(outArray, outbytes)
response = outArray
return response
}
func computeStatsJson(this js.Value, args []js.Value) (response interface{}) {
defer func() {
if err := recover(); err != nil {
errStr := ""
switch errt := err.(type) {
case string:
errStr = errt
case error:
errStr = errt.Error()
}
errStr += "\nStack Trace:\n" + string(debug.Stack())
result := &proto.ComputeStatsResult{
ErrorResult: errStr,
}
output, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(result)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
}
response = js.ValueOf(string(output))
}
}()
csr := &proto.ComputeStatsRequest{}
log.Printf("Compute stats request: %s", getArgsJson(args[0]))
if err := protojson.Unmarshal(getArgsJson(args[0]), csr); err != nil {
log.Printf("Failed to parse request: %s", err)
return nil
}
result := core.ComputeStats(csr)
output, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(result)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
return nil
}
response = js.ValueOf(string(output))
return response
}
func raidSim(this js.Value, args []js.Value) interface{} {
rsr := &proto.RaidSimRequest{}
if err := googleProto.Unmarshal(getArgsBinary(args[0]), rsr); err != nil {
log.Printf("Failed to parse request: %s", err)
return nil
}
result := core.RunRaidSim(rsr)
outbytes, err := googleProto.Marshal(result)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
return nil
}
outArray := js.Global().Get("Uint8Array").New(len(outbytes))
js.CopyBytesToJS(outArray, outbytes)
return outArray
}
func raidSimJson(this js.Value, args []js.Value) interface{} {
rsr := &proto.RaidSimRequest{}
if err := protojson.Unmarshal(getArgsJson(args[0]), rsr); err != nil {
log.Printf("Failed to parse request: %s", err)
return nil
}
result := core.RunRaidSim(rsr)
output, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(result)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
return nil
}
response := js.ValueOf(string(output))
return response
}
func raidSimAsync(this js.Value, args []js.Value) interface{} {
rsr := &proto.RaidSimRequest{}
if err := googleProto.Unmarshal(getArgsBinary(args[0]), rsr); err != nil {
log.Printf("Failed to parse request: %s", err)
return nil
}
reporter := make(chan *proto.ProgressMetrics, 100)
go core.RunRaidSimAsync(rsr, reporter)
return processAsyncProgress(args[1], reporter)
}
func statWeights(this js.Value, args []js.Value) interface{} {
swr := &proto.StatWeightsRequest{}
if err := googleProto.Unmarshal(getArgsBinary(args[0]), swr); err != nil {
log.Printf("Failed to parse request: %s", err)
return nil
}
result := core.StatWeights(swr)
outbytes, err := googleProto.Marshal(result)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
return nil
}
outArray := js.Global().Get("Uint8Array").New(len(outbytes))
js.CopyBytesToJS(outArray, outbytes)
return outArray
}
func statWeightsAsync(this js.Value, args []js.Value) interface{} {
rsr := &proto.StatWeightsRequest{}
if err := googleProto.Unmarshal(getArgsBinary(args[0]), rsr); err != nil {
log.Printf("Failed to parse request: %s", err)
return nil
}
reporter := make(chan *proto.ProgressMetrics, 100)
core.StatWeightsAsync(rsr, reporter)
result := processAsyncProgress(args[1], reporter)
return result
}
func bulkSimAsync(this js.Value, args []js.Value) interface{} {
rsr := &proto.BulkSimRequest{}
if err := googleProto.Unmarshal(getArgsBinary(args[0]), rsr); err != nil {
log.Printf("Failed to parse request: %s", err)
return nil
}
reporter := make(chan *proto.ProgressMetrics, 100)
// for now just use context.Background() until we can figure out the best way to handle
// allowing front end to cancel.
core.RunBulkSimAsync(context.Background(), rsr, reporter)
result := processAsyncProgress(args[1], reporter)
return result
}
// Assumes args[0] is a Uint8Array
func getArgsBinary(value js.Value) []byte {
data := make([]byte, value.Get("length").Int())
js.CopyBytesToGo(data, value)
return data
}
func getArgsJson(value js.Value) []byte {
str := value.String()
return []byte(str)
}
func processAsyncProgress(progFunc js.Value, reporter chan *proto.ProgressMetrics) js.Value {
reader:
for {
// TODO: cleanup so we dont collect these
select {
case progMetric, ok := <-reporter:
if !ok {
break reader
}
outbytes, err := googleProto.Marshal(progMetric)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
return js.Undefined()
}
outArray := js.Global().Get("Uint8Array").New(len(outbytes))
js.CopyBytesToJS(outArray, outbytes)
progFunc.Invoke(outArray)
if progMetric.FinalWeightResult != nil || progMetric.FinalRaidResult != nil || progMetric.FinalBulkResult != nil {
return outArray
}
}
}
return js.Undefined()
}