-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.go
268 lines (226 loc) · 5.32 KB
/
game.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
259
260
261
262
263
264
265
266
267
268
// +build linux
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/creack/pty"
"github.com/hpcloud/tail"
)
func logError(err error, message string) {
if err != nil {
log.Println(message, err)
}
}
func logErrorD(f func() error, message string) {
logError(f(), message)
}
type dataBuffer struct {
queue []string
threshold int
recent []string
fuzzy [][]string
running bool
}
func dwarfFortress(ctx context.Context, buffer *dataBuffer, ch chan<- string) {
addch := make(chan string, 100)
debugch := make(chan struct{})
go watchLog(ctx, addch)
go watchDebug(ctx, debugch)
for {
select {
case <-ctx.Done():
return
default:
}
runGame(ctx, buffer, ch, addch, debugch)
}
}
func runGame(ctx context.Context, buffer *dataBuffer, ch chan<- string, addch <-chan string, debugch <-chan struct{}) {
cmd := exec.CommandContext(ctx, "/df_linux/dfhack", "--exec")
cmd.Env = append(cmd.Env, "TERM=xterm-256color")
cmd.Dir = "/df_linux"
f, err := pty.Start(cmd)
if err != nil {
panic(err)
}
defer logErrorD(f.Close, "Closing Pseudo-TTY:")
exited := make(chan error, 1)
go func() {
exited <- cmd.Wait()
}()
defer logErrorD(clearSaves, "Removing saved worlds:")
signal := func(s os.Signal) {
logError(cmd.Process.Signal(s), "Sending "+s.String()+":")
}
buffer.running = true
first := true
for {
var nextLine string
out := ch
if len(buffer.queue) == 0 {
out = nil
} else {
nextLine = buffer.queue[0]
}
select {
case <-time.After(time.Minute * 30):
if buffer.running {
log.Println("30 minutes without any log output. Assuming DF is stuck. Resetting.")
signal(syscall.SIGTERM)
select {
case <-time.After(time.Minute):
log.Println("Process has not exited. Killing.")
signal(syscall.SIGKILL)
case <-exited:
return
}
}
case out <- nextLine:
buffer.queue = buffer.queue[1:]
checkQueueLength(buffer, signal)
case line := <-addch:
if isDuplicate(buffer, line) {
continue
}
if first || len(buffer.queue) == 0 {
log.Println("First toot queued:", line)
first = false
}
buffer.queue = append(buffer.queue, line)
checkQueueLength(buffer, signal)
case err := <-exited:
log.Println("Dwarf Fortress process exited:", err)
return
case <-debugch:
signal(syscall.SIGKILL)
if err := os.Remove("/df_linux/df-ai-debug.log"); err != nil {
log.Println("Removing debug log:", err)
}
return
}
}
}
func isDuplicate(buffer *dataBuffer, line string) bool {
if minLinesBeforeDuplicate == 0 && fuzzyDuplicateWindow == 0 {
return false
}
firstLine := line[:strings.IndexByte(line, '\n')]
for _, dup := range buffer.recent {
if dup == firstLine {
return true
}
}
firstLineWords := strings.Fields(firstLine)
fuzzy := 0
for _, dup := range buffer.fuzzy {
if isFuzzyDuplicate(dup, firstLineWords) {
fuzzy++
}
}
if fuzzy > maxFuzzyDuplicates {
return true
}
if len(buffer.recent) != 0 {
copy(buffer.recent[:], buffer.recent[1:])
buffer.recent[len(buffer.recent)-1] = firstLine
}
if len(buffer.fuzzy) != 0 {
copy(buffer.fuzzy[:], buffer.fuzzy[1:])
buffer.fuzzy[len(buffer.fuzzy)-1] = firstLineWords
}
return false
}
func isFuzzyDuplicate(dup, words []string) bool {
// Only allow changes, not additions or deletions, to simplify this.
if len(dup) != len(words) {
return false
}
different := 0
for i := range dup {
if dup[i] != words[i] {
different++
}
}
return different <= maxFuzzyDifferentWords
}
func checkQueueLength(buffer *dataBuffer, signal func(os.Signal)) {
if l := len(buffer.queue); l%100 == 0 && (buffer.threshold >= l+100 || buffer.threshold <= l-100) {
buffer.threshold = l
log.Println(l, "lines are buffered.")
}
if len(buffer.queue) < minQueuedLines {
if !buffer.running {
log.Println("Unsuspending Dwarf Fortress")
buffer.running = true
}
signal(syscall.SIGCONT)
} else if len(buffer.queue) > maxQueuedLines {
if buffer.running {
log.Println("Suspending Dwarf Fortress")
buffer.running = false
}
signal(syscall.SIGSTOP)
}
}
func watchDebug(ctx context.Context, ch chan<- struct{}) {
for {
// Wait at least a minute between kills to make sure there's time to clean up.
select {
case <-ctx.Done():
return
case <-time.After(time.Minute):
}
f, err := tail.TailFile("/df_linux/df-ai-debug.log", tail.Config{
Follow: true,
})
if err != nil {
panic(err)
}
select {
case <-ctx.Done():
return
case first := <-f.Lines:
log.Println("df-ai crashed! debug log follows:")
go func() {
f.Kill(f.StopAtEOF())
f.Cleanup()
}()
fmt.Println(first.Text)
for line := range f.Lines {
fmt.Println(line.Text)
}
ch <- struct{}{}
}
}
}
func watchLog(ctx context.Context, ch chan<- string) {
f, err := tail.TailFile("/df_linux/gamelog.txt", tail.Config{
ReOpen: true,
Follow: true,
})
if err != nil {
panic(err)
}
defer f.Cleanup()
go func() {
<-ctx.Done()
f.Kill(ctx.Err())
}()
for line := range f.Lines {
if line.Err != nil {
log.Println(line.Err)
continue
}
text := mapCP437(strings.TrimSpace(line.Text))
// Chatter messages are formatted as "Urist McName, Occupation: It was inevitable."
if i, j := strings.Index(text, ", "), strings.Index(text, ": "); i > 0 && j > i {
ch <- text[j+2:] + "\n\n\u2014 " + text[:j]
}
}
}