Allow the server tick to be customised
This commit is contained in:
parent
573bfbf9c7
commit
c6fbf1341a
2 changed files with 30 additions and 12 deletions
|
@ -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 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 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 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() {
|
func InnerMain() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -35,7 +36,8 @@ func InnerMain() {
|
||||||
// Create the server data
|
// Create the server data
|
||||||
s := server.NewServer(
|
s := server.NewServer(
|
||||||
server.OptionAddress(*address),
|
server.OptionAddress(*address),
|
||||||
server.OptionPersistentData())
|
server.OptionPersistentData(),
|
||||||
|
server.OptionTick(*tick))
|
||||||
|
|
||||||
// Initialise the server
|
// Initialise the server
|
||||||
if err := s.Initialise(); err != nil {
|
if err := s.Initialise(); err != nil {
|
||||||
|
|
|
@ -41,6 +41,7 @@ type Server struct {
|
||||||
// Config settings
|
// Config settings
|
||||||
address string
|
address string
|
||||||
persistence int
|
persistence int
|
||||||
|
tick int
|
||||||
|
|
||||||
// sync point for sub-threads
|
// sync point for sub-threads
|
||||||
sync sync.WaitGroup
|
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
|
// NewServer sets up a new server
|
||||||
func NewServer(opts ...ServerOption) *Server {
|
func NewServer(opts ...ServerOption) *Server {
|
||||||
|
|
||||||
|
@ -128,19 +137,26 @@ func (s *Server) Addr() string {
|
||||||
func (s *Server) Run() {
|
func (s *Server) Run() {
|
||||||
defer s.sync.Done()
|
defer s.sync.Done()
|
||||||
|
|
||||||
// Set up the schedule
|
// Set up the schedule if requested
|
||||||
s.schedule.AddFunc("0,30", func() {
|
if s.tick != 0 {
|
||||||
// Ensure we don't quit during this function
|
if err := s.schedule.AddFunc(fmt.Sprintf("* */%d * * *", s.tick), func() {
|
||||||
s.sync.Add(1)
|
// Ensure we don't quit during this function
|
||||||
defer s.sync.Done()
|
s.sync.Add(1)
|
||||||
|
defer s.sync.Done()
|
||||||
|
|
||||||
// Run the command queues
|
fmt.Println("Execuring server tick")
|
||||||
s.world.ExecuteCommandQueues()
|
|
||||||
|
|
||||||
// Save out the new world state
|
// Run the command queues
|
||||||
s.SaveWorld()
|
s.world.ExecuteCommandQueues()
|
||||||
})
|
|
||||||
s.schedule.Start()
|
// 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
|
// Serve the http requests
|
||||||
if err := s.server.Serve(s.listener); err != nil && err != http.ErrServerClosed {
|
if err := s.server.Serve(s.listener); err != nil && err != http.ErrServerClosed {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue