Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: var value handle #532

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from '../../util/logger';
import { IStageState } from '@/store/stageInterface';
import { restoreScene } from '../scene/restoreScene';
import { webgalStore } from '@/store/store';
import { getValueFromState } from '@/Core/gameScripts/setVar';
import { getValueFromStateElseKey } from '@/Core/gameScripts/setVar';
import { strIf } from '@/Core/controller/gamePlay/strIf';
import { nextSentence } from '@/Core/controller/gamePlay/nextSentence';
import cloneDeep from 'lodash/cloneDeep';
Expand All @@ -25,7 +25,7 @@ export const whenChecker = (whenValue: string | undefined): boolean => {
if (e.match(/true/) || e.match(/false/)) {
return e;
}
return getValueFromState(e).toString();
return getValueFromStateElseKey(e);
} else return e;
})
.reduce((pre, curr) => pre + curr, '');
Expand Down Expand Up @@ -59,8 +59,8 @@ export const scriptExecutor = () => {

if (contentExp !== null) {
contentExp.forEach((e) => {
const contentVarValue = getValueFromState(e.replace(/(?<!\\)\{(.*)\}/, '$1'));
retContent = retContent.replace(e, contentVarValue ? contentVarValue.toString() : e);
const contentVarValue = getValueFromStateElseKey(e.replace(/(?<!\\)\{(.*)\}/, '$1'));
retContent = retContent.replace(e, contentVarValue);
});
}
retContent = retContent.replace(/\\{/g, '{').replace(/\\}/g, '}');
Expand Down
35 changes: 27 additions & 8 deletions packages/webgal/src/Core/gameScripts/setVar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ export const setVar = (sentence: ISentence): IPerform => {
// 将变量替换为变量的值,然后合成表达式字符串
const valExp2 = valExpArr
.map((e) => {
if (e.match(/\$?[.a-zA-Z]/)) {
const _r = getValueFromState(e.trim());
return typeof _r === 'string' ? `'${_r}'` : _r;
} else return e;
if (!e.trim().match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
// 检查是否是变量名,不是就返回本身
return e;
}
const _r = getValueFromStateElseKey(e.trim());
return typeof _r === 'string' ? `'${_r}'` : _r;
})
.reduce((pre, curr) => pre + curr, '');
let result = '';
Expand All @@ -60,11 +62,13 @@ export const setVar = (sentence: ISentence): IPerform => {
if (valExp.match(/false/)) {
webgalStore.dispatch(targetReducerFunction({ key, value: false }));
}
} else 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: valExp }));
webgalStore.dispatch(targetReducerFunction({ key, value: getValueFromStateElseKey(valExp) }));
}
}
if (setGlobal) {
Expand All @@ -85,10 +89,13 @@ export const setVar = (sentence: ISentence): IPerform => {
};
};

type BaseVal = string | number | boolean;
type BaseVal = string | number | boolean | undefined;

/**
* 取不到时返回 undefined
*/
export function getValueFromState(key: string) {
let ret: any = 0;
let ret: any;
const stage = webgalStore.getState().stage;
const userData = webgalStore.getState().userData;
const _Merge = { stage, userData }; // 不要直接合并到一起,防止可能的键冲突
Expand All @@ -98,7 +105,19 @@ export function getValueFromState(key: string) {
ret = userData.globalGameVar[key];
} else if (key.startsWith('$')) {
const propertyKey = key.replace('$', '');
ret = get(_Merge, propertyKey, 0) as BaseVal;
ret = get(_Merge, propertyKey, undefined) as BaseVal;
}
return ret;
}

/**
* 取不到时返回 key
*/
export function getValueFromStateElseKey(key: string) {
const valueFromState = getValueFromState(key);
if (valueFromState === null || valueFromState === undefined) {
logger.warn('valueFromState result null, key = ' + key);
return key;
}
return valueFromState;
}
Loading