Adding the new event scheduler.

This commit is contained in:
Greg Taylor 2006-12-05 20:57:45 +00:00
parent d7cf02e8c0
commit 27b31f8cbe
3 changed files with 62 additions and 32 deletions

21
evennia/trunk/events.py Normal file
View file

@ -0,0 +1,21 @@
"""
This is the events module, where all of the code for periodic events needs
to reside.
ADDING AN EVENT:
* Add an entry to the 'schedule' dictionary.
* Add the proper event_ function here.
* Profit.
"""
schedule = {
'event_example': 60,
}
lastrun = {}
def event_example(server):
"""
This is where the example event would be placed.
"""
pass

View file

@ -0,0 +1,36 @@
import time
import events
class Scheduler:
"""
A really simple scheduler. We can probably get a lot fancier with this
in the future, but it'll do for now.
Open up events.py for a schedule of events and their respective functions.
"""
def __init__(self, server):
self.server = server
# The timer method to be triggered by the main server loop.
def heartbeat(self):
"""
Handle one tic/heartbeat.
"""
tictime = time.time()
for event in events.schedule:
try:
events.lastrun[event]
except:
events.lastrun[event] = time.time()
diff = tictime - events.lastrun[event]
if diff >= events.schedule[event]:
event_func = getattr(events, event)
if callable(event_func):
event_func(self.server)
# We'll get a new reading for time for accuracy.
events.lastrun[event] = time.time()

View file

@ -7,35 +7,7 @@ from apps.config.models import ConfigValue, CommandAlias
from apps.objects.models import Object, Attribute
from django.contrib.auth.models import User
import functions_db
#
## Begin: Time Functions
#
schedule = {'heal':100.0}
lastrun = {}
def heal():
pass
# The timer loop
def Timer(timer):
sched = schedule.iteritems()
for i in sched:
try: lastrun[i[0]]
except: lastrun[i[0]] = time.time()
diff = timer - lastrun[i[0]]
# Every 100 seconds, run heal(), defined above.
if diff >= schedule['heal']:
heal()
lastrun['heal'] = time.time()
#
## End: Time Functions
#
from scheduler import Scheduler
class Server(dispatcher):
"""
@ -173,11 +145,12 @@ BEGIN MAIN APPLICATION LOGIC
"""
if __name__ == '__main__':
server = Server()
scheduler = Scheduler(server)
try:
while server.game_running:
asyncore.loop(timeout=5, count=1) # Timer() called every 5 seconds.
Timer(time.time())
asyncore.loop(timeout=5, count=1)
scheduler.heartbeat()
except KeyboardInterrupt:
server.announce_all('The server has been shutdown. Please check back soon.\n\r')