mirror of
https://github.com/VickScarlet/lifeRestart.git
synced 2026-04-16 04:42:42 +08:00
bug fix
This commit is contained in:
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];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user