Scripts:AutoShutdown

From BF2 Technical Information Wiki
Revision as of 15:26, 22 June 2018 by Pireax (talk | contribs) (Created page with "== Introduction == I got tired of manually warning my users before shutdown, so I wrote this to automate the task for me. It warns players at intervals before shutting down....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

I got tired of manually warning my users before shutdown, so I wrote this to automate the task for me. It warns players at intervals before shutting down. The warnings occur at 60, 30, 15, 10, 5, 4, 3, 2, 1 minutes before shutdown. This script needs serious work from someone who actually knows Python. I post it here in the hopes that people will improve on it (perhaps by moving the configuration variables into an easily-modified external file?)


Usage

Copy/Paste this script into a text file. Save it as autoshutdown.py in your standard_admin folder. Modify your __init__.py file to import and execute it.

Code

import datetime
import time
import host
import bf2
from bf2 import g_debug

shutdownTime = "01:00" #use 24-hour time. Format should be "hh:mm"
quitOrPause = "pause" #options are "pause" (pause game but leave server running) or "quit" (kill the server)

shutdownTimer = None
def init():
    global shutdownTime, shutdownTimer
    shutdownHour = int(time.strftime("%H",time.strptime(shutdownTime,"%H:%M")))
    shutdownMinute = int(time.strftime("%M",time.strptime(shutdownTime,"%H:%M")))
    today = datetime.datetime.now()
    shutdownTime = datetime.datetime(today.year, today.month, today.day, shutdownHour, shutdownMinute)
    if shutdownHour < today.hour:
        shutdownTime = datetime.datetime(today.year, today.month, today.day + 1, shutdownHour, shutdownMinute)
    shutdownTimer = bf2.Timer(checktime, 1, 1)
    shutdownTimer.setRecurring(60)
    host.rcon_invoke('echo "autoshutdown.py loaded"')

def checktime(whatever):
    global shutdownTime
    global quitOrPause
   
    timeTilShutdown = shutdownTime - datetime.datetime.now()
    minsTilShutdown = int((timeTilShutdown.seconds / 60))
    host.rcon_invoke('echo "Debug: ' + str(minsTilShutdown) + ' minutes to shutdown."')
    msg = None
    if minsTilShutdown == 60:
        msg = 'ADMIN: SERVER SHUTDOWN IN 60 MINUTES'
    if minsTilShutdown == 30:
        msg = 'ADMIN: SERVER SHUTDOWN IN 30 MINUTES'
    if minsTilShutdown == 15:
        msg = 'ADMIN: SERVER SHUTDOWN IN 15 MINUTES'
    if minsTilShutdown == 10:
        msg = 'ADMIN: SERVER SHUTDOWN IN 10 MINUTES'
    if minsTilShutdown == 5:
        msg = 'ADMIN: SERVER SHUTDOWN IN 5 MINUTES'
    if 0 < minsTilShutdown < 5:
        msg = 'ADMIN: SERVER SHUTDOWN IN ' + str(minsTilShutdown) + ' MINUTE(S)'
       
    if msg != None:
        adminMessage(msg)
       
    if minsTilShutdown <= 0:
        msg = 'ADMIN: SERVER WILL SHUTDOWN *NOW*'
        adminMessage(msg)
        timestamp = time.strftime("%Y-%m-%d, %I:%M:%S %p",time.localtime())
        if quitOrPause == "quit":
            if g_debug: print timestamp + ' -- Server halted.'
            host.rcon_invoke('quit')
        if quitOrPause == "pause":
            host.rcon_invoke('echo "' + timestamp + ' -- Game paused."')
            if g_debug: print timestamp + ' -- Game paused.'
            host.rcon_invoke('gamelogic.togglepause')

def adminMessage(msg):
    for i in range(1,26):
        host.rcon_invoke('game.sayAll "' + msg + '"')
    host.rcon_invoke('echo "' + msg + '"')
    if g_debug: print msg
Greeze 07:50, 1 Jul 2005 (MDT)
Greeze 04:13, 2 Jul 2005 (MDT) --
  • Removed game status check (was creating multiple timer instances everytime the map changed); moved timer creation to init()
  • Slightly reordered message-sends in the checkTime function