From 211771121fe91cc7d8a4017279676c41bad9671b Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 19 Jul 2020 18:47:44 +0100 Subject: [PATCH] Extract rover naming to rover.go --- pkg/rove/rover.go | 38 +++++++++++++++++++++++++++++++++++++- pkg/rove/world.go | 35 ----------------------------------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/pkg/rove/rover.go b/pkg/rove/rover.go index fb898eb..6968c57 100644 --- a/pkg/rove/rover.go +++ b/pkg/rove/rover.go @@ -1,8 +1,11 @@ package rove import ( + "bufio" "fmt" "log" + "math/rand" + "os" "time" "github.com/google/uuid" @@ -60,7 +63,7 @@ func DefaultRover() Rover { Capacity: 10, Charge: 10, MaximumCharge: 10, - Name: uuid.New().String(), + Name: GenerateRoverName(), } } @@ -75,3 +78,36 @@ func (r *Rover) AddLogEntryf(format string, args ...interface{}) { }, ) } + +var wordsFile = os.Getenv("WORDS_FILE") +var roverWords []string + +// GenerateRoverName generates a new rover name +func GenerateRoverName() string { + + // Try and load the rover words file + if len(roverWords) == 0 { + // Try and load the words file + if file, err := os.Open(wordsFile); err != nil { + log.Printf("Couldn't read words file [%s], running without words: %s\n", wordsFile, err) + } else { + defer file.Close() + scanner := bufio.NewScanner(file) + for scanner.Scan() { + roverWords = append(roverWords, scanner.Text()) + } + if scanner.Err() != nil { + log.Printf("Failure during word file scan: %s\n", scanner.Err()) + } + } + } + + // Assign a random name if we have words + if len(roverWords) > 0 { + // Loop until we find a unique name + return fmt.Sprintf("%s-%s", roverWords[rand.Intn(len(roverWords))], roverWords[rand.Intn(len(roverWords))]) + } + + // Default to a unique string + return uuid.New().String() +} diff --git a/pkg/rove/world.go b/pkg/rove/world.go index 74d56b7..25aa933 100644 --- a/pkg/rove/world.go +++ b/pkg/rove/world.go @@ -1,11 +1,9 @@ package rove import ( - "bufio" "fmt" "log" "math/rand" - "os" "sync" "github.com/mdiluz/rove/pkg/maths" @@ -35,36 +33,15 @@ type World struct { worldMutex sync.RWMutex // Mutex to lock around command operations cmdMutex sync.RWMutex - // Set of possible words to use for names - words []string } -var wordsFile = os.Getenv("WORDS_FILE") - // NewWorld creates a new world object func NewWorld(chunkSize int) *World { - - // Try and load the words file - var lines []string - if file, err := os.Open(wordsFile); err != nil { - log.Printf("Couldn't read words file [%s], running without words: %s\n", wordsFile, err) - } else { - defer file.Close() - scanner := bufio.NewScanner(file) - for scanner.Scan() { - lines = append(lines, scanner.Text()) - } - if scanner.Err() != nil { - log.Printf("Failure during word file scan: %s\n", scanner.Err()) - } - } - return &World{ Rovers: make(map[string]Rover), CommandQueue: make(map[string]CommandStream), CommandIncoming: make(map[string]CommandStream), Atlas: NewChunkAtlas(chunkSize), - words: lines, TicksPerDay: 24, CurrentTicks: 0, } @@ -78,18 +55,6 @@ func (w *World) SpawnRover() (string, error) { // Initialise the rover rover := DefaultRover() - // Assign a random name if we have words - if len(w.words) > 0 { - for { - // Loop until we find a unique name - name := fmt.Sprintf("%s-%s", w.words[rand.Intn(len(w.words))], w.words[rand.Intn(len(w.words))]) - if _, ok := w.Rovers[name]; !ok { - rover.Name = name - break - } - } - } - // Spawn in a random place near the origin rover.Pos = maths.Vector{ X: 10 - rand.Intn(20),