2020-04-12 05:38:19 -05:00
|
|
|
import { utils, values } from '@syuilo/aiscript';
|
|
|
|
|
2020-04-12 05:57:18 -05:00
|
|
|
export function createAiScriptEnv(vm, opts) {
|
2020-04-12 13:23:23 -05:00
|
|
|
let apiRequests = 0;
|
2020-04-12 05:38:19 -05:00
|
|
|
return {
|
2020-04-19 02:28:19 -05:00
|
|
|
USER_ID: vm.$store.getters.isSignedIn ? values.STR(vm.$store.state.i.id) : values.NULL,
|
|
|
|
USER_NAME: vm.$store.getters.isSignedIn ? values.STR(vm.$store.state.i.name) : values.NULL,
|
|
|
|
USER_USERNAME: vm.$store.getters.isSignedIn ? values.STR(vm.$store.state.i.username) : values.NULL,
|
2020-04-12 05:38:19 -05:00
|
|
|
'Mk:dialog': values.FN_NATIVE(async ([title, text, type]) => {
|
|
|
|
await vm.$root.dialog({
|
|
|
|
type: type ? type.value : 'info',
|
|
|
|
title: title.value,
|
|
|
|
text: text.value,
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
'Mk:confirm': values.FN_NATIVE(async ([title, text]) => {
|
|
|
|
const confirm = await vm.$root.dialog({
|
|
|
|
type: 'warning',
|
|
|
|
showCancelButton: true,
|
|
|
|
title: title.value,
|
|
|
|
text: text.value,
|
|
|
|
});
|
|
|
|
return confirm.canceled ? values.FALSE : values.TRUE
|
|
|
|
}),
|
|
|
|
'Mk:api': values.FN_NATIVE(async ([ep, param, token]) => {
|
2020-04-12 13:23:23 -05:00
|
|
|
apiRequests++;
|
|
|
|
if (apiRequests > 16) return values.NULL;
|
2020-04-12 05:38:19 -05:00
|
|
|
const res = await vm.$root.api(ep.value, utils.valToJs(param), token || null);
|
|
|
|
return utils.jsToVal(res);
|
|
|
|
}),
|
2020-04-12 05:57:18 -05:00
|
|
|
'Mk:save': values.FN_NATIVE(([key, value]) => {
|
|
|
|
utils.assertString(key);
|
|
|
|
localStorage.setItem('aiscript:' + opts.storageKey + ':' + key.value, JSON.stringify(utils.valToJs(value)));
|
|
|
|
return values.NULL;
|
|
|
|
}),
|
|
|
|
'Mk:load': values.FN_NATIVE(([key]) => {
|
|
|
|
utils.assertString(key);
|
|
|
|
return utils.jsToVal(JSON.parse(localStorage.getItem('aiscript:' + opts.storageKey + ':' + key.value)));
|
|
|
|
}),
|
2020-04-12 05:38:19 -05:00
|
|
|
};
|
|
|
|
}
|