/
basic_sim.go
70 lines (60 loc) · 1.77 KB
/
basic_sim.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
package cmd
import (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/wowsims/cata/sim/core"
"github.com/wowsims/cata/sim/core/proto"
"google.golang.org/protobuf/encoding/protojson"
)
var simCmd = &cobra.Command{
Use: "sim",
Short: "simulate items & settings",
Run: simMain,
}
func init() {
simCmd.Flags().StringVar(&infile, "infile", "input.json", "location of input file (RaidSimRequest in protojson format)")
simCmd.Flags().StringVar(&outfile, "outfile", "", "location of output file, defaults to stdout")
simCmd.Flags().BoolVar(&verbose, "verbose", false, "print information during runtime")
simCmd.MarkFlagRequired("infile")
}
func simMain(cmd *cobra.Command, args []string) {
data, err := os.ReadFile(infile)
if err != nil {
log.Fatalf("failed to load input json file %q: %v", infile, err)
}
input := &proto.RaidSimRequest{}
err = protojson.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(data, input)
if err != nil {
log.Fatalf("failed to load input json file: %s", err)
}
var output []byte
reporter := make(chan *proto.ProgressMetrics, 10)
core.RunRaidSimAsync(input, reporter)
var finalResult *proto.RaidSimResult
for v := range reporter {
if v.FinalRaidResult != nil {
finalResult = v.FinalRaidResult
break
}
if verbose {
fmt.Printf("Sim Progress: %d / %d\n", v.CompletedIterations, v.TotalIterations)
}
}
output, err = protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(finalResult)
if err != nil {
log.Fatalf("failed to marshal final results: %s", err)
}
if outfile == "" {
fmt.Print(string(output))
} else {
err = os.WriteFile(outfile, output, 0666)
if err != nil {
log.Fatalf("failed to write output file:: %s", err)
}
if verbose {
fmt.Printf("Wrote output file: `%s` successfully.\n", outfile)
}
}
}