-
-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathsetVar.ts
More file actions
54 lines (53 loc) · 2.12 KB
/
setVar.ts
File metadata and controls
54 lines (53 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { ISentence } from '@/Core/controller/scene/sceneInterface';
import { IPerform } from '@/Core/Modules/perform/performInterface';
import { webgalStore } from '@/store/store';
import { setStageVar } from '@/store/stageReducer';
import { logger } from '@/Core/util/logger';
import { setScriptManagedGlobalVar } from '@/store/userDataReducer';
import { ActionCreatorWithPayload } from '@reduxjs/toolkit';
import { ISetGameVar } from '@/store/stageInterface';
import { dumpToStorageFast } from '@/Core/controller/storage/storageController';
import { getBooleanArgByKey } from '../util/getSentenceArg';
import { EvaluateExpression } from '../util/evalSentenceFn';
/**
* 设置变量
* @param sentence
*/
export const setVar = (sentence: ISentence): IPerform => {
let setGlobal = getBooleanArgByKey(sentence, 'global') ?? false;
let targetReducerFunction: ActionCreatorWithPayload<ISetGameVar, string>;
if (setGlobal) {
targetReducerFunction = setScriptManagedGlobalVar;
} else {
targetReducerFunction = setStageVar;
}
// 先把表达式拆分为变量名和赋值语句
if (sentence.content.match(/\s*=\s*/)) {
const key = sentence.content.split(/\s*=\s*/)[0];
const valExp = sentence.content.split(/\s*=\s*/)[1];
if (valExp.length === 0) {
webgalStore.dispatch(targetReducerFunction({ key, value: '' }));
} else if (!isNaN(Number(valExp))) {
webgalStore.dispatch(targetReducerFunction({ key, value: Number(valExp) }));
} else {
webgalStore.dispatch(
targetReducerFunction({ key, value: EvaluateExpression(valExp, { InvalidValueReturns: 'origin' }) }),
);
}
if (setGlobal) {
logger.debug('设置全局变量:', { key, value: webgalStore.getState().userData.globalGameVar[key] });
dumpToStorageFast();
} else {
logger.debug('设置变量:', { key, value: webgalStore.getState().stage.GameVar[key] });
}
}
return {
performName: 'none',
duration: 0,
isHoldOn: false,
stopFunction: () => {},
blockingNext: () => false,
blockingAuto: () => true,
stopTimeout: undefined, // 暂时不用,后面会交给自动清除
};
};