Added more functionality to page command (resolving and going beyond issue102). Cleaned up the output of sevreral commands as well as added a few more useful functions in src/utils/utils.py.

This commit is contained in:
Griatch 2010-09-04 17:21:26 +00:00
parent 60851ade11
commit cfbb249d96
8 changed files with 122 additions and 61 deletions

View file

@ -4,7 +4,7 @@ Comsys command module.
from src.comms.models import Channel, Msg, ChannelConnection from src.comms.models import Channel, Msg, ChannelConnection
from game.gamesrc.commands.default.muxcommand import MuxCommand from game.gamesrc.commands.default.muxcommand import MuxCommand
from src.utils import create from src.utils import create, utils
from src.permissions.permissions import has_perm from src.permissions.permissions import has_perm
@ -712,9 +712,10 @@ class CmdPage(MuxCommand):
Usage: Usage:
page[/switches] [<player>,<player>,... = <message>] page[/switches] [<player>,<player>,... = <message>]
tell '' tell ''
page/list <number>
Switch: Switch:
list - show your last 10 tells/pages. list - show your last <number> of tells/pages.
Send a message to target user (if online). If no Send a message to target user (if online). If no
argument is given, you will instead see who was the last argument is given, you will instead see who was the last
@ -733,30 +734,44 @@ class CmdPage(MuxCommand):
caller = self.caller caller = self.caller
player = caller.player player = caller.player
# get the last messages we sent # get the messages we've sent
messages = list(Msg.objects.get_messages_by_sender(player)) messages_we_sent = list(Msg.objects.get_messages_by_sender(player))
pages = [msg for msg in messages pages_we_sent = [msg for msg in messages_we_sent
if msg.receivers] if msg.receivers]
# get last messages we've got
if pages: pages_we_got = list(Msg.objects.get_messages_by_receiver(player))
lastpage = pages[-1]
print "we sent:", pages_we_sent
print "we_got:", pages_we_got
if 'list' in self.switches: if 'list' in self.switches:
if len(messages) > 10: pages = pages_we_sent + pages_we_got
lastpages = messages[-10:] pages.sort(lambda x,y: cmp(x.date_sent, y.date_sent))
number = 10
if self.args:
try:
number = int(self.args)
except ValueError:
pass
if len(pages) > number:
lastpages = pages[-number:]
else: else:
lastpages = messages lastpages = pages
lastpages = "\n ".join(["{w%s{n to {c%s{n: %s" % (page.date_sent,
"{n,{c ".join([obj.name for obj in page.receivers]), lastpages = "\n ".join(["{w%s{n {c%s{n to {c%s{n: %s" % (utils.datetime_format(page.date_sent),
page.sender.name,
"{n,{c ".join([obj.name for obj in page.receivers]),
page.message) page.message)
for page in pages]) for page in lastpages])
caller.msg("Your latest pages:\n %s" % lastpages ) caller.msg("Your latest pages:\n %s" % lastpages )
return return
if not self.args or not self.rhs: if not self.args or not self.rhs:
if pages: if pages_we_sent:
string = "You last paged {c%s{n." % (", ".join([obj.name string = "You last paged {c%s{n." % (", ".join([obj.name
for obj in lastpage.receivers])) for obj in pages_we_sent[-1].receivers]))
caller.msg(string) caller.msg(string)
return return
else: else:
@ -764,29 +779,49 @@ class CmdPage(MuxCommand):
caller.msg(string) caller.msg(string)
return return
# Build a list of targets # We are sending. Build a list of targets
if not self.lhs: if not self.lhs:
# If there are no targets, then set the targets # If there are no targets, then set the targets
# to the last person they paged. # to the last person they paged.
receivers = lastpage.receivers if pages_we_sent:
receivers = pages_we_sent[-1].receivers
else:
caller.msg("Who do you want to page?")
return
else: else:
receivers = self.lhslist receivers = self.lhslist
recobjs = [] recobjs = []
for receiver in set(receivers): for receiver in set(receivers):
pobj = caller.search("*%s" % (receiver.lstrip('*')), global_search=True) if isinstance(receiver, basestring):
if not pobj: pobj = caller.search("*%s" % (receiver.lstrip('*')), global_search=True)
return if not pobj:
return
elif hasattr(receiver, 'character'):
pobj = receiver.character
else:
caller.msg("Who do you want to page?")
return
recobjs.append(pobj) recobjs.append(pobj)
if not recobjs:
caller.msg("No players matching your target were found.")
return
header = "{wPlayer{n {c%s{n {wpages:{n" % caller.key header = "{wPlayer{n {c%s{n {wpages:{n" % caller.key
message = self.rhs message = self.rhs
# create the persistent message object # create the persistent message object
msg = create.create_message(player, message, msg = create.create_message(player, message,
receivers=recobjs) receivers=recobjs)
# tell the players they got a message. # tell the players they got a message.
received = []
for pobj in recobjs: for pobj in recobjs:
pobj.msg("%s %s" % (header, message)) pobj.msg("%s %s" % (header, message))
target_names = "{n,{c ".join([pobj.name for pobj in recobjs]) if not pobj.has_player:
caller.msg("You paged {c%s{n with: '%s'." % (target_names, message)) received.append("{C%s{n" % pobj.name)
caller.msg("%s is offline. They will see your message if they list their pages later." % received[-1])
else:
received.append("{c%s{n" % pobj.name)
received = ", ".join(received)
caller.msg("You paged %s with: '%s'." % (received, message))

