-
Notifications
You must be signed in to change notification settings - Fork 4
/
engine.js
195 lines (151 loc) · 4.54 KB
/
engine.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
var fs = require("fs");
var async = require("async");
var git = require('gitty');
var execSync = require('child_process').execSync;
var Engine = function(){
var _this = this;
var dis_snap_path = "data/dis_snapshot.json";
var rules_path = "data/rules.json";
var myRepo = git('.');
var url = 'https://github.com/wizardamigosinstitute/RepPointsServer';
var dis_snapshot, rules;
var event_queue = async.queue(function(task, callback){
_this.eventWrapper(task.fn, task.args, task.message, callback);
});
this.enqueueTask = function(_fn, _args, _message){
event_queue.push({
fn: _fn,
args: _args,
message: _message
});
}
var redistribute_timer_id;
var creation_timer_id;
this.start = function(){
async.series([
function(cb){
_this.gitInit(cb);
},
function(cb){
_this.loadData(cb);
},
function(cb){
redistribute_timer_id = setInterval(function(){
_this.enqueueTask(_this.performRedistribution, [], "redistribution of points by RepPointsServer");
}, rules.redistribution_interval);
creation_timer_id = setInterval(function(){
_this.enqueueTask(_this.performCreatePoints, [], "points created by RepPointsServer");
}, rules.creation_interval);
cb();
}
]);
}
this.gitInit = function(cb){
if(!fs.existsSync('.git')){
console.log('initializing git');
myRepo.initSync();
myRepo.addRemoteSync('origin', url);
execSync('git config --global user.email ' + process.env.GITHUB_USERNAME);
execSync('git config --global user.name ' + process.env.GITHUB_USERNAME);
execSync('git config credential.helper store');
myRepo.addSync(['-A']);
myRepo.commitSync('git init from heroku');
}
cb();
}
this.loadData = function(cb){
console.log("--> git pull origin master");
myRepo.pull('origin', 'master', function(err){
if(err) return console.log(err);
console.log('--> git pull successful!');
dis_snapshot = JSON.parse(fs.readFileSync(dis_snap_path, 'utf8'));
console.log("read dis_snapshot");
rules = JSON.parse(fs.readFileSync(rules_path, 'utf8'));
console.log("read rules");
if(cb !== undefined) cb();
});
}
this.saveData = function(message, cb){
//write to file
fs.writeFileSync(dis_snap_path, JSON.stringify(dis_snapshot, null, 4));
//stage
myRepo.addSync([dis_snap_path]);
//commit
myRepo.commitSync(message);
//push
execSync('git push --all --repo=' + url);
console.log("pushed to remote");
if(cb !== undefined) cb();
}
this.eventWrapper = function(fn, args, message, queue_callback){
console.log("performing '" + message + "'");
async.series([
function(cb){
_this.loadData(cb);
},
function(cb){
fn.apply(null, args);
cb();
},
function(cb){
_this.saveData(message, cb);
},
function(cb){
cb();
queue_callback();
}
]);
}
this.performRedistribution = function(){
var sum = 0;
dis_snapshot.users.forEach(function(e){
var r;
sum += r = e.points * rules.redistribution_rate;
e.points -= r;
});
dis_snapshot.users.forEach(function(e, i, arr){
e.points += sum / arr.length;
});
dis_snapshot.timestamp = Date.now();
}
this.performCreatePoints = function(){
dis_snapshot.users.forEach(function(e, i, arr){
e.points += rules.creation_rate / arr.length;
});
dis_snapshot.timestamp = Date.now();
}
/**
* Transfers reputation points from user A to user B
* if balance of A is sufficient
* #transferPoints
* @param {String} fromUserId
* @param {String} toUserId
* @param {int} amount
*/
this.transferPoints = function(fromUserId, toUserId, amount){
var valid = false;
var users = dis_snapshot.users;
for(var i = 0 ; i < users.length ; i++){
if(users[i].name === fromUserId){
console.log("fromUser found");
if(users[i].points >= amount){
console.log("withdrawal successful");
users[i].points -= amount;
valid = true;
}
break;
}
}
if(valid){
for(var i = 0 ; i < users.length ; i++){
if(users[i].name == toUserId){
console.log("toUser found");
users[i].points = parseInt(users[i].points) + parseInt(amount);
break;
}
}
}
dis_snapshot.timestamp = Date.now();
}
}
module.exports = Engine;