-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonMonitor.js
168 lines (137 loc) · 5.65 KB
/
jsonMonitor.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
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
const fs = require('fs');
const chokidar = require('chokidar');
const express = require('express');
const WebSocket = require('ws');
const app = express();
const server = app.listen(3000, () => {
console.log('Server listening');
});
app.use(express.static('public'));
const directoryToWatch = './profiles';
const watcher = chokidar.watch(directoryToWatch, {
persistent: true,
});
console.log(`Monitoring JSON files in ${directoryToWatch} for changes...`);
const wss = new WebSocket.Server({server});
// Function to extract relevant player information from a JSON file
function extractPlayerInfo(filePath) {
const fileData = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(fileData);
const userID = jsonData.info.aid;
const username = jsonData.characters.pmc.Info.Nickname;
// const registrationDate = jsonData.characters.pmc.Info.RegistrationDate;
const registrationDate = new Date(jsonData.characters.pmc.Info.RegistrationDate * 1000).toLocaleString();
const level = jsonData.characters.pmc.Info.Level;
const lastSession = jsonData.characters.pmc.Stats.Eft.LastSessionDate;
const chestHealth = jsonData.characters.pmc.Health.BodyParts.Chest.Health;
const headHealth = jsonData.characters.pmc.Health.BodyParts.Head.Health;
const leftArmHealth = jsonData.characters.pmc.Health.BodyParts.LeftArm.Health;
const leftLegHealth = jsonData.characters.pmc.Health.BodyParts.LeftLeg.Health;
const rightArmHealth = jsonData.characters.pmc.Health.BodyParts.RightArm.Health;
const rightLegHealth = jsonData.characters.pmc.Health.BodyParts.RightLeg.Health;
const stomachHealth = jsonData.characters.pmc.Health.BodyParts.Stomach.Health;
const Energy = jsonData.characters.pmc.Health.Energy;
const Hydration = jsonData.characters.pmc.Health.Hydration;
const Temperature = jsonData.characters.pmc.Health.Temperature;
// Handle inraid status with error handling
let inRaidLocation = "none";
let inRaidCharacter = "unknown";
if (jsonData.inraid && jsonData.inraid.location && jsonData.inraid.character) {
inRaidLocation = jsonData.inraid.location || "none";
inRaidCharacter = jsonData.inraid.character || "unknown";
}
const scavUp = jsonData.characters.scav.Info.SavageLockTime;
// Assuming jsonData contains your JSON data
const CurrentWinStreakValue = jsonData.characters.pmc.Stats.Eft.OverallCounters.Items.find(item => {
const keys = item.Key;
// Check if the item has the keys "CurrentWinStreak" and "Pmc"
return keys.includes("CurrentWinStreak") && keys.includes("Pmc");
}).Value;
const KilledUsec = jsonData.characters.pmc.Stats.Eft.OverallCounters.Items.find(item => {
const keys = item.Key;
// Check if the item has the keys "CurrentWinStreak" and "Pmc"
return keys.includes("KilledUsec") && keys.includes("KilledUsec");
}).Value;
const KilledBear = jsonData.characters.pmc.Stats.Eft.OverallCounters.Items.find(item => {
const keys = item.Key;
// Check if the item has the keys "CurrentWinStreak" and "Pmc"
return keys.includes("KilledBear") && keys.includes("KilledBear");
}).Value;
const TotalInGameTime = jsonData.characters.pmc.Stats.Eft.TotalInGameTime;
const insuranceReady = jsonData.insurance;
// Add more fields as needed
return {
username,
level,
registrationDate,
CurrentWinStreakValue,
KilledUsec,
KilledBear,
inRaidLocation,
inRaidCharacter,
TotalInGameTime,
scavUp,
insuranceReady,
Energy,
Hydration,
Temperature,
chestHealth,
headHealth,
leftArmHealth,
leftLegHealth,
rightArmHealth,
rightLegHealth,
stomachHealth,
userID,
lastSession
};
}
// Function to send player information to all connected WebSocket clients
function sendPlayerInfoToClients() {
const playerInfoArray = [];
fs.readdir(directoryToWatch, (err, files) => {
if (err) {
console.error(`Error reading directory: ${err}`);
return;
}
files.forEach((file) => {
const filePath = `${directoryToWatch}/${file}`;
try {
const playerInfo = extractPlayerInfo(filePath);
playerInfoArray.push(playerInfo);
} catch (error) {
console.error(`Error extracting player info from file ${filePath}: ${error}`);
}
});
// Sort players based on level (assuming level is a number)
playerInfoArray.sort((a, b) => b.level - a.level);
// Send player info to all connected clients
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(playerInfoArray));
}
});
});
}
// WebSocket message handling
wss.on('connection', (ws) => {
console.log('Client connected');
// Event listener for WebSocket messages
ws.on('message', (message) => {
const receivedMessage = message.toString(); // Convert message to string
console.log('Received message from client:', receivedMessage);
if (receivedMessage === 'updateLeaderboard') {
console.log('Received request to update leaderboard');
sendPlayerInfoToClients();
}
});
});
// Send player info from all files in the directory when the server starts
sendPlayerInfoToClients();
watcher.on('change', (path) => {
console.log(`File ${path} has been changed`);
sendPlayerInfoToClients();
});
watcher.on('error', (error) => {
console.error(`Watcher error: ${error}`);
});