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 pathParser.js
More file actions
250 lines (230 loc) · 8.13 KB
/
Copy pathParser.js
File metadata and controls
250 lines (230 loc) · 8.13 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
var etree = require('elementtree')
, https = require('https')
, request = require('request')
, _ = require('lodash')
, encoding = require('encoding')
var Dict = function() {
"use strict";
this._root = {};
};
Dict.prototype = {
/**
* @param {string} word
*/
add: function add(word) {
var i, char, node = this._root, uword;
if( !(typeof word === 'string' || word instanceof String) ) {
throw new TypeError("word is not string");
}
uword = word.toUpperCase();
// Assert start position with a space character
node = node[" "] || (node[" "] = {_parent: node});
for( i = 0 ; i < uword.length; i++ ) {
char = uword.charAt(i);
node = node[char] || (node[char] = {_parent: node});
}
node._result = word;
},
compile: function compile() {
var queue = [], entry, node, child, fall;
queue.push(this._root);
while( queue.length > 0 ) {
node = queue.shift();
delete node._fall;
var keys = Object.keys(node);
for( var i = 0 ; i < keys.length; i++ ) {
var key = keys[i];
if(key.length > 1) {
continue;
}
queue.push(node[key]);
}
}
this._root._fall = this._root;
queue.push({char: null, node: this._root});
while( queue.length > 0 ) {
entry = queue.shift();
node = entry.node;
var keys = Object.keys(node);
for( var i = 0 ; i < keys.length; i++ ) {
var key = keys[i];
if(key.length > 1) {
continue;
}
var char = key;
child = node[key];
queue.push({char: char, node: child});
}
if( node === this._root ) {
continue;
}
fall = node._parent._fall;
while( fall[entry.char] === undefined && fall !== this._root ) {
fall = fall._fall;
}
node._fall = fall[entry.char] || this._root;
if( node._fall === node ) {
node._fall = this._root;
}
}
},
search: function search(text) {
var result = [], state = this._root, node, i, self=this;
if( !(typeof text === 'string' || text instanceof String) ) {
throw new TypeError("word is not string");
}
text = text.toUpperCase();
var step = function search_step(char) {
node = state;
while( node[char] === undefined && node !== self._root ) {
node = node._fall;
}
if( node === self._root ) {
node = node[char] || self._root;
}
else {
node = node[char];
}
state = node;
while( node !== self._root ) {
if( node._result ) {
result.push(node._result);
}
node = node._fall;
}
};
step(" ");
for( i = 0 ; i < text.length ; i++ ) {
step(text.charAt(i));
}
return result;
}
};
var getCharIDs = function getCharIDs(characterList, next) {
https.get('https://api.eveonline.com/Eve/CharacterID.xml.aspx?names=' + characterList.join(","), function(response){
var result = '';
response.on('data', function(chunk){result += chunk;});
response.on('end', function() {
var xml = etree.parse(result);
xml.findall('./result/rowset/row').map(function(row) {
if( characters[row.attrib.name.toUpperCase()] !== undefined ) {
return;
}
var charID = parseInt(row.attrib.characterID);
if( charID !== 0 ){
characters[row.attrib.name.toUpperCase()] = charID;
charDict.add(row.attrib.name);
}
else {
characters[row.attrib.name.toUpperCase()] = null;
}
});
charDict.compile();
next();
});
});
};
var system_list;
request({url: Data.config.domain + '/data/map.json', json: true}, function (error, response, body) {
if (error) console.log(error);
system_list = body.Systems;
});
var lineRegex = /\[ (\d{4}\.\d{2}\.\d{2} \d{2}:\d{2}:\d{2}) \] ([\w ]+) > (.*)/i;
var killmailRegex = /Kill: (.*) \((.*)\)/i;
var characters = Object.create(null);
var charDict = new Dict();
var processLine = function processLine(line, next) {
if(line != undefined) {
// Strip control characters
var buffer = encoding.convert(line, 'utf-16');
var result = lineRegex.exec(buffer.slice(7).toString().replace(/\0/g,''));
if(result) {
var timestamp = result[1];
var sender = result[2];
var text = result[3];
var requestList = [];
var resolvedCharacters = {};
var clear = false;
if( sender === 'EVE System') {
next();
return;
}
if(characters[sender.toUpperCase()] === undefined) {
requestList.push(sender);
}
var killmailResult = killmailRegex.exec(line);
if(killmailResult) {
var deadCharacter = killmailResult[1];
var lostShip = killmailResult[2];
requestList.push(deadCharacter);
var getCharIDsComplete = function getCharIDsComplete() {
next({time: Util.getTime(), raw: line, type: 'sourcedKillmail',
data: { type: 'sourcedKillmail', reporterId: characters[sender.toUpperCase()], reporterName: sender,
pilots: {deadCharacter: characters[deadCharacter.toUpperCase()]}, lostShip: lostShip}});
};
if(requestList.length > 0) {
getCharIDs(requestList, getCharIDsComplete);
}
else {
getCharIDsComplete();
}
}
else {
var split = text.split(" ");
var system;
split.forEach(function (element) {
var matched_system = _.findLast(system_list, function(s) {
var r = new RegExp('^' + s.name + '[a-z0-9\\-]{0,3}$', 'i');
return r.test(element);
});
if(matched_system !== undefined) {
system = {systemName: matched_system.name, systemId: matched_system.id, regionId: matched_system.regionID};
if (element.toUpperCase() === system.systemName.toUpperCase()) return;
}
if(characters[element.toUpperCase()] === undefined) {
var searchResult = charDict.search(element).sort(function(a,b){return b.length - a.length}).shift();
if( searchResult !== undefined && characters[searchResult.toUpperCase()] !== null ) {
resolvedCharacters[searchResult] = characters[searchResult.toUpperCase()];
}
else if( /(clear|clr|empty)/.test(element) ) {
clear = true;
}
else if( /^[A-Za-z0-9\-\_\' ]+$/.test(element) ) {
requestList.push(element);
}
}
else if( characters[element.toUpperCase()] !== null ) {
resolvedCharacters[element] = characters[element.toUpperCase()];
}
});
if( system ) {
var type = (clear) ? 'sourcedClear' : 'sourcedHostile';
if( requestList.length > 0 ) {
var getCharIDsComplete = function getCharIDsComplete() {
// if (!Object.keys(resolvedCharacters).length) return next();
requestList.filter(function(c){return c !== sender && characters[c.toUpperCase()] != null;})
.forEach(function(c){resolvedCharacters[c] = characters[c.toUpperCase()]});
return next({time: Util.getTime(), type: type, raw: line, data: {clear: clear, type: type,
reporterId: characters[sender.toUpperCase()], reporterName: sender,
systemName: system.systemName, systemId: system.systemId,
pilots: resolvedCharacters}});
};
getCharIDs(requestList, getCharIDsComplete);
}
else {
return next({time: Util.getTime(), type: type, raw: line, data: {clear: clear, type: type,
reporterId: characters[sender.toUpperCase()], reporterName: sender,
systemName: system.systemName, systemId: system.systemId,
pilots: resolvedCharacters}});
}
}
else {
return next();
}
}
}
else {
return next();
}
}
};