Simplify - remove rove-accountant

This was a fun little gRPC experiment but it's simply not needed
This commit is contained in:
Marc Di Luzio 2020-06-30 23:34:49 +01:00
parent 984ff56664
commit abcebcebb6
12 changed files with 20 additions and 930 deletions

View file

@ -4,11 +4,9 @@ import (
"context"
"fmt"
"github.com/mdiluz/rove/pkg/accounts"
"github.com/mdiluz/rove/pkg/game"
"github.com/mdiluz/rove/pkg/rove"
"github.com/mdiluz/rove/pkg/version"
"google.golang.org/grpc"
)
func (s *Server) Status(context.Context, *rove.StatusRequest) (*rove.StatusResponse, error) {
@ -33,7 +31,7 @@ func (s *Server) Register(ctx context.Context, req *rove.RegisterRequest) (*rove
return nil, fmt.Errorf("empty account name")
}
if _, err := s.accountant.Register(ctx, &accounts.RegisterInfo{Name: req.Name}, grpc.WaitForReady(true)); err != nil {
if _, err := s.accountant.RegisterAccount(req.Name); err != nil {
return nil, err
} else if _, err := s.SpawnRoverForAccount(req.Name); err != nil {
@ -51,10 +49,10 @@ func (s *Server) Rover(ctx context.Context, req *rove.RoverRequest) (*rove.Rover
if len(req.Account) == 0 {
return nil, fmt.Errorf("empty account name")
} else if resp, err := s.accountant.GetValue(ctx, &accounts.DataKey{Account: req.Account, Key: "rover"}); err != nil {
return nil, fmt.Errorf("gRPC failed to contact accountant: %s", err)
} else if resp, err := s.accountant.GetValue(req.Account, "rover"); err != nil {
return nil, err
} else if rover, err := s.world.GetRover(resp.Value); err != nil {
} else if rover, err := s.world.GetRover(resp); err != nil {
return nil, fmt.Errorf("error getting rover: %s", err)
} else {
@ -79,14 +77,14 @@ func (s *Server) Radar(ctx context.Context, req *rove.RadarRequest) (*rove.Radar
response := &rove.RadarResponse{}
resp, err := s.accountant.GetValue(ctx, &accounts.DataKey{Account: req.Account, Key: "rover"})
resp, err := s.accountant.GetValue(req.Account, "rover")
if err != nil {
return nil, fmt.Errorf("gRPC failed to contact accountant: %s", err)
return nil, err
} else if rover, err := s.world.GetRover(resp.Value); err != nil {
} else if rover, err := s.world.GetRover(resp); err != nil {
return nil, fmt.Errorf("error getting rover attributes: %s", err)
} else if radar, err := s.world.RadarFromRover(resp.Value); err != nil {
} else if radar, err := s.world.RadarFromRover(resp); err != nil {
return nil, fmt.Errorf("error getting radar from rover: %s", err)
} else {
@ -101,8 +99,7 @@ func (s *Server) Commands(ctx context.Context, req *rove.CommandsRequest) (*rove
if len(req.Account) == 0 {
return nil, fmt.Errorf("empty account")
}
resp, err := s.accountant.GetValue(ctx, &accounts.DataKey{Account: req.Account, Key: "rover"})
resp, err := s.accountant.GetValue(req.Account, "rover")
if err != nil {
return nil, err
}
@ -114,7 +111,7 @@ func (s *Server) Commands(ctx context.Context, req *rove.CommandsRequest) (*rove
Command: c.Command})
}
if err := s.world.Enqueue(resp.Value, cmds...); err != nil {
if err := s.world.Enqueue(resp, cmds...); err != nil {
return nil, err
}

View file

@ -1,11 +1,9 @@
package internal
import (
"context"
"fmt"
"log"
"net"
"os"
"sync"
"github.com/mdiluz/rove/pkg/accounts"
@ -30,9 +28,8 @@ type Server struct {
// Internal state
world *game.World
// Accountant server
accountant accounts.AccountantClient
clientConn *grpc.ClientConn
// Accountant
accountant *accounts.Accountant
// gRPC server
netListener net.Listener
@ -84,6 +81,7 @@ func NewServer(opts ...ServerOption) *Server {
persistence: EphemeralData,
schedule: cron.New(),
world: game.NewWorld(32),
accountant: accounts.NewAccountant(),
}
// Apply all options
@ -100,18 +98,6 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
// Add to our sync
s.sync.Add(1)
// Connect to the accountant
accountantAddress := os.Getenv("ROVE_ACCOUNTANT_GRPC")
if len(accountantAddress) == 0 {
accountantAddress = "localhost:9091"
}
log.Printf("Dialing accountant on %s\n", accountantAddress)
s.clientConn, err = grpc.Dial(accountantAddress, grpc.WithInsecure())
if err != nil {
return err
}
s.accountant = accounts.NewAccountantClient(s.clientConn)
// Load the world file
if err := s.LoadWorld(); err != nil {
return err
@ -173,11 +159,6 @@ func (s *Server) Stop() error {
// Stop the gRPC
s.grpcServ.Stop()
// Close the accountant connection
if err := s.clientConn.Close(); err != nil {
return err
}
return nil
}
@ -190,7 +171,7 @@ func (s *Server) Close() error {
return s.SaveWorld()
}
// Close waits until the server is finished and closes up shop
// StopAndClose waits until the server is finished and closes up shop
func (s *Server) StopAndClose() error {
// Stop the server
if err := s.Stop(); err != nil {
@ -225,19 +206,12 @@ func (s *Server) LoadWorld() error {
return nil
}
// used as the type for the return struct
type BadRequestError struct {
Error string `json:"error"`
}
// SpawnRoverForAccount spawns the rover rover for an account
func (s *Server) SpawnRoverForAccount(account string) (string, error) {
if inst, err := s.world.SpawnRover(); err != nil {
return "", err
} else {
keyval := accounts.DataKeyValue{Account: account, Key: "rover", Value: inst}
_, err := s.accountant.AssignValue(context.Background(), &keyval)
err := s.accountant.AssignData(account, "rover", inst)
if err != nil {
log.Printf("Failed to assign rover to account, %s", err)

View file

@ -1,7 +1,6 @@
package internal
import (
"os"
"testing"
)
@ -31,7 +30,6 @@ func TestNewServer_OptionPersistentData(t *testing.T) {
}
func TestServer_Run(t *testing.T) {
os.Setenv("ROVE_ACCOUNTANT_GRPC", "n/a")
server := NewServer()
if server == nil {
t.Error("Failed to create server")
@ -47,7 +45,6 @@ func TestServer_Run(t *testing.T) {
}
func TestServer_RunPersistentData(t *testing.T) {
os.Setenv("ROVE_ACCOUNTANT_GRPC", "n/a")
server := NewServer(OptionPersistentData())
if server == nil {
t.Error("Failed to create server")