Skip to content

Commit 09aea9c

Browse files
authored
DEV 1666: Fix /end requests and clean up logging (#109)
* ensure /end request is always called, and refactor win/draw logic * clean up logging and error handling during initialization * automatically generate friendly snake names * title-case snake names * print out list of alive snake names instead of count * log snake names, IDs, and URLs at startup * print out state for turn zero
1 parent 006f394 commit 09aea9c

File tree

10 files changed

+315
-149
lines changed

10 files changed

+315
-149
lines changed

board/server.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package board
33
import (
44
"context"
55
"encoding/json"
6-
"log"
76
"net"
87
"net/http"
98

109
"github.com/gorilla/websocket"
1110
"github.com/rs/cors"
11+
log "github.com/spf13/jwalterweatherman"
1212
)
1313

1414
// A minimal server capable of handling the requests from a single browser client running the board viewer.
@@ -51,7 +51,7 @@ func (server *BoardServer) handleGame(w http.ResponseWriter, r *http.Request) {
5151
Game Game
5252
}{server.game})
5353
if err != nil {
54-
log.Printf("Unable to serialize game: %v", err)
54+
log.ERROR.Printf("Unable to serialize game: %v", err)
5555
w.WriteHeader(http.StatusInternalServerError)
5656
return
5757
}
@@ -61,37 +61,37 @@ func (server *BoardServer) handleGame(w http.ResponseWriter, r *http.Request) {
6161
func (server *BoardServer) handleWebsocket(w http.ResponseWriter, r *http.Request) {
6262
ws, err := upgrader.Upgrade(w, r, nil)
6363
if err != nil {
64-
log.Printf("Unable to upgrade connection: %v", err)
64+
log.ERROR.Printf("Unable to upgrade connection: %v", err)
6565
return
6666
}
6767

6868
defer func() {
6969
err = ws.Close()
7070
if err != nil {
71-
log.Printf("Unable to close websocket stream")
71+
log.ERROR.Printf("Unable to close websocket stream")
7272
}
7373
}()
7474

7575
for event := range server.events {
7676
jsonStr, err := json.Marshal(event)
7777
if err != nil {
78-
log.Printf("Unable to serialize event for websocket: %v", err)
78+
log.ERROR.Printf("Unable to serialize event for websocket: %v", err)
7979
}
8080

8181
err = ws.WriteMessage(websocket.TextMessage, jsonStr)
8282
if err != nil {
83-
log.Printf("Unable to write to websocket: %v", err)
83+
log.ERROR.Printf("Unable to write to websocket: %v", err)
8484
break
8585
}
8686
}
8787

88-
log.Printf("Finished writing all game events, signalling game server to stop")
88+
log.DEBUG.Printf("Finished writing all game events, signalling game server to stop")
8989
close(server.done)
9090

91-
log.Printf("Sending websocket close message")
91+
log.DEBUG.Printf("Sending websocket close message")
9292
err = ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
9393
if err != nil {
94-
log.Printf("Problem closing websocket: %v", err)
94+
log.ERROR.Printf("Problem closing websocket: %v", err)
9595
}
9696
}
9797

@@ -103,7 +103,7 @@ func (server *BoardServer) Listen() (string, error) {
103103
go func() {
104104
err = server.httpServer.Serve(listener)
105105
if err != http.ErrServerClosed {
106-
log.Printf("Error in board HTTP server: %v", err)
106+
log.ERROR.Printf("Error in board HTTP server: %v", err)
107107
}
108108
}()
109109

@@ -115,13 +115,13 @@ func (server *BoardServer) Listen() (string, error) {
115115
func (server *BoardServer) Shutdown() {
116116
close(server.events)
117117

118-
log.Printf("Waiting for websocket clients to finish")
118+
log.DEBUG.Printf("Waiting for websocket clients to finish")
119119
<-server.done
120-
log.Printf("Server is done, exiting")
120+
log.DEBUG.Printf("Server is done, exiting")
121121

122122
err := server.httpServer.Shutdown(context.Background())
123123
if err != nil {
124-
log.Printf("Error shutting down HTTP server: %v", err)
124+
log.ERROR.Printf("Error shutting down HTTP server: %v", err)
125125
}
126126
}
127127

cli/commands/info.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package commands
22

33
import (
44
"fmt"
5-
"log"
65

7-
"github.com/BattlesnakeOfficial/rules/maps"
86
"github.com/spf13/cobra"
7+
log "github.com/spf13/jwalterweatherman"
8+
9+
"github.com/BattlesnakeOfficial/rules/maps"
910
)
1011

1112
type mapInfo struct {
@@ -35,7 +36,7 @@ func NewMapInfoCommand() *cobra.Command {
3536
if len(args) < 1 {
3637
err := cmd.Help()
3738
if err != nil {
38-
log.Fatal(err)
39+
log.ERROR.Fatal(err)
3940
}
4041
return
4142
}
@@ -59,7 +60,7 @@ func NewMapInfoCommand() *cobra.Command {
5960
func (m *mapInfo) display(id string) {
6061
gameMap, err := maps.GetMap(id)
6162
if err != nil {
62-
log.Fatalf("Failed to load game map %#v: %v", id, err)
63+
log.ERROR.Fatalf("Failed to load game map %v: %v", id, err)
6364
}
6465
meta := gameMap.Meta()
6566
fmt.Println("Name:", meta.Name)

cli/commands/map.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package commands
22

33
import (
4-
"log"
5-
64
"github.com/spf13/cobra"
5+
log "github.com/spf13/jwalterweatherman"
76
)
87

98
func NewMapCommand() *cobra.Command {
@@ -15,7 +14,7 @@ func NewMapCommand() *cobra.Command {
1514
Run: func(cmd *cobra.Command, args []string) {
1615
err := cmd.Help()
1716
if err != nil {
18-
log.Fatal(err)
17+
log.ERROR.Fatal(err)
1918
}
2019
},
2120
}

cli/commands/names.go

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// This file uses material from the Wikipedia article <a href="https://en.wikipedia.org/wiki/List_of_snakes_by_common_name">"List of snakes by common name"</a>, which is released under the <a href="https://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share-Alike License 3.0</a>.
2+
package commands
3+
4+
import (
5+
"math/rand"
6+
"time"
7+
8+
"github.com/google/uuid"
9+
)
10+
11+
var snakeNames = []string{
12+
"Adder",
13+
"Aesculapian Snake",
14+
"Anaconda",
15+
"Arafura File Snake",
16+
"Asp",
17+
"African Beaked Snake",
18+
"Ball Python",
19+
"Bird Snake",
20+
"Black-headed Snake",
21+
"Mexican Black Kingsnake",
22+
"Black Rat Snake",
23+
"Black Snake",
24+
"Blind Snake",
25+
"Boa",
26+
"Boiga",
27+
"Boomslang",
28+
"Brown Snake",
29+
"Bull Snake",
30+
"Bushmaster",
31+
"Dwarf Beaked Snake",
32+
"Rufous Beaked Snake",
33+
"Canebrake",
34+
"Cantil",
35+
"Cascabel",
36+
"Cat-eyed Snake",
37+
"Cat Snake",
38+
"Chicken Snake",
39+
"Coachwhip Snake",
40+
"Cobra",
41+
"Collett's Snake",
42+
"Congo Snake",
43+
"Copperhead",
44+
"Coral Snake",
45+
"Corn Snake",
46+
"Cottonmouth",
47+
"Crowned Snake",
48+
"Cuban Wood Snake",
49+
"Egg-eater",
50+
"Eyelash Viper",
51+
"Fer-de-lance",
52+
"Fierce Snake",
53+
"Fishing Snake",
54+
"Flying Snake",
55+
"Fox Snake",
56+
"Forest Flame Snake",
57+
"Garter Snake",
58+
"Glossy Snake",
59+
"Gopher Snake",
60+
"Grass Snake",
61+
"Green Snake",
62+
"Ground Snake",
63+
"Habu",
64+
"Harlequin Snake",
65+
"Herald Snake",
66+
"Hognose Snake",
67+
"Hoop Snake",
68+
"Hundred Pacer",
69+
"Ikaheka Snake",
70+
"Indigo Snake",
71+
"Jamaican Tree Snake",
72+
"Jararacussu",
73+
"Keelback",
74+
"King Brown",
75+
"King Cobra",
76+
"King Snake",
77+
"Krait",
78+
"Lancehead",
79+
"Lora",
80+
"Lyre Snake",
81+
"Machete Savane",
82+
"Mamba",
83+
"Mamushi",
84+
"Mangrove Snake",
85+
"Milk Snake",
86+
"Moccasin Snake",
87+
"Montpellier Snake",
88+
"Mud Snake",
89+
"Mussurana",
90+
"Night Snake",
91+
"Nose-horned Viper",
92+
"Parrot Snake",
93+
"Patchnose Snake",
94+
"Pine Snake",
95+
"Pipe Snake",
96+
"Python",
97+
"Queen Snake",
98+
"Racer",
99+
"Raddysnake",
100+
"Rat Snake",
101+
"Rattlesnake",
102+
"Ribbon Snake",
103+
"Rinkhals",
104+
"River Jack",
105+
"Sea Snake",
106+
"Shield-tailed Snake",
107+
"Sidewinder",
108+
"Small-eyed Snake",
109+
"Stiletto Snake",
110+
"Striped Snake",
111+
"Sunbeam Snake",
112+
"Taipan",
113+
"Tentacled Snake",
114+
"Tic Polonga",
115+
"Tiger Snake",
116+
"Tigre Snake",
117+
"Tree Snake",
118+
"Trinket Snake",
119+
"Twig Snake",
120+
"Twin Headed King Snake",
121+
"Titanoboa",
122+
"Urutu",
123+
"Vine Snake",
124+
"Viper",
125+
"Wart Snake",
126+
"Water Moccasin",
127+
"Water Snake",
128+
"Whip Snake",
129+
"Wolf Snake",
130+
"Worm Snake",
131+
"Wutu",
132+
"Yarara",
133+
"Zebra Snake",
134+
}
135+
136+
func init() {
137+
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
138+
randGen.Shuffle(len(snakeNames), func(i, j int) {
139+
snakeNames[i], snakeNames[j] = snakeNames[j], snakeNames[i]
140+
})
141+
}
142+
143+
// Generate a random unique snake name, or return a UUID if there are no more names available.
144+
func GenerateSnakeName() string {
145+
if len(snakeNames) == 0 {
146+
return uuid.New().String()
147+
}
148+
149+
name := snakeNames[0]
150+
snakeNames = snakeNames[1:]
151+
152+
return name
153+
}

cli/commands/output.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package commands
33
import (
44
"encoding/json"
55
"fmt"
6-
"log"
76
"os"
87

8+
log "github.com/spf13/jwalterweatherman"
9+
910
"github.com/BattlesnakeOfficial/rules/client"
1011
)
1112

@@ -30,7 +31,7 @@ func (ge *GameExporter) FlushToFile(filepath string, format string) error {
3031
if format == "JSONL" {
3132
formattedOutput, formattingErr = ge.ConvertToJSON()
3233
} else {
33-
log.Fatalf("Invalid output format passed: %s", format)
34+
log.ERROR.Fatalf("Invalid output format passed: %s", format)
3435
}
3536

3637
if formattingErr != nil {
@@ -50,7 +51,7 @@ func (ge *GameExporter) FlushToFile(filepath string, format string) error {
5051
}
5152
}
5253

53-
log.Printf("Written %d lines of output to file: %s\n", len(formattedOutput), filepath)
54+
log.DEBUG.Printf("Written %d lines of output to file: %s\n", len(formattedOutput), filepath)
5455

5556
return nil
5657
}

0 commit comments

Comments
 (0)