Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV 1666: Fix /end requests and clean up logging #109

Merged
merged 7 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions board/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package board
import (
"context"
"encoding/json"
"log"
"net"
"net/http"

"github.com/gorilla/websocket"
"github.com/rs/cors"
log "github.com/spf13/jwalterweatherman"
)

// A minimal server capable of handling the requests from a single browser client running the board viewer.
Expand Down Expand Up @@ -51,7 +51,7 @@ func (server *BoardServer) handleGame(w http.ResponseWriter, r *http.Request) {
Game Game
}{server.game})
if err != nil {
log.Printf("Unable to serialize game: %v", err)
log.ERROR.Printf("Unable to serialize game: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -61,37 +61,37 @@ func (server *BoardServer) handleGame(w http.ResponseWriter, r *http.Request) {
func (server *BoardServer) handleWebsocket(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Unable to upgrade connection: %v", err)
log.ERROR.Printf("Unable to upgrade connection: %v", err)
return
}

defer func() {
err = ws.Close()
if err != nil {
log.Printf("Unable to close websocket stream")
log.ERROR.Printf("Unable to close websocket stream")
}
}()

for event := range server.events {
jsonStr, err := json.Marshal(event)
if err != nil {
log.Printf("Unable to serialize event for websocket: %v", err)
log.ERROR.Printf("Unable to serialize event for websocket: %v", err)
}

err = ws.WriteMessage(websocket.TextMessage, jsonStr)
if err != nil {
log.Printf("Unable to write to websocket: %v", err)
log.ERROR.Printf("Unable to write to websocket: %v", err)
break
}
}

log.Printf("Finished writing all game events, signalling game server to stop")
log.DEBUG.Printf("Finished writing all game events, signalling game server to stop")
close(server.done)

log.Printf("Sending websocket close message")
log.DEBUG.Printf("Sending websocket close message")
err = ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Printf("Problem closing websocket: %v", err)
log.ERROR.Printf("Problem closing websocket: %v", err)
}
}

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

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

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

err := server.httpServer.Shutdown(context.Background())
if err != nil {
log.Printf("Error shutting down HTTP server: %v", err)
log.ERROR.Printf("Error shutting down HTTP server: %v", err)
}
}

Expand Down
9 changes: 5 additions & 4 deletions cli/commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package commands

import (
"fmt"
"log"

"github.com/BattlesnakeOfficial/rules/maps"
"github.com/spf13/cobra"
log "github.com/spf13/jwalterweatherman"

"github.com/BattlesnakeOfficial/rules/maps"
)

type mapInfo struct {
Expand Down Expand Up @@ -35,7 +36,7 @@ func NewMapInfoCommand() *cobra.Command {
if len(args) < 1 {
err := cmd.Help()
if err != nil {
log.Fatal(err)
log.ERROR.Fatal(err)
}
return
}
Expand All @@ -59,7 +60,7 @@ func NewMapInfoCommand() *cobra.Command {
func (m *mapInfo) display(id string) {
gameMap, err := maps.GetMap(id)
if err != nil {
log.Fatalf("Failed to load game map %#v: %v", id, err)
log.ERROR.Fatalf("Failed to load game map %v: %v", id, err)
}
meta := gameMap.Meta()
fmt.Println("Name:", meta.Name)
Expand Down
5 changes: 2 additions & 3 deletions cli/commands/map.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package commands

import (
"log"

"github.com/spf13/cobra"
log "github.com/spf13/jwalterweatherman"
)

func NewMapCommand() *cobra.Command {
Expand All @@ -15,7 +14,7 @@ func NewMapCommand() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
if err != nil {
log.Fatal(err)
log.ERROR.Fatal(err)
}
},
}
Expand Down
153 changes: 153 additions & 0 deletions cli/commands/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// 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>.
package commands

import (
"math/rand"
"time"

"github.com/google/uuid"
)

var snakeNames = []string{
"Adder",
"Aesculapian Snake",
"Anaconda",
"Arafura File Snake",
"Asp",
"African Beaked Snake",
"Ball Python",
"Bird Snake",
"Black-headed Snake",
"Mexican Black Kingsnake",
"Black Rat Snake",
"Black Snake",
"Blind Snake",
"Boa",
"Boiga",
"Boomslang",
"Brown Snake",
"Bull Snake",
"Bushmaster",
"Dwarf Beaked Snake",
"Rufous Beaked Snake",
"Canebrake",
"Cantil",
"Cascabel",
"Cat-eyed Snake",
"Cat Snake",
"Chicken Snake",
"Coachwhip Snake",
"Cobra",
"Collett's Snake",
"Congo Snake",
"Copperhead",
"Coral Snake",
"Corn Snake",
"Cottonmouth",
"Crowned Snake",
"Cuban Wood Snake",
"Egg-eater",
"Eyelash Viper",
"Fer-de-lance",
"Fierce Snake",
"Fishing Snake",
"Flying Snake",
"Fox Snake",
"Forest Flame Snake",
"Garter Snake",
"Glossy Snake",
"Gopher Snake",
"Grass Snake",
"Green Snake",
"Ground Snake",
"Habu",
"Harlequin Snake",
"Herald Snake",
"Hognose Snake",
"Hoop Snake",
"Hundred Pacer",
"Ikaheka Snake",
"Indigo Snake",
"Jamaican Tree Snake",
"Jararacussu",
"Keelback",
"King Brown",
"King Cobra",
"King Snake",
"Krait",
"Lancehead",
"Lora",
"Lyre Snake",
"Machete Savane",
"Mamba",
"Mamushi",
"Mangrove Snake",
"Milk Snake",
"Moccasin Snake",
"Montpellier Snake",
"Mud Snake",
"Mussurana",
"Night Snake",
"Nose-horned Viper",
"Parrot Snake",
"Patchnose Snake",
"Pine Snake",
"Pipe Snake",
"Python",
"Queen Snake",
"Racer",
"Raddysnake",
"Rat Snake",
"Rattlesnake",
"Ribbon Snake",
"Rinkhals",
"River Jack",
"Sea Snake",
"Shield-tailed Snake",
"Sidewinder",
"Small-eyed Snake",
"Stiletto Snake",
"Striped Snake",
"Sunbeam Snake",
"Taipan",
"Tentacled Snake",
"Tic Polonga",
"Tiger Snake",
"Tigre Snake",
"Tree Snake",
"Trinket Snake",
"Twig Snake",
"Twin Headed King Snake",
"Titanoboa",
"Urutu",
"Vine Snake",
"Viper",
"Wart Snake",
"Water Moccasin",
"Water Snake",
"Whip Snake",
"Wolf Snake",
"Worm Snake",
"Wutu",
"Yarara",
"Zebra Snake",
}

func init() {
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
randGen.Shuffle(len(snakeNames), func(i, j int) {
snakeNames[i], snakeNames[j] = snakeNames[j], snakeNames[i]
})
}

// Generate a random unique snake name, or return a UUID if there are no more names available.
func GenerateSnakeName() string {
if len(snakeNames) == 0 {
return uuid.New().String()
}

name := snakeNames[0]
snakeNames = snakeNames[1:]

return name
}
7 changes: 4 additions & 3 deletions cli/commands/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package commands
import (
"encoding/json"
"fmt"
"log"
"os"

log "github.com/spf13/jwalterweatherman"

"github.com/BattlesnakeOfficial/rules/client"
)

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

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

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

return nil
}
Expand Down
Loading