summaryrefslogtreecommitdiff
path: root/src/client/scripts/hpml/lib.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2021-02-07 18:23:23 +0900
committersyuilo <syuilotan@yahoo.co.jp>2021-02-07 18:23:23 +0900
commit49e6c2ed75a7b960615a8d680046b5aab11072f1 (patch)
tree75e65504c619ef31c7d917a0130731c5d9378617 /src/client/scripts/hpml/lib.ts
parentMerge branch 'develop' (diff)
parent12.69.0 (diff)
downloadmisskey-49e6c2ed75a7b960615a8d680046b5aab11072f1.tar.gz
misskey-49e6c2ed75a7b960615a8d680046b5aab11072f1.tar.bz2
misskey-49e6c2ed75a7b960615a8d680046b5aab11072f1.zip
Merge branch 'develop'
Diffstat (limited to 'src/client/scripts/hpml/lib.ts')
-rw-r--r--src/client/scripts/hpml/lib.ts80
1 files changed, 71 insertions, 9 deletions
diff --git a/src/client/scripts/hpml/lib.ts b/src/client/scripts/hpml/lib.ts
index 11e4f2fc39..7454562184 100644
--- a/src/client/scripts/hpml/lib.ts
+++ b/src/client/scripts/hpml/lib.ts
@@ -2,9 +2,31 @@ import * as tinycolor from 'tinycolor2';
import Chart from 'chart.js';
import { Hpml } from './evaluator';
import { values, utils } from '@syuilo/aiscript';
-import { Block, Fn, HpmlScope } from '.';
+import { Fn, HpmlScope } from '.';
+import { Expr } from './expr';
import * as seedrandom from 'seedrandom';
+import {
+ faShareAlt,
+ faPlus,
+ faMinus,
+ faTimes,
+ faDivide,
+ faQuoteRight,
+ faEquals,
+ faGreaterThan,
+ faLessThan,
+ faGreaterThanEqual,
+ faLessThanEqual,
+ faNotEqual,
+ faDice,
+ faExchangeAlt,
+ faRecycle,
+ faIndent,
+ faCalculator,
+} from '@fortawesome/free-solid-svg-icons';
+import { faFlag } from '@fortawesome/free-regular-svg-icons';
+
// https://stackoverflow.com/questions/38493564/chart-area-background-color-chartjs
Chart.pluginService.register({
beforeDraw: (chart, easing) => {
@@ -125,7 +147,47 @@ export function initAiLib(hpml: Hpml) {
};
}
-export function initHpmlLib(block: Block, scope: HpmlScope, randomSeed: string, visitor?: any) {
+export const funcDefs: Record<string, { in: any[]; out: any; category: string; icon: any; }> = {
+ if: { in: ['boolean', 0, 0], out: 0, category: 'flow', icon: faShareAlt, },
+ for: { in: ['number', 'function'], out: null, category: 'flow', icon: faRecycle, },
+ not: { in: ['boolean'], out: 'boolean', category: 'logical', icon: faFlag, },
+ or: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, },
+ and: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, },
+ add: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faPlus, },
+ subtract: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faMinus, },
+ multiply: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faTimes, },
+ divide: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, },
+ mod: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, },
+ round: { in: ['number'], out: 'number', category: 'operation', icon: faCalculator, },
+ eq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faEquals, },
+ notEq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faNotEqual, },
+ gt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThan, },
+ lt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThan, },
+ gtEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThanEqual, },
+ ltEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThanEqual, },
+ strLen: { in: ['string'], out: 'number', category: 'text', icon: faQuoteRight, },
+ strPick: { in: ['string', 'number'], out: 'string', category: 'text', icon: faQuoteRight, },
+ strReplace: { in: ['string', 'string', 'string'], out: 'string', category: 'text', icon: faQuoteRight, },
+ strReverse: { in: ['string'], out: 'string', category: 'text', icon: faQuoteRight, },
+ join: { in: ['stringArray', 'string'], out: 'string', category: 'text', icon: faQuoteRight, },
+ stringToNumber: { in: ['string'], out: 'number', category: 'convert', icon: faExchangeAlt, },
+ numberToString: { in: ['number'], out: 'string', category: 'convert', icon: faExchangeAlt, },
+ splitStrByLine: { in: ['string'], out: 'stringArray', category: 'convert', icon: faExchangeAlt, },
+ pick: { in: [null, 'number'], out: null, category: 'list', icon: faIndent, },
+ listLen: { in: [null], out: 'number', category: 'list', icon: faIndent, },
+ rannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, },
+ dailyRannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, },
+ seedRannum: { in: [null, 'number', 'number'], out: 'number', category: 'random', icon: faDice, },
+ random: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, },
+ dailyRandom: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, },
+ seedRandom: { in: [null, 'number'], out: 'boolean', category: 'random', icon: faDice, },
+ randomPick: { in: [0], out: 0, category: 'random', icon: faDice, },
+ dailyRandomPick: { in: [0], out: 0, category: 'random', icon: faDice, },
+ seedRandomPick: { in: [null, 0], out: 0, category: 'random', icon: faDice, },
+ DRPWPM: { in: ['stringArray'], out: 'string', category: 'random', icon: faDice, }, // dailyRandomPickWithProbabilityMapping
+};
+
+export function initHpmlLib(expr: Expr, scope: HpmlScope, randomSeed: string, visitor?: any) {
const date = new Date();
const day = `${visitor ? visitor.id : ''} ${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
@@ -166,12 +228,12 @@ export function initHpmlLib(block: Block, scope: HpmlScope, randomSeed: string,
splitStrByLine: (a: string) => a.split('\n'),
pick: (list: any[], i: number) => list[i - 1],
listLen: (list: any[]) => list.length,
- random: (probability: number) => Math.floor(seedrandom(`${randomSeed}:${block.id}`)() * 100) < probability,
- rannum: (min: number, max: number) => min + Math.floor(seedrandom(`${randomSeed}:${block.id}`)() * (max - min + 1)),
- randomPick: (list: any[]) => list[Math.floor(seedrandom(`${randomSeed}:${block.id}`)() * list.length)],
- dailyRandom: (probability: number) => Math.floor(seedrandom(`${day}:${block.id}`)() * 100) < probability,
- dailyRannum: (min: number, max: number) => min + Math.floor(seedrandom(`${day}:${block.id}`)() * (max - min + 1)),
- dailyRandomPick: (list: any[]) => list[Math.floor(seedrandom(`${day}:${block.id}`)() * list.length)],
+ random: (probability: number) => Math.floor(seedrandom(`${randomSeed}:${expr.id}`)() * 100) < probability,
+ rannum: (min: number, max: number) => min + Math.floor(seedrandom(`${randomSeed}:${expr.id}`)() * (max - min + 1)),
+ randomPick: (list: any[]) => list[Math.floor(seedrandom(`${randomSeed}:${expr.id}`)() * list.length)],
+ dailyRandom: (probability: number) => Math.floor(seedrandom(`${day}:${expr.id}`)() * 100) < probability,
+ dailyRannum: (min: number, max: number) => min + Math.floor(seedrandom(`${day}:${expr.id}`)() * (max - min + 1)),
+ dailyRandomPick: (list: any[]) => list[Math.floor(seedrandom(`${day}:${expr.id}`)() * list.length)],
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)],
@@ -185,7 +247,7 @@ export function initHpmlLib(block: Block, scope: HpmlScope, randomSeed: string,
totalFactor += factor;
xs.push({ factor, text });
}
- const r = seedrandom(`${day}:${block.id}`)() * totalFactor;
+ const r = seedrandom(`${day}:${expr.id}`)() * totalFactor;
let stackedFactor = 0;
for (const x of xs) {
if (r >= stackedFactor && r <= stackedFactor + x.factor) {