-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataParser.js
More file actions
261 lines (231 loc) · 9.22 KB
/
Copy pathdataParser.js
File metadata and controls
261 lines (231 loc) · 9.22 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
251
252
253
254
255
256
257
258
259
260
261
/**
* Data parser for Elite Dangerous activity reports
* Parses copy/pasted Discord messages containing BGS and Powerplay data
*/
export class ActivityParser {
constructor() {
// Regex patterns for parsing
this.patterns = {
commander: /(?:CMDR|Commander|cmdr)[\s:]*([^\n]+)/i,
system: /(?:System|system|SYSTEM)[\s:]*([^\n]+)/i,
inf: /(?:INF|inf|Influence)[\s:]*([0-9,]+)/i,
merits: /(?:Merits|merits|MERITS)[\s:]*([0-9,]+)/i,
bounties: /(?:Bounties|bounties|BOUNTIES)[\s:]*(?:CR\s*)?([0-9,]+)/i,
combat_bonds: /(?:Combat\s*Bonds?|combat\s*bonds?)[\s:]*(?:CR\s*)?([0-9,]+)/i,
trade: /(?:Trade|trade|TRADE)[\s:]*(?:CR\s*)?([0-9,]+)/i,
exploration: /(?:Exploration|exploration|EXPLORATION|Cartographic|cartographic)[\s:]*(?:CR\s*)?([0-9,]+)/i,
missions: /(?:Mission|mission|MISSION).*INF[\s:]*([0-9,]+)/i,
};
}
/**
* Parse activity report from text
* @param {string} text - Raw text from Discord post
* @returns {Object} Dictionary containing parsed data
*/
parseActivity(text) {
const data = {
timestamp: new Date().toISOString(),
commander: this._extractValue(text, 'commander'),
system: this._extractValue(text, 'system'),
inf_points: this._extractNumeric(text, 'inf'),
merits: this._extractNumeric(text, 'merits'),
bounties: this._extractNumeric(text, 'bounties'),
combat_bonds: this._extractNumeric(text, 'combat_bonds'),
trade_profit: this._extractNumeric(text, 'trade'),
exploration_data: this._extractNumeric(text, 'exploration'),
mission_inf: this._extractNumeric(text, 'missions'),
raw_text: text
};
return data;
}
/**
* Extract string value using pattern
* @private
*/
_extractValue(text, patternKey) {
if (!(patternKey in this.patterns)) {
return null;
}
const match = text.match(this.patterns[patternKey]);
if (match) {
return match[1].trim();
}
return null;
}
/**
* Extract numeric value using pattern
* @private
*/
_extractNumeric(text, patternKey) {
const value = this._extractValue(text, patternKey);
if (value) {
try {
// Remove commas and convert to int
return parseInt(value.replace(/,/g, ''));
} catch (error) {
return 0;
}
}
return 0;
}
/**
* Determine if activity is BGS, Powerplay, or both
* @param {Object} data - Parsed activity data
* @returns {string} 'BGS', 'POWERPLAY', 'BOTH', or 'UNKNOWN'
*/
categorizeActivity(data) {
const hasBGS = [
data.inf_points || 0,
data.bounties || 0,
data.combat_bonds || 0,
data.trade_profit || 0,
data.exploration_data || 0,
data.mission_inf || 0
].some(val => val > 0);
const hasPowerplay = (data.merits || 0) > 0;
if (hasBGS && hasPowerplay) {
return 'BOTH';
} else if (hasPowerplay) {
return 'POWERPLAY';
} else if (hasBGS) {
return 'BGS';
} else {
return 'UNKNOWN';
}
}
/**
* Format data for BGS tracking sheet
* @param {Object} data - Activity data
* @returns {Array} Row data for sheets
*/
formatBGSRow(data) {
return [
data.timestamp || '',
data.commander || '',
data.system || '',
data.faction || '',
String(data.inf_points || 0),
String(data.bounties || 0),
String(data.combat_bonds || 0),
String(data.trade_profit || 0),
String(data.credits_spent || 0),
String(data.exploration_data || 0),
String(data.high_cz_won || 0),
String(data.medium_cz_won || 0),
String(data.low_cz_won || 0),
String(data.election_inf || 0)
];
}
/**
* Parse OD Elite Tracker format and return list of activities
* @param {string} text - Raw text from OD Elite Tracker
* @param {string} commander - Commander name
* @returns {Array} Array of activity objects
*/
parseODElite(text, commander) {
const activities = [];
const timestamp = new Date().toISOString();
// Split by lines
const lines = text.trim().split('\n');
let currentSystem = null;
for (const line of lines) {
const trimmedLine = line.trim();
// Check if it's a system header (bolded in Discord markdown)
if (trimmedLine.startsWith('> **') && trimmedLine.endsWith('**')) {
// Extract system name (remove > ** and **)
currentSystem = trimmedLine.substring(4, trimmedLine.length - 2).trim();
continue;
}
// Check if it's a faction activity line (starts with > )
if (trimmedLine.startsWith('> ') && currentSystem && trimmedLine.includes(':')) {
// Extract faction and activities
const lineContent = trimmedLine.substring(5).trim(); // Remove '> '
// Split by : to separate faction from activities
const parts = lineContent.split(':', 2);
if (parts.length < 2) {
continue;
}
const faction = parts[0].trim();
const activitiesStr = parts[1].trim();
// Parse the activities
const activityData = {
timestamp: timestamp,
commander: commander,
system: currentSystem,
faction: faction,
inf_points: 0,
bounties: 0,
combat_bonds: 0,
trade_profit: 0,
credits_spent: 0,
exploration_data: 0,
high_cz_won: 0,
medium_cz_won: 0,
low_cz_won: 0,
election_inf: 0
};
// Split activities by comma
const activityItems = activitiesStr.split(',').map(item => item.trim());
for (const item of activityItems) {
// Parse +n Inf
const infMatch = item.match(/\+(\d+)\s*Inf/i);
if (infMatch) {
activityData.inf_points += parseInt(infMatch[1]);
}
// Parse nx Failed (each failed = -2 INF)
const failedMatch = item.match(/(\d+)x\s*Failed/i);
if (failedMatch) {
const failedCount = parseInt(failedMatch[1]);
activityData.inf_points -= (failedCount * 2);
}
// Parse Spend nn.nM (credits spent)
const spendMatch = item.match(/Spend\s+([\d.]+)M/i);
if (spendMatch) {
const creditsM = parseFloat(spendMatch[1]);
activityData.credits_spent = Math.floor(creditsM * 1_000_000);
}
// Parse nn.nM BVs (bounty vouchers)
const bvMatch = item.match(/([\d.]+)M\s*BVs/i);
if (bvMatch) {
const bvM = parseFloat(bvMatch[1]);
activityData.bounties = Math.floor(bvM * 1_000_000);
}
// Also handle BVs with k suffix (thousands)
const bvKMatch = item.match(/([\d.]+)k\s*BVs/i);
if (bvKMatch) {
const bvK = parseFloat(bvKMatch[1]);
activityData.bounties = Math.floor(bvK * 1_000);
}
}
// Only add if there's actual data
if (activityData.inf_points !== 0 ||
activityData.bounties > 0 ||
activityData.credits_spent > 0) {
activities.push(activityData);
}
}
}
return activities;
}
/**
* Format data for Powerplay tracking sheet
* @param {Object} data - Activity data
* @returns {Array} Row data for sheets
*/
formatPowerplayRow(data) {
// Determine activity type based on what data is present
const activityTypes = [];
if ((data.merits || 0) > 0) {
activityTypes.push('Merits');
}
const activityType = activityTypes.length > 0 ? activityTypes.join(', ') : 'Unknown';
return [
data.timestamp || '',
data.commander || '',
data.system || '',
String(data.merits || 0),
activityType,
'' // Notes field
];
}
}