forked from SOMAS2020/SOMAS2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_js.go
175 lines (153 loc) · 3.81 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
// +build js,wasm
package main
import (
"bytes"
"encoding/json"
"flag"
"io"
"log"
"reflect"
"runtime"
"strings"
"syscall/js"
"time"
"github.com/SOMAS2020/SOMAS2020/internal/common/config"
"github.com/SOMAS2020/SOMAS2020/internal/server"
"github.com/SOMAS2020/SOMAS2020/pkg/logger"
"github.com/pkg/errors"
)
type flagInfo struct {
Name string
Usage string
DefValue string
Type string
}
func main() {
js.Global().Set(
"RunGame", js.FuncOf(RunGame),
)
js.Global().Set(
"GetFlagsFormats", js.FuncOf(GetFlagsFormats),
)
select {}
}
// RunGame runs the game.
// args[0] are optional arguments to set flag values
// The format is `arg1=value,arg2=value,...`
func RunGame(this js.Value, args []js.Value) interface{} {
timeStart := time.Now()
gameConfig, err := getConfigFromArgs(args)
if err != nil {
return js.ValueOf(map[string]interface{}{
"error": convertError(err),
})
}
// log into a buffer
logBuf := bytes.Buffer{}
log.SetOutput(logger.NewLogWriter([]io.Writer{&logBuf}))
s := server.NewSOMASServer(gameConfig)
var o output
var outputJSON string
gameStates, err := s.EntryPoint()
if err != nil {
return js.ValueOf(map[string]interface{}{
"error": convertError(err),
})
}
timeEnd := time.Now()
o = output{
GameStates: gameStates,
Config: gameConfig,
// no git info
RunInfo: runInfo{
TimeStart: timeStart,
TimeEnd: timeEnd,
DurationSeconds: timeEnd.Sub(timeStart).Seconds(),
Version: runtime.Version(),
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
},
}
outputJSON, err = getOutputJSON(o)
return js.ValueOf(map[string]interface{}{
"output": outputJSON,
"logs": logBuf.String(),
"error": convertError(err),
})
}
// GetFlagsFormats returns the format of the flags.
// See flagInfo type for more information.
func GetFlagsFormats(this js.Value, args []js.Value) interface{} {
flagInfos := []flagInfo{}
flagVisitor := flagVisitOuter(&flagInfos)
flag.VisitAll(flagVisitor)
output, err := getFlagsFormatsJSON(flagInfos)
if err != nil {
return js.ValueOf(map[string]interface{}{
"error": convertError(err),
})
}
return js.ValueOf(map[string]interface{}{
"output": output,
"error": convertError(err),
})
}
func getConfigFromArgs(jsArgs []js.Value) (config.Config, error) {
args := strings.Split(jsArgs[0].String(), ",")
if len(args) == 1 && args[0] == "" {
// handle empty input
args = []string{}
}
flag.Parse()
for _, arg := range args {
nameValuePair := strings.Split(arg, "=")
if len(nameValuePair) != 2 {
return config.Config{}, errors.Errorf("Invalid arg: %v", arg)
}
name := nameValuePair[0]
val := nameValuePair[1]
err := flag.Set(name, val)
if err != nil {
return config.Config{}, errors.Errorf("Cannot set flag name '%v' with value '%v': %v", name, val, err)
}
}
return parseConfig(), nil
}
// getFlagBaseType processes the String of the reflect.Type of a flag to
// only return the bare type.
// e.g. *flag.float64Value -> float64
func getFlagBaseType(f *flag.Flag) string {
s := reflect.TypeOf(f.Value).String()
return s[6 : len(s)-5]
}
func flagVisitOuter(fis *[]flagInfo) func(*flag.Flag) {
return func(f *flag.Flag) {
*fis = append(*fis, flagInfo{
Name: f.Name,
Usage: f.Usage,
DefValue: f.DefValue,
Type: getFlagBaseType(f),
})
return
}
}
func getFlagsFormatsJSON(fis []flagInfo) (string, error) {
jsonBuf, err := json.Marshal(fis)
if err != nil {
return "", errors.Errorf("Failed to Marshal flags formats: %v", err)
}
return string(jsonBuf), nil
}
func getOutputJSON(o output) (string, error) {
jsonBuf, err := json.Marshal(o)
if err != nil {
return "", errors.Errorf("Failed to Marshal output: %v", err)
}
return string(jsonBuf), nil
}
func convertError(err error) string {
if err == nil {
return ""
}
return err.Error()
}