-
Notifications
You must be signed in to change notification settings - Fork 0
/
riskeventmanager.js
102 lines (92 loc) · 3 KB
/
riskeventmanager.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
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var ObjectID = require('mongodb').ObjectID;
/*
{
userKey: userKey,
action: conquer / notconquer / selectRegion / selectCountry,
target: country/region name,
gameKey: gameKey,
timestamp: timestamp
}
*/
RiskEventManager = function(host, port) {
this.db = new Db('tinyarm', new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function(err, client){this.client = client;});
};
RiskEventManager.prototype.getCollection= function(callback) {
this.db.collection('risk_event', function(error, events_collection) {
if( error ) callback(error);
else callback(null, events_collection);
});
};
RiskEventManager.prototype.findAll = function(callback) {
this.getCollection(function(error, events_collection) {
if( error ) callback(error);
else {
events_collection.find().toArray(function(error, results) {
if( error ) callback(error);
else callback(null, results);
//this.client.close();
});
}
});
};
RiskEventManager.prototype.find = function(query, callback) {
this.getCollection(function(error, events_collection) {
if( error ) callback(error);
else {
events_collection.find(query).toArray(function(error, results) {
if( error ) callback(error);
else callback(null, results);
//this.client.close();
});
}
});
};
RiskEventManager.prototype.findLast = function(callback) {
//console.log('findLastActivity: ');
this.getCollection(function(error, events_collection) {
if( error ) callback(error);
else {
events_collection.find().sort({"date":-1}).limit(1).toArray(function(error, results) {
//console.log('findLastActivity: resp');
if( error ) callback(error);
else callback(null, results);
//this.client.close();
});
}
});
};
RiskEventManager.prototype.findById = function(id, callback) {
this.getCollection(function(error, events_collection) {
if( error ) callback(error);
else {
events_collection.findOne(
{_id: events_collection.db.bson_serializer.ObjectID.createFromHexString(id)}
, function(error, result) {
if( error ) callback(error);
else callback(null, result);
//this.client.close();
});
}
});
};
RiskEventManager.prototype.save = function(events) {
this.getCollection(function(error, events_collection) {
if( error ) console.log(error);
else {
if( typeof(events.length)=="undefined")
events = [events];
for( var i =0;i< events.length;i++ ) {
var event = events[i];
event.timestamp = new Date();
}
events_collection.insert(events, function() {
//console.log(events);
//this.client.close();
});
}
});
};
exports.RiskEventManager = RiskEventManager;