-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualizeData.js
210 lines (184 loc) · 6.57 KB
/
visualizeData.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
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
const fs = require('fs');
const sqlite3 = require('sqlite3').verbose();
const { SingleBar } = require('cli-progress');
// Connect to SQLite database
const db = new sqlite3.Database('game_data.db');
db.all("SELECT COUNT(*) as count FROM players", [], (err, rows) => {
if (err) {
console.error("Error counting players:", err);
} else {
console.log("Number of players in database:", rows[0].count);
}
});
// Query to retrieve planets and their voxel data overrides
const planetsQuery = `
SELECT
p.uuid AS planet_uuid, p.name AS planet_name, p.voxel_data AS voxel_data
FROM planets p
`;
// Query to retrieve players and their inventories
const playersQuery = `
SELECT
pl.uuid AS player_uuid, pl.name AS player_name,
i.item_name AS item_name, i.quantity AS item_quantity
FROM players pl
LEFT JOIN inventory i ON pl.uuid = i.player_uuid
`;
// Initialize progress bars
const progressBarPlanets = new SingleBar({
format: 'Planets |{bar}| {percentage}% | {value}/{total} Queries',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});
const progressBarPlayers = new SingleBar({
format: 'Players |{bar}| {percentage}% | {value}/{total} Queries',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});
// Maximum size allowed for voxel data (adjust as needed)
const MAX_VOXEL_DATA_SIZE = 100; // Example maximum size
// Data storage for planets and players
const planetsData = {};
const playersData = {};
// Function to execute a query with progress bar
function executeQuery(query, progressBar, onDataReceived, onComplete) {
console.log("Executing query:", query);
db.all(query, [], (err, rows) => {
if (err) {
console.error('Error querying database:', err.message);
return;
}
console.log(`Query returned ${rows.length} rows`);
progressBar.start(rows.length, 0);
rows.forEach((row, index) => {
onDataReceived(row);
progressBar.update(index + 1);
});
progressBar.stop();
onComplete();
});
}
// Execute planets query
executeQuery(planetsQuery, progressBarPlanets, row => {
const { planet_uuid, planet_name, voxel_data } = row;
// Check if voxel data exceeds maximum size
if (voxel_data && voxel_data.length > MAX_VOXEL_DATA_SIZE) {
planetsData[planet_uuid] = {
uuid: planet_uuid,
name: planet_name,
voxelData: "OVERFLOW" // Placeholder for overflow case
};
} else {
planetsData[planet_uuid] = {
uuid: planet_uuid,
name: planet_name,
voxelData: voxel_data ? JSON.parse(voxel_data) : []
};
}
}, checkQueryCompletion);
// Execute players query
executeQuery(playersQuery, progressBarPlayers, row => {
const { player_uuid, player_name, item_name, item_quantity } = row;
if (!playersData[player_uuid]) {
playersData[player_uuid] = {
uuid: player_uuid,
name: player_name,
inventory: []
};
}
if (item_name !== null && item_quantity !== null) {
playersData[player_uuid].inventory.push({
name: item_name,
quantity: item_quantity
});
}
}, checkQueryCompletion);
// Function to generate ASCII table
function generateTable(headers, rows) {
const colWidths = headers.map((header, i) => Math.max(header.length, ...rows.map(row => row[i].toString().length)));
const separator = '+' + colWidths.map(w => '-'.repeat(w + 2)).join('+') + '+\n';
const headerRow = '| ' + headers.map((header, i) => header.padEnd(colWidths[i])).join(' | ') + ' |\n';
const dataRows = rows.map(row => '| ' + row.map((cell, i) => cell.toString().padEnd(colWidths[i])).join(' | ') + ' |\n').join('');
return separator + headerRow + separator + dataRows + separator;
}
// Handle completion of all queries
function handleQueryCompletion() {
console.log("Handling query completion");
console.log("Players data at completion:", JSON.stringify(playersData, null, 2));
if (Object.keys(playersData).length === 0) {
console.error("No player data available. Aborting visualization.");
return;
}
// Prepare text content for file
let content = '=== Game Data Visualization ===\n\n';
// Add planets and their voxel data overrides
content += '--- Planets and Voxel Data ---\n\n';
let planetHeaders = ['Planet UUID', 'Planet Name', 'Voxel Data'];
let planetRows = [];
Object.values(planetsData).forEach(planet => {
if (planet.voxelData === "OVERFLOW") {
planetRows.push([
planet.uuid,
planet.name,
"OVERFLOW"
]);
} else {
planet.voxelData.forEach(data => {
planetRows.push([
planet.uuid,
planet.name,
JSON.stringify(data)
]);
});
}
});
content += generateTable(planetHeaders, planetRows);
// Add players and their inventory
console.log("Players data before table generation:", JSON.stringify(playersData, null, 2));
content += '--- Players and Inventory ---\n\n';
let playerHeaders = ['Player UUID', 'Player Name', 'Item Name', 'Item Quantity'];
let playerRows = [];
Object.values(playersData).forEach(player => {
if (player.inventory.length === 0) {
playerRows.push([
player.uuid,
player.name,
'No items',
'0'
]);
} else {
player.inventory.forEach(item => {
playerRows.push([
player.uuid,
player.name,
item.name,
item.quantity
]);
});
}
});
content += generateTable(playerHeaders, playerRows);
// Write content to file
const fileName = 'visualization.txt';
fs.writeFile(fileName, content, err => {
if (err) {
console.error('Error writing to file:', err);
return;
}
console.log(`Visualization data written to ${fileName}`);
// Close the database connection
db.close();
});
}
// Ensure both progress bars complete before finalizing
let queriesCompleted = 0;
function checkQueryCompletion() {
queriesCompleted++;
if (queriesCompleted === 2) {
handleQueryCompletion();
}
}
progressBarPlanets.on('stop', checkQueryCompletion);
progressBarPlayers.on('stop', checkQueryCompletion);