mirror of
https://github.com/VickScarlet/lifeRestart.git
synced 2025-07-24 21:13:56 +08:00
bug fix
This commit is contained in:
27
src/event.js
27
src/event.js
@@ -2,9 +2,9 @@ import { clone } from './functions/util.js';
|
||||
import { checkCondition } from './functions/condition.js';
|
||||
|
||||
class Event {
|
||||
constructor(initialData={}) {
|
||||
this.initial(initialData);
|
||||
}
|
||||
constructor() {}
|
||||
|
||||
#events;
|
||||
|
||||
initial({events}) {
|
||||
this.#events = events;
|
||||
@@ -19,10 +19,10 @@ class Event {
|
||||
}
|
||||
}
|
||||
|
||||
check(eventId) {
|
||||
check(eventId, property) {
|
||||
const { include, exclude } = this.get(eventId);
|
||||
if(exclude && checkCondition(exclude)) return false;
|
||||
if(include) return checkCondition(include);
|
||||
if(exclude && checkCondition(property, exclude)) return false;
|
||||
if(include) return checkCondition(property, include);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -32,17 +32,18 @@ class Event {
|
||||
return clone(event);
|
||||
}
|
||||
|
||||
description(eventId) {
|
||||
return this.get(eventId).description;
|
||||
information(eventId) {
|
||||
const { event: description } = this.get(eventId)
|
||||
return { description };
|
||||
}
|
||||
|
||||
do(eventId) {
|
||||
const { effect, branch } = this.get(eventId);
|
||||
do(eventId, property) {
|
||||
const { effect, branch, event: description, postEvent } = this.get(eventId);
|
||||
if(branch)
|
||||
for(const [cond, next] of branch)
|
||||
if(checkCondition(cond))
|
||||
return { effect, next };
|
||||
return { effect };
|
||||
if(checkCondition(property, cond))
|
||||
return { effect, next, description };
|
||||
return { effect, postEvent, description };
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -53,7 +53,7 @@ function checkParsedConditions(property, conditions) {
|
||||
if(conditions.length == 0) return true;
|
||||
if(conditions.length == 1) return checkParsedConditions(property, conditions[0]);
|
||||
|
||||
let ret = checkParsedConditions(conditions[0]);
|
||||
let ret = checkParsedConditions(property, conditions[0]);
|
||||
for(let i=1; i<conditions.length; i+=2) {
|
||||
switch(conditions[i]) {
|
||||
case '&':
|
||||
|
65
src/life.js
65
src/life.js
@@ -1,4 +1,3 @@
|
||||
import { readFile } from 'fs/promises';
|
||||
import Property from './property.js';
|
||||
import Event from './event.js';
|
||||
import Talent from './talent.js';
|
||||
@@ -10,10 +9,15 @@ class Life {
|
||||
this.#talent = new Talent();
|
||||
}
|
||||
|
||||
#property;
|
||||
#event;
|
||||
#talent;
|
||||
#triggerTalents;
|
||||
|
||||
async initial() {
|
||||
const age = JSON.parse(await readFile('data/age.json'));
|
||||
const talents = JSON.parse(await readFile('data/talents.json'));
|
||||
const events = JSON.parse(await readFile('data/events.json'));
|
||||
const age = JSON.parse(await json('data/age.json'));
|
||||
const talents = JSON.parse(await json('data/talents.json'));
|
||||
const events = JSON.parse(await json('data/events.json'));
|
||||
|
||||
this.#property.initial({age});
|
||||
this.#talent.initial({talents});
|
||||
@@ -21,17 +25,61 @@ class Life {
|
||||
}
|
||||
|
||||
restart(allocation) {
|
||||
this.#prop.restart(allocation);
|
||||
this.#triggerTalents = new Set();
|
||||
this.#property.restart(allocation);
|
||||
this.doTalent();
|
||||
}
|
||||
|
||||
next() {
|
||||
const {age, event, talent} = this.#prop.ageNext();
|
||||
const {age, event, talent} = this.#property.ageNext();
|
||||
|
||||
const eventId = this.random(event);
|
||||
const talentContent = this.doTalent(talent);
|
||||
const eventContent = this.doEvent(this.random(event));
|
||||
|
||||
const isEnd = this.#property.isEnd();
|
||||
|
||||
const content = [talentContent, eventContent].flat();
|
||||
return { age, content, isEnd };
|
||||
}
|
||||
|
||||
doTalent(talents) {
|
||||
if(talents) this.#property.change(this.#property.TYPES.TLT, talents);
|
||||
talents = this.#property.get(this.#property.TYPES.TLT)
|
||||
.filter(talentId=>!this.#triggerTalents.has(talentId));
|
||||
|
||||
const contents = [];
|
||||
for(const talentId of talents) {
|
||||
const result = this.#talent.do(talentId);
|
||||
if(!result) continue;
|
||||
this.#triggerTalents.add(talentId);
|
||||
const { effect, name, desctiption } = result;
|
||||
contents.push({
|
||||
type: this.#property.TYPES.TLT,
|
||||
name,
|
||||
rate,
|
||||
desctiption,
|
||||
})
|
||||
if(!result.effect) continue;
|
||||
this.#property.effect(effect);
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
|
||||
doEvent(eventId) {
|
||||
const { effect, next, description, postEvent } = this.#event.do(eventId, this.#property);
|
||||
this.#property.change(this.#property.TYPES.EVT, eventId);
|
||||
this.#property.effect(effect);
|
||||
const content = {
|
||||
type: this.#property.TYPES.EVT,
|
||||
description,
|
||||
postEvent,
|
||||
}
|
||||
if(next) return [content, this.doEvent(next)].flat();
|
||||
return [content];
|
||||
}
|
||||
|
||||
random(events) {
|
||||
events.filter(([eventId])=>this.#event.check(eventId));
|
||||
events = events.filter(([eventId])=>this.#event.check(eventId, this.#property));
|
||||
|
||||
let totalWeights = 0;
|
||||
for(const [, weight] of events)
|
||||
@@ -41,6 +89,7 @@ class Life {
|
||||
for(const [eventId, weight] of events)
|
||||
if((random-=weight)<0)
|
||||
return eventId;
|
||||
return events[events.length-1];
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,7 @@
|
||||
import { clone } from './functions/util.js';
|
||||
|
||||
class Property {
|
||||
constructor(initialData={}) {
|
||||
this.initial(initialData);
|
||||
}
|
||||
constructor() {}
|
||||
|
||||
TYPES = {
|
||||
AGE: "AGE",
|
||||
@@ -17,15 +15,19 @@ class Property {
|
||||
EVT: "EVT",
|
||||
};
|
||||
|
||||
#ageData;
|
||||
#data;
|
||||
|
||||
initial({age}) {
|
||||
|
||||
this.#ageData = age;
|
||||
for(const age in a)
|
||||
a[age].event = a[age].event?.map(v=>{
|
||||
for(const a in age) {
|
||||
age[a].event = age[a].event?.split(',').map(v=>{
|
||||
const value = v.split('*').map(n=>Number(n));
|
||||
if(value.length==1) value.push(1);
|
||||
return value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
restart(data) {
|
||||
@@ -41,7 +43,7 @@ class Property {
|
||||
[this.TYPES.EVT]: [],
|
||||
};
|
||||
for(const key in data)
|
||||
this.change(key, value);
|
||||
this.change(key, data[key]);
|
||||
}
|
||||
|
||||
get(prop) {
|
||||
@@ -80,7 +82,7 @@ class Property {
|
||||
change(prop, value) {
|
||||
if(Array.isArray(value)) {
|
||||
for(const v of value)
|
||||
this.change(prop, v);
|
||||
this.change(prop, Number(v));
|
||||
return;
|
||||
}
|
||||
switch(prop) {
|
||||
@@ -118,7 +120,7 @@ class Property {
|
||||
ageNext() {
|
||||
this.change(this.TYPES.AGE, 1);
|
||||
const age = this.get(this.TYPES.AGE);
|
||||
const {event, talent} = this.getAge(age);
|
||||
const {event, talent} = this.getAgeData(age);
|
||||
return {age, event, talent};
|
||||
}
|
||||
|
||||
|
@@ -1,10 +1,11 @@
|
||||
import { clone } from './functions/util.js';
|
||||
class Talent {
|
||||
constructor(initialData={}) {
|
||||
this.initial(initialData);
|
||||
}
|
||||
constructor() {}
|
||||
|
||||
initial({talent}) {
|
||||
this.#talent = talent;
|
||||
#talents;
|
||||
|
||||
initial({talents}) {
|
||||
this.#talents = talents;
|
||||
}
|
||||
|
||||
check(talentId) {
|
||||
@@ -13,20 +14,22 @@ class Talent {
|
||||
}
|
||||
|
||||
get(talentId) {
|
||||
const talent = this.#talent[talentId];
|
||||
const talent = this.#talents[talentId];
|
||||
if(!talent) throw new Error(`[ERROR] No Talent[${talentId}]`);
|
||||
return clone(talent);
|
||||
}
|
||||
|
||||
description(talentId) {
|
||||
return this.get(talentId).description;
|
||||
information(talentId) {
|
||||
const { rate, name, description } = this.get(talentId)
|
||||
return { rate, name, description };
|
||||
}
|
||||
|
||||
do(talentId) {
|
||||
const { effect, condition } = this.get(talentId);
|
||||
const { effect, condition, initiative, rate, name, description } = this.get(talentId);
|
||||
if(!initiative) return null;
|
||||
if(condition && !checkCondition(condition))
|
||||
return null;
|
||||
return { effect };
|
||||
return { effect, rate, name, description };
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user