View file

@ -2,7 +2,7 @@
Commands that are generally staff-oriented that show information regarding Commands that are generally staff-oriented that show information regarding
the server instance. the server instance.
""" """
import os import os, datetime
import django, twisted import django, twisted
from django.contrib.auth.models import User from django.contrib.auth.models import User
from src.objects.models import ObjectDB from src.objects.models import ObjectDB
@ -52,26 +52,22 @@ class CmdTime(MuxCommand):
def func(self): def func(self):
"Show times." "Show times."
string2 = "\nCurrent server uptime:\n %i yrs, %i months, " string1 = "\nCurrent server uptime: "
string2 += "%i weeks, %i days, %i hours, %i minutes and %i secs." string1 += "{w%s{n" % (utils.time_format(gametime.uptime(format=False), 2))
string2 = string2 % gametime.uptime(format=True)
string3 = "\nTotal running time (gametime x %g):" % (1.0/gametime.TIMEFACTOR) string2 = "\nTotal server running time: "
string3 += "\n %i yrs, %i months, %i weeks, %i days, " string2 += "{w%s{n" % (utils.time_format(gametime.runtime(format=False), 2))
string3 += "%i hours, %i minutes and %i secs."
string3 = string3 % gametime.runtime(format=True) string3 = "\nTotal in-game time (realtime x %g): " % (gametime.TIMEFACTOR)
#print "runtime:", gametime.runtime() string3 += "{w%s{n" % (utils.time_format(gametime.gametime(format=False), 2))
string1 = "\nTotal game time (realtime x %g):" % (gametime.TIMEFACTOR)
string1 += "\n %i yrs, %i months, %i weeks, %i days, " string4 = "\nServer time stamp: {w%s{n" % (str(datetime.datetime.now()))
string1 += "%i hours, %i minutes and %i secs." string5 = ""
string1 = string1 % (gametime.gametime(format=True))
#print "gametime:", gametime.gametime()
string4 = ""
if not utils.host_os_is('nt'): if not utils.host_os_is('nt'):
# os.getloadavg() is not available on Windows. # os.getloadavg() is not available on Windows.
loadavg = os.getloadavg() loadavg = os.getloadavg()
string4 = "\n Server load (1 min) : %g%%" % (100 * loadavg[0]) string5 += "\nServer load (per minute): {w%g%%{n" % (100 * loadavg[0])
string = "%s%s%s%s" % (string2, string3, string1, string4) string = "%s%s%s%s%s" % (string1, string2, string3, string4, string5)
self.caller.msg(string) self.caller.msg(string)
class CmdList(MuxCommand): class CmdList(MuxCommand):

View file

@ -303,7 +303,7 @@ class CmdBoot(MuxCommand):
pobj = caller.search("*%s" % args, global_search=True) pobj = caller.search("*%s" % args, global_search=True)
if not pobj: if not pobj:
return return
pobj = pobj[0] pobj = pobj
if pobj.has_player: if pobj.has_player:
if not has_perm(caller, pobj, 'can_boot'): if not has_perm(caller, pobj, 'can_boot'):
string = "You don't have the permission to boot %s." string = "You don't have the permission to boot %s."

View file

@ -102,7 +102,6 @@ class CmdConnect(MuxCommand):
#print "character:", character, character.scripts.all(), character.cmdset.current #print "character:", character, character.scripts.all(), character.cmdset.current
character.execute_cmd('look') character.execute_cmd('look')
class CmdCreate(MuxCommand): class CmdCreate(MuxCommand):
""" """
Create a new account. Create a new account.
@ -291,7 +290,7 @@ To login to the system, you need to do one of the following:
> connect anna@myemail.com tuK3221mP > connect anna@myemail.com tuK3221mP
This should log you in. Run 'help' again once you're logged in This should log you in. Run 'help' again once you're logged in
to get more aid. Welcome to Evennia! to get more aid. Hope you enjoy your stay!
You can use the 'look' command if you want to see the connect screen again. You can use the 'look' command if you want to see the connect screen again.
""" """

View file

