Comsys moved to src/

This commit is contained in:
Greg Taylor 2008-06-15 19:41:27 +00:00
parent 4bb00013ae
commit 811016867b
3 changed files with 20 additions and 20 deletions

View file

@ -11,7 +11,7 @@ import defines_global
import cmdtable
import functions_general
import functions_log
import functions_comsys
from src import comsys
class UnknownCommand(Exception):
"""
@ -171,26 +171,26 @@ def handle(cdat):
parsed_input['splitted'][1] = parsed_input['splitted'][1][1:]
parsed_input['root_cmd'] = 'pose'
# Channel alias match.
elif functions_comsys.plr_has_channel(session,
elif comsys.plr_has_channel(session,
parsed_input['root_cmd'],
alias_search=True,
return_muted=True):
calias = parsed_input['root_cmd']
cname = functions_comsys.plr_cname_from_alias(session, calias)
cname = comsys.plr_cname_from_alias(session, calias)
cmessage = ' '.join(parsed_input['splitted'][1:])
if cmessage == "who":
functions_comsys.msg_cwho(session, cname)
comsys.msg_cwho(session, cname)
return
elif cmessage == "on":
functions_comsys.plr_chan_on(session, calias)
comsys.plr_chan_on(session, calias)
return
elif cmessage == "off":
functions_comsys.plr_chan_off(session, calias)
comsys.plr_chan_off(session, calias)
return
elif cmessage == "last":
functions_comsys.msg_chan_hist(session, cname)
comsys.msg_chan_hist(session, cname)
return
second_arg = "%s=%s" % (cname, cmessage)

View file

@ -7,7 +7,7 @@ import time
from django.conf import settings
import functions_general
import functions_comsys
import src.comsys
import defines_global
from src import ansi
@ -44,18 +44,18 @@ def cmd_addcom(cdat):
session.msg("You are already on that channel.")
return
name_matches = functions_comsys.cname_search(chan_name, exact=True)
name_matches = src.comsys.cname_search(chan_name, exact=True)
if name_matches:
chan_name_parsed = name_matches[0].get_name()
session.msg("You join %s, with an alias of %s." % \
(chan_name_parsed, chan_alias))
functions_comsys.plr_set_channel(session, chan_alias, chan_name_parsed, True)
src.comsys.plr_set_channel(session, chan_alias, chan_name_parsed, True)
# Announce the user's joining.
join_msg = "[%s] %s has joined the channel." % \
(chan_name_parsed, pobject.get_name(show_dbref=False))
functions_comsys.send_cmessage(chan_name_parsed, join_msg)
src.comsys.send_cmessage(chan_name_parsed, join_msg)
else:
session.msg("Could not find channel %s." % (chan_name,))
@ -81,12 +81,12 @@ def cmd_delcom(cdat):
chan_name = session.channels_subscribed[chan_alias][0]
session.msg("You have left %s." % (chan_name,))
functions_comsys.plr_del_channel(session, chan_alias)
src.comsys.plr_del_channel(session, chan_alias)
# Announce the user's leaving.
leave_msg = "[%s] %s has left the channel." % \
(chan_name, pobject.get_name(show_dbref=False))
functions_comsys.send_cmessage(chan_name, leave_msg)
src.comsys.send_cmessage(chan_name, leave_msg)
def cmd_comlist(cdat):
"""
@ -131,7 +131,7 @@ def cmd_clist(cdat):
"""
session = cdat['session']
session.msg("** Channel Owner Description")
for chan in functions_comsys.get_all_channels():
for chan in src.comsys.get_all_channels():
session.msg("%s%s %-13.13s %-15.15s %-45.45s" %
('-', '-', chan.get_name(), chan.get_owner().get_name(), 'No Description'))
session.msg("-- End of Channel List --")
@ -150,7 +150,7 @@ def cmd_cdestroy(cdat):
session.msg("You must supply a name!")
return
name_matches = functions_comsys.cname_search(cname, exact=True)
name_matches = src.comsys.cname_search(cname, exact=True)
if not name_matches:
session.msg("Could not find channel %s." % (cname,))
@ -218,7 +218,7 @@ def cmd_cemit(cdat):
session.msg("You must provide a message to emit.")
return
name_matches = functions_comsys.cname_search(cname, exact=True)
name_matches = src.comsys.cname_search(cname, exact=True)
try:
# Safety first, kids!
@ -234,7 +234,7 @@ def cmd_cemit(cdat):
final_cmessage = cmessage
else:
if "sendername" in switches:
if not functions_comsys.plr_has_channel(session, cname_parsed, return_muted=False):
if not src.comsys.plr_has_channel(session, cname_parsed, return_muted=False):
session.msg("You must be on %s to do that." % (cname_parsed,))
return
final_cmessage = "[%s] %s: %s" % (cname_parsed, pobject.get_name(show_dbref=False), cmessage)
@ -246,7 +246,7 @@ def cmd_cemit(cdat):
if not "quiet" in switches:
session.msg("Sent - %s" % (name_matches[0],))
functions_comsys.send_cmessage(cname_parsed, final_cmessage)
src.comsys.send_cmessage(cname_parsed, final_cmessage)
def cmd_cwho(cdat):
"""
@ -273,14 +273,14 @@ def cmd_ccreate(cdat):
session.msg("You must supply a name!")
return
name_matches = functions_comsys.cname_search(cname, exact=True)
name_matches = src.comsys.cname_search(cname, exact=True)
if name_matches:
session.msg("A channel with that name already exists.")
else:
# Create and set the object up.
cdat = {"name": cname, "owner": pobject}
new_chan = functions_comsys.create_channel(cdat)
new_chan = src.comsys.create_channel(cdat)
session.msg("Channel %s created." % (new_chan.get_name(),))
def cmd_cchown(cdat):

