-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
509 lines (443 loc) · 15.9 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
class Preference extends React.Component {
render() {
const quality = this.props.quality;
const scheme = qualityColors[quality];
const type = this.props.type;
let divClass = "col-auto rounded-lg h-100 p-2 wrap text-center text-"+scheme.text+" bg-"+scheme.bg;
if (this.props.index === 0) divClass += " ml-auto";
else divClass += " ml-2";
let text;
if (type === "biome") {
text = "In a "+quality+"\nbiome";
} else if (type === "neighbor") {
const neighborName = npcProps[this.props.npc].name;
text = "Has a "+quality+"\nneighbor:\n"+neighborName;
} else if (type === "nearby") {
const count = this.props.neighborCount;
if (count < 2) {
text = "Few\nneighbors";
} else if (count > 2) {
text = "Too many\nneighbors\nx" + (count-2);
}
}
return (
<div className={divClass}>
<h6>{text}</h6>
</div>
);
}
}
class NPC extends React.Component {
render() {
const npcType = this.props.npcType;
const ind = this.props.ind;
const hasMargin = ind >= 1;
let className = "row align-items-center mx-0 p-2 blank border border-dark rounded-lg";
if (hasMargin) className += " mt-1";
const imgSrc = "images/" + npcType + ".png";
const npcName = npcProps[npcType].name;
let upBtn = null;
let downBtn = null;
if (npcType !== "truffle") {
if (!this.props.isFirstBiome) upBtn =
(<button type="button" className="btn btn-outline-secondary btn-sm" onClick={this.props.moveNPCUp}><i className="fas fa-chevron-up"></i></button>);
if (!this.props.isLastBiome) downBtn =
(<button type="button" className="btn btn-outline-secondary btn-sm" onClick={this.props.moveNPCDown}><i className="fas fa-chevron-down"></i></button>);
}
const prefs = this.props.prefs;
let closeButtonClassName = "col-auto justify-content-end right-align vert-center"
if (prefs.length === 0) closeButtonClassName += " ml-auto";
let priceMult = 1;
for (let i = 0; i < prefs.length; i++) {
let p = prefs[i];
if (p.props.quality === "horrendous") {
priceMult = 1.5;
break;
}
if (p.props.type !== "nearby") {
priceMult *= prefImpacts[p.props.quality];
} else {
const neighborCount = p.props.neighborCount;
if (neighborCount < 2) priceMult *= 0.9;
else {
for (let i = 3; i <= neighborCount; i++) priceMult *= 1.04;
}
}
}
priceMult = Math.round(priceMult*20)/20;
let priceText = "Price: "+(priceMult*100).toFixed(0)+"%";
if (priceMult > 1.5) priceText += " (capped at 150%)";
else if (priceMult < 0.75) priceText += " (capped at 75%)";
let priceColor;
if (priceMult < 1) priceColor = "text-success";
else if (priceMult == 1) priceColor = "text-body";
else priceColor = "text-danger";
const textClass = "vert-center "+priceColor;
return (
<div className={className}>
<div className="col-auto">
<div className="btn-group-vertical">
{upBtn}
{downBtn}
</div>
</div>
<div className="col-auto">
<img src={imgSrc}></img>
</div>
<div className="col-auto">
<h5 className="vert-center">{npcName}</h5>
</div>
<div className="col-auto">
<h5 className={textClass}>{priceText}</h5>
</div>
{prefs}
<div className={closeButtonClassName}>
<button className="btn blank" onClick={this.props.removeNPC}><i className="fas fa-times"></i></button>
</div>
</div>
);
}
}
class Biome extends React.Component {
findPrefs(npc) {
let prefs = [];
const prefTypes = ["loved", "liked", "disliked", "hated"];
const neighbors = this.props.npcs;
let lovedCount = 0;
prefTypes.forEach(t => {
const curNPCPrefs = npcProps[npc].neighbors[t];
for (let i = 0; i < neighbors.length; i++) {
if (neighbors[i] === npc) continue;
if (npc === "princess" && t === "loved" && lovedCount < 3) {
// princess loves up to 3 neighbors
lovedCount++;
prefs.push({type: "neighbor", quality: t, npc: neighbors[i]});
continue;
}
if (t === "liked" && neighbors[i] === "princess") {
// everyone likes princess
prefs.push({type: "neighbor", quality: t, npc: neighbors[i]});
continue;
}
for (let j = 0; j < curNPCPrefs.length; j++) {
if (neighbors[i] === curNPCPrefs[j]) {
prefs.push({type: "neighbor", quality: t, npc: neighbors[i]});
break;
}
}
}
});
const npcProp = npcProps[npc];
const biomeType = this.props.biomeType;
if (npcProp.biome.liked === biomeType) {
if (npc === "santa") {
prefs.push({type: "biome", quality: "loved"});
} else {
prefs.push({type: "biome", quality: "liked"});
}
} else if (npcProp.biome.disliked === biomeType) {
if (npc === "santa") {
prefs.push({type: "biome", quality: "hated"});
} else if (npc !== "truffle") {
prefs.push({type: "biome", quality: "disliked"});
}
}
const neighborCount = neighbors.length-1;
if (npc === "princess") {
if (neighborCount < 2) {
prefs.push({type: "nearby", quality: "horrendous"});
}
} else {
if (neighborCount > 2) {
prefs.push({type: "nearby", quality: "disliked"});
} else if (neighborCount < 2) {
prefs.push({type: "nearby", quality: "loved"});
}
}
prefs.sort((a, b) => {
return (sortInds[a.quality] - sortInds[b.quality]);
});
const mappedPrefs = prefs.map((p, i) => {
if (p.type === "biome") {
return (
<Preference
key={i}
type="biome"
quality={p.quality}
index={i}
/>
);
} else if (p.type === "neighbor") {
return (
<Preference
key={i}
type="neighbor"
quality={p.quality}
npc={p.npc}
index={i}
/>
);
} else if (p.type === "nearby") {
return (
<Preference
key={i}
type="nearby"
quality={p.quality}
neighborCount={neighborCount}
index={i}
/>
);
}
});
return mappedPrefs;
}
render() {
const biomeType = this.props.biomeType;
const biomeProp = biomeProps[biomeType];
let divClass = "row mx-0 p-3 rounded-lg " + biomeType + " border border-"+biomeType;
if (!this.props.isFirst) divClass += " mt-2";
const btnClass = "btn btn-"+biomeType;
const biomeName = biomeProp.name;
const npcList = this.props.npcs.map((npc, ind) => {
const prefs = this.findPrefs(npc);
return (
<NPC
key={ind}
npcType={npc}
ind={ind}
removeNPC={() => {this.props.removeNPC(npc)}}
isFirstBiome={this.props.isFirst}
isLastBiome={this.props.isLast}
moveNPCUp={() => this.props.moveNPCUp(npc)}
moveNPCDown={() => this.props.moveNPCDown(npc)}
prefs={prefs}
/>
);
});
return (
<div className={divClass}>
<div className="col">
<div className="row">
<div className="col vert-center">
<h4>{biomeName}</h4>
</div>
<div className="col-auto ml-auto justify-content-end right-align vert-center">
<button className={btnClass} onClick={this.props.delete}><i className="fas fa-times"></i></button>
</div>
</div>
{npcList}
</div>
</div>
);
}
}
// main container for all the biomes
class Tool extends React.Component {
constructor(props) {
super(props);
this.state = { biomes: [] }
setTimeout(this.getCookies.bind(this), 50);
}
addBiome(type) {
const biomes = this.state.biomes;
const biomesConcat = biomes.concat({
type: type,
npcs: [],
});
biomesConcat.sort((a, b) => {
return (biomeProps[a.type].order - biomeProps[b.type].order);
});
this.setState({
biomes: biomesConcat,
});
this.setCookies(biomesConcat);
}
deleteBiome(ind) {
const biomes = this.state.biomes.slice();
const removed = biomes.splice(ind, 1);
removed[0].npcs.forEach(type => {
toggleNPCDropdown(type, false);
});
this.setState({
biomes: biomes,
});
this.setCookies(biomes);
}
deleteAllBiomes() {
const dropdown = document.getElementById("npcDropdownMenu");
let children = dropdown.children;
for (let i = 0; i < children.length; i++) {
children[i].className = "dropdown-item";
}
this.setState({
biomes: [],
});
this.setCookies([]);
}
// Adds NPC to first biome
addNPC(type) {
const biomes = this.state.biomes.slice();
if (biomes.length > 0) {
for (let i = 0; i < biomes.length; i++) {
if (biomes[i].npcs.includes(type)) {
console.error("Can't add NPC twice!");
return;
}
}
const prefType = npcProps[type].biome.liked;
let prefInd = -1;
for (let i = 0; i < biomes.length; i++) {
if (biomes[i].type === prefType) {
prefInd = i;
break;
}
}
if (prefInd == -1) {
if (type === "truffle") { // can't add truffle to anything other than mushroom biome
alert("Can't add Truffle without a mushroom biome!");
return;
}
prefInd = 0;
}
biomes[prefInd].npcs.push(type);
toggleNPCDropdown(type, true);
this.setState({
biomes: biomes,
});
this.setCookies(biomes);
}
}
removeNPC(type) {
const biomes = this.state.biomes.slice();
for (let i = 0; i < biomes.length; i++) {
const npcs = biomes[i].npcs;
for (let j = npcs.length-1; j >= 0; j--) {
if (npcs[j] === type) {
npcs.splice(j, 1);
}
}
}
toggleNPCDropdown(type, false);
this.setState({
biomes: biomes,
});
this.setCookies(biomes);
}
moveNPCUp(type, biomeInd) {
const biomes = this.state.biomes.slice();
if (biomeInd >= 1) {
const ind = biomes[biomeInd].npcs.indexOf(type);
if (ind != -1) {
biomes[biomeInd].npcs.splice(ind, 1);
biomes[biomeInd-1].npcs.push(type);
}
}
this.setState({
biomes: biomes,
});
this.setCookies(biomes);
}
moveNPCDown(type, biomeInd) {
const biomes = this.state.biomes.slice();
if (biomeInd < biomes.length - 1) {
const ind = biomes[biomeInd].npcs.indexOf(type);
if (ind != -1) {
biomes[biomeInd].npcs.splice(ind, 1);
biomes[biomeInd+1].npcs.unshift(type);
}
}
this.setState({
biomes: biomes,
});
this.setCookies(biomes);
}
getCookies() {
if (typeof Cookies.get("biomes") !== 'undefined') {
const biomes = JSON.parse(Cookies.get("biomes"));
biomes.forEach(b => {
b.npcs.forEach(n => {
toggleNPCDropdown(n, true);
});
});
this.setState({
biomes: biomes,
});
}
}
setCookies() {
Cookies.set("biomes", JSON.stringify(this.state.biomes), {sameSite: "lax"});
}
setCookies(biomes) {
Cookies.set("biomes", JSON.stringify(biomes), {sameSite: "lax"});
}
render() {
const biomeState = this.state.biomes;
const biomeList = biomeState.map((biome, ind) => {
return (
<Biome
key={ind}
biomeType={biome.type}
isFirst={ind == 0}
isLast={ind == biomeState.length-1}
delete={() => this.deleteBiome(ind)}
npcs={biome.npcs}
removeNPC={type => this.removeNPC(type, ind)}
moveNPCUp={type => this.moveNPCUp(type, ind)}
moveNPCDown={type => this.moveNPCDown(type, ind)}
/>
);
});
return (
<div className="col m-3 p-2 border rounded-lg">{biomeList}</div>
);
}
}
const tool = ReactDOM.render(<Tool />, document.getElementById("tool"));
const biomeDropdown = document.getElementById("biomeDropdownMenu");
biomeList.forEach(biomeName => {
const biomeProp = biomeProps[biomeName];
const className = "dropdown-item btn-"+biomeName;
const elem = document.createElement("button");
elem.innerHTML = biomeProp.name;
elem.type = "button";
elem.className = className;
elem.onclick = () => {
tool.addBiome(biomeName);
}
biomeDropdown.appendChild(elem);
});
const npcDropdown = document.getElementById("npcDropdownMenu");
npcList.forEach(npcName => {
const npcProp = npcProps[npcName];
const elem = document.createElement("button");
elem.innerHTML = npcProp.name;
elem.type = "button";
elem.className = "dropdown-item";
elem.onclick = () => {
tool.addNPC(npcName);
}
npcDropdown.appendChild(elem);
});
function addAll() {
biomeList.forEach(biomeName => {
tool.addBiome(biomeName);
});
}
function addAllNPCs() {
npcList.forEach(npcName => {
tool.addNPC(npcName);
});
}
function toggleNPCDropdown(type, disabled) {
const dropdown = document.getElementById("npcDropdownMenu");
const children = dropdown.children;
for (let i = 0; i < children.length; i++) {
if (children[i].innerHTML === npcProps[type].name) {
children[i].className = "dropdown-item";
if (disabled) children[i].className += " disabled";
}
}
}
function clearBiomes() {
tool.deleteAllBiomes();
}