More session stuff, event code re-arrangement.
This commit is contained in:
parent
5816370857
commit
925cfe6c15
6 changed files with 58 additions and 68 deletions
|
|
@ -148,16 +148,14 @@ class Object(models.Model):
|
||||||
attribs = Attribute.objects.filter(object=self)
|
attribs = Attribute.objects.filter(object=self)
|
||||||
return attribs
|
return attribs
|
||||||
|
|
||||||
def destroy(self, session_list):
|
def destroy(self):
|
||||||
"""
|
"""
|
||||||
Destroys an object, sets it to GOING. Can still be recovered
|
Destroys an object, sets it to GOING. Can still be recovered
|
||||||
if the user decides to.
|
if the user decides to.
|
||||||
|
|
||||||
session_list: (list) The server's session_list attribute.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# See if we need to kick the player off.
|
# See if we need to kick the player off.
|
||||||
session = functions_db.session_from_object(session_list, self)
|
session = session_mgr.session_from_object(self)
|
||||||
if session:
|
if session:
|
||||||
session.msg("You have been destroyed, goodbye.")
|
session.msg("You have been destroyed, goodbye.")
|
||||||
session.handle_close()
|
session.handle_close()
|
||||||
|
|
@ -165,9 +163,7 @@ class Object(models.Model):
|
||||||
# If the object is a player, set the player account object to inactive.
|
# If the object is a player, set the player account object to inactive.
|
||||||
# It can still be recovered at this point.
|
# It can still be recovered at this point.
|
||||||
if self.is_player():
|
if self.is_player():
|
||||||
print 'PLAYER'
|
|
||||||
uobj = User.objects.get(id=self.id)
|
uobj = User.objects.get(id=self.id)
|
||||||
print 'VICTIM', uobj
|
|
||||||
uobj.is_active = False
|
uobj.is_active = False
|
||||||
uobj.save()
|
uobj.save()
|
||||||
|
|
||||||
|
|
@ -175,11 +171,9 @@ class Object(models.Model):
|
||||||
self.type = 5
|
self.type = 5
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def delete(self, session_list):
|
def delete(self):
|
||||||
"""
|
"""
|
||||||
Deletes an object permanently. Marks it for re-use by a new object.
|
Deletes an object permanently. Marks it for re-use by a new object.
|
||||||
|
|
||||||
session_list: (list) The server's session_list attribute.
|
|
||||||
"""
|
"""
|
||||||
# Delete the associated player object permanently.
|
# Delete the associated player object permanently.
|
||||||
uobj = User.objects.filter(id=self.id)
|
uobj = User.objects.filter(id=self.id)
|
||||||
|
|
@ -445,3 +439,4 @@ class Object(models.Model):
|
||||||
return "%s(#%d%s)" % (self.name, self.id, self.flag_string())
|
return "%s(#%d%s)" % (self.name, self.id, self.flag_string())
|
||||||
|
|
||||||
import functions_db
|
import functions_db
|
||||||
|
import session_mgr
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
from apps.objects.models import Object
|
|
||||||
import functions_db
|
import functions_db
|
||||||
import functions_general
|
import functions_general
|
||||||
import commands_general
|
import commands_general
|
||||||
import cmdhandler
|
import cmdhandler
|
||||||
import session_mgr
|
import session_mgr
|
||||||
|
from apps.objects.models import Object
|
||||||
"""
|
"""
|
||||||
Staff commands may be a bad description for this file, but it'll do for now.
|
Staff commands may be a bad description for this file, but it'll do for now.
|
||||||
Any command here is prefixed by an '@' sign, usually denoting a builder, staff
|
Any command here is prefixed by an '@' sign, usually denoting a builder, staff
|
||||||
|
|
@ -60,7 +59,7 @@ def cmd_destroy(cdat):
|
||||||
target_obj = results[0]
|
target_obj = results[0]
|
||||||
|
|
||||||
session.msg("You destroy %s." % (target_obj,))
|
session.msg("You destroy %s." % (target_obj,))
|
||||||
target_obj.destroy(session_mgr.get_session_list())
|
target_obj.destroy()
|
||||||
|
|
||||||
def cmd_name(cdat):
|
def cmd_name(cdat):
|
||||||
"""
|
"""
|
||||||
|
|
@ -375,7 +374,6 @@ def cmd_wall(cdat):
|
||||||
Announces a message to all connected players.
|
Announces a message to all connected players.
|
||||||
"""
|
"""
|
||||||
session = cdat['session']
|
session = cdat['session']
|
||||||
server = cdat['server']
|
|
||||||
wallstring = ' '.join(cdat['uinput']['splitted'][1:])
|
wallstring = ' '.join(cdat['uinput']['splitted'][1:])
|
||||||
|
|
||||||
if wallstring == '':
|
if wallstring == '':
|
||||||
|
|
@ -383,7 +381,7 @@ def cmd_wall(cdat):
|
||||||
return
|
return
|
||||||
|
|
||||||
message = "%s shouts \"%s\"" % (session.get_pobject().get_name(), wallstring)
|
message = "%s shouts \"%s\"" % (session.get_pobject().get_name(), wallstring)
|
||||||
functions_general.announce_all(server, message)
|
functions_general.announce_all(message)
|
||||||
|
|
||||||
def cmd_shutdown(cdat):
|
def cmd_shutdown(cdat):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,7 @@
|
||||||
This is the events module, where all of the code for periodic events needs
|
This is the events module, where all of the code for periodic events needs
|
||||||
to reside.
|
to reside.
|
||||||
|
|
||||||
ADDING AN EVENT:
|
|
||||||
* Add an entry to the 'schedule' dictionary.
|
|
||||||
* Add the proper event_ function here.
|
|
||||||
* Profit.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
schedule = {
|
schedule = {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import session_mgr
|
||||||
"""
|
"""
|
||||||
General commonly used functions.
|
General commonly used functions.
|
||||||
"""
|
"""
|
||||||
|
|
@ -62,7 +63,7 @@ def time_format(seconds, style=0):
|
||||||
retval = '%s%s%s%s' % (days_str, hours_str, minutes_str, seconds_str,)
|
retval = '%s%s%s%s' % (days_str, hours_str, minutes_str, seconds_str,)
|
||||||
return retval
|
return retval
|
||||||
|
|
||||||
def announce_all(server, message, with_ann_prefix=True, with_nl=True):
|
def announce_all(message, with_ann_prefix=True, with_nl=True):
|
||||||
"""
|
"""
|
||||||
Announces something to all connected players.
|
Announces something to all connected players.
|
||||||
"""
|
"""
|
||||||
|
|
@ -76,5 +77,5 @@ def announce_all(server, message, with_ann_prefix=True, with_nl=True):
|
||||||
else:
|
else:
|
||||||
newline = ''
|
newline = ''
|
||||||
|
|
||||||
for session in server.get_session_list():
|
for session in session_mgr.get_session_list():
|
||||||
session.msg_no_nl('%s %s%s' % (prefix, message,newline,))
|
session.msg_no_nl('%s %s%s' % (prefix, message,newline,))
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,51 @@
|
||||||
import time
|
import time
|
||||||
import events
|
"""
|
||||||
|
A really simple scheduler. We can probably get a lot fancier with this
|
||||||
|
in the future, but it'll do for now.
|
||||||
|
|
||||||
class Scheduler:
|
ADDING AN EVENT:
|
||||||
|
* Add an entry to the 'schedule' dictionary.
|
||||||
|
* Add the proper event_ function here.
|
||||||
|
* Profit.
|
||||||
|
"""
|
||||||
|
|
||||||
|
schedule = {
|
||||||
|
'event_example': 60,
|
||||||
|
}
|
||||||
|
|
||||||
|
lastrun = {}
|
||||||
|
|
||||||
|
"""
|
||||||
|
BEGIN EVENTS
|
||||||
|
"""
|
||||||
|
def event_example():
|
||||||
"""
|
"""
|
||||||
A really simple scheduler. We can probably get a lot fancier with this
|
This is where the example event would be placed.
|
||||||
in the future, but it'll do for now.
|
"""
|
||||||
|
pass
|
||||||
|
"""
|
||||||
|
END EVENTS
|
||||||
|
"""
|
||||||
|
|
||||||
Open up events.py for a schedule of events and their respective functions.
|
# The timer method to be triggered by the main server loop.
|
||||||
|
def heartbeat():
|
||||||
"""
|
"""
|
||||||
def __init__(self, server):
|
Handle one tic/heartbeat.
|
||||||
self.server = server
|
"""
|
||||||
|
tictime = time.time()
|
||||||
|
for event in schedule:
|
||||||
|
try:
|
||||||
|
lastrun[event]
|
||||||
|
except:
|
||||||
|
lastrun[event] = time.time()
|
||||||
|
|
||||||
# The timer method to be triggered by the main server loop.
|
diff = tictime - lastrun[event]
|
||||||
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()
|
|
||||||
|
|
||||||
|
if diff >= schedule[event]:
|
||||||
|
event_func = getattr(self, event)
|
||||||
|
|
||||||
|
if callable(event_func):
|
||||||
|
event_func()
|
||||||
|
|
||||||
|
# We'll get a new reading for time for accuracy.
|
||||||
|
lastrun[event] = time.time()
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,9 @@ from asynchat import async_chat
|
||||||
import socket, asyncore, time, sys
|
import socket, asyncore, time, sys
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
from django.contrib.auth.models import User
|
|
||||||
from apps.config.models import ConfigValue, CommandAlias
|
from apps.config.models import ConfigValue, CommandAlias
|
||||||
from apps.objects.models import Object, Attribute
|
import scheduler
|
||||||
from scheduler import Scheduler
|
|
||||||
import functions_db
|
import functions_db
|
||||||
import functions_general
|
import functions_general
|
||||||
import global_defines
|
import global_defines
|
||||||
|
|
@ -17,8 +16,6 @@ class Server(dispatcher):
|
||||||
The main server class from which everything branches.
|
The main server class from which everything branches.
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.session_list = []
|
|
||||||
self.object_list = {}
|
|
||||||
self.cmd_alias_list = {}
|
self.cmd_alias_list = {}
|
||||||
self.configvalue = {}
|
self.configvalue = {}
|
||||||
self.game_running = True
|
self.game_running = True
|
||||||
|
|
@ -101,20 +98,8 @@ class Server(dispatcher):
|
||||||
"""
|
"""
|
||||||
return self.configvalue[configname]
|
return self.configvalue[configname]
|
||||||
|
|
||||||
def get_session_list(self):
|
|
||||||
"""
|
|
||||||
Lists the server's connected session objects.
|
|
||||||
"""
|
|
||||||
return session_mgr.get_session_list()
|
|
||||||
|
|
||||||
def remove_session(self, session):
|
|
||||||
"""
|
|
||||||
Removes a session from the server's session list.
|
|
||||||
"""
|
|
||||||
session_mgr.remove_session(session)
|
|
||||||
|
|
||||||
def shutdown(self, message='The server has been shutdown. Please check back soon.'):
|
def shutdown(self, message='The server has been shutdown. Please check back soon.'):
|
||||||
functions_general.announce_all(server, message)
|
functions_general.announce_all(message)
|
||||||
self.game_running = False
|
self.game_running = False
|
||||||
"""
|
"""
|
||||||
END Server CLASS
|
END Server CLASS
|
||||||
|
|
@ -125,7 +110,6 @@ 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:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue