Fix up gocritic issues

This commit is contained in:
Marc Di Luzio 2020-06-10 12:32:15 +01:00
parent 2ee68e74ac
commit 14c4e61660
9 changed files with 72 additions and 56 deletions

View file

@ -101,10 +101,12 @@ func InnerMain(command string) error {
// Handle all the commands
switch command {
case "status":
if response, err := server.Status(); err != nil {
response, err := server.Status()
switch {
case err != nil:
return err
} else {
default:
fmt.Printf("Ready: %t\n", response.Ready)
fmt.Printf("Version: %s\n", response.Version)
fmt.Printf("Tick: %d\n", response.Tick)
@ -115,13 +117,15 @@ func InnerMain(command string) error {
d := rove.RegisterData{
Name: *name,
}
if response, err := server.Register(d); err != nil {
response, err := server.Register(d)
switch {
case err != nil:
return err
} else if !response.Success {
case !response.Success:
return fmt.Errorf("Server returned failure: %s", response.Error)
} else {
default:
fmt.Printf("Registered account with id: %s\n", response.Id)
config.Accounts[config.Host] = response.Id
}
@ -129,13 +133,17 @@ func InnerMain(command string) error {
d := rove.SpawnData{}
if err := verifyId(account); err != nil {
return err
} else if response, err := server.Spawn(account, d); err != nil {
}
response, err := server.Spawn(account, d)
switch {
case err != nil:
return err
} else if !response.Success {
case !response.Success:
return fmt.Errorf("Server returned failure: %s", response.Error)
} else {
default:
fmt.Printf("Spawned rover with attributes %+v\n", response.Attributes)
}
@ -152,26 +160,34 @@ func InnerMain(command string) error {
if err := verifyId(account); err != nil {
return err
} else if response, err := server.Command(account, d); err != nil {
}
response, err := server.Command(account, d)
switch {
case err != nil:
return err
} else if !response.Success {
case !response.Success:
return fmt.Errorf("Server returned failure: %s", response.Error)
} else {
default:
fmt.Printf("Request succeeded\n")
}
case "radar":
if err := verifyId(account); err != nil {
return err
} else if response, err := server.Radar(account); err != nil {
}
response, err := server.Radar(account)
switch {
case err != nil:
return err
} else if !response.Success {
case !response.Success:
return fmt.Errorf("Server returned failure: %s", response.Error)
} else {
default:
// Print out the radar
game.PrintTiles(response.Tiles)
}
@ -179,13 +195,17 @@ func InnerMain(command string) error {
case "rover":
if err := verifyId(account); err != nil {
return err
} else if response, err := server.Rover(account); err != nil {
}
response, err := server.Rover(account)
switch {
case err != nil:
return err
} else if !response.Success {
case !response.Success:
return fmt.Errorf("Server returned failure: %s", response.Error)
} else {
default:
fmt.Printf("attributes: %+v\n", response.Attributes)
}
@ -198,10 +218,8 @@ func InnerMain(command string) error {
// Save out the persistent file
if b, err := json.MarshalIndent(config, "", "\t"); err != nil {
return fmt.Errorf("failed to marshal data error: %s", err)
} else {
if err := ioutil.WriteFile(*data, b, os.ModePerm); err != nil {
return fmt.Errorf("failed to save file %s error: %s", *data, err)
}
} else if err := ioutil.WriteFile(*data, b, os.ModePerm); err != nil {
return fmt.Errorf("failed to save file %s error: %s", *data, err)
}
return nil

View file

@ -59,7 +59,7 @@ func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
}
// AssignRover assigns rover ownership of an rover to an account
func (a *Accountant) AssignRover(account uuid.UUID, rover uuid.UUID) error {
func (a *Accountant) AssignRover(account, rover uuid.UUID) error {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {

View file

@ -29,7 +29,7 @@ type Atlas struct {
}
// NewAtlas creates a new empty atlas
func NewAtlas(size int, chunkSize int) Atlas {
func NewAtlas(size, chunkSize int) Atlas {
if size%2 != 0 {
log.Fatal("atlas size must always be even")
}
@ -77,11 +77,11 @@ func (a *Atlas) SpawnWalls() error {
if err := a.SetTile(vector.Vector{X: i, Y: extent - 1}, TileWall); err != nil { // N
return err
} else if a.SetTile(vector.Vector{X: extent - 1, Y: i}, TileWall); err != nil { // E
} else if err := a.SetTile(vector.Vector{X: extent - 1, Y: i}, TileWall); err != nil { // E
return err
} else if a.SetTile(vector.Vector{X: i, Y: -extent}, TileWall); err != nil { // S
} else if err := a.SetTile(vector.Vector{X: i, Y: -extent}, TileWall); err != nil { // S
return err
} else if a.SetTile(vector.Vector{X: -extent, Y: i}, TileWall); err != nil { // W
} else if err := a.SetTile(vector.Vector{X: -extent, Y: i}, TileWall); err != nil { // W
return err
}
}
@ -155,7 +155,7 @@ func (a *Atlas) ChunkOrigin(chunk int) vector.Vector {
}
// GetWorldExtent gets the min and max valid coordinates of world
func (a *Atlas) GetWorldExtents() (min vector.Vector, max vector.Vector) {
func (a *Atlas) GetWorldExtents() (min, max vector.Vector) {
min = vector.Vector{
X: -(a.Size / 2) * a.ChunkSize,
Y: -(a.Size / 2) * a.ChunkSize,

View file

@ -3,7 +3,6 @@ package game
import (
"testing"
"github.com/mdiluz/rove/pkg/bearing"
"github.com/mdiluz/rove/pkg/vector"
"github.com/stretchr/testify/assert"
)
@ -23,10 +22,9 @@ func TestCommand_Move(t *testing.T) {
err = world.WarpRover(a, pos)
assert.NoError(t, err, "Failed to set position for rover")
bearing := bearing.North
duration := 1
// Try the move command
moveCommand := Command{Command: CommandMove, Bearing: bearing.String(), Duration: duration}
moveCommand := Command{Command: CommandMove, Bearing: "N", Duration: duration}
assert.NoError(t, world.Enqueue(a, moveCommand), "Failed to execute move command")
// Tick the world

View file

@ -36,7 +36,7 @@ type World struct {
}
// NewWorld creates a new world object
func NewWorld(size int, chunkSize int) *World {
func NewWorld(size, chunkSize int) *World {
return &World{
Rovers: make(map[uuid.UUID]Rover),
CommandQueue: make(map[uuid.UUID]CommandStream),
@ -183,7 +183,7 @@ func (w *World) WarpRover(id uuid.UUID, pos vector.Vector) error {
}
// SetPosition sets an rovers position
func (w *World) MoveRover(id uuid.UUID, bearing bearing.Bearing) (RoverAttributes, error) {
func (w *World) MoveRover(id uuid.UUID, b bearing.Bearing) (RoverAttributes, error) {
w.worldMutex.Lock()
defer w.worldMutex.Unlock()
@ -192,7 +192,7 @@ func (w *World) MoveRover(id uuid.UUID, bearing bearing.Bearing) (RoverAttribute
distance := i.Attributes.Speed
// Calculate the full movement based on the bearing
move := bearing.Vector().Multiplied(distance)
move := b.Vector().Multiplied(distance)
// Try the new move position
newPos := i.Attributes.Pos.Added(move)

View file

@ -79,16 +79,16 @@ func TestWorld_GetSetMovePosition(t *testing.T) {
assert.NoError(t, err, "Failed to set position for rover")
assert.Equal(t, pos, newAttribs.Pos, "Failed to correctly set position for rover")
bearing := bearing.North
b := bearing.North
duration := 1
newAttribs, err = world.MoveRover(a, bearing)
newAttribs, err = world.MoveRover(a, b)
assert.NoError(t, err, "Failed to set position for rover")
pos.Add(vector.Vector{X: 0, Y: attribs.Speed * duration}) // We should have move one unit of the speed north
assert.Equal(t, pos, newAttribs.Pos, "Failed to correctly move position for rover")
// Place a tile in front of the rover
assert.NoError(t, world.Atlas.SetTile(vector.Vector{X: 0, Y: 2}, TileWall))
newAttribs, err = world.MoveRover(a, bearing)
newAttribs, err = world.MoveRover(a, b)
assert.Equal(t, pos, newAttribs.Pos, "Failed to correctly not move position for rover into wall")
}

View file

@ -25,7 +25,7 @@ func Pmod(x, d int) int {
}
// Max returns the highest int
func Max(x int, y int) int {
func Max(x, y int) int {
if x < y {
return y
}
@ -33,7 +33,7 @@ func Max(x int, y int) int {
}
// Min returns the lowest int
func Min(x int, y int) int {
func Min(x, y int) int {
if x > y {
return y
}

View file

@ -12,13 +12,13 @@ import (
var dataPath = os.TempDir()
// SetPath sets the persistent path for the data storage
func SetPath(path string) error {
if info, err := os.Stat(path); err != nil {
func SetPath(p string) error {
if info, err := os.Stat(p); err != nil {
return err
} else if !info.IsDir() {
return fmt.Errorf("path for persistence is not directory")
}
dataPath = path
dataPath = p
return nil
}
@ -29,40 +29,40 @@ func jsonPath(name string) string {
// Save will serialise the interface into a json file
func Save(name string, data interface{}) error {
path := jsonPath(name)
p := jsonPath(name)
if b, err := json.MarshalIndent(data, "", " "); err != nil {
return err
} else {
if err := ioutil.WriteFile(jsonPath(name), b, os.ModePerm); err != nil {
if err := ioutil.WriteFile(p, b, os.ModePerm); err != nil {
return err
}
}
fmt.Printf("Saved %s\n", path)
fmt.Printf("Saved %s\n", p)
return nil
}
// Load will load the interface from the json file
func Load(name string, data interface{}) error {
path := jsonPath(name)
p := jsonPath(name)
// Don't load anything if the file doesn't exist
_, err := os.Stat(path)
_, err := os.Stat(p)
if os.IsNotExist(err) {
fmt.Printf("File %s didn't exist, loading with fresh data\n", path)
fmt.Printf("File %s didn't exist, loading with fresh data\n", p)
return nil
}
// Read and unmarshal the json
if b, err := ioutil.ReadFile(path); err != nil {
if b, err := ioutil.ReadFile(p); err != nil {
return err
} else if len(b) == 0 {
fmt.Printf("File %s was empty, loading with fresh data\n", path)
fmt.Printf("File %s was empty, loading with fresh data\n", p)
return nil
} else if err := json.Unmarshal(b, data); err != nil {
return fmt.Errorf("failed to load file %s error: %s", path, err)
return fmt.Errorf("failed to load file %s error: %s", p, err)
}
fmt.Printf("Loaded %s\n", path)
fmt.Printf("Loaded %s\n", p)
return nil
}

View file

@ -13,12 +13,12 @@ type Server string
// GET performs a GET request
func (s Server) GET(path string, out interface{}) error {
url := url.URL{
u := url.URL{
Scheme: "http",
Host: string(s),
Path: path,
}
if resp, err := http.Get(url.String()); err != nil {
if resp, err := http.Get(u.String()); err != nil {
return err
} else if resp.StatusCode != http.StatusOK {
@ -30,8 +30,8 @@ func (s Server) GET(path string, out interface{}) error {
}
// POST performs a POST request
func (s Server) POST(path string, in interface{}, out interface{}) error {
url := url.URL{
func (s Server) POST(path string, in, out interface{}) error {
u := url.URL{
Scheme: "http",
Host: string(s),
Path: path,
@ -45,7 +45,7 @@ func (s Server) POST(path string, in interface{}, out interface{}) error {
}
// Set up the request
req, err := http.NewRequest("POST", url.String(), bytes.NewReader(marshalled))
req, err := http.NewRequest("POST", u.String(), bytes.NewReader(marshalled))
if err != nil {
return err
}