Better reporting user data

And fix a bug in reading books.

With a webclient experiment.
This commit is contained in:
Howard Abrams 2025-08-18 17:14:19 -07:00
parent ea68f8f3b9
commit e2a43b8694
3 changed files with 43 additions and 22 deletions

View file

@ -501,7 +501,11 @@ class CmdRead(Command):
buf = myfile.read() buf = myfile.read()
session = reader.sessions.get()[0] session = reader.sessions.get()[0]
width = self.client_width() width = self.client_width()
tidied = md_to_evennia(buf, reader.utf(), width)
if reader.is_webclient():
tidied = md_to_html(buf)
else:
tidied = md_to_evennia(buf, reader.is_utf(), width)
if prefix: if prefix:
tidied = prefix + "|/" + tidied tidied = prefix + "|/" + tidied
EvMore(reader, tidied, session=session, EvMore(reader, tidied, session=session,
@ -509,9 +513,19 @@ class CmdRead(Command):
def md_to_evennia(text, utf, width): def md_to_evennia(text, utf, width):
brk = '' if utf else '-' brk = '' if utf else '-'
no_data = sub(r">.*[\n\r]+", "", text, MULTILINE) line_brk = '|W' + (brk * width) + '|n'
return sub(r"#.*", '|W' + (brk * width) + '|n', breaks = [line_brk if line.startswith("#") else line
no_data, MULTILINE) for line in md_preprocessor(text)]
return "\n".join(breaks)
def md_to_html(text):
breaks = ["<br/>" if line.startswith("#") else line
for line in md_preprocessor(text)]
return "\n".join(breaks)
def md_preprocessor(text):
lines = text.splitlines()
return [line for line in lines if not line.startswith(">")]
class CmdTake(CmdGet, NumberedTargetCommand): class CmdTake(CmdGet, NumberedTargetCommand):
""" """

View file

@ -22,6 +22,7 @@ from evennia.utils.search import (search_object, search_account,
search_objects_by_typeclass, search_objects_by_typeclass,
search_channel) search_channel)
from utils.user_info import location
from utils.word_list import routput, choices, fix_msg from utils.word_list import routput, choices, fix_msg
from typeclasses.objects import Object, ObjectParent from typeclasses.objects import Object, ObjectParent
from typeclasses.tutorial import TutorBird, TutorialState from typeclasses.tutorial import TutorBird, TutorialState
@ -76,7 +77,7 @@ class Character(Object, GenderCharacter, ContribRPCharacter):
self.create_ticket() self.create_ticket()
self.create_pouch() self.create_pouch()
def utf(self): def is_utf(self):
""" """
Return True if character's user encoding is UTF-8. Return True if character's user encoding is UTF-8.
""" """
@ -86,6 +87,16 @@ class Character(Object, GenderCharacter, ContribRPCharacter):
return flags.get("ENCODING") == 'utf-8' return flags.get("ENCODING") == 'utf-8'
return False return False
def is_webclient(self):
"""
Return True if using the 'web' protocol.
"""
session = self.sessions.get()[0]
if session:
prot = session.protocol_key
return prot == 'web'
return False
def delete_inv(self, typeclass): def delete_inv(self, typeclass):
""" """
Delete items from a character's inventory of typeclass. Delete items from a character's inventory of typeclass.
@ -152,21 +163,6 @@ class Character(Object, GenderCharacter, ContribRPCharacter):
self.delete_inv("typeclasses.drinkables.Cocktail") self.delete_inv("typeclasses.drinkables.Cocktail")
self.move_to(search_object("mp05").first(), quiet=True, use_destination=True) self.move_to(search_object("mp05").first(), quiet=True, use_destination=True)
def location_info(self):
"""
Return string containing location information from IP address.
"""
ip_address = self.sessions.get()[0].address
if ip_address == "127.0.0.1":
return "(from localhost)"
response = requests.get(f'https://ipapi.co/{ip_address}/json/',
timeout=1).json()
city = response.get("city") or "Unknown City"
region = response.get("region") or "Unknown Region"
country = response.get("country_name") or "Unknown Country"
return f"(from {city}, {region}, {country})"
def at_post_puppet(self, **kwargs): def at_post_puppet(self, **kwargs):
""" """
Setup the character based for _this world_. Setup the character based for _this world_.
@ -185,7 +181,11 @@ class Character(Object, GenderCharacter, ContribRPCharacter):
name = self.name name = self.name
desc = self.db._sdesc desc = self.db._sdesc
where = self.location_info() (city, region, country, _) = location(self)
if country:
where = f"(from {city}, {region}, {country})"
else:
where = "(from localhost)"
msg = f"{name}, the {desc}, played by `{player}`, connected {where}." msg = f"{name}, the {desc}, played by `{player}`, connected {where}."
logger.info(msg) logger.info(msg)

View file

@ -9,6 +9,7 @@ from evennia.prototypes.spawner import spawn
from typeclasses.objects import Object from typeclasses.objects import Object
from typeclasses.consumables import Producer from typeclasses.consumables import Producer
from utils.user_info import location
from utils.word_list import routput from utils.word_list import routput
from commands.misc import CmdSetWrite from commands.misc import CmdSetWrite
@ -117,7 +118,13 @@ class WriteableBook(Book):
myfile.write(message) myfile.write(message)
myfile.write("\n\n") myfile.write("\n\n")
myfile.write(f"> User: {writer.account.name}\n") myfile.write(f"> User: {writer.account.name}\n")
myfile.write(f"> Addr: {session.protocol_key}:{session.address}\n") myfile.write(f"> Desc: {writer.db._sdesc}\n")
(city, region, country, _) = location(self)
if country:
myfile.write(f"> From: {city}, {region}, {country}")
else:
myfile.write("> From: localhost")
myfile.write(f"> Cmds: {session.cmd_total}\n\n") myfile.write(f"> Cmds: {session.cmd_total}\n\n")
def do_write_end(self, writer): def do_write_end(self, writer):