mirror of
https://github.com/evennia/evennia.git
synced 2026-04-05 07:27:17 +02:00
Shiny new event system that uses Twisted's task stuff. This one is a good bit more flexible and semantically correct. More to come on this soon.
This commit is contained in:
parent
7d78cbcf4b
commit
53b111f4ae
2 changed files with 63 additions and 21 deletions
10
events.py
10
events.py
|
|
@ -6,16 +6,8 @@ import session_mgr
|
|||
Holds the events scheduled in scheduler.py.
|
||||
"""
|
||||
|
||||
# Dictionary of events with a list in the form of: [<interval>, <lastrantime>]
|
||||
schedule = {
|
||||
'check_sessions': [60, None]
|
||||
}
|
||||
|
||||
|
||||
def check_sessions():
|
||||
"""
|
||||
Event: Check all of the connected sessions.
|
||||
"""
|
||||
session_mgr.check_all_sessions()
|
||||
schedule['check_sessions'][1] = time.time()
|
||||
reactor.callLater(schedule['check_sessions'][0], check_sessions)
|
||||
session_mgr.check_all_sessions()
|
||||
74
scheduler.py
74
scheduler.py
|
|
@ -1,22 +1,72 @@
|
|||
import events
|
||||
from twisted.internet import protocol, reactor, defer
|
||||
from twisted.internet import task
|
||||
"""
|
||||
A really simple scheduler. We can probably get a lot fancier with this
|
||||
in the future, but it'll do for now.
|
||||
|
||||
ADDING AN EVENT:
|
||||
* Add an entry to the 'schedule' dictionary in the 'events' file.
|
||||
* Add the proper event_ function here.
|
||||
* Create an event function to call.
|
||||
* Add an entry to the 'schedule' dictionary here.
|
||||
* Profit.
|
||||
"""
|
||||
|
||||
# The timer method to be triggered by the main server loop.
|
||||
|
||||
# Dictionary of events with a list in the form of:
|
||||
# [<function>, <interval>, <lastrantime>, <taskobject>, <description>]
|
||||
schedule = {
|
||||
'check_sessions': [events.check_sessions, 5, None, None, "Session check."]
|
||||
}
|
||||
|
||||
def start_events():
|
||||
"""
|
||||
Handle one tic/heartbeat.
|
||||
Start the event system, which is built on Twisted's framework.
|
||||
"""
|
||||
for event in events.schedule:
|
||||
event_func = getattr(events, event)
|
||||
for event in schedule:
|
||||
event_func = get_event_function(event)
|
||||
|
||||
if callable(event_func):
|
||||
reactor.callLater(events.schedule[event][0], event_func)
|
||||
# Set the call-back function for the task to trigger_event, but pass
|
||||
# a reference to the event function.
|
||||
event_task = task.LoopingCall(trigger_event, event_func, event)
|
||||
# Start the task up with the specified interval.
|
||||
event_task.start(get_event_interval(event), now=False)
|
||||
# Set a reference to the event's task object in the dictionary so we
|
||||
# can re-schedule, start, and stop events from elsewhere.
|
||||
set_event_taskobj(event, event_task)
|
||||
|
||||
def get_event(event_name):
|
||||
"""
|
||||
Return the relevant entry in the schedule dictionary for the named event.
|
||||
|
||||
event_name: (string) The key of the event in the schedule dictionary.
|
||||
"""
|
||||
return schedule.get(event_name, None)
|
||||
|
||||
def get_event_function(event_name):
|
||||
"""
|
||||
Return a reference to the event's function.
|
||||
|
||||
event_name: (string) The key of the event in the schedule dictionary.
|
||||
"""
|
||||
return get_event(event_name)[0]
|
||||
|
||||
def get_event_interval(event_name):
|
||||
"""
|
||||
Return the event's execution interval.
|
||||
|
||||
event_name: (string) The key of the event in the schedule dictionary.
|
||||
"""
|
||||
return get_event(event_name)[1]
|
||||
|
||||
def set_event_taskobj(event_name, taskobj):
|
||||
"""
|
||||
Sets an event's task object.
|
||||
|
||||
event_name: (string) The key of the event in the schedule dictionary.
|
||||
"""
|
||||
get_event(event_name)[3] = taskobj
|
||||
|
||||
def trigger_event(event_func, event_name):
|
||||
"""
|
||||
Update the last ran time and fire off the event.
|
||||
|
||||
event_func: (func_reference) Reference to the event function to fire.
|
||||
eventname: (string) The name of the event (as per schedule dict).
|
||||
"""
|
||||
event_func()
|
||||
Loading…
Add table
Add a link
Reference in a new issue