Move gametime conversion factors to contrib and remove them from the settings file with the motivation that the gametime constats were always highly game specific and a remnant from another time. Reworked the gametime library and add server epoch building from vincent-lg's original PR. The gametime module now deals exclusively in seconds.
This commit is contained in:
parent
2a48a4d645
commit
008566f358
7 changed files with 96 additions and 229 deletions
|
|
@ -18,7 +18,7 @@ from evennia.server.sessionhandler import SESSIONS
|
||||||
from evennia.scripts.models import ScriptDB
|
from evennia.scripts.models import ScriptDB
|
||||||
from evennia.objects.models import ObjectDB
|
from evennia.objects.models import ObjectDB
|
||||||
from evennia.players.models import PlayerDB
|
from evennia.players.models import PlayerDB
|
||||||
from evennia.utils import logger, utils, gametime, create, prettytable
|
from evennia.utils import logger, utils, gametime, create
|
||||||
from evennia.utils.eveditor import EvEditor
|
from evennia.utils.eveditor import EvEditor
|
||||||
from evennia.utils.evtable import EvTable
|
from evennia.utils.evtable import EvTable
|
||||||
from evennia.utils.utils import crop, class_from_module
|
from evennia.utils.utils import crop, class_from_module
|
||||||
|
|
@ -540,11 +540,10 @@ class CmdService(COMMAND_DEFAULT_CLASS):
|
||||||
if not switches or switches[0] == "list":
|
if not switches or switches[0] == "list":
|
||||||
# Just display the list of installed services and their
|
# Just display the list of installed services and their
|
||||||
# status, then exit.
|
# status, then exit.
|
||||||
table = prettytable.PrettyTable(["{wService{n (use @services/start|stop|delete)", "{wstatus"])
|
table = EvTable("{wService{n (use @services/start|stop|delete)", "{wstatus", align="l")
|
||||||
table.align = 'l'
|
|
||||||
for service in service_collection.services:
|
for service in service_collection.services:
|
||||||
table.add_row([service.name, service.running and "{gRunning" or "{rNot Running"])
|
table.add_row(service.name, service.running and "{gRunning" or "{rNot Running")
|
||||||
caller.msg(str(table))
|
caller.msg(unicode(table))
|
||||||
return
|
return
|
||||||
|
|
||||||
# Get the service to start / stop
|
# Get the service to start / stop
|
||||||
|
|
@ -651,19 +650,18 @@ class CmdTime(COMMAND_DEFAULT_CLASS):
|
||||||
|
|
||||||
def func(self):
|
def func(self):
|
||||||
"Show server time data in a table."
|
"Show server time data in a table."
|
||||||
virtual_epoch = datetime.datetime.fromtimestamp(
|
table1 = EvTable("|wserver time","|wtime", align="l", width=78)
|
||||||
gametime.virtual_epoch())
|
table1.add_row("Current server uptime", utils.time_format(gametime.uptime(), 3))
|
||||||
virtual_current = datetime.datetime.fromtimestamp(
|
table1.add_row("Total server running time", utils.time_format(gametime.runtime(), 2))
|
||||||
gametime.abs_gametime())
|
table1.add_row("Server epoch (first start)", datetime.datetime.fromtimestamp(gametime.server_epoch()))
|
||||||
table = prettytable.PrettyTable(["{wserver time statistic","{wtime"])
|
table1.add_row("Server time stamp", datetime.datetime.now())
|
||||||
table.align = 'l'
|
table1.reformat_column(0, width=30)
|
||||||
table.add_row(["Current server uptime", utils.time_format(gametime.uptime(), 3)])
|
table2 = EvTable("|wgame time", "|wtime (real x %g)" % gametime.TIMEFACTOR, align="l", width=77, border_top=0)
|
||||||
table.add_row(["Total server running time", utils.time_format(gametime.runtime(), 2)])
|
table2.add_row("Game time epoch", datetime.datetime.fromtimestamp(gametime.game_epoch()))
|
||||||
table.add_row(["Game time epoch", virtual_epoch])
|
table2.add_row("Time passed in game:", utils.time_format(gametime.gametime(), 2))
|
||||||
table.add_row(["Total in-game time (realtime x %g)" % (gametime.TIMEFACTOR), utils.time_format(gametime.gametime(), 2)])
|
table2.add_row("Current game time", datetime.datetime.fromtimestamp(gametime.gametime(absolute=True)))
|
||||||
table.add_row(["Current game time", virtual_current])
|
table2.reformat_column(0, width=30)
|
||||||
table.add_row(["Server time stamp", datetime.datetime.now()])
|
self.caller.msg(unicode(table1) + "\n" + unicode(table2))
|
||||||
self.caller.msg(str(table))
|
|
||||||
|
|
||||||
|
|
||||||
class CmdServerLoad(COMMAND_DEFAULT_CLASS):
|
class CmdServerLoad(COMMAND_DEFAULT_CLASS):
|
||||||
|
|
|
||||||
|
|
@ -93,9 +93,9 @@ REGEXMAP = {"morning": (RE_MORNING, RE_AFTERNOON, RE_EVENING, RE_NIGHT),
|
||||||
# set up the seasons and time slots. This assumes gametime started at the
|
# set up the seasons and time slots. This assumes gametime started at the
|
||||||
# beginning of the year (so month 1 is equivalent to January), and that
|
# beginning of the year (so month 1 is equivalent to January), and that
|
||||||
# one CAN divide the game's year into four seasons in the first place ...
|
# one CAN divide the game's year into four seasons in the first place ...
|
||||||
MONTHS_PER_YEAR = settings.TIME_MONTH_PER_YEAR
|
MONTHS_PER_YEAR = 12
|
||||||
SEASONAL_BOUNDARIES = (3 / 12.0, 6 / 12.0, 9 / 12.0)
|
SEASONAL_BOUNDARIES = (3 / 12.0, 6 / 12.0, 9 / 12.0)
|
||||||
HOURS_PER_DAY = settings.TIME_HOUR_PER_DAY
|
HOURS_PER_DAY = 24
|
||||||
DAY_BOUNDARIES = (0, 6 / 24.0, 12 / 24.0, 18 / 24.0)
|
DAY_BOUNDARIES = (0, 6 / 24.0, 12 / 24.0, 18 / 24.0)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,26 +12,11 @@ def check_errors(settings):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
settings (Settings): The Django settings file
|
settings (Settings): The Django settings file
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
DeprecationWarning
|
DeprecationWarning if a critical deprecation is found.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from django.conf import settings
|
|
||||||
def imp(path, split=True):
|
|
||||||
mod, fromlist = path, "None"
|
|
||||||
if split:
|
|
||||||
mod, fromlist = path.rsplit('.', 1)
|
|
||||||
__import__(mod, fromlist=[fromlist])
|
|
||||||
|
|
||||||
# core modules
|
|
||||||
imp(settings.COMMAND_PARSER)
|
|
||||||
imp(settings.SEARCH_AT_RESULT)
|
|
||||||
imp(settings.CONNECTION_SCREEN_MODULE)
|
|
||||||
#imp(settings.AT_INITIAL_SETUP_HOOK_MODULE, split=False)
|
|
||||||
for path in settings.LOCK_FUNC_MODULES:
|
|
||||||
imp(path, split=False)
|
|
||||||
# cmdsets
|
|
||||||
|
|
||||||
deprstring = ("settings.%s should be renamed to %s. If defaults are used, "
|
deprstring = ("settings.%s should be renamed to %s. If defaults are used, "
|
||||||
"their path/classname must be updated "
|
"their path/classname must be updated "
|
||||||
"(see evennia/settings_default.py).")
|
"(see evennia/settings_default.py).")
|
||||||
|
|
@ -72,23 +57,22 @@ def check_errors(settings):
|
||||||
"Update your settings file (see evennia/settings_default.py "
|
"Update your settings file (see evennia/settings_default.py "
|
||||||
"for more info).")
|
"for more info).")
|
||||||
|
|
||||||
from evennia.commands import cmdsethandler
|
gametime_deprecation = ("The settings TIME_SEC_PER_MIN, TIME_MIN_PER_HOUR,"
|
||||||
if not cmdsethandler.import_cmdset(settings.CMDSET_UNLOGGEDIN, None):
|
"TIME_HOUR_PER_DAY, TIME_DAY_PER_WEEK, \n"
|
||||||
print("Warning: CMDSET_UNLOGGED failed to load!")
|
"TIME_WEEK_PER_MONTH and TIME_MONTH_PER_YEAR "
|
||||||
if not cmdsethandler.import_cmdset(settings.CMDSET_CHARACTER, None):
|
"are no longer supported. Remove them from your "
|
||||||
print("Warning: CMDSET_CHARACTER failed to load")
|
"settings file to continue.\nIf you want to use "
|
||||||
if not cmdsethandler.import_cmdset(settings.CMDSET_PLAYER, None):
|
"and manipulate these time units, the tools from utils.gametime "
|
||||||
print("Warning: CMDSET_PLAYER failed to load")
|
"are now found in contrib/convert_gametime.py instead.")
|
||||||
# typeclasses
|
if any(hasattr(settings, value) for value in ("TIME_SEC_PER_MIN", "TIME_MIN_PER_HOUR",
|
||||||
imp(settings.BASE_PLAYER_TYPECLASS)
|
"TIME_HOUR_PER_DAY", "TIME_DAY_PER_WEEK", "TIME_WEEK_PER_MONTH",
|
||||||
imp(settings.BASE_OBJECT_TYPECLASS)
|
"TIME_MONTH_PER_YEAR")):
|
||||||
imp(settings.BASE_CHARACTER_TYPECLASS)
|
raise DeprecationWarning(gametime_deprecation)
|
||||||
imp(settings.BASE_ROOM_TYPECLASS)
|
|
||||||
imp(settings.BASE_EXIT_TYPECLASS)
|
|
||||||
imp(settings.BASE_SCRIPT_TYPECLASS)
|
|
||||||
|
|
||||||
def check_warnings(settings):
|
def check_warnings(settings):
|
||||||
"""
|
"""
|
||||||
Check deprecations that should produce warnings but which
|
Check deprecations that should produce warnings but which
|
||||||
does not stop launch.
|
does not stop launch.
|
||||||
"""
|
"""
|
||||||
|
pass
|
||||||
|
|
|
||||||
|
|
@ -768,14 +768,11 @@ def error_check_python_modules():
|
||||||
python source files themselves). Best they fail already here
|
python source files themselves). Best they fail already here
|
||||||
before we get any further.
|
before we get any further.
|
||||||
|
|
||||||
Raises:
|
|
||||||
DeprecationWarning: For trying to access various modules
|
|
||||||
(usually in `settings.py`) which are no longer supported.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
def imp(path, split=True):
|
def _imp(path, split=True):
|
||||||
|
"helper method"
|
||||||
mod, fromlist = path, "None"
|
mod, fromlist = path, "None"
|
||||||
if split:
|
if split:
|
||||||
mod, fromlist = path.rsplit('.', 1)
|
mod, fromlist = path.rsplit('.', 1)
|
||||||
|
|
@ -783,16 +780,20 @@ def error_check_python_modules():
|
||||||
|
|
||||||
# check the historical deprecations
|
# check the historical deprecations
|
||||||
from evennia.server import deprecations
|
from evennia.server import deprecations
|
||||||
|
try:
|
||||||
deprecations.check_errors(settings)
|
deprecations.check_errors(settings)
|
||||||
deprecations.check_warnings(settings)
|
deprecations.check_warnings(settings)
|
||||||
|
except DeprecationWarning as err:
|
||||||
|
print(err)
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
# core modules
|
# core modules
|
||||||
imp(settings.COMMAND_PARSER)
|
_imp(settings.COMMAND_PARSER)
|
||||||
imp(settings.SEARCH_AT_RESULT)
|
_imp(settings.SEARCH_AT_RESULT)
|
||||||
imp(settings.CONNECTION_SCREEN_MODULE)
|
_imp(settings.CONNECTION_SCREEN_MODULE)
|
||||||
#imp(settings.AT_INITIAL_SETUP_HOOK_MODULE, split=False)
|
#imp(settings.AT_INITIAL_SETUP_HOOK_MODULE, split=False)
|
||||||
for path in settings.LOCK_FUNC_MODULES:
|
for path in settings.LOCK_FUNC_MODULES:
|
||||||
imp(path, split=False)
|
_imp(path, split=False)
|
||||||
|
|
||||||
from evennia.commands import cmdsethandler
|
from evennia.commands import cmdsethandler
|
||||||
if not cmdsethandler.import_cmdset(settings.CMDSET_UNLOGGEDIN, None):
|
if not cmdsethandler.import_cmdset(settings.CMDSET_UNLOGGEDIN, None):
|
||||||
|
|
@ -802,12 +803,12 @@ def error_check_python_modules():
|
||||||
if not cmdsethandler.import_cmdset(settings.CMDSET_PLAYER, None):
|
if not cmdsethandler.import_cmdset(settings.CMDSET_PLAYER, None):
|
||||||
print("Warning: CMDSET_PLAYER failed to load")
|
print("Warning: CMDSET_PLAYER failed to load")
|
||||||
# typeclasses
|
# typeclasses
|
||||||
imp(settings.BASE_PLAYER_TYPECLASS)
|
_imp(settings.BASE_PLAYER_TYPECLASS)
|
||||||
imp(settings.BASE_OBJECT_TYPECLASS)
|
_imp(settings.BASE_OBJECT_TYPECLASS)
|
||||||
imp(settings.BASE_CHARACTER_TYPECLASS)
|
_imp(settings.BASE_CHARACTER_TYPECLASS)
|
||||||
imp(settings.BASE_ROOM_TYPECLASS)
|
_imp(settings.BASE_ROOM_TYPECLASS)
|
||||||
imp(settings.BASE_EXIT_TYPECLASS)
|
_imp(settings.BASE_EXIT_TYPECLASS)
|
||||||
imp(settings.BASE_SCRIPT_TYPECLASS)
|
_imp(settings.BASE_SCRIPT_TYPECLASS)
|
||||||
|
|
||||||
def init_game_directory(path, check_db=True):
|
def init_game_directory(path, check_db=True):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ Everything starts at handle_setup()
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import django
|
import time
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from evennia.players.models import PlayerDB
|
from evennia.players.models import PlayerDB
|
||||||
|
|
@ -160,6 +160,7 @@ def reset_server():
|
||||||
also checks so the warm-reset mechanism works as it should.
|
also checks so the warm-reset mechanism works as it should.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
ServerConfig.objects.conf("server_epoch", time.time())
|
||||||
from evennia.server.sessionhandler import SESSIONS
|
from evennia.server.sessionhandler import SESSIONS
|
||||||
logger.log_info(" Initial setup complete. Restarting Server once.")
|
logger.log_info(" Initial setup complete. Restarting Server once.")
|
||||||
SESSIONS.server.shutdown(mode='reset')
|
SESSIONS.server.shutdown(mode='reset')
|
||||||
|
|
|
||||||
|
|
@ -433,19 +433,12 @@ BASE_BATCHPROCESS_PATHS = ['world', 'evennia.contrib', 'evennia.contrib.tutorial
|
||||||
# The time factor dictates if the game world runs faster (timefactor>1)
|
# The time factor dictates if the game world runs faster (timefactor>1)
|
||||||
# or slower (timefactor<1) than the real world.
|
# or slower (timefactor<1) than the real world.
|
||||||
TIME_FACTOR = 2.0
|
TIME_FACTOR = 2.0
|
||||||
# These measures might or might not make sense to your game world.
|
# The starting point of your game time (the epoch), in seconds.
|
||||||
TIME_SEC_PER_MIN = 60
|
# In Python a value of 0 means Jan 1 1970. All absolute game times
|
||||||
TIME_MIN_PER_HOUR = 60
|
# will be calculated relative to this. Defaults to the first time
|
||||||
TIME_HOUR_PER_DAY = 24
|
# the server was started. This is mainly useful if you want to
|
||||||
TIME_DAY_PER_WEEK = 7
|
# convert to real-world times.
|
||||||
TIME_WEEK_PER_MONTH = 4
|
TIME_GAME_EPOCH = None
|
||||||
TIME_MONTH_PER_YEAR = 12
|
|
||||||
# The initial timestamp of your virtual time (in-game)
|
|
||||||
# You can set this setting to set a fixed, initial timestamp. Your
|
|
||||||
# game time will be this timestamp plus your current variable game time.
|
|
||||||
# You can set this setting to a timestamp in 1980, or 2020, or 2500 if
|
|
||||||
# you want to. Leave it to None to deduce the timestamp from the runtime.
|
|
||||||
TIME_VIRTUAL_START = None
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# Inlinefunc
|
# Inlinefunc
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ from evennia.server.models import ServerConfig
|
||||||
# to real time.
|
# to real time.
|
||||||
|
|
||||||
TIMEFACTOR = settings.TIME_FACTOR
|
TIMEFACTOR = settings.TIME_FACTOR
|
||||||
TIME_VIRTUAL_START = settings.TIME_VIRTUAL_START
|
|
||||||
|
|
||||||
# Only set if gametime_reset was called at some point.
|
# Only set if gametime_reset was called at some point.
|
||||||
GAME_TIME_OFFSET = ServerConfig.objects.conf("gametime_offset", default=0)
|
GAME_TIME_OFFSET = ServerConfig.objects.conf("gametime_offset", default=0)
|
||||||
|
|
@ -22,55 +21,19 @@ GAME_TIME_OFFSET = ServerConfig.objects.conf("gametime_offset", default=0)
|
||||||
# Common real-life time measure, in seconds.
|
# Common real-life time measure, in seconds.
|
||||||
# You should not change this.
|
# You should not change this.
|
||||||
|
|
||||||
REAL_MIN = 60.0 # seconds per minute in real world
|
|
||||||
|
|
||||||
# Game-time units, in real-life seconds. These are supplied as
|
|
||||||
# a convenient measure for determining the current in-game time,
|
|
||||||
# e.g. when defining in-game events. The words month, week and year can
|
|
||||||
# be used to mean whatever units of time are used in the game.
|
|
||||||
|
|
||||||
MIN = settings.TIME_SEC_PER_MIN
|
|
||||||
HOUR = MIN * settings.TIME_MIN_PER_HOUR
|
|
||||||
DAY = HOUR * settings.TIME_HOUR_PER_DAY
|
|
||||||
WEEK = DAY * settings.TIME_DAY_PER_WEEK
|
|
||||||
MONTH = WEEK * settings.TIME_WEEK_PER_MONTH
|
|
||||||
YEAR = MONTH * settings.TIME_MONTH_PER_YEAR
|
|
||||||
|
|
||||||
# these are kept updated by the server maintenance loop
|
# these are kept updated by the server maintenance loop
|
||||||
SERVER_START_TIME = 0.0
|
SERVER_START_TIME = 0.0
|
||||||
SERVER_RUNTIME_LAST_UPDATED = 0.0
|
SERVER_RUNTIME_LAST_UPDATED = 0.0
|
||||||
SERVER_RUNTIME = 0.0
|
SERVER_RUNTIME = 0.0
|
||||||
|
|
||||||
|
# note that these should not be accessed directly since they may
|
||||||
def _format(seconds, *divisors) :
|
# need further processing. Access from server_epoch() and game_epoch().
|
||||||
"""
|
_SERVER_EPOCH = None
|
||||||
Helper function. Creates a tuple of even dividends given a range
|
_GAME_EPOCH = None
|
||||||
of divisors.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
seconds (int): Number of seconds to format
|
|
||||||
*divisors (int): a sequence of numbers of integer dividends. The
|
|
||||||
number of seconds will be integer-divided by the first number in
|
|
||||||
this sequence, the remainder will be divided with the second and
|
|
||||||
so on.
|
|
||||||
Returns:
|
|
||||||
time (tuple): This tuple has length len(*args)+1, with the
|
|
||||||
last element being the last remaining seconds not evenly
|
|
||||||
divided by the supplied dividends.
|
|
||||||
|
|
||||||
"""
|
|
||||||
results = []
|
|
||||||
seconds = int(seconds)
|
|
||||||
for divisor in divisors:
|
|
||||||
results.append(seconds // divisor)
|
|
||||||
seconds %= divisor
|
|
||||||
results.append(seconds)
|
|
||||||
return tuple(results)
|
|
||||||
|
|
||||||
|
|
||||||
# Access functions
|
# Access functions
|
||||||
|
|
||||||
def runtime(format=False):
|
def runtime():
|
||||||
"""
|
"""
|
||||||
Get the total runtime of the server since first start (minus
|
Get the total runtime of the server since first start (minus
|
||||||
downtimes)
|
downtimes)
|
||||||
|
|
@ -83,13 +46,22 @@ def runtime(format=False):
|
||||||
into time units.
|
into time units.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
rtime = SERVER_RUNTIME + (time.time() - SERVER_RUNTIME_LAST_UPDATED)
|
return SERVER_RUNTIME + time.time() - SERVER_RUNTIME_LAST_UPDATED
|
||||||
if format:
|
|
||||||
return _format(rtime, 31536000, 2628000, 604800, 86400, 3600, 60)
|
|
||||||
return rtime
|
|
||||||
|
|
||||||
|
|
||||||
def uptime(format=False):
|
def server_epoch():
|
||||||
|
"""
|
||||||
|
Get the server epoch. We may need to calculate this on the fly.
|
||||||
|
|
||||||
|
"""
|
||||||
|
global _SERVER_EPOCH
|
||||||
|
if not _SERVER_EPOCH:
|
||||||
|
_SERVER_EPOCH = ServerConfig.objects.conf("server_epoch", default=None) \
|
||||||
|
or time.time() - runtime()
|
||||||
|
return _SERVER_EPOCH
|
||||||
|
|
||||||
|
|
||||||
|
def uptime():
|
||||||
"""
|
"""
|
||||||
Get the current uptime of the server since last reload
|
Get the current uptime of the server since last reload
|
||||||
|
|
||||||
|
|
@ -101,130 +73,48 @@ def uptime(format=False):
|
||||||
into time units.
|
into time units.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
utime = time.time() - SERVER_START_TIME
|
return time.time() - SERVER_START_TIME
|
||||||
if format:
|
|
||||||
return _format(utime, 31536000, 2628000, 604800, 86400, 3600, 60)
|
|
||||||
return utime
|
|
||||||
|
|
||||||
def virtual_epoch():
|
|
||||||
"""Return the number of VIRTUAL seconds since the server started.
|
|
||||||
|
|
||||||
This number is set in the TIME_VIRTUAL_START setting. If not
|
def game_epoch():
|
||||||
set, it will be deduced from the current time and server runtime.
|
"""
|
||||||
Notice, in this case, that it will be slightly fluctuant every
|
Get the game epoch.
|
||||||
reload or restart.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The number of virtual seconds since the game first started.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if TIME_VIRTUAL_START is None:
|
return settings.TIME_GAME_EPOCH or server_epoch()
|
||||||
virtual_start = time.time() - runtime()
|
|
||||||
else:
|
|
||||||
virtual_start = TIME_VIRTUAL_START
|
|
||||||
|
|
||||||
return virtual_start
|
|
||||||
|
|
||||||
def abs_gametime():
|
def gametime(absolute=False):
|
||||||
"""Return the absolute number of virtual seconds (in game time).
|
|
||||||
|
|
||||||
This function returns the number of seconds, using your configured
|
|
||||||
virtual start (setting TIME_VIRTUAL_START), and adding the
|
|
||||||
current relative gametime(). If you want to use a standard
|
|
||||||
calendar, it might save you time and efforts. You could easily
|
|
||||||
convert the value like this:
|
|
||||||
>>> from datetime import datetime
|
|
||||||
>>> current = datetime.fromtimestamp(abs_gametime())
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The number of virtual seconds using the virtual epoch as a basis.
|
|
||||||
|
|
||||||
"""
|
|
||||||
virtual_start = virtual_epoch()
|
|
||||||
return virtual_start + gametime()
|
|
||||||
|
|
||||||
def gametime(format=False):
|
|
||||||
"""
|
"""
|
||||||
Get the total gametime of the server since first start (minus downtimes)
|
Get the total gametime of the server since first start (minus downtimes)
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
format (bool, optional): Format into time representation.
|
format (bool, optional): Format into time representation.
|
||||||
|
absolute (bool, optional): Get the absolute game time, including
|
||||||
|
the epoch. This could be converted to an absolute in-game
|
||||||
|
date.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
time (float or tuple): The gametime or the same time split up
|
time (float or tuple): The gametime or the same time split up
|
||||||
into time units.
|
into time units.
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
If one is using a standard calendar, one could convert the unformatted
|
||||||
|
return to a date using Python's standard `datetime` module like this:
|
||||||
|
`datetime.datetime.fromtimestamp(gametime(absolute=True))`
|
||||||
|
|
||||||
"""
|
"""
|
||||||
gtime = (runtime() - GAME_TIME_OFFSET) * TIMEFACTOR
|
epoch = game_epoch() if absolute else 0
|
||||||
if format:
|
gtime = epoch + (runtime() - GAME_TIME_OFFSET) * TIMEFACTOR
|
||||||
return _format(gtime, YEAR, MONTH, WEEK, DAY, HOUR, MIN)
|
|
||||||
return gtime
|
return gtime
|
||||||
|
|
||||||
|
|
||||||
def reset_gametime():
|
def reset_gametime():
|
||||||
"""
|
"""
|
||||||
Resets the game time to make it start from the current time.
|
Resets the game time to make it start from the current time. Note that
|
||||||
|
the epoch set by `settings.TIME_GAME_EPOCH` will still apply.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
global GAME_TIME_OFFSET
|
global GAME_TIME_OFFSET
|
||||||
GAME_TIME_OFFSET = runtime()
|
GAME_TIME_OFFSET = runtime()
|
||||||
ServerConfig.objects.conf("gametime_offset", GAME_TIME_OFFSET)
|
ServerConfig.objects.conf("gametime_offset", GAME_TIME_OFFSET)
|
||||||
|
|
||||||
|
|
||||||
# Conversion functions
|
|
||||||
|
|
||||||
|
|
||||||
def gametime_to_realtime(secs=0, mins=0, hrs=0, days=0,
|
|
||||||
weeks=0, months=0, yrs=0, format=False):
|
|
||||||
"""
|
|
||||||
This method helps to figure out the real-world time it will take until an
|
|
||||||
in-game time has passed. E.g. if an event should take place a month later
|
|
||||||
in-game, you will be able to find the number of real-world seconds this
|
|
||||||
corresponds to (hint: Interval events deal with real life seconds).
|
|
||||||
|
|
||||||
Kwargs:
|
|
||||||
times (int): The various components of the time.
|
|
||||||
format (bool): Formatting the output.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
time (float or tuple): The realtime difference or the same
|
|
||||||
time split up into time units.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
gametime_to_realtime(days=2) -> number of seconds in real life from
|
|
||||||
now after which 2 in-game days will have passed.
|
|
||||||
|
|
||||||
"""
|
|
||||||
rtime = (secs + mins * MIN + hrs * HOUR + days * DAY + weeks * WEEK + \
|
|
||||||
months * MONTH + yrs * YEAR) / TIMEFACTOR
|
|
||||||
if format:
|
|
||||||
return _format(rtime, 31536000, 2628000, 604800, 86400, 3600, 60)
|
|
||||||
return rtime
|
|
||||||
|
|
||||||
|
|
||||||
def realtime_to_gametime(secs=0, mins=0, hrs=0, days=0,
|
|
||||||
weeks=0, months=0, yrs=0, format=False):
|
|
||||||
"""
|
|
||||||
This method calculates how much in-game time a real-world time
|
|
||||||
interval would correspond to. This is usually a lot less
|
|
||||||
interesting than the other way around.
|
|
||||||
|
|
||||||
Kwargs:
|
|
||||||
times (int): The various components of the time.
|
|
||||||
format (bool): Formatting the output.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
time (float or tuple): The gametime difference or the same
|
|
||||||
time split up into time units.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
realtime_to_gametime(days=2) -> number of game-world seconds
|
|
||||||
corresponding to 2 real days.
|
|
||||||
|
|
||||||
"""
|
|
||||||
gtime = TIMEFACTOR * (secs + mins * 60 + hrs * 3600 + days * 86400 +
|
|
||||||
weeks * 604800 + months * 2628000 + yrs * 31536000)
|
|
||||||
if format:
|
|
||||||
return _format(gtime, YEAR, MONTH, WEEK, DAY, HOUR, MIN)
|
|
||||||
return gtime
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue