Add persistent web chat log
This commit is contained in:
1 parent
9be381c2de
commit
492238aa22
12 files changed
+506
-51
No files matched your search
@@ -0,0 +1,35 @@
|
||||
"""Small shared in-memory chat history for the web dashboard."""
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
MAX_CHAT_EVENTS = 200
|
||||
_lock = threading.RLock()
|
||||
_last_event_id = 0
|
||||
|
||||
|
||||
def append_chat_event(global_state, event_type, message, sender=None):
|
||||
global _last_event_id
|
||||
with _lock:
|
||||
event_id = max(time.time_ns(), _last_event_id + 1)
|
||||
_last_event_id = event_id
|
||||
event = {
|
||||
'id': str(event_id),
|
||||
'timestamp': time.time(),
|
||||
'type': event_type,
|
||||
'message': str(message),
|
||||
}
|
||||
if sender:
|
||||
event['sender'] = str(sender)
|
||||
events = global_state.get('chat_log')
|
||||
if not isinstance(events, list):
|
||||
events = []
|
||||
global_state.chat_log = events
|
||||
events.append(event)
|
||||
del events[:-MAX_CHAT_EVENTS]
|
||||
return event
|
||||
|
||||
|
||||
def snapshot_chat_log(global_state):
|
||||
with _lock:
|
||||
return [dict(event) for event in global_state.get('chat_log', ())]
|
||||
@@ -9,6 +9,7 @@ from minecraft.networking.modern import (
|
||||
CancelPostjoinDialog, ChatCommand, PlayGameEventPacket, PlayShowDialog,
|
||||
PlayLogin, SubmitPostjoinLoginDialog, SystemChat, TeleportPosition,
|
||||
)
|
||||
from mosfet.chat_log import append_chat_event
|
||||
from mosfet.protocol.managers import WorldState
|
||||
from mosfet.vector import Point3D
|
||||
|
||||
@@ -63,10 +64,21 @@ def component_text(value):
|
||||
if 'translate' in fields:
|
||||
translation = component_text(fields['translate'])
|
||||
arguments = fields.get('with', [])
|
||||
if translation.startswith(('death.attack.', 'death.fell.')):
|
||||
from mosfet.info.death_messages import translate_death_message
|
||||
translated = translate_death_message(
|
||||
translation, arguments, component_text)
|
||||
if translated is not None:
|
||||
return translated
|
||||
if translation == 'commands.message.display.incoming' and len(arguments) >= 2:
|
||||
return '{} whispers to you: {}'.format(
|
||||
component_text(arguments[0]),
|
||||
component_text(arguments[1]))
|
||||
if translation in ('multiplayer.player.joined',
|
||||
'multiplayer.player.left') and arguments:
|
||||
name = component_text(arguments[0])
|
||||
action = 'joined' if translation.endswith('.joined') else 'left'
|
||||
return '{} {} the game'.format(name, action)
|
||||
return translation + component_text(arguments)
|
||||
return ''.join(component_text(v) for v in fields.values())
|
||||
if isinstance(value, (list, tuple)):
|
||||
@@ -74,6 +86,19 @@ def component_text(value):
|
||||
return str(value)
|
||||
|
||||
|
||||
def _system_chat_log_entry(message):
|
||||
if message.endswith(' joined the game'):
|
||||
return 'join', message[:-len(' joined the game')], message
|
||||
if message.endswith(' left the game'):
|
||||
return 'leave', message[:-len(' left the game')], message
|
||||
from mosfet.protocol.modern import parse_incoming_private_message
|
||||
incoming = parse_incoming_private_message(message)
|
||||
if incoming:
|
||||
sender, body = incoming
|
||||
return 'dm', sender, body
|
||||
return 'system', None, message
|
||||
|
||||
|
||||
def main(global_state=None, *, server=None, port=None, username=None,
|
||||
password=None):
|
||||
server = server or os.environ.get('SERVER', '127.0.0.1')
|
||||
@@ -182,6 +207,8 @@ def main(global_state=None, *, server=None, port=None, username=None,
|
||||
chat_handled = False
|
||||
if global_state is not None:
|
||||
global_state.last_chat = message
|
||||
event_type, sender, log_message = _system_chat_log_entry(message)
|
||||
append_chat_event(global_state, event_type, log_message, sender)
|
||||
chat = global_state.get('chat')
|
||||
if chat is not None:
|
||||
chat_handled = chat.handle_system_chat(packet, message)
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
"""English templates for Minecraft 26.3 death chat components."""
|
||||
|
||||
DEATH_MESSAGE_TEMPLATES = {
|
||||
'death.attack.anvil': '{0} was squashed by a falling anvil',
|
||||
'death.attack.anvil.player': '{0} was squashed by a falling anvil while fighting {1}',
|
||||
'death.attack.arrow': '{0} was shot by {1}',
|
||||
'death.attack.arrow.item': '{0} was shot by {1} using {2}',
|
||||
'death.attack.badRespawnPoint.message': '{0} was killed by {1}',
|
||||
'death.attack.cactus': '{0} was pricked to death',
|
||||
'death.attack.cactus.player': '{0} walked into a cactus while trying to escape {1}',
|
||||
'death.attack.cramming': '{0} was squished too much',
|
||||
'death.attack.cramming.player': '{0} was squashed by {1}',
|
||||
'death.attack.dragonBreath': "{0} was roasted in dragon's breath",
|
||||
'death.attack.dragonBreath.player': "{0} was roasted in dragon's breath by {1}",
|
||||
'death.attack.drown': '{0} drowned',
|
||||
'death.attack.drown.player': '{0} drowned while trying to escape {1}',
|
||||
'death.attack.dryout': '{0} died from dehydration',
|
||||
'death.attack.dryout.player': '{0} died from dehydration while trying to escape {1}',
|
||||
'death.attack.even_more_magic': '{0} was killed by even more magic',
|
||||
'death.attack.explosion': '{0} blew up',
|
||||
'death.attack.explosion.player': '{0} was blown up by {1}',
|
||||
'death.attack.explosion.player.item': '{0} was blown up by {1} using {2}',
|
||||
'death.attack.fall': '{0} hit the ground too hard',
|
||||
'death.attack.fall.player': '{0} hit the ground too hard while trying to escape {1}',
|
||||
'death.attack.fallingBlock': '{0} was squashed by a falling block',
|
||||
'death.attack.fallingBlock.player': '{0} was squashed by a falling block while fighting {1}',
|
||||
'death.attack.fallingStalactite': '{0} was skewered by a falling stalactite',
|
||||
'death.attack.fallingStalactite.player': '{0} was skewered by a falling stalactite while fighting {1}',
|
||||
'death.attack.fireball': '{0} was fireballed by {1}',
|
||||
'death.attack.fireball.item': '{0} was fireballed by {1} using {2}',
|
||||
'death.attack.fireworks': '{0} went off with a bang',
|
||||
'death.attack.fireworks.item': '{0} went off with a bang due to a firework fired from {2} by {1}',
|
||||
'death.attack.fireworks.player': '{0} went off with a bang while fighting {1}',
|
||||
'death.attack.flyIntoWall': '{0} experienced kinetic energy',
|
||||
'death.attack.flyIntoWall.player': '{0} experienced kinetic energy while trying to escape {1}',
|
||||
'death.attack.freeze': '{0} froze to death',
|
||||
'death.attack.freeze.player': '{0} was frozen to death by {1}',
|
||||
'death.attack.generic': '{0} died',
|
||||
'death.attack.generic.player': '{0} died because of {1}',
|
||||
'death.attack.genericKill': '{0} was killed',
|
||||
'death.attack.genericKill.player': '{0} was killed while fighting {1}',
|
||||
'death.attack.hotFloor': '{0} discovered the floor was lava',
|
||||
'death.attack.hotFloor.player': '{0} walked into the danger zone due to {1}',
|
||||
'death.attack.indirectMagic': '{0} was killed by {1} using magic',
|
||||
'death.attack.indirectMagic.item': '{0} was killed by {1} using {2}',
|
||||
'death.attack.inFire': '{0} went up in flames',
|
||||
'death.attack.inFire.player': '{0} walked into fire while fighting {1}',
|
||||
'death.attack.inWall': '{0} suffocated in a wall',
|
||||
'death.attack.inWall.player': '{0} suffocated in a wall while fighting {1}',
|
||||
'death.attack.lava': '{0} tried to swim in lava',
|
||||
'death.attack.lava.player': '{0} tried to swim in lava to escape {1}',
|
||||
'death.attack.lightningBolt': '{0} was struck by lightning',
|
||||
'death.attack.lightningBolt.player': '{0} was struck by lightning while fighting {1}',
|
||||
'death.attack.mace_smash': '{0} was smashed by {1}',
|
||||
'death.attack.mace_smash.item': '{0} was smashed by {1} with {2}',
|
||||
'death.attack.magic': '{0} was killed by magic',
|
||||
'death.attack.magic.player': '{0} was killed by magic while trying to escape {1}',
|
||||
'death.attack.mob': '{0} was slain by {1}',
|
||||
'death.attack.mob.item': '{0} was slain by {1} using {2}',
|
||||
'death.attack.onFire': '{0} burned to death',
|
||||
'death.attack.onFire.item': '{0} was burned to a crisp while fighting {1} wielding {2}',
|
||||
'death.attack.onFire.player': '{0} was burned to a crisp while fighting {1}',
|
||||
'death.attack.outOfWorld': '{0} fell out of the world',
|
||||
'death.attack.outOfWorld.player': "{0} didn't want to live in the same world as {1}",
|
||||
'death.attack.outsideBorder': '{0} left the confines of this world',
|
||||
'death.attack.outsideBorder.player': '{0} left the confines of this world while fighting {1}',
|
||||
'death.attack.player': '{0} was slain by {1}',
|
||||
'death.attack.player.item': '{0} was slain by {1} using {2}',
|
||||
'death.attack.sonic_boom': '{0} was obliterated by a sonically-charged shriek',
|
||||
'death.attack.sonic_boom.item': '{0} was obliterated by a sonically-charged shriek while trying to escape {1} wielding {2}',
|
||||
'death.attack.sonic_boom.player': '{0} was obliterated by a sonically-charged shriek while trying to escape {1}',
|
||||
'death.attack.spear': '{0} was speared by {1}',
|
||||
'death.attack.spear.item': '{0} was speared by {1} using {2}',
|
||||
'death.attack.stalagmite': '{0} was impaled on a stalagmite',
|
||||
'death.attack.stalagmite.player': '{0} was impaled on a stalagmite while fighting {1}',
|
||||
'death.attack.starve': '{0} starved to death',
|
||||
'death.attack.starve.player': '{0} starved to death while fighting {1}',
|
||||
'death.attack.sting': '{0} was stung to death',
|
||||
'death.attack.sting.item': '{0} was stung to death by {1} using {2}',
|
||||
'death.attack.sting.player': '{0} was stung to death by {1}',
|
||||
'death.attack.sulfurCubeHot': '{0} died because not just the floor is lava',
|
||||
'death.attack.sulfurCubeHot.item': '{1} showed {0} that not just the floor is lava using {2}',
|
||||
'death.attack.sulfurCubeHot.player': '{1} showed {0} that not just the floor is lava',
|
||||
'death.attack.sweetBerryBush': '{0} was poked to death by a sweet berry bush',
|
||||
'death.attack.sweetBerryBush.player': '{0} was poked to death by a sweet berry bush while trying to escape {1}',
|
||||
'death.attack.thorns': '{0} was killed while trying to hurt {1}',
|
||||
'death.attack.thorns.item': '{0} was killed by {2} while trying to hurt {1}',
|
||||
'death.attack.thrown': '{0} was pummeled by {1}',
|
||||
'death.attack.thrown.item': '{0} was pummeled by {1} using {2}',
|
||||
'death.attack.trident': '{0} was impaled by {1}',
|
||||
'death.attack.trident.item': '{0} was impaled by {1} with {2}',
|
||||
'death.attack.wither': '{0} withered away',
|
||||
'death.attack.wither.player': '{0} withered away while fighting {1}',
|
||||
'death.attack.witherSkull': '{0} was shot by a skull from {1}',
|
||||
'death.attack.witherSkull.item': '{0} was shot by a skull from {1} using {2}',
|
||||
'death.fell.accident.generic': '{0} fell from a high place',
|
||||
'death.fell.accident.ladder': '{0} fell off a ladder',
|
||||
'death.fell.accident.other_climbable': '{0} fell while climbing',
|
||||
'death.fell.accident.scaffolding': '{0} fell off scaffolding',
|
||||
'death.fell.accident.twisting_vines': '{0} fell off some twisting vines',
|
||||
'death.fell.accident.vines': '{0} fell off some vines',
|
||||
'death.fell.accident.weeping_vines': '{0} fell off some weeping vines',
|
||||
'death.fell.assist': '{0} was doomed to fall by {1}',
|
||||
'death.fell.assist.item': '{0} was doomed to fall by {1} using {2}',
|
||||
'death.fell.finish': '{0} fell too far and was finished by {1}',
|
||||
'death.fell.finish.item': '{0} fell too far and was finished by {1} using {2}',
|
||||
'death.fell.killer': '{0} was doomed to fall',
|
||||
}
|
||||
|
||||
|
||||
def translate_death_message(translation, arguments, render_component):
|
||||
"""Format a known death translation with its component arguments."""
|
||||
template = DEATH_MESSAGE_TEMPLATES.get(translation)
|
||||
if template is None:
|
||||
return None
|
||||
values = [render_component(argument) for argument in arguments]
|
||||
try:
|
||||
return template.format(*values)
|
||||
except (IndexError, KeyError, ValueError):
|
||||
return None
|
||||
@@ -10,6 +10,7 @@ from minecraft.networking.types import (
|
||||
from minecraft.networking.modern import (
|
||||
ChatCommand, ClientboundPlayerChatPacket, ServerboundChatPacket, SystemChat,
|
||||
)
|
||||
from mosfet.chat_log import append_chat_event
|
||||
|
||||
|
||||
class UpdateTimePacket(Packet):
|
||||
@@ -106,6 +107,8 @@ class ModernChatManager:
|
||||
)
|
||||
)
|
||||
label = '[DM] ' if is_dm else ''
|
||||
append_chat_event(
|
||||
self.g, 'dm' if is_dm else 'chat', packet.message, sender_name)
|
||||
print('[chat] {}{}: {}'.format(label, sender_name, packet.message),
|
||||
flush=True)
|
||||
if self.handler:
|
||||
@@ -140,6 +143,8 @@ class ModernChatManager:
|
||||
self.g.connection.write_packet(
|
||||
ChatCommand(command=message[1:]), force=True)
|
||||
else:
|
||||
append_chat_event(self.g, 'outgoing', message,
|
||||
self.g.get('name', 'Mosfet'))
|
||||
packet = ServerboundChatPacket(
|
||||
message=message, timestamp=int(time.time() * 1000))
|
||||
self.g.connection.write_packet(packet, force=True)
|
||||
Reference in new issue
Block a user