This repository was archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.js
More file actions
101 lines (86 loc) · 3.31 KB
/
Copy pathreport.js
File metadata and controls
101 lines (86 loc) · 3.31 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
var mongoose = require('mongoose-q')()
, Schema = mongoose.Schema
, neow = require('neow')
, _ = require('lodash')
, chunk = require('chunk')
, Q = require('q')
, moment = require('moment')
, settings = require(__dirname + '/../config/settings')
, Hostile = require('./hostile');
var ReportSchema = new Schema({
ts: { type: Number, default: function() { return moment().unix(); } },
fleetKey: { type: String },
type: String,
reporterId: Number,
reporterName: String,
systemId: Number,
systemName: String,
operation: { type: String, default: 'validate' },
data: [ Schema.Types.Mixed ],
hostiles: [ Schema.Types.Mixed ]
});
ReportSchema.index({ ts: 1, fleetKey: 1 }, { expireAfterSeconds: settings.reportTtl });
ReportSchema.statics.prepare = function prepare(type, fleetKey, report) {
return new this({
type: type,
fleetKey: fleetKey,
reporterId: report.reporterId,
reporterName: report.reporterName,
systemId: report.systemId,
systemName: report.systemName,
operation: report.operation || report.text,
data: report.data || [],
hostiles: []
});
};
var is_whitelisted = function(whitelist, character) {
if (_.contains(whitelist.alliances, character.allianceID)) return true;
if (_.contains(whitelist.corporations, character.allianceID)) return true;
if (_.contains(whitelist.alliances, character.corporationID)) return true;
if (_.contains(whitelist.corporations, character.corporationID)) return true;
};
ReportSchema.methods.parse_standings = function parse_standings(whitelist) {
var fleetKey = this.fleetKey;
var reporter = {characterId: this.reporterId, characterName: this.reporterName};
var response = Q.defer();
if (this.hostiles.length > 0) return response.reject('Report hostiles already exist');
var hostiles = [];
var client = new neow.EveClient();
Q.all(_.map(chunk(this.data, 100), function(pilots) {
var pilot_list = pilots.join(',');
var batch = Q.defer();
client.fetch('eve:CharacterId', {names: pilots.join(',')})
.then(function(results) {
return client.fetch('eve:CharacterAffiliation', {ids: _.map(results.characters, function(c) { return c.characterID; }).join(",")});
})
.then(function(results) {
var resolved = _.map(results.characters, function(character) {
if (is_whitelisted(whitelist, character)) return false;
if (character.characterID === 0) return false;
if (character.characterName === '') return false;
var hostile = Hostile.prepare(fleetKey, reporter, character);
if (!hostile) throw 'Invalid hostile: ' + character;
return hostile.toObject();
});
batch.resolve(_.where(resolved));
})
.catch(function(error) {
batch.reject(error + ': ' + pilot_list);
})
.done();
return batch.promise;
})
)
.then(function(results) {
response.resolve(_.flatten(results));
})
.catch(function(error) {
console.error(error);
response.reject('CCP API data corrupt: ' + error);
})
.done();
return response.promise;
};
var Report = mongoose.model('Report', ReportSchema);
module.exports = Report;
exports.is_whitelisted = is_whitelisted;