feat: check function

This commit is contained in:
hypercross 2026-03-03 13:12:45 +08:00
parent a226a9516c
commit 82876d47c0
1 changed files with 15 additions and 1 deletions

View File

@ -38,7 +38,21 @@ export function createYarnStore(element: HTMLElement, props: RunnerOptions){
const ast = parseYarn(content); const ast = parseYarn(content);
const program = compile(ast); const program = compile(ast);
return new YarnRunner(program, props); const runner = new YarnRunner(program, {
functions: {
check(id: string, stat: string): number {
const statVal = runner.getVariable(stat) as number || 10;
const pass = Math.ceil(Math.random() * 20) <= statVal;
const key = `${id}_${pass ? 'pass' : 'fail'}`;
const progress = runner.getVariable(key) as number || 0;
const newProgress = progress + Math.ceil(Math.random() * 4);
runner.setVariable(key, newProgress);
return pass ? newProgress : -newProgress;
}
},
startAt: props.startAt,
});
return runner;
} catch (error) { } catch (error) {
console.error('Failed to initialize YarnRunner:', error); console.error('Failed to initialize YarnRunner:', error);
return null; return null;