Restart service when last player disconnects

Revision as of 17:17, 18 January 2018 by TCAWiki (talk | contribs) (Created page with "=== Create the scripts === Go to the game's settings. Click on the Custom Scripts icon. Add the following script. This can also be configured as a global script. '''Operatin...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Create the scripts

Go to the game's settings. Click on the Custom Scripts icon. Add the following script. This can also be configured as a global script.

Operating System: Any
Description: Restart game server after last player disconnects
Script Engine: IronPython
Event: Query Monitoring
Ignore execution errors Checked
Script:
import clr
import System
from System import String

if QueryResults.Running == False :
  Script.Exit()

NumPlayers = QueryResults.NumPlayers
LastNumPlayers = 0

if ThisService.Variables.HasValue("LastNumPlayers") :
  LastNumPlayers = ThisService.Variables["LastNumPlayers"]

Script.WriteToConsole(String.Format("{0} - Previous Players: {1} Current Players: {2}", ThisService.ConnectionInfo, LastNumPlayers, NumPlayers))

if LastNumPlayers > 0 and NumPlayers == 0 :
  Script.WriteToConsole(String.Format("{0} - Last player disconnected. Restarting...", ThisService.ConnectionInfo))
  ThisService.Restart()

if NumPlayers != LastNumPlayers or ThisService.Variables.HasValue("LastNumPlayers") == False:
  ThisService.Variables["LastNumPlayers"] = NumPlayers
  ThisService.Save()
Retrieved from "https://help.tcadmin.com/index.php?title=Restart_service_when_last_player_disconnects&oldid=1437"