diff --git a/.gitignore b/.gitignore index 308031e..b6a50f6 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,4 @@ nosetests.xml .DS_Store /server/ssh-private.key /server/ssh-public.key +/transcripts/202*.html diff --git a/transcripts/header-template.html b/transcripts/header-template.html new file mode 100644 index 0000000..c05b575 --- /dev/null +++ b/transcripts/header-template.html @@ -0,0 +1,34 @@ + + + Wyldwood Bar Transcript + + + +

Wyldwood Bar Transcript

diff --git a/typeclasses/puppets.py b/typeclasses/puppets.py index 3d903a8..7286632 100755 --- a/typeclasses/puppets.py +++ b/typeclasses/puppets.py @@ -1,6 +1,9 @@ #!/usr/bin/env python -from re import split, match, IGNORECASE +from os.path import exists +from datetime import datetime +from re import split, match, sub, IGNORECASE +from shutil import copyfile from evennia import CmdSet from evennia.utils import logger @@ -146,6 +149,7 @@ class CmdShrubSay(Command): msg = routput("The shrub uses a branch to << brush ^ wipe >> << off ^ >> << the chalk from ^ >> its << small ^ >> chalkboard, and with another branch, << slowly ^ carefully ^ deliberately ^ >> starts to write << something ^ a message ^ >>.") here.msg_contents(msg) self.caller.db.inside = self.args.strip() + self.caller.record_msg(f"The chalkboard reads: |w{self.args}|n") class CmdSetShrubSay(CmdSet): @@ -164,3 +168,85 @@ class Shrub(Puppet): def at_object_creation(self): "Called when a character is first created." self.cmdset.add(CmdSetShrubSay, persistent=True) + + def find_actor(self, msg, default=None): + """ + Extract the character performing the action from the text. + """ + if default: + return default + + m = match(r".*\|b(.*?)\|n.*", msg) + if m: + return m.group(1) + + def capitalize_msg(self, msg, msg_type=None): + """ + If msg is lowercase, capitalize it. + + Maybe prepend a 'The' to the front. + """ + if msg_type == 'traverse' or msg_type == 'teleport': + if match(r"^(\|[A-z])?[aeiou]", msg): + return "An " + msg + else: + return "A " + msg + elif msg_type == 'say' and match(r"^\|b[a-z]", msg): + return "The " + msg + elif match(r"^(\|[A-z])[a-z]", msg) and len(msg) > 2: + return msg[0:1] + msg[2].upper() + msg[3:] + elif match(r"^(\|[A-z])?[a-z]", msg) and len(msg) > 2: + return msg[0].upper() + msg[1:] + return msg + + def to_html(self, msg, msg_type=None): + """ + Convert the 'msg' to an HTML formatted string. + """ + # Bold anything white: + msg = sub(r"\|w(.*?)\|n", '\\1', msg) + # Remove the rest: + msg = sub(r"\|[A-z]", '', msg) + # Remove initial or final carriage returns: + msg = sub(r"^\n", '', msg) + msg = sub(r"\n$", '', msg) + # Convert the carriage returns: + msg = sub(r"\n", '
\n', msg) + + if msg_type == 'look': + msg = msg[0].upper + msg[1:] + return f"

{msg}

\n" + return f"

{msg}

\n" + + def record_msg(self, text, actor=None): + msg_type = None + + if isinstance(text, tuple): + if text[1] and isinstance(text[1], dict): + msg_type = text[1]['type'] + msg = self.capitalize_msg(text[0], msg_type) + else: + msg = self.capitalize_msg(str(text)) + + actor = self.find_actor(msg, actor) + msg = self.to_html(msg, msg_type) + filename = datetime.today().strftime('transcripts/%Y-%m-%d.html') + if not exists(filename): + copyfile("transcripts/header-template.html", filename) + + with open(filename, "a") as myfile: + myfile.write(msg) + + # Follow-up Actions ... + if match(r".* arrives .*", msg) and actor and \ + msg_type in ('traverse', 'teleport'): + self.execute_cmd(f"look {actor}") + + def msg(self, text=None, from_obj=None, session=None, **kwargs): + """ + Record everything that happens in the room for a transcript. + + Some key events may trigger more actions to get more information. + """ + self.record_msg(text, from_obj) + super().msg(text, from_obj, session)