Extract rover naming to rover.go
This commit is contained in:
parent
d3c480cb04
commit
211771121f
2 changed files with 37 additions and 36 deletions
|
@ -1,8 +1,11 @@
|
||||||
package rove
|
package rove
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -60,7 +63,7 @@ func DefaultRover() Rover {
|
||||||
Capacity: 10,
|
Capacity: 10,
|
||||||
Charge: 10,
|
Charge: 10,
|
||||||
MaximumCharge: 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()
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
package rove
|
package rove
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/mdiluz/rove/pkg/maths"
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
|
@ -35,36 +33,15 @@ type World struct {
|
||||||
worldMutex sync.RWMutex
|
worldMutex sync.RWMutex
|
||||||
// Mutex to lock around command operations
|
// Mutex to lock around command operations
|
||||||
cmdMutex sync.RWMutex
|
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
|
// NewWorld creates a new world object
|
||||||
func NewWorld(chunkSize int) *World {
|
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{
|
return &World{
|
||||||
Rovers: make(map[string]Rover),
|
Rovers: make(map[string]Rover),
|
||||||
CommandQueue: make(map[string]CommandStream),
|
CommandQueue: make(map[string]CommandStream),
|
||||||
CommandIncoming: make(map[string]CommandStream),
|
CommandIncoming: make(map[string]CommandStream),
|
||||||
Atlas: NewChunkAtlas(chunkSize),
|
Atlas: NewChunkAtlas(chunkSize),
|
||||||
words: lines,
|
|
||||||
TicksPerDay: 24,
|
TicksPerDay: 24,
|
||||||
CurrentTicks: 0,
|
CurrentTicks: 0,
|
||||||
}
|
}
|
||||||
|
@ -78,18 +55,6 @@ func (w *World) SpawnRover() (string, error) {
|
||||||
// Initialise the rover
|
// Initialise the rover
|
||||||
rover := DefaultRover()
|
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
|
// Spawn in a random place near the origin
|
||||||
rover.Pos = maths.Vector{
|
rover.Pos = maths.Vector{
|
||||||
X: 10 - rand.Intn(20),
|
X: 10 - rand.Intn(20),
|
||||||
|
|
Loading…
Add table
Reference in a new issue