diff --git a/cmd/rove-server/internal/routes.go b/cmd/rove-server/internal/routes.go index 86becd7..69dd820 100644 --- a/cmd/rove-server/internal/routes.go +++ b/cmd/rove-server/internal/routes.go @@ -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 } diff --git a/cmd/rove-server/internal/server.go b/cmd/rove-server/internal/server.go index 786eb00..564656e 100644 --- a/cmd/rove-server/internal/server.go +++ b/cmd/rove-server/internal/server.go @@ -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) diff --git a/cmd/rove-server/internal/accounts.go b/pkg/accounts/accounts.go similarity index 98% rename from cmd/rove-server/internal/accounts.go rename to pkg/accounts/accounts.go index 9ae6db9..eb8637d 100644 --- a/cmd/rove-server/internal/accounts.go +++ b/pkg/accounts/accounts.go @@ -1,4 +1,4 @@ -package internal +package accounts // Accountant decribes something that stores accounts and account values type Accountant interface { diff --git a/cmd/rove-server/internal/accounts_test.go b/pkg/accounts/accounts_test.go similarity index 98% rename from cmd/rove-server/internal/accounts_test.go rename to pkg/accounts/accounts_test.go index 9e7891f..bd2416f 100644 --- a/cmd/rove-server/internal/accounts_test.go +++ b/pkg/accounts/accounts_test.go @@ -1,4 +1,4 @@ -package internal +package accounts import ( "testing" diff --git a/cmd/rove-server/internal/simpleAccountant.go b/pkg/accounts/simpleAccountant.go similarity index 99% rename from cmd/rove-server/internal/simpleAccountant.go rename to pkg/accounts/simpleAccountant.go index 611ff59..9d6c43f 100644 --- a/cmd/rove-server/internal/simpleAccountant.go +++ b/pkg/accounts/simpleAccountant.go @@ -1,4 +1,4 @@ -package internal +package accounts import ( "fmt" diff --git a/pkg/rove/world.go b/pkg/rove/world.go index afe7605..0d828ae 100644 --- a/pkg/rove/world.go +++ b/pkg/rove/world.go @@ -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(), } }