diff --git a/cmd/rove-server/main.go b/cmd/rove-server/main.go index fc9000b..11b57ca 100644 --- a/cmd/rove-server/main.go +++ b/cmd/rove-server/main.go @@ -17,6 +17,7 @@ var ver = flag.Bool("version", false, "Display version number") var quit = flag.Int("quit", 0, "Quit after n seconds, useful for testing") var address = flag.String("address", "", "The address to host on, automatically selected if empty") var data = flag.String("data", "", "Directory to store persistant data, no storage if empty") +var tick = flag.Int("tick", 30, "Number of minutes per server tick (0 for no tick)") func InnerMain() { flag.Parse() @@ -35,7 +36,8 @@ func InnerMain() { // Create the server data s := server.NewServer( server.OptionAddress(*address), - server.OptionPersistentData()) + server.OptionPersistentData(), + server.OptionTick(*tick)) // Initialise the server if err := s.Initialise(); err != nil { diff --git a/pkg/server/server.go b/pkg/server/server.go index 3fcfed7..8666ad5 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -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 {