Migrate to gRPC rather than REST with swagger

Will also be adding in a RESTful endpoint to the server as well so it can consume both types
This commit is contained in:
Marc Di Luzio 2020-06-12 22:51:18 +01:00
parent b815284199
commit 7ababb79f6
23 changed files with 1110 additions and 1101 deletions

View file

@ -12,7 +12,7 @@ import (
// Chunk represents a fixed square grid of tiles
type Chunk struct {
// Tiles represents the tiles within the chunk
Tiles []Tile `json:"tiles"`
Tiles []byte `json:"tiles"`
}
// Atlas represents a grid of Chunks
@ -43,7 +43,7 @@ func NewAtlas(size, chunkSize int) Atlas {
// Initialise all the chunks
for i := range a.Chunks {
a.Chunks[i] = Chunk{
Tiles: make([]Tile, chunkSize*chunkSize),
Tiles: make([]byte, chunkSize*chunkSize),
}
}
@ -90,7 +90,7 @@ func (a *Atlas) SpawnWalls() error {
}
// SetTile sets an individual tile's kind
func (a *Atlas) SetTile(v vector.Vector, tile Tile) error {
func (a *Atlas) SetTile(v vector.Vector, tile byte) error {
chunk := a.toChunk(v)
if chunk >= len(a.Chunks) {
return fmt.Errorf("location outside of allocated atlas")
@ -106,7 +106,7 @@ func (a *Atlas) SetTile(v vector.Vector, tile Tile) error {
}
// GetTile will return an individual tile
func (a *Atlas) GetTile(v vector.Vector) (Tile, error) {
func (a *Atlas) GetTile(v vector.Vector) (byte, error) {
chunk := a.toChunk(v)
if chunk >= len(a.Chunks) {
return 0, fmt.Errorf("location outside of allocated atlas")

View file

@ -81,13 +81,13 @@ func TestAtlas_GetSetTile(t *testing.T) {
assert.NoError(t, a.SetTile(vector.Vector{X: 0, Y: 0}, 1))
tile, err := a.GetTile(vector.Vector{X: 0, Y: 0})
assert.NoError(t, err)
assert.Equal(t, Tile(1), tile)
assert.Equal(t, byte(1), tile)
// Set another tile to 1 and test it
assert.NoError(t, a.SetTile(vector.Vector{X: 5, Y: -2}, 2))
tile, err = a.GetTile(vector.Vector{X: 5, Y: -2})
assert.NoError(t, err)
assert.Equal(t, Tile(2), tile)
assert.Equal(t, byte(2), tile)
}
func TestAtlas_Grown(t *testing.T) {
@ -108,15 +108,15 @@ func TestAtlas_Grown(t *testing.T) {
tile, err := a.GetTile(vector.Vector{X: 0, Y: 0})
assert.NoError(t, err)
assert.Equal(t, Tile(1), tile)
assert.Equal(t, byte(1), tile)
tile, err = a.GetTile(vector.Vector{X: -1, Y: -1})
assert.NoError(t, err)
assert.Equal(t, Tile(2), tile)
assert.Equal(t, byte(2), tile)
tile, err = a.GetTile(vector.Vector{X: 1, Y: -2})
assert.NoError(t, err)
assert.Equal(t, Tile(3), tile)
assert.Equal(t, byte(3), tile)
// Grow it again even bigger
err = a.Grow(10)
@ -125,15 +125,15 @@ func TestAtlas_Grown(t *testing.T) {
tile, err = a.GetTile(vector.Vector{X: 0, Y: 0})
assert.NoError(t, err)
assert.Equal(t, Tile(1), tile)
assert.Equal(t, byte(1), tile)
tile, err = a.GetTile(vector.Vector{X: -1, Y: -1})
assert.NoError(t, err)
assert.Equal(t, Tile(2), tile)
assert.Equal(t, byte(2), tile)
tile, err = a.GetTile(vector.Vector{X: 1, Y: -2})
assert.NoError(t, err)
assert.Equal(t, Tile(3), tile)
assert.Equal(t, byte(3), tile)
}
func TestAtlas_SpawnWorld(t *testing.T) {

View file

@ -1,12 +1,9 @@
package atlas
// Tile represents the type of a tile on the map
type Tile byte
const (
TileEmpty = Tile(0)
TileRover = Tile(1)
TileEmpty = byte(0)
TileRover = byte(1)
TileWall = Tile(2)
TileRock = Tile(3)
TileWall = byte(2)
TileRock = byte(3)
)