From 58f9d8baf2c419c4c08c1e2cdeecf70491e067f9 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Thu, 25 Jun 2020 22:02:11 +0100 Subject: [PATCH] Reformat to use a words file rather than babble --- docker-compose.yml | 1 + pkg/game/world.go | 36 ++++++++++++++++++++++++++++++------ snap/snapcraft.yaml | 16 ++++++++++++---- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3c45612..91c69c1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,6 +40,7 @@ services: - PORT=9090 - DATA_PATH=/mnt/rove-server - ROVE_ACCOUNTANT_GRPC=rove-accountant:9091 + - WORDS_FILE=data/words_alpha.txt volumes: - persistent-data:/mnt/rove-server:rw command: [ "./script/wait-for-it.sh", "rove-accountant:9091", "--", "./rove-server"] diff --git a/pkg/game/world.go b/pkg/game/world.go index 626a4da..192e86a 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -1,11 +1,12 @@ package game import ( + "bufio" "fmt" "log" "math" "math/rand" - "strings" + "os" "sync" "github.com/google/uuid" @@ -13,7 +14,6 @@ import ( "github.com/mdiluz/rove/pkg/bearing" "github.com/mdiluz/rove/pkg/maths" "github.com/mdiluz/rove/pkg/vector" - "github.com/tjarratt/babble" ) // World describes a self contained universe and everything in it @@ -35,15 +35,37 @@ type World struct { // 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(size, 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[uuid.UUID]Rover), CommandQueue: make(map[uuid.UUID]CommandStream), Incoming: make(map[uuid.UUID]CommandStream), Atlas: atlas.NewAtlas(size, chunkSize), + words: lines, } } @@ -66,15 +88,17 @@ func (w *World) SpawnRover() (uuid.UUID, error) { rover := Rover{ Id: uuid.New(), Attributes: RoverAttributes{ - Speed: 1.0, Range: 5.0, - - // Set the name randomly - Name: strings.ReplaceAll(babble.NewBabbler().Babble(), "'s", ""), + Name: "rover", }, } + // Assign a random name if we have words + if len(w.words) > 0 { + rover.Attributes.Name = fmt.Sprintf("%s-%s", w.words[rand.Intn(len(w.words))], w.words[rand.Intn(len(w.words))]) + } + // Spawn in a random place near the origin rover.Attributes.Pos = vector.Vector{ X: w.Atlas.ChunkSize/2 - rand.Intn(w.Atlas.ChunkSize), diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 55815cb..41692a3 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -21,6 +21,8 @@ apps: plugs: - network - network-bind + environment: + WORDS_FILE : "$SNAP/data/words_alpha.txt" rove-accountant: command: bin/rove-accountant plugs: @@ -41,8 +43,14 @@ parts: - gcc-multilib override-pull: | snapcraftctl pull - snapcraftctl set-version $(git describe --always --long --dirty --tags) + version=$(git describe --always --long --dirty --tags) + sed -i "s/undefined/$version/" pkg/version/version.go + snapcraftctl set-version $version git describe --exact-match --tags HEAD 2> /dev/null && snapcraftctl set-grade "stable" || snapcraftctl set-grade "devel" - sed -i "s/undefined/$(git describe --always --long --dirty --tags)/" pkg/version/version.go - - # TODO: Server needs /usr/share/dict/words + + copy-data: + plugin: dump + source-type: local + source: data + organize: + '*.txt' : data/