-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataProcessor.js
More file actions
561 lines (469 loc) · 17.5 KB
/
dataProcessor.js
File metadata and controls
561 lines (469 loc) · 17.5 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/**
* Data Processing Module for Laser Tag Analytics
* Processes JSON match data to calculate performance metrics
*/
/**
* returns true if the value is within the start and end time range
*/
function isWithinTimeRange(value, startTime = 0, endTime = -1) {
return value >= startTime &&
(endTime < 0 || value <= endTime)
}
class LaserTagDataProcessor {
constructor(matchData) {
this.data = matchData;
// Handle new nested structure
this.matchData = matchData.MatchData || matchData;
this.hits = this.matchData.hits || [];
this.events = this.matchData.events || [];
// Team data is at root level, not nested in MatchData
this.teamData = matchData.TeamData || matchData.teamData || {};
this.playerData = matchData.PlayerData || matchData.playerData || {};
this.metrics = {};
}
/**
* Calculate all performance metrics
*/
calculateMetrics() {
this.metrics = {
trickshot: this.calculateTrickshot(),
stealth: this.calculateStealth(),
speed: this.calculateSpeed(),
rpm: this.calculateRPM(),
range: this.calculateRange(),
accuracy: this.calculateAccuracy()
};
return this.metrics;
}
/**
* Calculate player interaction network data for node graph
*/
calculatePlayerNetwork(endTime = -1) {
// Get player data
const playerData = this.data.PlayerData || {};
console.log('PlayerData:', playerData);
console.log('Number of players:', Object.keys(playerData).length);
// Create nodes for each player
const nodes = Object.entries(playerData).map(([id, data]) => {
const playerId = parseInt(id);
const teamId = this.getPlayerTeam(playerId);
let color = '#fff'; // default color
let bgColor = "#000";
console.log(`Player ${id} (${data.playerName}): teamId = ${teamId}`);
let colors = this.getColors(playerId, endTime)
color = `rgb(${Math.floor(colors.primary.r * 255)}, ${Math.floor(colors.primary.g * 255)}, ${Math.floor(colors.primary.b * 255)})`;
bgColor = `rgb(${Math.floor(colors.secondary.r * 255)}, ${Math.floor(colors.secondary.g * 255)}, ${Math.floor(colors.secondary.b * 255)})`;
return {
id: id,
name: data.playerName || `Player ${id}`,
color: color,
background: bgColor,
teamId: teamId
};
});
// Track shot relationships
const shotCounts = {};
console.log('Processing hits for network graph, total hits:', this.hits.length);
this.hits.forEach(hit => {
const shooterId = hit.instigatorStateId?.index?.toString();
const targetId = hit.hitStateId?.index?.toString();
if (shooterId && targetId && shooterId !== targetId) {
const key = `${shooterId}-${targetId}`;
shotCounts[key] = (shotCounts[key] || 0) + 1;
}
});
console.log('Shot counts:', shotCounts);
console.log('Number of shot relationships:', Object.keys(shotCounts).length);
// Create links based on shot relationships
const links = Object.entries(shotCounts).map(([key, count]) => {
const [source, target] = key.split('-');
return {
source: source,
target: target,
value: count,
width: Math.min(Math.max(count * 2, 2), 15) // Increased scaling: count * 2, min 2, max 15
};
});
// Filter out nodes that have no connections
const connectedNodeIds = new Set();
links.forEach(link => {
connectedNodeIds.add(link.source);
connectedNodeIds.add(link.target);
});
const connectedNodes = nodes.filter(node => connectedNodeIds.has(node.id));
console.log('Network data result:');
console.log('- Nodes:', connectedNodes.length);
console.log('- Links:', links.length);
console.log('Connected nodes:', connectedNodes);
console.log('Links:', links);
return {
nodes: connectedNodes,
links: links
};
}
/**
* Trickshot = complex shots ratio based on distance and target type
*/
calculateTrickshot() {
if (this.hits.length === 0) return 0;
let complexShots = 0;
let totalMultiplier = 0;
this.hits.forEach(hit => {
// Check for long-range shots or difficult targets as "trick shots"
const distance = hit.distance || 0;
const isLongRange = distance > 2000; // Long range shots
const hasReflectiveMaterial = hit.hitResult && hit.hitResult.physMaterial &&
hit.hitResult.physMaterial.includes('Reflective');
const isRectangleTarget = hit.hitComponent && hit.hitComponent.includes('Rectangle');
if (isLongRange || hasReflectiveMaterial || isRectangleTarget || (hit.pointMultiplier > 1)) {
complexShots++;
}
totalMultiplier += hit.pointMultiplier || 1;
});
const trickshotRatio = complexShots / this.hits.length;
const avgMultiplier = totalMultiplier / this.hits.length;
return (trickshotRatio * avgMultiplier) * 100;
}
/**
* Stealth = average time between hits (in seconds)
*/
calculateStealth() {
if (this.hits.length <= 1) return 0;
const hitTimes = this.hits
.map(hit => hit.hitMatchState?.gameTime || 0)
.sort((a, b) => a - b);
let totalTimeDiff = 0;
for (let i = 1; i < hitTimes.length; i++) {
totalTimeDiff += hitTimes[i] - hitTimes[i - 1];
}
return totalTimeDiff / (hitTimes.length - 1);
}
/**
* Speed = average distance between shot start locations
*/
calculateSpeed() {
if (this.hits.length <= 1) return 0;
const startPositions = this.hits
.filter(hit => hit.hitResult?.traceStart)
.map(hit => hit.hitResult.traceStart);
if (startPositions.length <= 1) return 0;
let totalDistance = 0;
let distanceCount = 0;
for (let i = 1; i < startPositions.length; i++) {
const dist = this.calculateDistance3D(
startPositions[i - 1],
startPositions[i]
);
totalDistance += dist;
distanceCount++;
}
return distanceCount > 0 ? totalDistance / distanceCount : 0;
}
/**
* RPM = rounds per minute (shots per minute)
*/
calculateRPM() {
if (this.hits.length === 0) return 0;
const hitTimes = this.hits
.map(hit => hit.hitMatchState?.gameTime || 0)
.filter(time => time > 0);
if (hitTimes.length === 0) return 0;
const minTime = Math.min(...hitTimes);
const maxTime = Math.max(...hitTimes);
const timeSpanMinutes = (maxTime - minTime) / 60;
return timeSpanMinutes > 0 ? this.hits.length / timeSpanMinutes : 0;
}
/**
* Range = average range value for hits
*/
calculateRange() {
if (this.hits.length === 0) return 0;
const totalRange = this.hits.reduce((sum, hit) => {
return sum + (hit.distance || 0);
}, 0);
return totalRange / this.hits.length;
}
/**
* Accuracy = shots hit / shots fired
* We'll estimate shots fired from hit events and calculate accuracy per player
*/
calculateAccuracy() {
// Count hit events from the events array
const hitEvents = this.events.filter(event => event.eventName === "HitEvent");
const shotsHit = hitEvents.length;
// Estimate total shots fired - in team matches, assume some misses
// Use player data and hit distribution to estimate
const uniquePlayers = [...new Set(this.hits.map(hit => hit.instigatorStateId?.index))].filter(id => id !== undefined);
const avgHitsPerPlayer = this.hits.length / uniquePlayers.length;
// Estimate that accuracy decreases with more active players (more chaos)
const estimatedAccuracy = Math.min(95, Math.max(60, 100 - (avgHitsPerPlayer * 2)));
return estimatedAccuracy;
}
/**
* Helper function to calculate 3D distance between two points
*/
calculateDistance3D(point1, point2) {
const dx = point2.x - point1.x;
const dy = point2.y - point1.y;
const dz = point2.z - point1.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
/**
* Get normalized metrics for radar chart (0-100 scale)
*/
getNormalizedMetrics() {
const metrics = this.calculateMetrics();
return {
trickshot: Math.min(100, Math.max(0, metrics.trickshot)),
stealth: this.normalizeValue(metrics.stealth, 0, 60, true), // Reverse scale (lower is better) - increased max to 60s
speed: this.normalizeValue(metrics.speed, 0, 2000, false),
rpm: this.normalizeValue(metrics.rpm, 0, 60, false),
range: this.normalizeValue(metrics.range, 0, 500, false), // Reduced max to 500 units for better scaling
accuracy: Math.min(100, Math.max(0, metrics.accuracy))
};
}
/**
* Normalize a value to 0-100 scale
*/
normalizeValue(value, min, max, reverse = false) {
const normalized = ((value - min) / (max - min)) * 100;
const clamped = Math.min(100, Math.max(0, normalized));
return reverse ? 100 - clamped : clamped;
}
/**
* Get detailed metrics information
*/
getMetricsInfo() {
const raw = this.calculateMetrics();
const normalized = this.getNormalizedMetrics();
return {
trickshot: {
value: raw.trickshot.toFixed(2),
normalized: normalized.trickshot,
unit: '%',
description: 'Complex shot efficiency'
},
stealth: {
value: raw.stealth.toFixed(2),
normalized: normalized.stealth,
unit: 's',
description: 'Average time between hits'
},
speed: {
value: raw.speed.toFixed(0),
normalized: normalized.speed,
unit: ' units',
description: 'Movement between shots'
},
rpm: {
value: raw.rpm.toFixed(1),
normalized: normalized.rpm,
unit: ' shots/min',
description: 'Rate of fire'
},
range: {
value: raw.range.toFixed(0),
normalized: normalized.range,
unit: ' units',
description: 'Average shot distance'
},
accuracy: {
value: raw.accuracy.toFixed(1),
normalized: normalized.accuracy,
unit: '%',
description: 'Hit percentage'
}
};
}
/**
* Get team performance data
*/
getTeamPerformance( startTime = 0, endTime = -1) {
const teams = {};
Object.keys(this.teamData).forEach(teamId => {
const team = this.teamData[teamId];
// Skip spectator teams
if (team.bTeamIsSpectator) return;
const teamHits = this.hits.filter(hit => {
// Find hits by players on this team
const playerId = hit.instigatorStateId?.index;
return this.getPlayerTeam(playerId) === parseInt(teamId)
&& isWithinTimeRange(history.hitMatchState.gameTime, startTime, endTime)
});
teams[teamId] = {
name: team.teamName,
points: team.points,
hits: teamHits.length,
avgRange: teamHits.length > 0 ? teamHits.reduce((sum, hit) => sum + (hit.distance || 0), 0) / teamHits.length : 0,
color: team.primaryColor || { r: 0.5, g: 0.5, b: 0.5 } // Default color if missing
};
});
return teams;
}
/**
* Build mapping between internal PlayerIDs and PlayerData indices
* This helps correlate TeamChange events with actual player data
*/
buildPlayerIdMapping() {
if (this.playerIdMapping) return this.playerIdMapping;
this.playerIdMapping = new Map();
// Get all unique PlayerIDs from TeamChange events
const teamChangePlayerIds = new Set();
this.events.filter(event => event.eventName === "TeamChange")
.forEach(event => {
if (event.data && event.data.PlayerID !== undefined) {
teamChangePlayerIds.add(event.data.PlayerID);
}
});
// Get all player data indices
const playerDataIndices = Object.keys(this.playerData).map(id => parseInt(id)).sort((a, b) => a - b);
// Create mapping based on order and patterns
const sortedTeamChangeIds = Array.from(teamChangePlayerIds).sort((a, b) => a - b);
// If we have the same number of players, map them in order
if (sortedTeamChangeIds.length >= playerDataIndices.length) {
for (let i = 0; i < playerDataIndices.length; i++) {
const playerDataIndex = playerDataIndices[i];
const teamChangeId = sortedTeamChangeIds[i];
this.playerIdMapping.set(playerDataIndex, teamChangeId);
}
} else {
// Fallback: map what we can
for (let i = 0; i < Math.min(playerDataIndices.length, sortedTeamChangeIds.length); i++) {
const playerDataIndex = playerDataIndices[i];
const teamChangeId = sortedTeamChangeIds[i];
this.playerIdMapping.set(playerDataIndex, teamChangeId);
}
}
console.log('PlayerID mapping built:', this.playerIdMapping);
console.log('PlayerData indices:', playerDataIndices);
console.log('TeamChange PlayerIDs:', sortedTeamChangeIds);
return this.playerIdMapping;
}
/**
* Get player team assignment by looking at team change events starting at startTime
*/
getPlayerTeam(playerId, endTime = -1) {
if (playerId === undefined || playerId === null) return 0;
// Build player ID mapping if not already done
this.buildPlayerIdMapping();
// Convert playerData index to internal PlayerID used in events
const internalPlayerId = this.playerIdMapping.get(parseInt(playerId));
if (internalPlayerId !== undefined) {
// Look through team change events to find current team
const teamChangeEvents = this.events.filter(event =>
event.eventName === "TeamChange" &&
event.data && event.data.PlayerID === internalPlayerId &&
isWithinTimeRange(event.matchState.gameTime, 0, endTime)
);
if (teamChangeEvents.length > 0) {
const lastChange = teamChangeEvents[teamChangeEvents.length - 1];
const teamId = parseInt(lastChange.data.new || 0);
console.log(`Player ${playerId} (PlayerData) -> ${internalPlayerId} (TeamChange) -> team ${teamId}`);
return teamId;
}
}
// Try direct lookup as fallback
const directLookup = this.events.filter(event =>
event.eventName === "TeamChange" &&
event.data && event.data.PlayerID === parseInt(playerId) &&
isWithinTimeRange(event.matchState.gameTime, startTime, endTime)
);
if (directLookup.length > 0) {
const lastChange = directLookup[directLookup.length - 1];
const teamId = parseInt(lastChange.data.new || 0);
console.log(`Player ${playerId} (direct lookup) -> team ${teamId}`);
return teamId;
}
// Smart team assignment for players without explicit TeamChange events
// Skip player 10 (human player) and assign bots to teams alternately
/*if (parseInt(playerId) === 10) {
console.log(`Player ${playerId} (human player) -> team 0 (spectator)`);
return 0; // Human player as spectator
}
// For bot players, assign alternately to team 1 and 2
// Skip players that already have explicit assignments
const explicitlyAssignedPlayers = new Set();
this.playerIdMapping.forEach((internalId, playerDataIndex) => {
explicitlyAssignedPlayers.add(playerDataIndex);
});
if (!explicitlyAssignedPlayers.has(parseInt(playerId))) {
const botTeam = (parseInt(playerId) % 2) + 1; // Alternates between 1 and 2
console.log(`Player ${playerId} (auto-assigned bot) -> team ${botTeam}`);
return botTeam;
}
console.log(`Player ${playerId} -> team 0 (default)`);
return 0; // Default team*/
return -1; //not in game
}
getPointInfoForHit(hitIndex = -1){
const eventsWithHitInfo = this.events.filter(event => event.data?.HitID === hitIndex)
let playerPointsDelta = new Map()
let playerPointFinal = new Map()
let teamPointsDelta = new Map()
let teamPointFinal = new Map()
eventsWithHitInfo.forEach((event, index) => {
switch(event.eventName) {
case "TeamPointsChange":
let deltaTeam = event.data.new - event.data.old
teamPointsDelta.set(event.data?.ID, deltaTeam)
teamPointFinal.set(event.data?.ID, event.data.new)
break;
case "PlayerScore":
let delta = event.data.newScore - event.data.oldScore
playerPointsDelta.set(event.data.ID, delta)
playerPointFinal.set(event.data.ID, event.data.newScore)
break;
}
})
return {
playerDelta: playerPointsDelta,
playerFinal: playerPointFinal,
teamDelta: teamPointsDelta,
teamFinal: teamPointFinal
}
}
/**
* Get player statistics
*/
getPlayerStats(startTime = 0, endTime = -1) {
const players = {};
const hitsInRange = this.hits.filter(hit => isWithinTimeRange(hit.hitMatchState.gameTime, startTime, endTime))
Object.keys(this.playerData).forEach(playerId => {
const player = this.playerData[playerId];
const playerHits = this.hitsInRange.filter(hit => hit.instigatorStateId?.index === parseInt(playerId));
const playerGotHit = this.hitsInRange.filter(hit => hit.hitStateId?.index === parseInt(playerId));
const playerScoreDelta = playerHits
players[playerId] = {
name: player.playerName,
hits: playerHits.length,
gotHit: playerGotHit.length,
avgRange: playerHits.length > 0 ? playerHits.reduce((sum, hit) => sum + (hit.distance || 0), 0) / playerHits.length : 0,
team: this.getPlayerTeam(parseInt(playerId)),
color: player.preferredPrimaryColor
};
});
return players;
}
/**
* gets either the team or player preferred primary/secondary colors, depending on FFA and team setting
* @param {*} playerID
*/
getColors(playerID, endTime = -1) {
let team = this.getPlayerTeam(playerID, endTime)
if(this.matchData.bFreeForAll || (team >= 0 && !this.teamData[team].bPreferTeamColors)) {
return {primary: this.playerData[playerID].preferredPrimaryColor,
secondary: this.playerData[playerID].preferredSecondaryColor
}
} else {
return {primary: this.teamData[team].primaryColor,
secondary: this.teamData[team].secondaryColor
}
}
}
//converts the color of the format from data into a css friendly format
static convertColor(color) {
return `rgba(${color.r * 255}, ${color.g * 255}, ${color.b * 255}, ${color.a * 255})`
}
}
// Export for use in other modules
window.LaserTagDataProcessor = LaserTagDataProcessor;