-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstate.js
More file actions
94 lines (76 loc) · 2.19 KB
/
state.js
File metadata and controls
94 lines (76 loc) · 2.19 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
var fs = require('fs'),
_ = require('lodash'),
printit = require('printit');
var log = printit({
prefix: 'TweetWall::State',
date: true
});
// Delays
const SAVE_STATE_DELAY = 60;
const ROLL_DELAY = 5*60;
// The app's state is all the variables containing the data on the battle
class State {
// Initialise the object
// @param: fileName (string) File to read/write battls history. If the
// file does not exist, it will be created.
constructor(fileName) {
this.fileName = fileName;
if(fs.existsSync(this.fileName)) {
let back = require(this.fileName);
this.times = back.times;
this.battle = back.battle;
} else {
this.times = [];
this.battle = {};
}
for(let hashtag of require('./config.json').battle) {
if(!this.battle[hashtag]) {
this.battle[hashtag] = 0;
}
}
}
// Periodically write the battle state in the history file
autoSaveCurrentState() {
setTimeout(() => {
let state = {
times: this.times,
battle: this.battle
};
let stateStr = JSON.stringify(state, null, 4);
fs.writeFile(this.fileName, stateStr, (err) => {
if (err) {
log.error(err);
}
else {
log.info('Saved');
}
this.autoSaveCurrentState();
});
}, SAVE_STATE_DELAY * 1000);
}
// Periodically save the current battle state so we can compare it with the
// other saves
autoRoll(onRolled) {
setTimeout(() => {
this.battle._time = Math.floor(new Date().getTime() / 1000);
this.times.push(_.clone(this.battle));
log.debug('Rolled state');
onRolled(this.battle);
this.autoRoll(onRolled);
}, ROLL_DELAY * 1000);
}
// Update the counter for a given hashtag, adding a given amount to it
// @param: hashtag (string) The hashtag to update the counter of
// @param: amount (integer) The ammount to add to the counter
updateCounter(hashtag, amount) {
this.battle[hashtag] += amount;
}
// Indicates if the counter for a given hashtag is null
// @param: hashtag (string) Hashtag to check the counter of
// @return: true if the counter equals to 0
// false if the counter is set to a non-null value
isEmpty(hashtag) {
return (!this.battle || !this.battle[hashtag]);
}
}
module.exports = (new State('./back.json'));