Move accountant into world
This commit is contained in:
parent
1e4d642038
commit
e840b3e47b
6 changed files with 18 additions and 17 deletions
|
@ -34,7 +34,7 @@ func (s *Server) Register(ctx context.Context, req *roveapi.RegisterRequest) (*r
|
||||||
return nil, fmt.Errorf("empty account name")
|
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
|
return nil, err
|
||||||
|
|
||||||
} else if _, err := s.SpawnRoverForAccount(req.Name); err != nil {
|
} 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) {
|
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)
|
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
|
return nil, err
|
||||||
|
|
||||||
} else if !valid {
|
} else if !valid {
|
||||||
return nil, fmt.Errorf("Secret incorrect for account %s", req.Account.Name)
|
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
|
return nil, err
|
||||||
|
|
||||||
} else if rover, err := s.world.GetRover(resp); err != nil {
|
} 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) {
|
func (s *Server) Radar(ctx context.Context, req *roveapi.RadarRequest) (*roveapi.RadarResponse, error) {
|
||||||
log.Printf("Handling radar request: %s\n", req.Account.Name)
|
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
|
return nil, err
|
||||||
|
|
||||||
} else if !valid {
|
} else if !valid {
|
||||||
|
@ -126,7 +126,7 @@ func (s *Server) Radar(ctx context.Context, req *roveapi.RadarRequest) (*roveapi
|
||||||
|
|
||||||
response := &roveapi.RadarResponse{}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
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)
|
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
|
return nil, err
|
||||||
|
|
||||||
} else if !valid {
|
} else if !valid {
|
||||||
return nil, fmt.Errorf("Secret incorrect for account %s", req.Account.Name)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,9 +27,6 @@ type Server struct {
|
||||||
// Internal state
|
// Internal state
|
||||||
world *rove.World
|
world *rove.World
|
||||||
|
|
||||||
// Accountant
|
|
||||||
accountant Accountant
|
|
||||||
|
|
||||||
// gRPC server
|
// gRPC server
|
||||||
netListener net.Listener
|
netListener net.Listener
|
||||||
grpcServ *grpc.Server
|
grpcServ *grpc.Server
|
||||||
|
@ -80,7 +77,6 @@ func NewServer(opts ...ServerOption) *Server {
|
||||||
persistence: EphemeralData,
|
persistence: EphemeralData,
|
||||||
schedule: cron.New(),
|
schedule: cron.New(),
|
||||||
world: rove.NewWorld(32),
|
world: rove.NewWorld(32),
|
||||||
accountant: NewSimpleAccountant(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply all options
|
// Apply all options
|
||||||
|
@ -188,7 +184,7 @@ func (s *Server) SaveWorld() error {
|
||||||
if s.persistence == PersistentData {
|
if s.persistence == PersistentData {
|
||||||
s.world.RLock()
|
s.world.RLock()
|
||||||
defer s.world.RUnlock()
|
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)
|
return fmt.Errorf("failed to save out persistent data: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,7 +196,7 @@ func (s *Server) LoadWorld() error {
|
||||||
if s.persistence == PersistentData {
|
if s.persistence == PersistentData {
|
||||||
s.world.Lock()
|
s.world.Lock()
|
||||||
defer s.world.Unlock()
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -214,7 +210,7 @@ func (s *Server) SpawnRoverForAccount(account string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.accountant.AssignData(account, "rover", inst)
|
err = s.world.Accountant.AssignData(account, "rover", inst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to assign rover to account, %s", err)
|
log.Printf("Failed to assign rover to account, %s", err)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package internal
|
package accounts
|
||||||
|
|
||||||
// Accountant decribes something that stores accounts and account values
|
// Accountant decribes something that stores accounts and account values
|
||||||
type Accountant interface {
|
type Accountant interface {
|
|
@ -1,4 +1,4 @@
|
||||||
package internal
|
package accounts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
|
@ -1,4 +1,4 @@
|
||||||
package internal
|
package accounts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/pkg/accounts"
|
||||||
"github.com/mdiluz/rove/pkg/maths"
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
"github.com/mdiluz/rove/proto/roveapi"
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
)
|
)
|
||||||
|
@ -40,6 +41,9 @@ type World struct {
|
||||||
// Commands is the set of currently executing command streams per rover
|
// Commands is the set of currently executing command streams per rover
|
||||||
CommandQueue map[string]CommandStream
|
CommandQueue map[string]CommandStream
|
||||||
|
|
||||||
|
// Accountant
|
||||||
|
Accountant accounts.Accountant
|
||||||
|
|
||||||
// Mutex to lock around all world operations
|
// Mutex to lock around all world operations
|
||||||
worldMutex sync.RWMutex
|
worldMutex sync.RWMutex
|
||||||
// Mutex to lock around command operations
|
// Mutex to lock around command operations
|
||||||
|
@ -54,6 +58,7 @@ func NewWorld(chunkSize int) *World {
|
||||||
Atlas: NewChunkAtlas(chunkSize),
|
Atlas: NewChunkAtlas(chunkSize),
|
||||||
TicksPerDay: 24,
|
TicksPerDay: 24,
|
||||||
CurrentTicks: 0,
|
CurrentTicks: 0,
|
||||||
|
Accountant: accounts.NewSimpleAccountant(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue