Adding the new event scheduler.
This commit is contained in:
parent
d7cf02e8c0
commit
27b31f8cbe
3 changed files with 62 additions and 32 deletions
21
evennia/trunk/events.py
Normal file
21
evennia/trunk/events.py
Normal 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
|
||||||
36
evennia/trunk/scheduler.py
Normal file
36
evennia/trunk/scheduler.py
Normal 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()
|
||||||
|
|
||||||
|
|
@ -7,35 +7,7 @@ from apps.config.models import ConfigValue, CommandAlias
|
||||||
from apps.objects.models import Object, Attribute
|
from apps.objects.models import Object, Attribute
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
import functions_db
|
import functions_db
|
||||||
|
from scheduler import Scheduler
|
||||||
#
|
|
||||||
## 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
|
|
||||||
#
|
|
||||||
|
|
||||||
class Server(dispatcher):
|
class Server(dispatcher):
|
||||||
"""
|
"""
|
||||||
|
|
@ -173,11 +145,12 @@ BEGIN MAIN APPLICATION LOGIC
|
||||||
"""
|
"""
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
server = Server()
|
server = Server()
|
||||||
|
scheduler = Scheduler(server)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while server.game_running:
|
while server.game_running:
|
||||||
asyncore.loop(timeout=5, count=1) # Timer() called every 5 seconds.
|
asyncore.loop(timeout=5, count=1)
|
||||||
Timer(time.time())
|
scheduler.heartbeat()
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
server.announce_all('The server has been shutdown. Please check back soon.\n\r')
|
server.announce_all('The server has been shutdown. Please check back soon.\n\r')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue