From c13b173c1848f59c7f84b017c95c0ff6b0a15881 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Sat, 26 Mar 2022 10:13:53 -0700 Subject: [PATCH] Do not parse player messages Signed-off-by: James Ketrenos --- client/src/Chat.js | 81 ++++++++++++++++++++++++------------------ server/routes/games.js | 7 ++-- 2 files changed, 51 insertions(+), 37 deletions(-) diff --git a/client/src/Chat.js b/client/src/Chat.js index c501f67..e882f11 100644 --- a/client/src/Chat.js +++ b/client/src/Chat.js @@ -115,45 +115,56 @@ const Chat = () => { }); const messages = chat.map((item, index) => { - const punctuation = item.message.match(/(\.+$)/); - let period; - if (punctuation) { - period = punctuation[1]; + let message; + /* Do not perform extra parsing on player-generated + * messages */ + if (item.normalChat) { + message =
{item.message}
; } else { - period = ''; - } - let lines = item.message.split('.'); - const message = lines - .filter(line => line.trim() !== '') - .map((line, index) => { - /* If the date is in the future, set it to now */ - const dice = line.match(/^(.*rolled )([1-6])(, ([1-6]))?(.*)$/); - if (dice) { - if (dice[4]) { - return
{dice[1]}, - {dice[5]}{ period }
; - } else { - return
{dice[1]}{dice[5]}{ period }
; - } - } - - let start = line, message; - while (start) { - const resource = start.match(/^(.*)(([0-9]+) (wood|sheep|wheat|stone|brick),?)(.*)$/); - if (resource) { - const count = resource[3] ? parseInt(resource[3]) : 1; - message = <>{resource[5]}{message}; - start = resource[1]; - } else { - message = <>{start}{message}; - start = ''; - } + const punctuation = item.message.match(/(\.+$)/); + let period; + if (punctuation) { + period = punctuation[1]; + } else { + period = ''; } - return
{ message }{ period }
; - }); + let lines = item.message.split('.'); + message = lines + .filter(line => line.trim() !== '') + .map((line, index) => { + /* If the date is in the future, set it to now */ + const dice = line.match(/^(.*rolled )([1-6])(, ([1-6]))?(.*)$/); + if (dice) { + if (dice[4]) { + return
{dice[1]} + , + {dice[5]}{ period }
; + } else { + return
{dice[1]} + {dice[5]}{ period }
; + } + } + + let start = line, message; + while (start) { + const resource = start.match(/^(.*)(([0-9]+) (wood|sheep|wheat|stone|brick),?)(.*)$/); + if (resource) { + const count = resource[3] ? parseInt(resource[3]) : 1; + message = <>{resource[5]}{message}; + start = resource[1]; + } else { + message = <>{start}{message}; + start = ''; + } + } + return
{ message }{ period }
; + }); + } return ( - + { item.color && } diff --git a/server/routes/games.js b/server/routes/games.js index 451753b..86865fb 100755 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -1025,7 +1025,7 @@ const addActivity = (game, session, message) => { game.activities.push({ color: session ? session.color : '', message, date }); } -const addChatMessage = (game, session, message) => { +const addChatMessage = (game, session, message, isNormalChat) => { let now = Date.now(); let lastTime = 0; if (game.chat.length) { @@ -1039,6 +1039,9 @@ const addChatMessage = (game, session, message) => { date: now, message: message }; + if (isNormalChat) { + entry.normalChat = true; + } if (session && session.name) { entry.from = session.name; } @@ -3694,7 +3697,7 @@ router.ws("/ws/:id", async (ws, req) => { case 'chat': console.log(`${short}:${id} - ${data.type} - ${data.message}`) - addChatMessage(game, session, `${session.name}: ${data.message}`); + addChatMessage(game, session, `${session.name}: ${data.message}`, true); parseChatCommands(game, data.message); sendUpdateToPlayers(game, { chat: game.chat }); saveGame(game);