Skip to content

Commit

Permalink
improve handling of preset images
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Jul 16, 2024
1 parent 65bb9a5 commit 8ccbb17
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
35 changes: 32 additions & 3 deletions src/tarkov-data-manager/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,9 @@ app.get('/presets', async (req, res) => {
<th>
name
</th>
<th>
images
</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -1916,9 +1919,10 @@ app.get('/presets', async (req, res) => {
});

app.get('/presets/get', async (req, res) => {
const [presets, en] = await Promise.all([
const [presets, en, items] = await Promise.all([
query('SELECT * FROM manual_preset'),
tarkovData.locale('en'),
remoteData.get(),
]);
for (const preset of presets) {
const baseItemId = preset.items[0]._tpl;
Expand All @@ -1932,15 +1936,40 @@ app.get('/presets/get', async (req, res) => {
shortName: en[`${item._tpl} ShortName`],
};
});
preset.image_8x_link = items.get(preset.id)?.image_8x_link;
preset.image_512_link = items.get(preset.id)?.image_512_link;
preset.image_link = items.get(preset.id)?.image_link;
preset.base_image_link = items.get(preset.id)?.base_image_link;
preset.grid_image_link = items.get(preset.id)?.grid_image_link;
preset.icon_link = items.get(preset.id)?.icon_link;
}
res.json(presets);
});

app.put('/presets/:id', async (req, res) => {
const response = {message: 'No changes made.', errors: []};
try {
await query('UPDATE manual_preset SET append_name = ? WHERE id = ?', [req.body.append_name, req.params.id]);
response.message = 'Preset updated';
const preset = await query('SELECT * FROM manual_preset WHERE id = ?', [req.params.id]).then(results => results[0]);
if (preset.append_name !== req.body.append_name) {
await query('UPDATE manual_preset SET append_name = ? WHERE id = ?', [req.body.append_name, req.params.id]);
const [en, items] = await Promise.all([tarkovData.locale('en'), remoteData.get()]);
const baseItem = items.get(preset.items[0]._tpl);
await remoteData.setProperties(req.params.id, {
name: `${baseItem.name} ${en[req.body.append_name]}`,
short_name: `${baseItem.short_name} ${en[req.body.append_name]}`,
});
response.message = 'Preset updated';
try {
await regenerateFromExisting(req.params.id);
} catch (error) {
console.log(error);
if (Array.isArray(error)) {
response.errors = error.map(err => err.message || err);
} else {
response.errors.push(error.message || error);
}
}
}
} catch (error) {
response.errors.push(error.message);
}
Expand Down
4 changes: 4 additions & 0 deletions src/tarkov-data-manager/modules/remote-data.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import midmean from 'compute-midmean';

import normalizeName from './normalize-name.js';
import timer from './console-timer.js';
import { query, maxQueryRows } from './db-connection.mjs';
import gameModes from './game-modes.mjs';
Expand Down Expand Up @@ -330,6 +331,9 @@ const methods = {
}
if (currentValue !== value) {
changeValues[property] = value;
if (property === 'name' && !properties.normalized_name) {
changeValues.normalized_name = normalizeName(value);
}
}
}
if (Object.keys(changeValues).length === 0) {
Expand Down
77 changes: 75 additions & 2 deletions src/tarkov-data-manager/public/presets.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
let table = false;

const existingImageElement = (itemId, imageType, url) => {
const tooltipId = `${itemId}-${imageType}-tooltip-content`;
return `
<a href="${url}" class="tooltipped" data-html="true" data-tooltip-id="${tooltipId}">✔️</a>
<div id="${tooltipId}" style="display: none;">
<div>${imageType} image</div>
<img src="${url}" style="max-height: 300px" loading="lazy" />
</div>
`;
};

$(document).ready( function () {
//$('.tooltipped').tooltip();
//$('.modal').modal();
Expand All @@ -26,7 +37,38 @@ $(document).ready( function () {
render: (data, type, wipe) => {
return data;
}
}
},
{
data: 'image_link',
render: (data, type, item) => {
if (type === 'display') {
let imageLink = item.image_512_link;
if (!imageLink) {
imageLink = item.base_image_link || item.grid_image_link || item.icon_link;
}
return `
<div class="row">
${imageLink ? `<div class="col s12"><img src="${imageLink}" loading="lazy" style="max-height: 200px" /></div>`: ''}
</div>
<div class="">
${item.image_8x_link ? existingImageElement(item.id, '8x', item.image_8x_link): missingImageElement('8x')}
${item.image_512_link ? existingImageElement(item.id, '512', item.image_512_link): missingImageElement('512')}
${data ? existingImageElement(item.id, 'inspect', data): missingImageElement('inspect')}
${item.base_image_link ? existingImageElement(item.id, 'base', item.base_image_link): missingImageElement('base')}
${item.grid_image_link ? existingImageElement(item.id, 'grid', item.grid_image_link): missingImageElement('grid')}
${item.icon_link ? existingImageElement(item.id, 'icon', item.icon_link) : missingImageElement('icon')}
</div>
<div class="row">
${item.image_8x_link || item.base_image_link ? `<a class="waves-effect waves-light regenerate btn-small tooltipped" data-id="${item.id}" data-tooltip="Regenerate images from source"><i class="material-icons">refresh</i></a>` : ''}
<a class="waves-effect waves-light refresh-images btn-small tooltipped" data-id="${item.id}" data-tooltip="Refresh images from game"><i class="material-icons">sync</i></a>
</div>
`;
}
return data;
},
className: 'image-column',
width: '10%',
},
];

table = $('table.main').DataTable({
Expand Down Expand Up @@ -91,6 +133,38 @@ $(document).ready( function () {
$('#modal-delete-confirm .delete-confirm').data('id', target.data('id'));
M.Modal.getInstance(document.getElementById('modal-delete-confirm')).open();
});

$('.btn-small.regenerate').off('click');
$('.btn-small.regenerate').click(event => {
let target = event.target;
if (target.nodeName !== 'A') {
target = target.parentElement;
}
$(target).addClass('disabled');
fetch(`/items/regenerate-images/${$(target).data('id')}`, {method: 'POST'}).then(response => response.json()).then(data => {
$(target).removeClass('disabled');
M.toast({text: data.message});
for (const error of data.errors) {
M.toast({text: error});
}
});
});

$('.btn-small.refresh-images').off('click');
$('.btn-small.refresh-images').click(event => {
let target = event.target;
if (target.nodeName !== 'A') {
target = target.parentElement;
}
$(target).addClass('disabled');
fetch(`/items/refresh-images/${$(target).data('id')}`, {method: 'POST'}).then(response => response.json()).then(data => {
$(target).removeClass('disabled');
M.toast({text: data.message});
for (const error of data.errors) {
M.toast({text: error});
}
});
});
}
});

Expand Down Expand Up @@ -129,7 +203,6 @@ $(document).ready( function () {
$('#modal-edit-preset .edit-preset-save').click(function(event) {
const form = $('#modal-edit-preset').find('form').first();
const formData = form.serialize();
console.log(formData)
$.ajax({
method: form.attr('method'),
url: form.attr('action'),
Expand Down

0 comments on commit 8ccbb17

Please sign in to comment.