@ -76,7 +76,7 @@ class MsgManager(models.Manager):
if not receiver: if not receiver:
return None return None
return [msg for msg in self.all() return [msg for msg in self.all()
if receiver in msg.recivers if receiver in msg.receivers
and receiver not in msg.hide_from_receivers] and receiver not in msg.hide_from_receivers]
def get_messages_by_channel(self, channel): def get_messages_by_channel(self, channel):

View file

@ -208,8 +208,7 @@ class Msg(SharedMemoryModel):
#@property #@property
def date_sent_get(self): def date_sent_get(self):
"Getter. Allows for value = self.date_sent" "Getter. Allows for value = self.date_sent"
date = self.db_date_sent return self.db_date_sent
return str(date).rsplit('.',1)[0]
#@date_sent.setter #@date_sent.setter
def date_sent_set(self, value): def date_sent_set(self, value):
"Setter. Allows for self.date_sent = value" "Setter. Allows for self.date_sent = value"

View file

@ -32,8 +32,8 @@ REAL_MIN = 60.0 # seconds per minute in real world
# e.g. when defining events. The words month, week and year can # e.g. when defining events. The words month, week and year can
# of course mean whatever units of time are used in the game. # of course mean whatever units of time are used in the game.
TICK = REAL_TICK / TIMEFACTOR TICK = REAL_TICK * TIMEFACTOR
MIN = REAL_MIN / TIMEFACTOR MIN = REAL_MIN * TIMEFACTOR
HOUR = MIN * settings.TIME_MIN_PER_HOUR HOUR = MIN * settings.TIME_MIN_PER_HOUR
DAY = HOUR * settings.TIME_HOUR_PER_DAY DAY = HOUR * settings.TIME_HOUR_PER_DAY
WEEK = DAY * settings.TIME_DAY_PER_WEEK WEEK = DAY * settings.TIME_DAY_PER_WEEK

View file

@ -6,8 +6,10 @@ be of use when designing your own game.
""" """
import os import os
import textwrap import textwrap
import datetime
from django.conf import settings from django.conf import settings
def is_iter(iterable): def is_iter(iterable):
""" """
Checks if an object behaves iterably. However, Checks if an object behaves iterably. However,
@ -85,9 +87,9 @@ def time_format(seconds, style=0):
# We'll just use integer math, no need for decimal precision. # We'll just use integer math, no need for decimal precision.
seconds = int(seconds) seconds = int(seconds)
days = seconds / 86400 days = seconds / 86400
seconds -= days * 86400 seconds -= days * 86400
hours = seconds / 3600 hours = seconds / 3600
seconds -= hours * 3600 seconds -= hours * 3600
minutes = seconds / 60 minutes = seconds / 60
seconds -= minutes * 60 seconds -= minutes * 60
@ -117,20 +119,50 @@ def time_format(seconds, style=0):
elif style is 2: elif style is 2:
""" """
Full-detailed, long-winded format. Full-detailed, long-winded format. We ignore seconds.
""" """
days_str = hours_str = minutes_str = '' days_str = hours_str = minutes_str = seconds_str = ''
if days > 0: if days > 0:
days_str = '%i days, ' % (days,) if days == 1:
days_str = '%i day, ' % days
else:
days_str = '%i days, ' % days
if days or hours > 0: if days or hours > 0:
hours_str = '%i hours, ' % (hours,) if hours == 1:
hours_str = '%i hour, ' % hours
else:
hours_str = '%i hours, ' % hours
if hours or minutes > 0: if hours or minutes > 0:
minutes_str = '%i minutes, ' % (minutes,) if minutes == 1:
seconds_str = '%i seconds' % (seconds,) minutes_str = '%i minute ' % minutes
else:
retval = '%s%s%s%s' % (days_str, hours_str, minutes_str, seconds_str,) minutes_str = '%i minutes ' % minutes
retval = '%s%s%s' % (days_str, hours_str, minutes_str)
return retval return retval
def datetime_format(dtobj):
"""
Takes a datetime object instance (e.g. from django's DateTimeField)
and returns a string.
"""
year, month, day = dtobj.year, dtobj.date, dtobj.day
hour, minute, second = dtobj.hour, dtobj.minute, dtobj.second
now = datetime.datetime.now()
if year < now.year:
# another year
timestring = str(dtobj.date())
elif dtobj.date() < now.date():
# another date, same year
timestring = "%02i-%02i" % (day, month)
elif hour < now.hour - 1:
# same day, more than 1 hour ago
timestring = "%02i:%02i" % (hour, minute)
else:
# same day, less than 1 hour ago
timestring = "%02i:%02i:%02i" % (hour, minute, second)
return timestring
def host_os_is(osname): def host_os_is(osname):
""" """
Check to see if the host OS matches the query. Check to see if the host OS matches the query.