-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreporting.js
More file actions
120 lines (112 loc) · 3.12 KB
/
Copy pathreporting.js
File metadata and controls
120 lines (112 loc) · 3.12 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
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
var async = require('async'),
_ = require('underscore'),
database = require('./database'),
_user = require('./user'),
points = require('./points'),
expose = require('./utils').expose(exports),
mongo = require('mongodb');
var POINT_PERCENTAGE = 0.333;
var reporting = null;
database.notifications.once("open", function() {
database.getCollection("reporting", function(err, collection) {
reporting = collection;
});
});
function getById(id, cb) {
if(typeof id == 'string') {
id = mongo.ObjectID.createFromHexString(id);
}
reporting.findOne({_id: id}, cb);
}
function removeById(id, cb) {
if(typeof id == 'string') {
id = mongo.ObjectID.createFromHexString(id);
}
reporting.remove({_id: id}, cb);
}
function reportKill(reporter, killer, killee, cb) {
reporting.insert({
time: (new Date()).getTime(),
killer: killer._id,
killee: killee._id,
points: Math.ceil(killee.points * POINT_PERCENTAGE),
reporter: reporter.username == killer.username ? killer._id : killee._id,
confirmer: reporter.username == killer.username ? killee._id : killer._id
}, cb);
}
function confirmReport(user, id, cb) {
async.waterfall([
function(cb) {
getById(id, cb);
},
function(doc, cb) {
async.parallel({
killer: function(cb) {
_user.getById(doc.killer, cb);
},
killee: function(cb) {
_user.getById(doc.killee, cb);
}
}, function(err, results) {
cb(err, doc, results);
});
},
function(doc, results, cb) {
points.addTransaction({
time: (new Date()).getTime(),
type: 2,
killer: results.killer._id,
killee: results.killee._id
}, function(err) {
cb(err, doc, results);
});
},
function(doc, results, cb) {
points.transferPoints(doc.points, results.killee, results.killer, function(err) {
cb(err, doc);
});
},
function(doc, cb) {
removeById(doc._id, cb);
}
], cb);
}
function cancelReport(user, id, cb) {
removeById(id, cb);
}
function getReports(user, cb) {
var template = _.template('\
<form method="post" action="/report/<%= id %>"> \
<div class="message"> \
Did <span class="username"><%= username %></span> kill you? \
<span class="bounty">(<%= points %>pt)</span> \
</div> \
<input type="submit" class="button" name="op" value="Confirm" /> \
<input type="submit" class="button red" name="op" value="Deny" /> \
</form> \
');
reporting.find({confirmer: user._id}).sort({time: -1}).toArray(function(err, docs) {
if(err) {
cb(err);
} else {
async.map(docs, function(doc, cb) {
console.log(doc, cb);
_user.getById(doc.killer, function(err, user) {
if(err) {
cb(err);
} else {
var output = template({
id: doc._id,
username: user.name,
points: doc.points
});
cb(null, output);
}
});
}, function(err, reports) {
cb(err, reports);
});
}
});
}
expose(reportKill, confirmReport, cancelReport, getReports);