From b6f8e9db788a6e022cdcc4d02344a58bde88cb55 Mon Sep 17 00:00:00 2001 From: Sad Date: Sat, 12 Oct 2024 19:24:19 +0800 Subject: [PATCH 1/5] Update z_character.js --- roll/z_character.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/roll/z_character.js b/roll/z_character.js index cfb9f5f4..a4e20f66 100644 --- a/roll/z_character.js +++ b/roll/z_character.js @@ -5,6 +5,8 @@ if (!process.env.mongoURL) { let variables = {}; const mathjs = require('mathjs'); const rollDice = require('./rollbase').rollDiceCommand; +const rollDiceCoc = require('./2_coc').rollDiceCommand; +const rollDiceAdv = require('./0_advroll').rollDiceCommand; const schema = require('../modules/schema.js'); const VIP = require('../modules/veryImportantPerson'); const FUNCTION_LIMIT = [4, 20, 20, 30, 30, 99, 99, 99]; @@ -25,6 +27,8 @@ const regexState = new RegExp(/state\[(.*?)\]~/, 'i'); const regexRoll = new RegExp(/roll\[(.*?)\]~/, 'i'); const regexNotes = new RegExp(/notes\[(.*?)\]~/, 'i'); const re = new RegExp(/(.*?):(.*?)(;|$)/, 'ig'); +const regexRollDice = new RegExp(/<([^<>]*)>/, 'ig'); + const opt = { upsert: true, runValidators: true @@ -74,6 +78,7 @@ const getHelpMessage = async function () { .ch 項目名稱 (數字) - 可以立即把如HP變成該數字 .ch 項目名稱 (+-*/數字) - 可以立即對如HP進行四則運算 .ch 項目名稱 (+-*/xDy) - 可以對如HP進行擲骰四則運算 +.ch 項目名稱 - 可以 .ch set 項目名稱 新內容 - 直接更改內容 .ch show - 顯示角色卡的state 和roll 內容 .ch showall - 顯示角色卡的所有內容 @@ -580,6 +585,7 @@ const rollDiceCommand = async function ({ tempMain = await mainCharacter(doc, mainMsg); rply = Object.assign({}, rply, tempMain) rply.characterName = doc.name; + console.log('rply', rply) return rply; default: break; @@ -612,6 +618,26 @@ function handleRequestRollingChMode(doc) { async function mainCharacter(doc, mainMsg) { mainMsg.shift(); + let msgString = mainMsg.match(regexRollDice); + console.log('msgString', msgString) + + + + let temp = await rollDice({ + mainMsg: ["1d3"], + inputStr: "1d3" + + }) + let temp2 = await rollDiceCoc({ + mainMsg: ["1d3"], + inputStr: "1d3" + }) + let temp3 = await rollDiceAdv({ + mainMsg: ["1d3"], + inputStr: "1d3" + }) + + let findState = []; let findNotes = []; let findRoll = {}; From 656343f1312d53d618d39519eb63a51c58541c0b Mon Sep 17 00:00:00 2001 From: Sad Date: Sun, 13 Oct 2024 04:06:23 +0800 Subject: [PATCH 2/5] Update z_character.js --- roll/z_character.js | 53 +++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/roll/z_character.js b/roll/z_character.js index a4e20f66..b3605209 100644 --- a/roll/z_character.js +++ b/roll/z_character.js @@ -582,10 +582,9 @@ const rollDiceCommand = async function ({ * */ - tempMain = await mainCharacter(doc, mainMsg); + tempMain = await mainCharacter(doc, mainMsg, inputStr); rply = Object.assign({}, rply, tempMain) rply.characterName = doc.name; - console.log('rply', rply) return rply; default: break; @@ -616,28 +615,10 @@ function handleRequestRollingChMode(doc) { return text; } -async function mainCharacter(doc, mainMsg) { +async function mainCharacter(doc, mainMsg, inputStr) { + let tempMsg = await replacePlaceholders(mainMsg, inputStr) + mainMsg = tempMsg.split(/\s+/); mainMsg.shift(); - let msgString = mainMsg.match(regexRollDice); - console.log('msgString', msgString) - - - - let temp = await rollDice({ - mainMsg: ["1d3"], - inputStr: "1d3" - - }) - let temp2 = await rollDiceCoc({ - mainMsg: ["1d3"], - inputStr: "1d3" - }) - let temp3 = await rollDiceAdv({ - mainMsg: ["1d3"], - inputStr: "1d3" - }) - - let findState = []; let findNotes = []; let findRoll = {}; @@ -960,6 +941,32 @@ async function Merge(target, source, prop, updateMode) { } +async function replacePlaceholders(mainMsg, inputStr) { + const matches = [...inputStr.matchAll(regexRollDice)]; + + const results = await Promise.all(matches.map(async (match) => { + const content = match[1]; + const contentSplit = content.split(/\s+/); + const [resultOne, resultTwo, resultThree] = await Promise.all([ + await rollDice({ mainMsg: contentSplit, inputStr: content }), + await rollDiceCoc({ mainMsg: contentSplit, inputStr: content }), + await rollDiceAdv({ mainMsg: contentSplit, inputStr: content }) + ]); + const texts = [resultOne?.text, resultTwo?.text, resultThree?.text]; + const numbers = texts + .map(text => (text ? text.match(/(\d+)(?=\D*$)/) : null)) + .filter(num => num !== null) + .map(num => num[0]); + return numbers.length > 0 ? numbers[numbers.length - 1] : match[0]; + })); + + let resultString = inputStr; + matches.forEach((match, index) => { + resultString = resultString.replace(match[0], results[index]); + }); + return resultString; +} + async function replaceAsync(str, regex, asyncFn) { const promises = []; str.replace(regex, (match, ...args) => { From 1dd0a6d3ffb0770fe8f03f25cf4d1bae94237d7c Mon Sep 17 00:00:00 2001 From: Sad Date: Mon, 14 Oct 2024 12:33:30 +0800 Subject: [PATCH 3/5] Update z_character.js --- roll/z_character.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/roll/z_character.js b/roll/z_character.js index b3605209..dfa02740 100644 --- a/roll/z_character.js +++ b/roll/z_character.js @@ -616,7 +616,7 @@ function handleRequestRollingChMode(doc) { } async function mainCharacter(doc, mainMsg, inputStr) { - let tempMsg = await replacePlaceholders(mainMsg, inputStr) + let tempMsg = await replacePlaceholders(mainMsg, inputStr, doc) mainMsg = tempMsg.split(/\s+/); mainMsg.shift(); let findState = []; @@ -941,9 +941,11 @@ async function Merge(target, source, prop, updateMode) { } -async function replacePlaceholders(mainMsg, inputStr) { +async function replacePlaceholders(mainMsg, inputStr, doc) { const matches = [...inputStr.matchAll(regexRollDice)]; + let resutltState = await findObject(doc.state, mainMsg[name]); + const results = await Promise.all(matches.map(async (match) => { const content = match[1]; const contentSplit = content.split(/\s+/); From cd0130e7263835f22504cc7b71b77f24a056888e Mon Sep 17 00:00:00 2001 From: Sad Date: Mon, 14 Oct 2024 14:48:55 +0800 Subject: [PATCH 4/5] with match state --- roll/z_character.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/roll/z_character.js b/roll/z_character.js index dfa02740..0c8907e9 100644 --- a/roll/z_character.js +++ b/roll/z_character.js @@ -944,22 +944,34 @@ async function Merge(target, source, prop, updateMode) { async function replacePlaceholders(mainMsg, inputStr, doc) { const matches = [...inputStr.matchAll(regexRollDice)]; - let resutltState = await findObject(doc.state, mainMsg[name]); - - const results = await Promise.all(matches.map(async (match) => { + const replacedMatches = await Promise.all(matches.map(async (match) => { const content = match[1]; const contentSplit = content.split(/\s+/); + let replacedContent = content; + + for (const str of contentSplit) { + const result = await findObject(doc.state, str); + if (result !== undefined) { + replacedContent = replacedContent.replace(str, result.itemA); + } + } + + return replacedContent; + })); + + const results = await Promise.all(replacedMatches.map(async (match) => { + const contentSplit = match.split(/\s+/); const [resultOne, resultTwo, resultThree] = await Promise.all([ - await rollDice({ mainMsg: contentSplit, inputStr: content }), - await rollDiceCoc({ mainMsg: contentSplit, inputStr: content }), - await rollDiceAdv({ mainMsg: contentSplit, inputStr: content }) + await rollDice({ mainMsg: contentSplit, inputStr: match }), + await rollDiceCoc({ mainMsg: contentSplit, inputStr: match }), + await rollDiceAdv({ mainMsg: contentSplit, inputStr: match }) ]); const texts = [resultOne?.text, resultTwo?.text, resultThree?.text]; const numbers = texts .map(text => (text ? text.match(/(\d+)(?=\D*$)/) : null)) .filter(num => num !== null) .map(num => num[0]); - return numbers.length > 0 ? numbers[numbers.length - 1] : match[0]; + return numbers.length > 0 ? numbers[numbers.length - 1] : match; })); let resultString = inputStr; @@ -969,6 +981,8 @@ async function replacePlaceholders(mainMsg, inputStr, doc) { return resultString; } + + async function replaceAsync(str, regex, asyncFn) { const promises = []; str.replace(regex, (match, ...args) => { From 8a9d72749704635c786489edd0e9d099fcc48f00 Mon Sep 17 00:00:00 2001 From: Sad Date: Tue, 15 Oct 2024 10:39:13 +0800 Subject: [PATCH 5/5] Update z_character.js --- roll/z_character.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roll/z_character.js b/roll/z_character.js index 0c8907e9..3564a04c 100644 --- a/roll/z_character.js +++ b/roll/z_character.js @@ -84,8 +84,8 @@ const getHelpMessage = async function () { .ch showall - 顯示角色卡的所有內容 .ch button - Discord限定,可以產生按鈕指令,會調用.ch 指令 -----範例及運算式----- -角色卡還可以進行運算,詳情請看 -https://github.com/hktrpg/TG.line.Discord.Roll.Bot/wiki/Character-Card ` +角色卡還可以進行運算,如<> {},詳情請看 +https://bothelp.hktrpg.com/hktrpg-guan-fang-shi-yong-jiao-xue/trpg-gong-neng/kai-shi-jin-hang-trpg/jiao-se-ka ` } const initialize = function () {