evennia/src/imc2/events.py
Griatch 1d4f075ca7 * Updated and expanded the State system; the API changed a bit. You now have to first *create* the state using
GLOBAL_STATE_TABLE.add_state() before adding commands to it. The state can now be much more configured by including as much or as
little of the normal default commands into it as wanted (so you can now have states which are almost as normal, except some
commands are missing or change their behaviour ... illness or darkness comes to mind). The possibilities here are limitless.
* States now also optionally allow traversing exits as well as using command tables defined on objects.
* States now better handle error messages (so if you try 'look' in a state which does not contain a look command you will no
longer get the 'Huh?' but will be told that the command is not available at the moment).
* All examples in commands/examples/ have been updated to use the new State system. Also added a @test_state function for trying out
the functionality.
* Added hooks at_before_move() and at_after_move(), useful for character based move-restrictions and checks (e.g. movement speed)
* Minor tweaks to the event system; avoiding the counters to go negative should they hit an uncaught traceback.
* Small fixes of typos and minor extra safety checks.
/Griatch
2009-08-16 01:18:58 +00:00

85 lines
3.3 KiB
Python

"""
This module contains all IMC2 events that are triggered periodically.
Most of these are used to maintain the existing connection and keep various
lists/caches up to date.
"""
from time import time
from src import events
from src import scheduler
from src.imc2 import connection as imc2_conn
from src.imc2.packets import *
from src.imc2.trackers import IMC2_MUDLIST
class IEvt_IMC2_Send_IsAlive(events.IntervalEvent):
"""
Event: Send periodic keepalives to network neighbors. This lets the other
games know that our game is still up and connected to the network. Also
provides some useful information about the client game.
"""
def __init__(self):
super(IEvt_IMC2_Send_IsAlive, self).__init__()
self.name = 'IEvt_IMC2_Send_IsAlive'
# Send keep-alive packets every 15 minutes.
self.interval = 900
self.description = "Send an IMC2 is-alive packet."
def event_function(self):
"""
This is the function that is fired every self.interval seconds.
"""
try:
imc2_conn.IMC2_PROTOCOL_INSTANCE.send_packet(IMC2PacketIsAlive())
except AttributeError:
#this will happen if we are not online in the first place
#(like during development) /Griatch
pass
class IEvt_IMC2_Send_Keepalive_Request(events.IntervalEvent):
"""
Event: Sends a keepalive-request to connected games in order to see who
is connected.
"""
def __init__(self):
super(IEvt_IMC2_Send_Keepalive_Request, self).__init__()
self.name = 'IEvt_IMC2_Send_Keepalive_Request'
self.interval = 3500
self.description = "Send an IMC2 keepalive-request packet."
def event_function(self):
"""
This is the function that is fired every self.interval seconds.
"""
imc2_conn.IMC2_PROTOCOL_INSTANCE.send_packet(IMC2PacketKeepAliveRequest())
class IEvt_IMC2_Prune_Inactive_Muds(events.IntervalEvent):
"""
Event: Prunes games that have not sent is-alive packets for a while. If
we haven't heard from them, they're probably not connected or don't
implement the protocol correctly. In either case, good riddance to them.
"""
def __init__(self):
super(IEvt_IMC2_Prune_Inactive_Muds, self).__init__()
self.name = 'IEvt_IMC2_Prune_Inactive_Muds'
# Check every 30 minutes.
self.interval = 1800
self.description = "Check IMC2 list for inactive games."
# Threshold for game inactivity (in seconds).
self.inactive_thresh = 3599
def event_function(self):
"""
This is the function that is fired every self.interval seconds.
"""
for name, mudinfo in IMC2_MUDLIST.mud_list.items():
# If we haven't heard from the game within our threshold time,
# we assume that they're dead.
if time() - mudinfo.last_updated > self.inactive_thresh:
del IMC2_MUDLIST.mud_list[name]
def add_events():
"""
Adds the IMC2 events to the scheduler.
"""
scheduler.add_event(IEvt_IMC2_Send_IsAlive())
scheduler.add_event(IEvt_IMC2_Prune_Inactive_Muds())
scheduler.add_event(IEvt_IMC2_Send_Keepalive_Request())