-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivinity.js
More file actions
95 lines (81 loc) · 2.1 KB
/
divinity.js
File metadata and controls
95 lines (81 loc) · 2.1 KB
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
const EventEmitter = require('events');
class Divinity {
constructor(name, timeFactor) {
this.name_ = name || 'UNKDIVINITY';
this.corn_ = 0;
this.gold_ = 0;
this.worldEvents_ = new EventEmitter();
this.timeFactor_ = timeFactor || 1000;
}
showResources() {
console.log(`${this.name_}: C ${this.corn_}, G ${this.gold_}`);
}
init() {
this.gaiaInterval_ = setInterval(() => {
this.worldEvents.emit('favor', {
corn: this.corn * 0.1,
gold: this.gold * 0.1
});
if (Math.random() > 0.95) {
this.worldEvents.emit('blessing', {
corn: 100 * this.corn,
gold: 100 * this.gold
});
}
if (Math.random() > 0.99) {
this.worldEvents.emit('retribution', Math.floor(10000 * Math.random()));
}
}, this.timeFactor);
}
offeringCorn(offer) {
return new Promise((resolve, reject) => {
if (typeof offer === 'number') {
setTimeout(() => {
this.corn_ = offer >= 0 ? this.corn + offer : 0;
resolve();
}, 4 * this.timeFactor * Math.random());
} else {
reject(
new Error(
`You didn't gave a number of corn to ${this.name}, Earth collapsed`
)
);
}
});
}
offeringGold(offer) {
return new Promise((resolve, reject) => {
if (typeof offer === 'number') {
setTimeout(() => {
this.gold_ = offer >= 0 ? this.gold + offer : 0;
resolve();
}, 4 * this.timeFactor * Math.random());
} else {
reject(
new Error(
`You didn't gave a number of gold to ${this.name}, Earth collapsed`
)
);
}
});
}
get corn() {
return this.corn_;
}
get gold() {
return this.gold_;
}
get worldEvents() {
return this.worldEvents_;
}
get name() {
return this.name_;
}
get timeFactor() {
return this.timeFactor_;
}
endWorld() {
clearInterval(this.gaiaInterval_);
}
}
module.exports = {Divinity};