Move accountant into world

This commit is contained in:
Marc Di Luzio 2020-07-24 20:06:06 +01:00
parent 1e4d642038
commit e840b3e47b
6 changed files with 18 additions and 17 deletions

View file

@ -34,7 +34,7 @@ func (s *Server) Register(ctx context.Context, req *roveapi.RegisterRequest) (*r
return nil, fmt.Errorf("empty account name")
}
if acc, err := s.accountant.RegisterAccount(req.Name); err != nil {
if acc, err := s.world.Accountant.RegisterAccount(req.Name); err != nil {
return nil, err
} else if _, err := s.SpawnRoverForAccount(req.Name); err != nil {
@ -57,13 +57,13 @@ func (s *Server) Register(ctx context.Context, req *roveapi.RegisterRequest) (*r
func (s *Server) Status(ctx context.Context, req *roveapi.StatusRequest) (response *roveapi.StatusResponse, err error) {
log.Printf("Handling status request: %s\n", req.Account.Name)
if valid, err := s.accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
if valid, err := s.world.Accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
return nil, err
} else if !valid {
return nil, fmt.Errorf("Secret incorrect for account %s", req.Account.Name)
} else if resp, err := s.accountant.GetValue(req.Account.Name, "rover"); err != nil {
} else if resp, err := s.world.Accountant.GetValue(req.Account.Name, "rover"); err != nil {
return nil, err
} else if rover, err := s.world.GetRover(resp); err != nil {
@ -117,7 +117,7 @@ func (s *Server) Status(ctx context.Context, req *roveapi.StatusRequest) (respon
func (s *Server) Radar(ctx context.Context, req *roveapi.RadarRequest) (*roveapi.RadarResponse, error) {
log.Printf("Handling radar request: %s\n", req.Account.Name)
if valid, err := s.accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
if valid, err := s.world.Accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
return nil, err
} else if !valid {
@ -126,7 +126,7 @@ func (s *Server) Radar(ctx context.Context, req *roveapi.RadarRequest) (*roveapi
response := &roveapi.RadarResponse{}
resp, err := s.accountant.GetValue(req.Account.Name, "rover")
resp, err := s.world.Accountant.GetValue(req.Account.Name, "rover")
if err != nil {
return nil, err
@ -149,14 +149,14 @@ func (s *Server) Radar(ctx context.Context, req *roveapi.RadarRequest) (*roveapi
func (s *Server) Command(ctx context.Context, req *roveapi.CommandRequest) (*roveapi.CommandResponse, error) {
log.Printf("Handling command request: %s and %+v\n", req.Account.Name, req.Commands)
if valid, err := s.accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
if valid, err := s.world.Accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
return nil, err
} else if !valid {
return nil, fmt.Errorf("Secret incorrect for account %s", req.Account.Name)
}
resp, err := s.accountant.GetValue(req.Account.Name, "rover")
resp, err := s.world.Accountant.GetValue(req.Account.Name, "rover")
if err != nil {
return nil, err
}

View file

@ -27,9 +27,6 @@ type Server struct {
// Internal state
world *rove.World
// Accountant
accountant Accountant
// gRPC server
netListener net.Listener
grpcServ *grpc.Server
@ -80,7 +77,6 @@ func NewServer(opts ...ServerOption) *Server {
persistence: EphemeralData,
schedule: cron.New(),
world: rove.NewWorld(32),
accountant: NewSimpleAccountant(),
}
// Apply all options
@ -188,7 +184,7 @@ func (s *Server) SaveWorld() error {
if s.persistence == PersistentData {
s.world.RLock()
defer s.world.RUnlock()
if err := persistence.SaveAll("world", s.world, "accounts", s.accountant); err != nil {
if err := persistence.SaveAll("world", s.world); err != nil {
return fmt.Errorf("failed to save out persistent data: %s", err)
}
}
@ -200,7 +196,7 @@ func (s *Server) LoadWorld() error {
if s.persistence == PersistentData {
s.world.Lock()
defer s.world.Unlock()
if err := persistence.LoadAll("world", &s.world, "accounts", &s.accountant); err != nil {
if err := persistence.LoadAll("world", &s.world); err != nil {
return err
}
}
@ -214,7 +210,7 @@ func (s *Server) SpawnRoverForAccount(account string) (string, error) {
return "", err
}
err = s.accountant.AssignData(account, "rover", inst)
err = s.world.Accountant.AssignData(account, "rover", inst)
if err != nil {
log.Printf("Failed to assign rover to account, %s", err)

View file

@ -1,4 +1,4 @@
package internal
package accounts
// Accountant decribes something that stores accounts and account values
type Accountant interface {

View file

@ -1,4 +1,4 @@
package internal
package accounts
import (
"testing"

View file

@ -1,4 +1,4 @@
package internal
package accounts
import (
"fmt"

View file

@ -7,6 +7,7 @@ import (
"math/rand"
"sync"
"github.com/mdiluz/rove/pkg/accounts"
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/proto/roveapi"
)
@ -40,6 +41,9 @@ type World struct {
// Commands is the set of currently executing command streams per rover
CommandQueue map[string]CommandStream
// Accountant
Accountant accounts.Accountant
// Mutex to lock around all world operations
worldMutex sync.RWMutex
// Mutex to lock around command operations
@ -54,6 +58,7 @@ func NewWorld(chunkSize int) *World {
Atlas: NewChunkAtlas(chunkSize),
TicksPerDay: 24,
CurrentTicks: 0,
Accountant: accounts.NewSimpleAccountant(),
}
}