2020-05-31 11:18:26 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-05-31 19:48:43 +01:00
|
|
|
"context"
|
2020-05-31 11:18:26 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-05-31 19:48:43 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-05-31 11:18:26 +01:00
|
|
|
|
2020-06-03 18:40:19 +01:00
|
|
|
"github.com/google/uuid"
|
2020-05-31 11:18:26 +01:00
|
|
|
"github.com/gorilla/mux"
|
2020-05-31 19:15:57 +01:00
|
|
|
"github.com/mdiluz/rove/pkg/accounts"
|
2020-05-31 11:18:26 +01:00
|
|
|
"github.com/mdiluz/rove/pkg/game"
|
2020-06-02 19:16:02 +01:00
|
|
|
"github.com/mdiluz/rove/pkg/persistence"
|
2020-05-31 11:18:26 +01:00
|
|
|
)
|
|
|
|
|
2020-05-31 19:15:57 +01:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2020-05-31 11:18:26 +01:00
|
|
|
// Server contains the relevant data to run a game server
|
|
|
|
type Server struct {
|
|
|
|
port int
|
|
|
|
|
2020-05-31 19:15:57 +01:00
|
|
|
accountant *accounts.Accountant
|
2020-05-31 11:18:26 +01:00
|
|
|
world *game.World
|
|
|
|
|
2020-05-31 19:48:43 +01:00
|
|
|
server *http.Server
|
2020-05-31 11:18:26 +01:00
|
|
|
router *mux.Router
|
2020-05-31 19:15:57 +01:00
|
|
|
|
2020-06-04 17:21:23 +01:00
|
|
|
persistence int
|
2020-05-31 19:48:43 +01:00
|
|
|
|
|
|
|
sync sync.WaitGroup
|
2020-05-31 19:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-06-02 19:16:02 +01:00
|
|
|
func OptionPersistentData() ServerOption {
|
2020-05-31 19:15:57 +01:00
|
|
|
return func(s *Server) {
|
|
|
|
s.persistence = PersistentData
|
|
|
|
}
|
2020-05-31 11:18:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer sets up a new server
|
2020-05-31 19:15:57 +01:00
|
|
|
func NewServer(opts ...ServerOption) *Server {
|
|
|
|
|
2020-05-31 19:48:43 +01:00
|
|
|
router := mux.NewRouter().StrictSlash(true)
|
|
|
|
|
2020-05-31 19:15:57 +01:00
|
|
|
// Set up the default server
|
|
|
|
s := &Server{
|
|
|
|
port: 8080,
|
|
|
|
persistence: EphemeralData,
|
2020-05-31 19:48:43 +01:00
|
|
|
router: router,
|
2020-05-31 19:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply all options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(s)
|
2020-05-31 11:18:26 +01:00
|
|
|
}
|
2020-05-31 19:15:57 +01:00
|
|
|
|
2020-06-01 18:10:25 +01:00
|
|
|
// Set up the server object
|
|
|
|
s.server = &http.Server{Addr: fmt.Sprintf(":%d", s.port), Handler: router}
|
|
|
|
|
2020-06-02 16:10:45 +01:00
|
|
|
// Create the accountant
|
2020-06-02 19:16:02 +01:00
|
|
|
s.accountant = accounts.NewAccountant()
|
|
|
|
s.world = game.NewWorld()
|
2020-06-02 16:10:45 +01:00
|
|
|
|
2020-05-31 19:15:57 +01:00
|
|
|
return s
|
2020-05-31 11:18:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise sets up internal state ready to serve
|
2020-05-31 19:15:57 +01:00
|
|
|
func (s *Server) Initialise() error {
|
2020-05-31 11:18:26 +01:00
|
|
|
|
2020-05-31 19:15:57 +01:00
|
|
|
// Load the accounts if requested
|
|
|
|
if s.persistence == PersistentData {
|
2020-06-02 19:16:02 +01:00
|
|
|
if err := persistence.LoadAll("accounts", &s.accountant, "world", &s.world); err != nil {
|
2020-06-02 17:51:54 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-05-31 19:15:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 18:10:55 +01:00
|
|
|
// Set up the handlers
|
|
|
|
for _, route := range Routes {
|
|
|
|
s.router.HandleFunc(route.path, s.wrapHandler(route.method, route.handler))
|
|
|
|
}
|
2020-05-31 19:15:57 +01:00
|
|
|
|
2020-05-31 19:48:43 +01:00
|
|
|
// Add to our sync
|
|
|
|
s.sync.Add(1)
|
|
|
|
|
2020-05-31 19:15:57 +01:00
|
|
|
return nil
|
2020-05-31 11:18:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the server
|
|
|
|
func (s *Server) Run() {
|
2020-05-31 19:48:43 +01:00
|
|
|
defer s.sync.Done()
|
|
|
|
|
2020-05-31 11:18:26 +01:00
|
|
|
// Listen and serve the http requests
|
2020-06-01 18:10:25 +01:00
|
|
|
fmt.Printf("Serving HTTP on port %d\n", s.port)
|
2020-05-31 19:48:43 +01:00
|
|
|
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
2020-05-31 11:18:26 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 19:15:57 +01:00
|
|
|
|
|
|
|
// Close closes up the server
|
|
|
|
func (s *Server) Close() error {
|
2020-05-31 19:48:43 +01:00
|
|
|
// Try and shut down the http server
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if err := s.server.Shutdown(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the server is shut down
|
|
|
|
s.sync.Wait()
|
2020-05-31 19:15:57 +01:00
|
|
|
|
|
|
|
// Save the accounts if requested
|
|
|
|
if s.persistence == PersistentData {
|
2020-06-02 19:16:02 +01:00
|
|
|
if err := persistence.SaveAll("accounts", s.accountant, "world", s.world); err != nil {
|
2020-06-02 17:51:54 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-05-31 19:15:57 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-03 18:40:19 +01:00
|
|
|
|
2020-06-04 17:53:25 +01:00
|
|
|
// wrapHandler wraps a request handler in http checks
|
|
|
|
func (s *Server) wrapHandler(method string, handler Handler) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Log the request
|
|
|
|
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
|
|
|
|
|
|
|
|
// Verify we're hit with the right method
|
|
|
|
if r.Method != method {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
|
|
|
} else if err := handler(s, r.Body, w); err != nil {
|
|
|
|
// Log the error
|
|
|
|
fmt.Printf("Failed to handle http request: %s", err)
|
|
|
|
|
|
|
|
// Respond that we've had an error
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Be a good citizen and set the header for the return
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// SpawnRoverForAccount spawns the rover rover for an account
|
|
|
|
func (s *Server) SpawnRoverForAccount(accountid uuid.UUID) (game.Vector, uuid.UUID, error) {
|
2020-06-03 18:40:19 +01:00
|
|
|
inst := uuid.New()
|
2020-06-04 21:17:43 +01:00
|
|
|
s.world.SpawnRover(inst)
|
2020-06-03 18:40:19 +01:00
|
|
|
if pos, err := s.world.GetPosition(inst); err != nil {
|
2020-06-04 21:17:43 +01:00
|
|
|
return game.Vector{}, uuid.UUID{}, fmt.Errorf("No position found for created rover")
|
2020-06-03 18:40:19 +01:00
|
|
|
|
|
|
|
} else {
|
2020-06-04 21:17:43 +01:00
|
|
|
if err := s.accountant.AssignRover(accountid, inst); err != nil {
|
|
|
|
// Try and clear up the rover
|
|
|
|
if err := s.world.DestroyRover(inst); err != nil {
|
|
|
|
fmt.Printf("Failed to destroy rover after failed rover assign: %s", err)
|
2020-06-03 18:40:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return game.Vector{}, uuid.UUID{}, err
|
|
|
|
} else {
|
|
|
|
return pos, inst, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|