4
4
"flag"
5
5
"fmt"
6
6
"io"
7
- "io/ioutil"
8
7
"os"
9
8
"strings"
10
9
"time"
@@ -17,8 +16,6 @@ import (
17
16
)
18
17
19
18
var (
20
- msgDump = ioutil .Discard
21
- snailDebug = ioutil .Discard
22
19
logOut = os .Stderr
23
20
predictionOut = os .Stdout
24
21
csvOut io.Writer // Opened in main
@@ -220,18 +217,12 @@ func updateState(msg kqio.Message, game *kq.GameState) bool {
220
217
data := msg .Val .(parser.GetOnSnailMessage )
221
218
game .Players [data .Rider .Index ()].OnSnail = 1
222
219
pos := data .Pos .X - game .Snails [0 ].MaxPos
223
- if pos != game .Snails [0 ].Pos {
224
- fmt .Fprintf (snailDebug , "%v: The snail moved from %v to %v without a rider\n " ,
225
- msg .Time , game .Snails [0 ].Pos , pos )
226
- }
227
220
// running drone speed 250 px/s. may be 1925ish pixels to wrap
228
221
// robot 200 px/s
229
222
// eat takes 3.5s, arantius vid says 3.67
230
223
if game .Players [data .Rider .Index ()].HasSpeed {
231
- fmt .Fprintln (snailDebug , "rider has speed" )
232
224
snailSpeed = 28.209890875 // 27
233
225
} else {
234
- fmt .Fprintln (snailDebug , "rider is slow" )
235
226
snailSpeed = 20.896215463 // 20
236
227
}
237
228
if data .Rider .Team () == BlueSide {
@@ -245,13 +236,6 @@ func updateState(msg kqio.Message, game *kq.GameState) bool {
245
236
}
246
237
data := msg .Val .(parser.GetOffSnailMessage )
247
238
game .Players [data .Rider .Index ()].OnSnail = 0
248
- fmt .Fprintf (snailDebug , "Off: The snail moved by %v pixels in %v, %v px/s\n " ,
249
- data .Pos .X - game .Snails [0 ].MaxPos - game .Snails [0 ].Pos ,
250
- msg .Time .Sub (snailTime ),
251
- float64 (data .Pos .X - game .Snails [0 ].MaxPos - game .Snails [0 ].Pos )/
252
- float64 (msg .Time .Sub (snailTime )/ time .Millisecond )* 1000 )
253
- fmt .Fprintf (snailDebug , "Estimated snail position is %v, actual is %v, diff %v\n " ,
254
- snailEstimate (msg .Time , game ), data .Pos .X - game .Snails [0 ].MaxPos , data .Pos .X - game .Snails [0 ].MaxPos - snailEstimate (msg .Time , game ))
255
239
game .Snails [0 ].Pos = data .Pos .X - game .Snails [0 ].MaxPos
256
240
snailTime = msg .Time
257
241
snailSpeed = 0
@@ -260,13 +244,6 @@ func updateState(msg kqio.Message, game *kq.GameState) bool {
260
244
return false
261
245
}
262
246
data := msg .Val .(parser.SnailStartEatMessage )
263
- fmt .Fprintf (snailDebug , "Eat: The snail moved by %v pixels in %v, %v px/s\n " ,
264
- data .Pos .X - game .Snails [0 ].MaxPos - game .Snails [0 ].Pos ,
265
- msg .Time .Sub (snailTime ),
266
- float64 (data .Pos .X - game .Snails [0 ].MaxPos - game .Snails [0 ].Pos )/
267
- float64 (msg .Time .Sub (snailTime )/ time .Millisecond )* 1000 )
268
- fmt .Fprintf (snailDebug , "Estimated snail position is %v, actual is %v, diff %v\n " ,
269
- snailEstimate (msg .Time , game ), data .Pos .X - game .Snails [0 ].MaxPos , data .Pos .X - game .Snails [0 ].MaxPos - snailEstimate (msg .Time , game ))
270
247
game .Snails [0 ].Pos = data .Pos .X - game .Snails [0 ].MaxPos
271
248
snailTime = msg .Time .Add (3500 * time .Millisecond )
272
249
case "snailEscape" :
@@ -281,14 +258,10 @@ func updateState(msg kqio.Message, game *kq.GameState) bool {
281
258
} else {
282
259
offset = 50
283
260
}
261
+ // In theory, the snail shouldn't move while someone is sacrificing.
262
+ // In practice it can, either because it got pushed with a berry or
263
+ // because the sacrifice carried momentum into the snail.
284
264
pos := data .Pos .X - game .Snails [0 ].MaxPos + offset
285
- if pos != game .Snails [0 ].Pos {
286
- // In theory, the snail shouldn't move while someone is sacrificing.
287
- // In practice it can, either because it got pushed with a berry or
288
- // because the sacrifice carried momentum into the snail.
289
- fmt .Fprintf (snailDebug , "%v: The snail moved from %v to %v during a sacrifice\n " ,
290
- msg .Time , game .Snails [0 ].Pos , pos )
291
- }
292
265
game .Snails [0 ].Pos = pos
293
266
snailTime = msg .Time
294
267
case "berryDeposit" :
@@ -757,14 +730,22 @@ func updateStats(msg *kqio.Message, state *kq.GameState) {
757
730
758
731
var configPath = flag .String ("config" , "config.json" , "the path to the config file; it is not an error if this file does not exist" )
759
732
733
+ type mainEventKey int
734
+
735
+ const (
736
+ GameStartTimeKey mainEventKey = iota
737
+ StatsUpdateKey
738
+ )
739
+
760
740
func main () {
761
741
flag .Parse ()
762
742
config , e := ReadConfig (* configPath )
763
743
if e != nil && ! os .IsNotExist (e ) {
764
744
panic (fmt .Sprintf ("Failed to load config %v" , e ))
765
745
}
766
- broadcast := make (chan interface {})
767
- go startWebServer (fmt .Sprintf (":%d" , config .ServerPort ), broadcast )
746
+ eventStream := NewEventStream ()
747
+ defer eventStream .Close ()
748
+ go startWebServer (fmt .Sprintf (":%d" , config .ServerPort ), eventStream )
768
749
<- time .After (5 * time .Second )
769
750
webStartTime , _ := time .Parse (time .RFC3339Nano , "2018-10-20T18:39:49.376-05:00" )
770
751
@@ -799,7 +780,7 @@ func main() {
799
780
fmt .Fprintln (csvOut , CsvHeader )
800
781
ticker := time .NewTicker (100 * time .Millisecond )
801
782
defer ticker .Stop ()
802
- famine := NewFamineTracker (broadcast )
783
+ famine := NewFamineTracker ()
803
784
for {
804
785
var isTick bool
805
786
select {
@@ -811,18 +792,18 @@ func main() {
811
792
812
793
e = reader .ReadMessage (& msg )
813
794
if e != nil {
814
- fmt .Fprintln (msgDump , "read error" , e )
815
795
if e == io .EOF {
816
796
break
817
797
}
818
798
continue
819
799
}
820
- fmt .Fprintln (msgDump , msg )
800
+
801
+ event := EventWithMessage (& msg , isTick )
821
802
updateStats (& msg , state )
822
803
if (updateState (msg , state ) || isTick ) && ! state .Start .IsZero () && (state .InGame () || msg .Type == "victory" ) {
823
804
fmt .Fprintln (csvOut , & CsvPrinter {state .Map , msg .Time .Sub (state .Start ), msg .Time , * state })
824
805
if msg .Type == "gamestart" {
825
- broadcast <- msg .Time
806
+ event . Data [ GameStartTimeKey ] = msg .Time
826
807
} else if ! msg .Time .Before (webStartTime ) {
827
808
var dp dataPoint
828
809
dp .when = msg .Time
@@ -844,7 +825,7 @@ func main() {
844
825
dp .winner = msg .Val .(parser.GameResultMessage ).Winner .String ()
845
826
dp .winType = msg .Val .(parser.GameResultMessage ).EndCondition .String ()
846
827
}
847
- broadcast <- dp
828
+ event . Data [ StatsUpdateKey ] = dp
848
829
}
849
830
if score != nil {
850
831
s := score (state , msg .Time )
@@ -861,7 +842,9 @@ func main() {
861
842
}
862
843
863
844
if state .InGame () {
864
- famine .Update (msg . Time , state , isTick )
845
+ famine .Update (event , state )
865
846
}
847
+
848
+ eventStream .AddEvent (event )
866
849
}
867
850
}
0 commit comments