Implement saving and loading for account data, currently a basic json file
This commit is contained in:
parent
f1e6311366
commit
179dd3f984
11 changed files with 306 additions and 130 deletions
|
@ -1,47 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Account represents a registered user
|
||||
type Account struct {
|
||||
// Name simply describes the account and must be unique
|
||||
Name string
|
||||
|
||||
// id represents a unique ID per account and is set one registered
|
||||
id uuid.UUID
|
||||
}
|
||||
|
||||
// Accountant manages a set of accounts
|
||||
type Accountant struct {
|
||||
accounts []Account
|
||||
}
|
||||
|
||||
// NewAccountant creates a new accountant
|
||||
func NewAccountant() *Accountant {
|
||||
return &Accountant{}
|
||||
}
|
||||
|
||||
// RegisterAccount adds an account to the set of internal accounts
|
||||
func (a *Accountant) RegisterAccount(acc Account) (Account, error) {
|
||||
|
||||
// Set the account ID to a new UUID
|
||||
acc.id = uuid.New()
|
||||
|
||||
// Verify this acount isn't already registered
|
||||
for _, a := range a.accounts {
|
||||
if a.Name == acc.Name {
|
||||
return Account{}, fmt.Errorf("Account name already registered")
|
||||
} else if a.id == acc.id {
|
||||
return Account{}, fmt.Errorf("Account ID already registered")
|
||||
}
|
||||
}
|
||||
|
||||
// Simply add the account to the list
|
||||
a.accounts = append(a.accounts, acc)
|
||||
|
||||
return acc, nil
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewAccountant(t *testing.T) {
|
||||
// Very basic verify here for now
|
||||
accountant := NewAccountant()
|
||||
if accountant == nil {
|
||||
t.Error("Failed to create accountant")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountant_RegisterAccount(t *testing.T) {
|
||||
|
||||
accountant := NewAccountant()
|
||||
|
||||
// Start by making two accounts
|
||||
|
||||
namea := "one"
|
||||
a := Account{Name: namea}
|
||||
acca, err := accountant.RegisterAccount(a)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if acca.Name != namea {
|
||||
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", namea, acca.Name)
|
||||
}
|
||||
|
||||
nameb := "two"
|
||||
b := Account{Name: nameb}
|
||||
accb, err := accountant.RegisterAccount(b)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if accb.Name != nameb {
|
||||
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
|
||||
}
|
||||
|
||||
// Verify our accounts have differing IDs
|
||||
if acca.id == accb.id {
|
||||
t.Error("Duplicate account IDs fo separate accounts")
|
||||
}
|
||||
|
||||
// Verify another request gets rejected
|
||||
_, err = accountant.RegisterAccount(a)
|
||||
if err == nil {
|
||||
t.Error("Duplicate account name did not produce error")
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
)
|
||||
|
||||
// NewRouter sets up the server mux
|
||||
|
@ -24,7 +25,7 @@ type StatusResponse struct {
|
|||
|
||||
// HandleStatus handles HTTP requests to the /status endpoint
|
||||
func (s *Server) HandleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("%s\t%s", r.Method, r.RequestURI)
|
||||
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
|
||||
|
||||
// Verify we're hit with a get request
|
||||
if r.Method != http.MethodGet {
|
||||
|
@ -59,7 +60,12 @@ type RegisterResponse struct {
|
|||
|
||||
// HandleRegister handles HTTP requests to the /register endpoint
|
||||
func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("%s\t%s", r.Method, r.RequestURI)
|
||||
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
|
||||
|
||||
// Set up the response
|
||||
var response = RegisterResponse{
|
||||
Success: false,
|
||||
}
|
||||
|
||||
// Verify we're hit with a get request
|
||||
if r.Method != http.MethodPost {
|
||||
|
@ -69,23 +75,26 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// Pull out the registration info
|
||||
var data RegisterData
|
||||
json.NewDecoder(r.Body).Decode(&data)
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to decode json: %s", err)
|
||||
|
||||
// Register the account with the server
|
||||
acc := Account{Name: data.Name}
|
||||
acc, err := s.accountant.RegisterAccount(acc)
|
||||
|
||||
// Set up the response
|
||||
var response = RegisterResponse{
|
||||
Success: false,
|
||||
}
|
||||
|
||||
// If we didn't fail, respond with the account ID string
|
||||
if err == nil {
|
||||
response.Success = true
|
||||
response.Id = acc.id.String()
|
||||
} else {
|
||||
response.Error = err.Error()
|
||||
} else {
|
||||
// log the data sent
|
||||
fmt.Printf("\t%v\n", data)
|
||||
|
||||
// Register the account with the server
|
||||
acc := accounts.Account{Name: data.Name}
|
||||
acc, err := s.accountant.RegisterAccount(acc)
|
||||
|
||||
// If we didn't fail, respond with the account ID string
|
||||
if err == nil {
|
||||
response.Success = true
|
||||
response.Id = acc.Id.String()
|
||||
} else {
|
||||
response.Error = err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
// Be a good citizen and set the header for the return
|
||||
|
|
|
@ -12,7 +12,7 @@ func TestHandleStatus(t *testing.T) {
|
|||
request, _ := http.NewRequest(http.MethodGet, "/status", nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s := NewServer(8080)
|
||||
s := NewServer()
|
||||
s.Initialise()
|
||||
|
||||
s.HandleStatus(response, request)
|
||||
|
@ -35,7 +35,7 @@ func TestHandleRegister(t *testing.T) {
|
|||
request, _ := http.NewRequest(http.MethodPost, "/register", bytes.NewReader(b))
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s := NewServer(8080)
|
||||
s := NewServer()
|
||||
s.Initialise()
|
||||
|
||||
s.HandleRegister(response, request)
|
||||
|
|
|
@ -6,37 +6,84 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
"github.com/mdiluz/rove/pkg/game"
|
||||
)
|
||||
|
||||
const (
|
||||
// PersistentData will allow the server to load and save it's state
|
||||
PersistentData = iota
|
||||
|
||||
// EphemeralData will let the server neither load or save out any of it's data
|
||||
EphemeralData
|
||||
)
|
||||
|
||||
// Server contains the relevant data to run a game server
|
||||
type Server struct {
|
||||
port int
|
||||
|
||||
accountant *Accountant
|
||||
accountant *accounts.Accountant
|
||||
world *game.World
|
||||
|
||||
router *mux.Router
|
||||
|
||||
persistence int
|
||||
}
|
||||
|
||||
// NewServer sets up a new server
|
||||
func NewServer(port int) *Server {
|
||||
return &Server{
|
||||
port: port,
|
||||
accountant: NewAccountant(),
|
||||
world: game.NewWorld(),
|
||||
// ServerOption defines a server creation option
|
||||
type ServerOption func(s *Server)
|
||||
|
||||
// OptionPort sets the server port for hosting
|
||||
func OptionPort(port int) ServerOption {
|
||||
return func(s *Server) {
|
||||
s.port = port
|
||||
}
|
||||
}
|
||||
|
||||
// OptionPersistentData sets the server data to be persistent
|
||||
func OptionPersistentData() ServerOption {
|
||||
return func(s *Server) {
|
||||
s.persistence = PersistentData
|
||||
}
|
||||
}
|
||||
|
||||
// NewServer sets up a new server
|
||||
func NewServer(opts ...ServerOption) *Server {
|
||||
|
||||
// Set up the default server
|
||||
s := &Server{
|
||||
port: 8080,
|
||||
accountant: accounts.NewAccountant(),
|
||||
world: game.NewWorld(),
|
||||
persistence: EphemeralData,
|
||||
}
|
||||
|
||||
// Apply all options
|
||||
for _, o := range opts {
|
||||
o(s)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Initialise sets up internal state ready to serve
|
||||
func (s *Server) Initialise() {
|
||||
func (s *Server) Initialise() error {
|
||||
// Set up the world
|
||||
s.world = game.NewWorld()
|
||||
fmt.Printf("World created\n\t%+v\n", s.world)
|
||||
|
||||
// Load the accounts if requested
|
||||
if s.persistence == PersistentData {
|
||||
if err := s.accountant.Load(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new router
|
||||
s.SetUpRouter()
|
||||
fmt.Printf("Routes Created\n")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run executes the server
|
||||
|
@ -47,3 +94,15 @@ func (s *Server) Run() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes up the server
|
||||
func (s *Server) Close() error {
|
||||
|
||||
// Save the accounts if requested
|
||||
if s.persistence == PersistentData {
|
||||
if err := s.accountant.Save(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue