Refactor accounts to store a data map rather than just a rover ID
This commit is contained in:
parent
7749854eb7
commit
8f25f55658
4 changed files with 33 additions and 31 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/mdiluz/rove/pkg/rove"
|
"github.com/mdiluz/rove/pkg/rove"
|
||||||
"github.com/mdiluz/rove/pkg/version"
|
"github.com/mdiluz/rove/pkg/version"
|
||||||
)
|
)
|
||||||
|
@ -118,10 +119,13 @@ func HandleCommand(s *Server, vars map[string]string, b io.ReadCloser, w io.Writ
|
||||||
} else if len(id) == 0 {
|
} else if len(id) == 0 {
|
||||||
response.Error = "No account ID provided"
|
response.Error = "No account ID provided"
|
||||||
|
|
||||||
} else if inst, err := s.accountant.GetRover(id); err != nil {
|
} else if inst, err := s.accountant.GetData(id, "rover"); err != nil {
|
||||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||||
|
|
||||||
} else if err := s.world.Enqueue(inst, data.Commands...); err != nil {
|
} else if id, err := uuid.Parse(inst); err != nil {
|
||||||
|
response.Error = fmt.Sprintf("Account had invalid rover id: %s", err)
|
||||||
|
|
||||||
|
} else if err := s.world.Enqueue(id, data.Commands...); err != nil {
|
||||||
response.Error = fmt.Sprintf("Failed to execute commands: %s", err)
|
response.Error = fmt.Sprintf("Failed to execute commands: %s", err)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -142,13 +146,16 @@ func HandleRadar(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
|
||||||
if len(id) == 0 {
|
if len(id) == 0 {
|
||||||
response.Error = "No account ID provided"
|
response.Error = "No account ID provided"
|
||||||
|
|
||||||
} else if inst, err := s.accountant.GetRover(id); err != nil {
|
} else if inst, err := s.accountant.GetData(id, "rover"); err != nil {
|
||||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||||
|
|
||||||
} else if attrib, err := s.world.RoverAttributes(inst); err != nil {
|
} else if id, err := uuid.Parse(inst); err != nil {
|
||||||
|
response.Error = fmt.Sprintf("Account had invalid rover id: %s", err)
|
||||||
|
|
||||||
|
} else if attrib, err := s.world.RoverAttributes(id); err != nil {
|
||||||
response.Error = fmt.Sprintf("Error getting rover attributes: %s", err)
|
response.Error = fmt.Sprintf("Error getting rover attributes: %s", err)
|
||||||
|
|
||||||
} else if radar, err := s.world.RadarFromRover(inst); err != nil {
|
} else if radar, err := s.world.RadarFromRover(id); err != nil {
|
||||||
response.Error = fmt.Sprintf("Error getting radar from rover: %s", err)
|
response.Error = fmt.Sprintf("Error getting radar from rover: %s", err)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -171,10 +178,13 @@ func HandleRover(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
|
||||||
if len(id) == 0 {
|
if len(id) == 0 {
|
||||||
response.Error = "No account ID provided"
|
response.Error = "No account ID provided"
|
||||||
|
|
||||||
} else if inst, err := s.accountant.GetRover(id); err != nil {
|
} else if inst, err := s.accountant.GetData(id, "rover"); err != nil {
|
||||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||||
|
|
||||||
} else if attribs, err := s.world.RoverAttributes(inst); err != nil {
|
} else if id, err := uuid.Parse(inst); err != nil {
|
||||||
|
response.Error = fmt.Sprintf("Account had invalid rover id: %s", err)
|
||||||
|
|
||||||
|
} else if attribs, err := s.world.RoverAttributes(id); err != nil {
|
||||||
response.Error = fmt.Sprintf("Error getting radar from rover: %s", err)
|
response.Error = fmt.Sprintf("Error getting radar from rover: %s", err)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -289,7 +289,7 @@ func (s *Server) SpawnRoverForAccount(account string) (game.RoverAttributes, uui
|
||||||
return game.RoverAttributes{}, uuid.UUID{}, fmt.Errorf("No attributes found for created rover: %s", err)
|
return game.RoverAttributes{}, uuid.UUID{}, fmt.Errorf("No attributes found for created rover: %s", err)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if err := s.accountant.AssignRover(account, inst); err != nil {
|
if err := s.accountant.AssignData(account, "rover", inst.String()); err != nil {
|
||||||
// Try and clear up the rover
|
// Try and clear up the rover
|
||||||
if err := s.world.DestroyRover(inst); err != nil {
|
if err := s.world.DestroyRover(inst); err != nil {
|
||||||
fmt.Printf("Failed to destroy rover after failed rover assign: %s", err)
|
fmt.Printf("Failed to destroy rover after failed rover assign: %s", err)
|
||||||
|
|
|
@ -2,8 +2,6 @@ package accounts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const kAccountsFileName = "rove-accounts.json"
|
const kAccountsFileName = "rove-accounts.json"
|
||||||
|
@ -13,8 +11,8 @@ type Account struct {
|
||||||
// Name simply describes the account and must be unique
|
// Name simply describes the account and must be unique
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|
||||||
// Rover represents the rover that this account owns
|
// Data represents internal account data
|
||||||
Rover uuid.UUID `json:"rover"`
|
Data map[string]string `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Represents the accountant data to store
|
// Represents the accountant data to store
|
||||||
|
@ -52,12 +50,12 @@ func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssignRover assigns rover ownership of an rover to an account
|
// AssignRover assigns data to an account
|
||||||
func (a *Accountant) AssignRover(account string, rover uuid.UUID) error {
|
func (a *Accountant) AssignData(account string, key string, value string) error {
|
||||||
|
|
||||||
// Find the account matching the ID
|
// Find the account matching the ID
|
||||||
if this, ok := a.Accounts[account]; ok {
|
if this, ok := a.Accounts[account]; ok {
|
||||||
this.Rover = rover
|
this.Data[key] = value
|
||||||
a.Accounts[account] = this
|
a.Accounts[account] = this
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("no account found for id: %s", account)
|
return fmt.Errorf("no account found for id: %s", account)
|
||||||
|
@ -67,13 +65,11 @@ func (a *Accountant) AssignRover(account string, rover uuid.UUID) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRover gets the rover rover for the account
|
// GetRover gets the rover rover for the account
|
||||||
func (a *Accountant) GetRover(account string) (uuid.UUID, error) {
|
func (a *Accountant) GetData(account string, key string) (string, error) {
|
||||||
// Find the account matching the ID
|
// Find the account matching the ID
|
||||||
if this, ok := a.Accounts[account]; !ok {
|
if this, ok := a.Accounts[account]; !ok {
|
||||||
return uuid.UUID{}, fmt.Errorf("no account found for id: %s", account)
|
return "", fmt.Errorf("no account found for id: %s", account)
|
||||||
} else if this.Rover == uuid.Nil {
|
|
||||||
return uuid.UUID{}, fmt.Errorf("no rover spawned for account %s", account)
|
|
||||||
} else {
|
} else {
|
||||||
return this.Rover, nil
|
return this.Data[key], nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ func TestAccountant_RegisterAccount(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccountant_AssignGetRover(t *testing.T) {
|
func TestAccountant_AssignGetData(t *testing.T) {
|
||||||
accountant := NewAccountant()
|
accountant := NewAccountant()
|
||||||
if len(accountant.Accounts) != 0 {
|
if len(accountant.Accounts) != 0 {
|
||||||
t.Error("New accountant created with non-zero account number")
|
t.Error("New accountant created with non-zero account number")
|
||||||
|
@ -55,16 +55,12 @@ func TestAccountant_AssignGetRover(t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
inst := uuid.New()
|
err = accountant.AssignData(a.Name, "key", "value")
|
||||||
|
|
||||||
err = accountant.AssignRover(a.Name, inst)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Failed to set rover for created account")
|
t.Error("Failed to set data for created account")
|
||||||
} else if accountant.Accounts[a.Name].Rover != inst {
|
} else if id, err := accountant.GetData(a.Name, "key"); err != nil {
|
||||||
t.Error("Rover for assigned account is incorrect")
|
t.Error("Failed to get data for account")
|
||||||
} else if id, err := accountant.GetRover(a.Name); err != nil {
|
} else if id != "value" {
|
||||||
t.Error("Failed to get rover for account")
|
t.Error("Fetched data is incorrect for account")
|
||||||
} else if id != inst {
|
|
||||||
t.Error("Fetched rover is incorrect for account")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue