forked from VickScarlet/lifeRestart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.js
210 lines (181 loc) · 6.35 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Property from './property.js';
import Event from './event.js';
import Talent from './talent.js';
import Achievement from './achievement.js';
class Life {
constructor() {
this.#property = new Property();
this.#event = new Event();
this.#talent = new Talent();
this.#achievement = new Achievement();
}
#property;
#event;
#talent;
#achievement;
#triggerTalents;
async initial() {
const [age, talents, events, achievements] = await Promise.all([
json('age'),
json('talents'),
json('events'),
json('achievement'),
])
this.#property.initial({age});
this.#talent.initial({talents});
this.#event.initial({events});
this.#achievement.initial({achievements});
}
restart(allocation) {
this.#triggerTalents = {};
this.#property.restart(allocation);
this.doTalent();
this.#property.restartLastStep();
this.#achievement.achieve(
this.#achievement.Opportunity.START,
this.#property
)
}
getTalentAllocationAddition(talents) {
return this.#talent.allocationAddition(talents);
}
getTalentCurrentTriggerCount(talentId) {
return this.#triggerTalents[talentId] || 0;
}
next() {
const {age, event, talent} = this.#property.ageNext();
const talentContent = this.doTalent(talent);
const eventContent = this.doEvent(this.random(event));
const isEnd = this.#property.isEnd();
const content = [talentContent, eventContent].flat();
this.#achievement.achieve(
this.#achievement.Opportunity.TRAJECTORY,
this.#property
)
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.getTalentCurrentTriggerCount(talentId) < this.#talent.get(talentId).max_triggers);
const contents = [];
for(const talentId of talents) {
const result = this.#talent.do(talentId, this.#property);
if(!result) continue;
this.#triggerTalents[talentId] = this.getTalentCurrentTriggerCount(talentId) + 1;
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() {
const times = this.#property.get(this.#property.TYPES.TMS);
const achievement = this.#property.get(this.#property.TYPES.CACHV);
return this.#talent.talentRandom(this.getLastExtendTalent(), { times, achievement });
}
talentExtend(talentId) {
this.#property.set(this.#property.TYPES.EXT, talentId);
}
getLastExtendTalent() {
return this.#property.get(this.#property.TYPES.EXT);
}
getSummary() {
this.#achievement.achieve(
this.#achievement.Opportunity.SUMMARY,
this.#property
)
return {
AGE: this.#property.get(this.#property.TYPES.HAGE),
CHR: this.#property.get(this.#property.TYPES.HCHR),
INT: this.#property.get(this.#property.TYPES.HINT),
STR: this.#property.get(this.#property.TYPES.HSTR),
MNY: this.#property.get(this.#property.TYPES.HMNY),
SPR: this.#property.get(this.#property.TYPES.HSPR),
SUM: this.#property.get(this.#property.TYPES.SUM),
};
}
getLastRecord() {
return this.#property.getLastRecord();
}
exclusive(talents, exclusive) {
return this.#talent.exclusive(talents, exclusive);
}
getAchievements() {
const ticks = {};
this.#property
.get(this.#property.TYPES.ACHV)
.forEach(([id, tick]) => ticks[id] = tick);
return this
.#achievement
.list(this.#property)
.sort((
{id: a, grade: ag, hide: ah},
{id: b, grade: bg, hide: bh}
)=>{
a = ticks[a];
b = ticks[b];
if(a&&b) return b - a;
if(!a&&!b) {
if(ah&&bh) return bg - ag;
if(ah) return 1;
if(bh) return -1;
return bg - ag;
}
if(!a) return 1;
if(!b) return -1;
});
}
getTotal() {
const TMS = this.#property.get(this.#property.TYPES.TMS);
const CACHV = this.#property.get(this.#property.TYPES.CACHV);
const CTLT = this.#property.get(this.#property.TYPES.CTLT);
const CEVT = this.#property.get(this.#property.TYPES.CEVT);
const totalTalent = this.#talent.count();
const totalEvent = this.#event.count();
return {
times: TMS,
achievement: CACHV,
talentRate: CTLT / totalTalent,
eventRate: CEVT / totalEvent,
}
}
get times() { return this.#property?.get(this.#property.TYPES.TMS) || 0; }
set times(v) {
this.#property?.set(this.#property.TYPES.TMS, v) || 0;
this.#achievement.achieve(
this.#achievement.Opportunity.END,
this.#property
)
}
}
export default Life;