Allow the server tick to be customised

This commit is contained in:
Marc Di Luzio 2020-06-06 16:01:49 +01:00
parent 573bfbf9c7
commit c6fbf1341a
2 changed files with 30 additions and 12 deletions

View file

@ -41,6 +41,7 @@ type Server struct {
// Config settings
address string
persistence int
tick int
// sync point for sub-threads
sync sync.WaitGroup
@ -66,6 +67,14 @@ func OptionPersistentData() ServerOption {
}
}
// OptionTick defines the number of minutes per tick
// 0 means no automatic server tick
func OptionTick(minutes int) ServerOption {
return func(s *Server) {
s.tick = minutes
}
}
// NewServer sets up a new server
func NewServer(opts ...ServerOption) *Server {
@ -128,19 +137,26 @@ func (s *Server) Addr() string {
func (s *Server) Run() {
defer s.sync.Done()
// Set up the schedule
s.schedule.AddFunc("0,30", func() {
// Ensure we don't quit during this function
s.sync.Add(1)
defer s.sync.Done()
// Set up the schedule if requested
if s.tick != 0 {
if err := s.schedule.AddFunc(fmt.Sprintf("* */%d * * *", s.tick), func() {
// Ensure we don't quit during this function
s.sync.Add(1)
defer s.sync.Done()
// Run the command queues
s.world.ExecuteCommandQueues()
fmt.Println("Execuring server tick")
// Save out the new world state
s.SaveWorld()
})
s.schedule.Start()
// Run the command queues
s.world.ExecuteCommandQueues()
// Save out the new world state
s.SaveWorld()
}); err != nil {
log.Fatal(err)
}
s.schedule.Start()
fmt.Printf("First server tick scheduled for %s\n", s.schedule.Entries()[0].Next.Format("15:04:05"))
}
// Serve the http requests
if err := s.server.Serve(s.listener); err != nil && err != http.ErrServerClosed {