Serialise the World as well

This commit is contained in:
Marc Di Luzio 2020-06-02 17:51:54 +01:00
parent 50c970fea2
commit 68d117e0d8
5 changed files with 76 additions and 31 deletions

View file

@ -63,7 +63,6 @@ func NewServer(opts ...ServerOption) *Server {
// Set up the default server
s := &Server{
port: 8080,
world: game.NewWorld(),
persistence: EphemeralData,
router: router,
}
@ -78,21 +77,22 @@ func NewServer(opts ...ServerOption) *Server {
// Create the accountant
s.accountant = accounts.NewAccountant(s.persistenceLocation)
s.world = game.NewWorld(s.persistenceLocation)
return s
}
// Initialise sets up internal state ready to serve
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
}
if err := s.world.Load(); err != nil {
return err
}
}
// Create a new router
@ -133,6 +133,9 @@ func (s *Server) Close() error {
if err := s.accountant.Save(); err != nil {
return err
}
if err := s.world.Save(); err != nil {
return err
}
}
return nil
}