257
src/comsys.py Normal file
View file

@ -0,0 +1,257 @@
"""
Comsys functions.
"""
import time
import datetime
from django.utils import simplejson
from apps.objects.models import CommChannel, CommChannelMessage
from src import session_mgr
from src import ansi
def plr_get_cdict(session):
"""
Returns the users's channel subscription dictionary. Use this instead of
directly referring to the session object.
session: (SessionProtocol) Reference to a player session.
"""
return session.channels_subscribed
def plr_listening_channel(session, cstr, alias_search=False):
"""
Returns a player's listening status for a channel.
session: (SessionProtocol) Reference to a player session.
cstr: (str) The channel name or alias (depending on alias_search).
alias_search: (bool) If true, search by alias. Else search by full name.
"""
return plr_has_channel(session, cstr, alias_search=alias_search,
return_muted=False)
def plr_cname_from_alias(session, calias):
"""
Returns a channel name given a channel alias.
session: (SessionProtocol) Reference to a player session.
calias: (str) The channel alias.
"""
return plr_get_cdict(session).get(calias, None)[0]
def plr_chan_off(session, calias):
"""
Turn a channel off for a player.
session: (SessionProtocol) Reference to a player session.
calias: (str) The channel alias.
"""
if not plr_listening_channel(session, calias, alias_search=True):
session.msg("You're already not listening to that channel.")
return
else:
cname = plr_cname_from_alias(session, calias)
cobj = get_cobj_from_name(cname)
plr_set_channel_listening(session, calias, False)
session.msg("You have left %s." % (cname,))
send_cmessage(cname, "%s %s has left the channel." % (cobj.get_header(),
session.get_pobject().get_name(show_dbref=False)))
def plr_chan_on(session, calias):
"""
Turn a channel on for a player.
session: (SessionProtocol) Reference to a player session.
calias: (str) The channel alias.
"""
plr_listening = plr_listening_channel(session, calias, alias_search=True)
if plr_listening:
session.msg("You're already listening to that channel.")
return
else:
cname = plr_cname_from_alias(session, calias)
cobj = get_cobj_from_name(cname)
send_cmessage(cname, "%s %s has joined the channel." % (cobj.get_header(),
session.get_pobject().get_name(show_dbref=False)))
plr_set_channel_listening(session, calias, True)
session.msg("You have joined %s." % (cname,))
def plr_has_channel(session, cname, alias_search=False, return_muted=False):
"""
Is this session subscribed to the named channel?
session: (SessionProtocol) Reference to a player session.
cname: (str) The channel name or alias (depending on alias_search)
alias_search: (bool) If False, search by full name. Else search by alias.
return_muted: (bool) Take the user's enabling/disabling of the channel
into consideration?
"""
has_channel = False
if alias_search:
# Search by aliases only.
cdat = plr_get_cdict(session).get(cname, False)
# No key match, fail immediately.
if not cdat:
return False
# If channel status is taken into consideration, see if the user
# has the channel muted or not.
if return_muted:
return True
else:
return cdat[1]
else:
# Search by complete channel name.
chan_list = plr_get_cdict(session).values()
for chan in chan_list:
# Check for a name match
if cname == chan[0]:
has_channel = True
# If channel status is taken into consideration, see if the user
# has the channel muted or not.
if return_muted is False and not chan[1]:
has_channel = False
break
return has_channel
def plr_set_channel_listening(session, alias, listening):
"""
Add a channel to a session's channel list.
session: (SessionProtocol) A reference to the player session.
alias: (str) The channel alias.
listening: (bool) A True or False value to determine listening status.
"""
plr_get_cdict(session).get(alias)[1] = listening
plr_jsondump_channels(session)
def plr_set_channel(session, alias, cname, listening):
"""
Set a channels alias, name, and listening status in one go, or add the
channel if it doesn't already exist on a user's list.
session: (SessionProtocol) A reference to the player session.
alias: (str) The channel alias (also the key in the user's cdict)
cname: (str) Desired channel name to set.
listening: (bool) A True or False value to determine listening status.
"""
plr_get_cdict(session)[alias] = [cname, listening]
plr_jsondump_channels(session)
def plr_jsondump_channels(session):
"""
Save the player's channel list to the CHANLIST attribute.
session: (SessionProtocol) A reference to the player session.
"""
session.get_pobject().set_attribute("__CHANLIST", simplejson.dumps(plr_get_cdict(session)))
def plr_del_channel(session, alias):
"""
Remove a channel from a session's channel list.
session: (SessionProtocol) A reference to the player session.
alias: (str) The channel alias (also the key in the user's cdict)
"""
del plr_get_cdict(session)[alias]
def msg_chan_hist(session, channel_name):
"""
Sends a listing of subscribers to a channel given a channel name.
session: (SessionProtocol) A reference to the player session.
channel_name: (str) The channel's full name.
"""
cobj = get_cobj_from_name(channel_name)
hist_list = CommChannelMessage.objects.filter(channel=cobj).order_by('date_sent')[0:20]
for entry in hist_list:
delta_days = datetime.datetime.now() - entry.date_sent
days_elapsed = delta_days.days
if days_elapsed > 0:
time_str = entry.date_sent.strftime("%m.%d / %H:%M")
else:
time_str = entry.date_sent.strftime("%H:%M")
session.msg("[%s] %s" % (time_str, entry.message))
def msg_cwho(session, channel_name):
"""
Sends a listing of subscribers to a channel given a channel name.
session: (SessionProtocol) A reference to the player session.
channel_name: (str) The channel's full name.
"""
session.msg("--- Users Listening to %s ---" % (channel_name,))
for plr_sess in get_cwho_list(channel_name):
session.msg(plr_sess.get_pobject().get_name(show_dbref=False))
session.msg("--- End Channel Listeners ---")
def get_cwho_list(channel_name, return_muted=False):
"""
Get all users on a channel.
channel_name: (string) The name of the channel.
return_muted: (bool) Return those who have the channel muted if True.
"""
sess_list = session_mgr.get_session_list()
return [sess for sess in sess_list if plr_has_channel(sess, channel_name, return_muted)]
def send_cmessage(channel_name, message):
"""
Sends a message to all players on the specified channel.
channel_name: (string) The name of the channel.
message: (string) Message to send.
"""
for user in get_cwho_list(channel_name, return_muted=False):
user.msg(message)
try:
chan_message = CommChannelMessage()
chan_message.channel = get_cobj_from_name(channel_name)
chan_message.message = message
chan_message.save()
except:
print "send_cmessage(): Can't find channel: %s" % (channel_name,)
def get_all_channels():
"""
Returns all channel objects.
"""
return CommChannel.objects.all()
def get_cobj_from_name(cname):
"""
Returns the channel's object when given a name.
"""
return CommChannel.objects.get(name=cname)
def create_channel(cdat):
"""
Create a new channel. cdat is a dictionary that contains the following keys.
REQUIRED KEYS:
* name: The name of the new channel.
* owner: The creator of the channel.
"""
new_chan = CommChannel()
new_chan.name = ansi.parse_ansi(cdat["name"], strip_ansi=True)
new_chan.ansi_name = "[%s]" % (ansi.parse_ansi(cdat["name"]),)
new_chan.set_owner(cdat["owner"])
new_chan.save()
return new_chan
def cname_search(search_text, exact=False):
"""
Searches for a particular channel name. Returns a QuerySet with the
results.
exact: (bool) Do an exact (case-insensitive) name match if true.
"""
if exact:
return CommChannel.objects.filter(name__iexact=search_text)
else:
return CommChannel.objects.filter(name__istartswith=search_text)