mirror of
https://github.com/VickScarlet/lifeRestart.git
synced 2026-03-30 05:58:03 +08:00
add Event
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
class Condition {
|
||||
constructor(prop) {
|
||||
this._prop = prop;
|
||||
constructor(initialData={}) {
|
||||
this.initial(initialData);
|
||||
}
|
||||
|
||||
initial({prop}) {
|
||||
if(prop) this.prop = prop;
|
||||
}
|
||||
|
||||
parse(condition) {
|
||||
|
||||
95
src/event.js
Normal file
95
src/event.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import {clone} from './util.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];
|
||||
if(!event.branch) continue;
|
||||
event.branch = event.branch.map(b=>{
|
||||
b = b.split(':');
|
||||
b[1] = Number(b[1]);
|
||||
return b;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Event;
|
||||
90
src/prop.js
90
src/prop.js
@@ -1,20 +1,29 @@
|
||||
import {clone} from './util.js';
|
||||
|
||||
class Prop {
|
||||
constructor(initialData) {
|
||||
this._data = {};
|
||||
for(const key in initialData)
|
||||
this.set(key, initialData[key]);
|
||||
constructor(initialData={}) {
|
||||
this.initial(initialData);
|
||||
}
|
||||
|
||||
initial(data) {
|
||||
this._data = {
|
||||
AGE: 0
|
||||
};
|
||||
for(const key in data)
|
||||
this.set(key, data[key]);
|
||||
}
|
||||
|
||||
get(prop) {
|
||||
switch(prop) {
|
||||
case 'CHR':
|
||||
case 'INT':
|
||||
case 'STR':
|
||||
case 'MNY':
|
||||
case 'SPR':
|
||||
case 'LIF':
|
||||
case 'TLT':
|
||||
case 'EVT':
|
||||
case this.TYPES.AGE:
|
||||
case this.TYPES.CHR:
|
||||
case this.TYPES.INT:
|
||||
case this.TYPES.STR:
|
||||
case this.TYPES.MNY:
|
||||
case this.TYPES.SPR:
|
||||
case this.TYPES.LIF:
|
||||
case this.TYPES.TLT:
|
||||
case this.TYPES.EVT:
|
||||
return this._data[prop];
|
||||
default: return 0;
|
||||
}
|
||||
@@ -22,15 +31,16 @@ class Prop {
|
||||
|
||||
set(prop, value) {
|
||||
switch(prop) {
|
||||
case 'CHR':
|
||||
case 'INT':
|
||||
case 'STR':
|
||||
case 'MNY':
|
||||
case 'SPR':
|
||||
case 'LIF':
|
||||
case 'TLT':
|
||||
case 'EVT':
|
||||
this._data[prop] = this.clone(value);
|
||||
case this.TYPES.AGE:
|
||||
case this.TYPES.CHR:
|
||||
case this.TYPES.INT:
|
||||
case this.TYPES.STR:
|
||||
case this.TYPES.MNY:
|
||||
case this.TYPES.SPR:
|
||||
case this.TYPES.LIF:
|
||||
case this.TYPES.TLT:
|
||||
case this.TYPES.EVT:
|
||||
this._data[prop] = clone(value);
|
||||
break;
|
||||
default: return 0;
|
||||
}
|
||||
@@ -38,16 +48,17 @@ class Prop {
|
||||
|
||||
change(prop, value) {
|
||||
switch(prop) {
|
||||
case 'CHR':
|
||||
case 'INT':
|
||||
case 'STR':
|
||||
case 'MNY':
|
||||
case 'SPR':
|
||||
case 'LIF':
|
||||
case this.TYPES.AGE:
|
||||
case this.TYPES.CHR:
|
||||
case this.TYPES.INT:
|
||||
case this.TYPES.STR:
|
||||
case this.TYPES.MNY:
|
||||
case this.TYPES.SPR:
|
||||
case this.TYPES.LIF:
|
||||
this._data[prop] += value;
|
||||
break;
|
||||
case 'TLT':
|
||||
case 'EVT':
|
||||
case this.TYPES.TLT:
|
||||
case this.TYPES.EVT:
|
||||
const v = this._data[prop];
|
||||
if(value<0) {
|
||||
const index = v.indexOf(value);
|
||||
@@ -59,15 +70,18 @@ class Prop {
|
||||
}
|
||||
}
|
||||
|
||||
clone(value) {
|
||||
switch(typeof value) {
|
||||
case 'object':
|
||||
if(Array.isArray(value)) return value.map(v=>this.clone(v));
|
||||
const newObj = {};
|
||||
for(const key in value) newObj[key] = this.clone(value[key]);
|
||||
return newObj;
|
||||
default: return value;
|
||||
}
|
||||
get TYPES() {
|
||||
return {
|
||||
AGE: "AGE",
|
||||
CHR: "CHR",
|
||||
INT: "INT",
|
||||
STR: "STR",
|
||||
MNY: "MNY",
|
||||
SPR: "SPR",
|
||||
LIF: "LIF",
|
||||
TLT: "TLT",
|
||||
EVT: "EVT",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
12
src/util.js
Normal file
12
src/util.js
Normal file
@@ -0,0 +1,12 @@
|
||||
function clone(value) {
|
||||
switch(typeof value) {
|
||||
case 'object':
|
||||
if(Array.isArray(value)) return value.map(v=>clone(v));
|
||||
const newObj = {};
|
||||
for(const key in value) newObj[key] = clone(value[key]);
|
||||
return newObj;
|
||||
default: return value;
|
||||
}
|
||||
}
|
||||
|
||||
export { clone };
|
||||
Reference in New Issue
Block a user