feat(client): Improve pages aiscript

This commit is contained in:
syuilo 2020-04-13 23:46:53 +09:00
parent 63225ed0fd
commit a19e252c9e
6 changed files with 61 additions and 37 deletions

View file

@ -42,7 +42,7 @@
"@koa/cors": "3.0.0",
"@koa/multer": "2.0.2",
"@koa/router": "8.0.8",
"@syuilo/aiscript": "0.1.4",
"@syuilo/aiscript": "0.2.0",
"@types/bcryptjs": "2.4.2",
"@types/bull": "3.12.1",
"@types/cbor": "5.0.0",

View file

@ -26,7 +26,7 @@ class Script {
this.aoiScript = aoiScript;
this.onError = onError;
if (this.page.script) {
if (this.page.script && this.aoiScript.aiscript) {
let ast;
try {
ast = parse(this.page.script);
@ -49,8 +49,10 @@ class Script {
});*/
});
} else {
setTimeout(() => {
this.eval();
cb();
}, 1);
}
}
@ -71,7 +73,7 @@ class Script {
}
public callAiScript(fn: string) {
this.aoiScript.aiscript.execFn(this.aoiScript.aiscript.scope.get(fn), []);
if (this.aoiScript.aiscript) this.aoiScript.aiscript.execFn(this.aoiScript.aiscript.scope.get(fn), []);
}
}
@ -103,18 +105,23 @@ export default Vue.extend({
randomSeed: Math.random(),
visitor: this.$store.state.i,
page: this.page,
url: url
url: url,
enableAiScript: !this.$store.state.device.disablePagesScript
}), e => {
console.dir(e);
}, () => {
this.script = s;
});
s.aoiScript.aiscript.scope.opts.onUpdated = (name, value) => {
if (s.aoiScript.aiscript) s.aoiScript.aiscript.scope.opts.onUpdated = (name, value) => {
s.eval();
};
},
beforeDestroy() {
if (this.script.aoiScript.aiscript) this.script.aoiScript.aiscript.abort();
},
methods: {
getPageVars() {
return collectPageVars(this.page.content);

View file

@ -65,6 +65,7 @@
<template #desc><mfm text="🍮🍦🍭🍩🍰🍫🍬🥞🍪"/></template>
</mk-switch>
<mk-switch v-model="showFixedPostForm">{{ $t('showFixedPostForm') }}</mk-switch>
<mk-switch v-model="disablePagesScript">{{ $t('disablePagesScript') }}</mk-switch>
</div>
<div class="_content">
<mk-select v-model="lang">
@ -171,6 +172,11 @@ export default Vue.extend({
set(value) { this.$store.commit('device/set', { key: 'imageNewTab', value }); }
},
disablePagesScript: {
get() { return this.$store.state.device.disablePagesScript; },
set(value) { this.$store.commit('device/set', { key: 'disablePagesScript', value }); }
},
showFixedPostForm: {
get() { return this.$store.state.device.showFixedPostForm; },
set(value) { this.$store.commit('device/set', { key: 'showFixedPostForm', value }); }

View file

@ -17,17 +17,20 @@ export class ASEvaluator {
private variables: Variable[];
private pageVars: PageVar[];
private envVars: Record<keyof typeof envVarsDef, any>;
public aiscript: AiScript;
public aiscript: AiScript | undefined;
private pageVarUpdatedCallback;
private opts: {
randomSeed: string; visitor?: any; page?: any; url?: string;
enableAiScript: boolean;
};
constructor(vm: any, variables: Variable[], pageVars: PageVar[], opts: ASEvaluator['opts']) {
this.variables = variables;
this.pageVars = pageVars;
this.opts = opts;
if (this.opts.enableAiScript) {
this.aiscript = new AiScript({ ...createAiScriptEnv(vm, {
storageKey: 'pages:' + opts.page.id
}), ...{
@ -52,6 +55,7 @@ export class ASEvaluator {
},
maxStep: 16384
});
}
const date = new Date();
@ -79,7 +83,7 @@ export class ASEvaluator {
if (pageVar !== undefined) {
pageVar.value = value;
if (this.pageVarUpdatedCallback) {
this.aiscript.execFn(this.pageVarUpdatedCallback, [values.STR(name), utils.jsToVal(value)]);
if (this.aiscript) this.aiscript.execFn(this.pageVarUpdatedCallback, [values.STR(name), utils.jsToVal(value)]);
}
} else {
throw new AoiScriptError(`No such page var '${name}'`);
@ -142,7 +146,11 @@ export class ASEvaluator {
}
if (block.type === 'aiScriptVar') {
if (this.aiscript) {
return utils.valToJs(this.aiscript.scope.get(block.value));
} else {
return null;
}
}
if (isFnBlock(block)) { // ユーザー関数定義

View file

@ -42,6 +42,7 @@ const defaultDeviceSettings = {
animatedMfm: true,
imageNewTab: false,
showFixedPostForm: false,
disablePagesScript: true,
sfxVolume: 0.3,
sfxNote: 'syuilo/down',
sfxNoteMy: 'syuilo/up',

View file

@ -144,13 +144,15 @@
dependencies:
type-detect "4.0.8"
"@syuilo/aiscript@0.1.4":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@syuilo/aiscript/-/aiscript-0.1.4.tgz#ff027552f32990ae3e29145ce6efe0a7a516b442"
integrity sha512-SMDlBInsGTL3DOe0U394X7na0N6ryYg0RGQPPtCVhXkJpVDZiaqUe5vDO+DkRyuRlkmBbN82LWToou19j/Uv8g==
"@syuilo/aiscript@0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@syuilo/aiscript/-/aiscript-0.2.0.tgz#dcb489bca13f6d965ac86034a45fd46514b1487a"
integrity sha512-N9fYchn3zjtniG9fNmZ81PwYZFdulk+RSBcjDZWBgHsFJQc1wxOCr9hZux/vSXrZ/ZWEzK0loNz1dorl2jJLeA==
dependencies:
"@types/seedrandom" "2.4.28"
autobind-decorator "2.4.0"
chalk "4.0.0"
seedrandom "3.0.5"
uuid "7.0.3"
"@tokenizer/token@^0.1.0", "@tokenizer/token@^0.1.1":