-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (185 loc) · 5.16 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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/kssilveira/idle-game-engine/game"
"github.com/kssilveira/idle-game-engine/kittens"
"github.com/kssilveira/idle-game-engine/kittens/solve"
"github.com/kssilveira/idle-game-engine/textui"
"github.com/kssilveira/idle-game-engine/ui"
)
var (
auto = flag.Bool("auto", false, "automatically trigger all actions")
autoType = flag.String("auto_type", "smart", "solve 'smart', 'random' or 'fixed'")
endAfterAuto = flag.Bool("end_after_auto", false, "end after auto actions")
silentOutput = flag.Bool("silent_output", false, "silent output")
autoSleepMS = flag.Int("auto_sleep_ms", 1000, "sleep between auto actions")
maxSkipSeconds = flag.Int("max_skip_seconds", 0, "max skip duration")
maxCreateIter = flag.Int("max_create_iter", 0, "max create iterations")
resourceMap = flag.String("resource_map", "", "map of resource quantities, e.g. 'catnip:1,Catnip Field:2,wood:3")
seasons = flag.String("seasons", strings.Join(kittens.AllSeasons, ","), "list of seasons")
)
func main() {
flag.Parse()
if err := all(); err != nil {
log.Fatal(err)
}
}
func all() error {
g, err := newGame()
if err != nil {
return err
}
input := make(chan string)
output := make(chan *ui.Data)
go g.Run(input, output)
var lastData ui.Data
waitingForLastData := make(chan bool)
refreshedLastData := make(chan bool)
go func() {
if err := handleInput(g, input, &lastData, waitingForLastData, refreshedLastData); err != nil {
log.Fatal(err)
}
}()
var lastString string
waitingForLastString := make(chan bool)
refreshedLastString := make(chan bool)
go handleOutput(output, &lastData, waitingForLastData, refreshedLastData, &lastString, waitingForLastString, refreshedLastString)
http.HandleFunc("/", handleHTTP(&lastString, input, *auto, *autoSleepMS, waitingForLastString, refreshedLastString))
return http.ListenAndServe(":8080", nil)
}
func newGame() (*game.Game, error) {
g := kittens.NewGame(kittens.Config{
Config: game.Config{
NowFn: func() time.Time { return time.Now() },
MaxSkipSeconds: time.Second * time.Duration(*maxSkipSeconds),
MaxCreateIter: *maxCreateIter,
},
Seasons: strings.Split(*seasons, ","),
})
if err := updateResources(g, *resourceMap); err != nil {
return nil, err
}
if err := g.Validate(); err != nil {
return nil, err
}
return g, nil
}
func updateResources(g *game.Game, resourceMap string) error {
if resourceMap == "" {
return nil
}
for _, one := range strings.Split(strings.TrimSpace(resourceMap), ",") {
words := strings.Split(strings.TrimSpace(one), ":")
if len(words) != 2 {
return fmt.Errorf("--resource_map has invalid value '%s'", one)
}
if !g.HasResource(words[0]) {
return fmt.Errorf("--resource_map has invalid resource '%s'", words[0])
}
value, err := strconv.ParseFloat(words[1], 64)
if err != nil {
return fmt.Errorf("--resource_map has invalid value '%s' err %v", words[0], err)
}
r := g.GetResource(words[0])
r.Count = value
if r.Cap != -1 && r.Cap < value {
r.Cap = value
}
}
return nil
}
func handleInput(g *game.Game, input game.Input, lastData *ui.Data, waiting chan bool, refreshed chan bool) error {
if *auto {
if err := solve.Solve(solve.Config{
Game: g,
Input: input,
LastData: lastData,
Waiting: waiting,
Refreshed: refreshed,
Type: *autoType,
SleepMS: *autoSleepMS,
PermFn: rand.Perm,
}); err != nil {
return err
}
if *endAfterAuto {
os.Exit(0)
}
}
for {
var got string
fmt.Scanln(&got)
input <- got
}
return nil
}
func handleOutput(output game.Output, lastData *ui.Data, waitingForLastData chan bool, refreshedLastData chan bool, lastString *string, waitingForLastString chan bool, refreshedLastString chan bool) {
textConfig := textui.Config{
Logger: log.New(os.Stdout, "" /* prefix */, 0 /* flags */),
Separator: "\033[H\033[2J",
RedColor: "\033[1;91m",
CloseColor: "\033[0m",
}
var buf bytes.Buffer
htmlConfig := textui.Config{
Logger: log.New(&buf, "" /* prefix */, 0 /* flags */),
IsHTML: true,
RedColor: "<span style='color:red;'>",
CloseColor: "</span>",
}
for data := range output {
if !*silentOutput {
textui.Show(textConfig, data)
textui.Show(htmlConfig, data)
}
*lastData = *data
*lastString = buf.String()
buf.Reset()
select {
case <-waitingForLastData:
refreshedLastData <- true
default:
}
select {
case <-waitingForLastData:
refreshedLastString <- true
default:
}
}
}
func handleHTTP(last *string, input game.Input, auto bool, autoSleepMS int, waiting chan bool, refreshed chan bool) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if !auto {
path := strings.TrimPrefix(r.URL.Path, "/")
if path != "" && path != "favicon.ico" {
input <- path
waiting <- true
<-refreshed
}
}
fmt.Fprintf(w, `
<html>
<head>
<meta http-equiv='refresh' content='%f; url=/'/>
<style>
body {
font-family: monospace;
}
</style>
</head>
<body>
%s
</body>
</html>
`, float64(autoSleepMS)/1000, strings.Replace(*last, "\n", "<br>\n", -1))
}
}