-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
389 lines (350 loc) · 14 KB
/
index.html
File metadata and controls
389 lines (350 loc) · 14 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CDDA Artifact Parser</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f9f9f9;
}
h1 {
color: #333;
}
#fileInput {
margin-bottom: 10px;
}
#info {
margin-bottom: 10px;
}
#parseButton, #downloadButton {
padding: 8px 16px;
font-size: 16px;
cursor: pointer;
margin-right: 10px;
}
#parseButton:disabled, #downloadButton:disabled {
background-color: #ccc;
cursor: not-allowed;
}
#results {
margin-top: 20px;
border-collapse: collapse;
width: 100%;
background-color: #fff;
}
#results th, #results td {
border: 1px solid #ddd;
padding: 8px;
}
#results th {
background-color: #f2f2f2;
text-align: left;
}
.modifier-add {
color: green;
font-weight: bold;
}
.modifier-mult {
color: blue;
font-weight: bold;
}
.error {
color: red;
margin-top: 10px;
}
/* Responsive Design */
@media (max-width: 600px) {
#results th, #results td {
padding: 4px;
font-size: 12px;
}
#parseButton, #downloadButton {
font-size: 14px;
padding: 6px 12px;
}
}
/* Processing Text */
#processingMessage {
display: none;
margin-top: 10px;
font-style: italic;
color: #555;
}
</style>
</head>
<body>
<h1>Artifact Parser</h1>
<div id="info">Select the .sav file from your CDDA save world directory.<br>This will display all the artifacts the character has in the inventory and their effects.<br>AR stands for <a href="https://github.com/CleverRaven/Cataclysm-DDA/blob/master/doc/ARTIFACTS.md">Artifact Resonance</a>.<br>Star marks a favourited item, and square brackets show item notes, like so: <code>* altered_scissors [slow, call of tindalos, flash, pain] (5/5)</code></div>
<div>
<input type="file" id="fileInput" accept=".sav">
<button id="parseButton">Parse .sav File</button>
<button id="downloadButton" disabled>Download CSV</button>
</div>
<div id="errorMessage" class="error"></div>
<div id="processingMessage">Processing...</div>
<table id="results">
<thead>
<tr>
<th>TypeID (Charges/Max Charges)</th>
<th>Effects</th>
</tr>
</thead>
<tbody>
<!-- Results will be inserted here -->
</tbody>
</table>
<script>
document.getElementById('parseButton').addEventListener('click', () => {
const fileInput = document.getElementById('fileInput');
const errorMessage = document.getElementById('errorMessage');
const processingMessage = document.getElementById('processingMessage');
errorMessage.textContent = ''; // Clear previous errors
processingMessage.style.display = 'none'; // Hide processing message
if (fileInput.files.length === 0) {
alert('Please select a .sav file.');
return;
}
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = function(event) {
try {
processingMessage.style.display = 'block'; // Show processing message
// Split the file content into lines
const lines = event.target.result.split('\n');
// Remove lines starting with '#'
const jsonLines = lines.filter(line => !line.trim().startsWith('#'));
// Join the remaining lines back into a single string
const jsonString = jsonLines.join('\n');
// Parse the JSON content
const data = JSON.parse(jsonString);
const relics = findRelics(data);
if (relics.length === 0) {
errorMessage.textContent = 'No relics found in the provided file.';
}
processRelics(relics);
} catch (e) {
errorMessage.textContent = 'Invalid .sav file or JSON structure.';
console.error(e);
} finally {
processingMessage.style.display = 'none'; // Hide processing message
}
};
reader.readAsText(file);
});
/**
* Recursively search the JSON object for all objects containing 'relic_data'
* @param {Object|Array} obj - The JSON object or array to search
* @returns {Array} - An array of objects that contain 'relic_data'
*/
function findRelics(obj) {
let relics = [];
if (Array.isArray(obj)) {
obj.forEach(item => {
relics = relics.concat(findRelics(item));
});
} else if (typeof obj === 'object' && obj !== null) {
if (obj.hasOwnProperty('relic_data')) {
relics.push(obj);
}
Object.values(obj).forEach(value => {
relics = relics.concat(findRelics(value));
});
}
return relics;
}
/**
* Processes the array of relic objects to extract required information
* @param {Array} relics - Array of relic objects
*/
function processRelics(relics) {
const processedRelics = relics.map(relic => {
let typeid = relic.typeid || 'unknown';
const chargeInfo = relic.relic_data.charge_info || {};
const charges = chargeInfo.charges || 0;
const maxCharges = chargeInfo.max_charges || 0;
// Handle 'is_favorite' and 'item_vars.item_note'
const isFavorite = relic.is_favorite || false;
const itemVars = relic.item_vars || {};
const itemNote = itemVars.item_note || '';
// Prefix with '*' if is_favorite
if (isFavorite) {
typeid = `* ${typeid}`;
}
// Suffix with [note] if item_note is present
if (itemNote) {
typeid = `${typeid} [${itemNote}]`;
}
// Process Active Effects
let activeEffects = (relic.relic_data.active_effects || []).map(effect => {
// Remove prefixes and lowercase
let id = effect.id.replace(/^(AEA_|AO_)/, '').toLowerCase();
// Replace underscores with spaces
id = id.replace(/_/g, ' ');
return id;
});
// Process Passive Effects
const passivesByCondition = {};
(relic.relic_data.passive_effects || []).forEach(effect => {
const condition = effect.condition || 'UNKNOWN';
if (!passivesByCondition[condition]) {
passivesByCondition[condition] = [];
}
(effect.values || []).forEach(val => {
let valueName = val.value ? val.value.toLowerCase().replace(/_/g, ' ') : '';
// Shorten 'artifact resonance' to 'AR'
if (valueName === 'artifact resonance') {
valueName = 'AR';
}
let modifier = '';
if ('add' in val) {
const add = parseFloat(val.add);
if (add < 0) {
modifier = `${add} ${valueName}`;
} else {
modifier = `+${add} ${valueName}`;
}
passivesByCondition[condition].push(modifier);
}
if ('mult' in val) {
const mult = parseFloat(val.mult);
modifier = `*${mult} ${valueName}`;
passivesByCondition[condition].push(modifier);
}
});
});
// Extract AR if present in passives
let arEffect = '';
if (passivesByCondition['ALWAYS']) {
const alwaysPassives = passivesByCondition['ALWAYS'];
const arIndex = alwaysPassives.findIndex(effect => effect.toLowerCase().includes('ar'));
if (arIndex !== -1) {
arEffect = alwaysPassives.splice(arIndex, 1)[0];
}
}
if (passivesByCondition['HELD']) {
const heldPassives = passivesByCondition['HELD'];
const arIndex = heldPassives.findIndex(effect => effect.toLowerCase().includes('ar'));
if (arIndex !== -1 && !arEffect) {
arEffect = heldPassives.splice(arIndex, 1)[0];
}
}
// Compile other passives
let otherPassives = [];
Object.keys(passivesByCondition).forEach(condition => {
if (condition === 'ALWAYS' || condition === 'HELD') {
otherPassives = otherPassives.concat(passivesByCondition[condition]);
} else if (condition === 'WORN') {
const wornPassives = passivesByCondition[condition].map(p => p);
if (wornPassives.length > 0) {
otherPassives.push(`worn: ${wornPassives.join(', ')}`);
}
}
});
// Sort AR first
let effectsList = [];
if (arEffect) {
effectsList.push(arEffect);
}
// Add Active Effects
effectsList = effectsList.concat(activeEffects);
// Add other Passives
effectsList = effectsList.concat(otherPassives);
// Combine Effects
const effectsString = effectsList.join(', ');
return {
identifier: `${typeid} (${charges}/${maxCharges})`,
effects: effectsString
};
});
displayResults(processedRelics);
enableDownload(processedRelics);
}
/**
* Displays the processed relics in the results table
* @param {Array} relics - Array of processed relic objects
*/
function displayResults(relics) {
const tbody = document.querySelector('#results tbody');
tbody.innerHTML = ''; // Clear previous results
relics.forEach(relic => {
const row = document.createElement('tr');
const typeidCell = document.createElement('td');
typeidCell.textContent = relic.identifier;
row.appendChild(typeidCell);
const effectsCell = document.createElement('td');
// Highlight modifiers and replace underscores with spaces
effectsCell.innerHTML = formatEffects(relic.effects);
row.appendChild(effectsCell);
tbody.appendChild(row);
});
}
/**
* Formats the effects string to highlight modifiers and handle specific formatting rules
* @param {string} effects - The combined effects string
* @returns {string} - HTML formatted string with highlighted modifiers
*/
function formatEffects(effects) {
return effects.split(', ').map(effect => {
// Handle 'worn:' prefix separately
if (effect.startsWith('worn:')) {
const wornEffects = effect.slice(5).trim().split(', ').map(wEffect => {
return formatSingleEffect(wEffect);
}).join(', ');
return `worn: ${wornEffects}`;
} else {
return formatSingleEffect(effect);
}
}).join(', ');
}
/**
* Formats a single effect based on its modifier
* @param {string} effect - A single effect string
* @returns {string} - HTML formatted effect
*/
function formatSingleEffect(effect) {
if (effect.startsWith('+') && !effect.startsWith('++')) {
return `<span class="modifier-add">${effect}</span>`;
} else if (effect.startsWith('*')) {
return `<span class="modifier-mult">${effect}</span>`;
} else if (effect.startsWith('-')) {
// Negative add modifier
return `<span class="modifier-add">${effect}</span>`;
} else {
return effect;
}
}
/**
* Enables the download button with the CSV data
* @param {Array} relics - Array of processed relic objects
*/
function enableDownload(relics) {
const downloadButton = document.getElementById('downloadButton');
downloadButton.disabled = false;
const csvContent = generateCSV(relics);
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
downloadButton.onclick = function() {
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', 'relics.csv');
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
}
/**
* Generates CSV content from the relics array
* @param {Array} relics - Array of processed relic objects
* @returns {string} - CSV formatted string
*/
function generateCSV(relics) {
const headers = ['TypeID (Charges/Max Charges)', 'Effects'];
const rows = relics.map(relic => `"${relic.identifier}","${relic.effects.replace(/"/g, '""')}"`);
return [headers.join(','), ...rows].join('\n');
}
</script>
</body>
</html>