From 7aa66f438f4c7854fa9bacb4c15584920e41c278 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 5 May 2019 20:12:35 +0900 Subject: [PATCH] Resolve #4853 --- locales/ja-JP.yml | 3 +++ src/misc/aiscript/evaluator.ts | 21 +++++++++++++++++++++ src/misc/aiscript/index.ts | 1 + 3 files changed, 25 insertions(+) diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml index 60378df1b2..52e0f7f480 100644 --- a/locales/ja-JP.yml +++ b/locales/ja-JP.yml @@ -2059,6 +2059,9 @@ pages: _seedRandomPick: arg1: "シード" arg2: "リスト" + dailyRPWPM: "確率付きリストからランダムに選択 (ユーザーごとに日替わり)" + _ddailyRPWPM: + arg1: "テキストのリスト" number: "数値" stringToNumber: "テキストを数値に" _stringToNumber: diff --git a/src/misc/aiscript/evaluator.ts b/src/misc/aiscript/evaluator.ts index 78b6acf35c..5e511576c8 100644 --- a/src/misc/aiscript/evaluator.ts +++ b/src/misc/aiscript/evaluator.ts @@ -178,6 +178,27 @@ export class ASEvaluator { seedRandom: (seed: any, probability: number) => Math.floor(seedrandom(seed)() * 100) < probability, seedRannum: (seed: any, min: number, max: number) => min + Math.floor(seedrandom(seed)() * (max - min + 1)), seedRandomPick: (seed: any, list: any[]) => list[Math.floor(seedrandom(seed)() * list.length)], + dailyRPWPM: (list: string[]) => { + const xs = []; + let totalFactor = 0; + for (const x of list) { + const parts = x.split(' '); + const factor = parseInt(parts.pop()!, 10); + const text = parts.join(' '); + totalFactor += factor; + xs.push({ factor, text }); + } + const r = seedrandom(`${day}:${block.id}`)() * totalFactor; + let stackedFactor = 0; + for (const x of xs) { + if (r >= stackedFactor && r <= x.factor) { + return x.text; + } else { + stackedFactor += x.factor; + } + } + return xs[0].text; + }, }; const fnName = block.type; diff --git a/src/misc/aiscript/index.ts b/src/misc/aiscript/index.ts index 1ce09b7bdb..61a6b7b139 100644 --- a/src/misc/aiscript/index.ts +++ b/src/misc/aiscript/index.ts @@ -81,6 +81,7 @@ export const funcDefs: Record = {