(shutdown chan bool, wg *sync.WaitGroup)
| 320 | ) |
| 321 | |
| 322 | func (w *World) MainWorker(shutdown chan bool, wg *sync.WaitGroup) { |
| 323 | |
| 324 | wg.Add(1) |
| 325 | |
| 326 | mudlog.Info("MainWorker", "state", "Started") |
| 327 | defer func() { |
| 328 | mudlog.Warn("MainWorker", "state", "Stopped") |
| 329 | wg.Done() |
| 330 | }() |
| 331 | |
| 332 | c := configs.GetConfig() |
| 333 | |
| 334 | roomUpdateTimer := time.NewTimer(roomMaintenancePeriod) |
| 335 | ansiAliasTimer := time.NewTimer(ansiAliasReloadPeriod) |
| 336 | eventLoopTimer := time.NewTimer(time.Millisecond) |
| 337 | turnTimer := time.NewTimer(time.Duration(c.Timing.TurnMs) * time.Millisecond) |
| 338 | statsTimer := time.NewTimer(time.Duration(10) * time.Second) |
| 339 | |
| 340 | loop: |
| 341 | for { |
| 342 | |
| 343 | // The reason for |
| 344 | // util.LockGame() / util.UnlockGame() |
| 345 | // In each of these cases is to lock down the |
| 346 | // logic for when other processes need to query data |
| 347 | // such as the webserver |
| 348 | |
| 349 | select { |
| 350 | case <-shutdown: |
| 351 | |
| 352 | mudlog.Warn(`MainWorker`, `action`, `shutdown received`) |
| 353 | |
| 354 | util.LockMud() |
| 355 | if err := rooms.SaveAllRooms(); err != nil { |
| 356 | mudlog.Error("rooms.SaveAllRooms()", "error", err.Error()) |
| 357 | } |
| 358 | users.SaveAllUsers() // Save all user data too. |
| 359 | util.UnlockMud() |
| 360 | |
| 361 | break loop |
| 362 | case <-statsTimer.C: |
| 363 | |
| 364 | // TODO: Move this to events |
| 365 | util.LockMud() |
| 366 | |
| 367 | w.UpdateStats() |
| 368 | // save the round counter. |
| 369 | util.SaveRoundCount(c.FilePaths.DataFiles.String() + `/` + util.RoundCountFilename) |
| 370 | |
| 371 | util.UnlockMud() |
| 372 | |
| 373 | statsTimer.Reset(time.Duration(10) * time.Second) |
| 374 | |
| 375 | case <-roomUpdateTimer.C: |
| 376 | |
| 377 | // TODO: Move this to events |
| 378 | util.LockMud() |
| 379 | scripting.PruneRoomVMs(rooms.RoomMaintenance()...) |
no test coverage detected