forked from AliyunWorkbench/lifeRestart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.js
124 lines (102 loc) · 3.37 KB
/
life.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import Property from './property.js';
import Event from './event.js';
import Talent from './talent.js';
class Life {
constructor() {
this.#property = new Property();
this.#event = new Event();
this.#talent = new Talent();
}
#property;
#event;
#talent;
#triggerTalents;
async initial() {
const [age, talents, events] = await Promise.all([
json('age'),
json('talents'),
json('events'),
])
this.#property.initial({age});
this.#talent.initial({talents});
this.#event.initial({events});
}
restart(allocation) {
this.#triggerTalents = new Set();
this.#property.restart(allocation);
this.doTalent();
this.#property.record();
}
getTalentAllocationAddition(talents) {
return this.#talent.allocationAddition(talents);
}
next() {
const {age, event, talent} = this.#property.ageNext();
const talentContent = this.doTalent(talent);
const eventContent = this.doEvent(this.random(event));
this.#property.record();
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, this.#property);
if(!result) continue;
this.#triggerTalents.add(talentId);
const { effect, name, description, grade } = result;
contents.push({
type: this.#property.TYPES.TLT,
name,
grade,
description,
})
if(!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 = events.filter(([eventId])=>this.#event.check(eventId, this.#property));
let totalWeights = 0;
for(const [, weight] of events)
totalWeights += weight;
let random = Math.random() * totalWeights;
for(const [eventId, weight] of events)
if((random-=weight)<0)
return eventId;
return events[events.length-1];
}
talentRandom() {
return this.#talent.talentRandom(JSON.parse(localStorage.extendTalent||'null'));
}
talentExtend(talentId) {
localStorage.extendTalent = JSON.stringify(talentId);
}
getRecord() {
return this.#property.getRecord();
}
getLastRecord() {
return this.#property.getLastRecord();
}
exclusive(talents, exclusive) {
return this.#talent.exclusive(talents, exclusive);
}
}
export default Life;