add talent life

This commit is contained in:
Vick Scarlet
2021-08-16 23:28:54 +08:00
parent dcc9cd2383
commit df85e62138
18 changed files with 5703 additions and 335 deletions

View File

@@ -1,86 +1,15 @@
import {clone} from './util.js';
import { clone } from './functions/util.js';
import { checkCondition } from './functions/condition.js';
class Event {
constructor(initialData={}) {
this.initial(initialData);
}
initial({prop, condition, events, pools}) {
if(prop) this.prop = prop;
if(condition) this.condition = condition;
if(events) this.events = events;
if(pools) this.pools = pools;
}
random() {
const age = this.getProp(this.prop.TYPES.AGE);
const pool = this.filterPool(
this.getPool(age)
);
let totalWeights = 0;
for(const [,weight] of pool)
totalWeights += weight;
let random = Math.random() * totalWeights;
for(const [event,weight] of pool)
if((random-=weight)<0)
return event;
}
check(eventId) {
const { Include, Exclude } = this.get(eventId);
if(Exclude && this.checkCondition(Exclude)) return false;
if(Include) return this.checkCondition(Include);
return true;
}
checkCondition(condition) {
return this.condition.check(condition);
}
filterPool(pool) {
return pool.filter(([event])=>this.check(event));
}
get(eventId) {
const event = this.events[eventId];
if(!event) console.error(`[ERROR] No Event[${eventId}]`);
return clone(event);
}
getPool(age) {
return clone(this.pools[age].Pool) || [];
}
getProp(prop) {
return this.prop.get(prop);
}
get prop() {return this._prop;}
set prop(p) {this._prop = p;}
get condition() {return this._condition;}
set condition(c) {this._condition = c;}
get pools() {return this._pools;}
set pools(p) {
this._pools = p;
for(const age in p)
p[age].Pool = p[age].Pool?.map(v=>{
const value = v.split('*').map(n=>Number(n));
if(value.length==1) value.push(1);
return value;
});
}
get events() {return this._events;}
set events(e) {
this._events = e;
for(const id in e) {
const event = e[id];
initial({events}) {
this.#events = events;
for(const id in events) {
const event = events[id];
if(!event.branch) continue;
event.branch = event.branch.map(b=>{
b = b.split(':');
@@ -90,6 +19,32 @@ class Event {
}
}
check(eventId) {
const { include, exclude } = this.get(eventId);
if(exclude && checkCondition(exclude)) return false;
if(include) return checkCondition(include);
return true;
}
get(eventId) {
const event = this.#events[eventId];
if(!event) throw new Error(`[ERROR] No Event[${eventId}]`);
return clone(event);
}
description(eventId) {
return this.get(eventId).description;
}
do(eventId) {
const { effect, branch } = this.get(eventId);
if(branch)
for(const [cond, next] of branch)
if(checkCondition(cond))
return { effect, next };
return { effect };
}
}
export default Event;