Skip to content

Commit

Permalink
feat: Update button for pp counters
Browse files Browse the repository at this point in the history
  • Loading branch information
cyperdark committed Oct 11, 2024
1 parent adf0765 commit a66e98e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ config.ini
tosu.exe
_version.js
**/tosu/gameOverlay/
logs/
logs/
settings/
36 changes: 28 additions & 8 deletions packages/server/assets/homepage.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,20 @@ function endDownload(element, id, text) {
};
}

async function downloadCounter(element, id) {
async function downloadCounter(element, id, update) {
if (downloading.includes(id)) return;

downloading.push(id);

const url = element.attributes.l?.value;
const folderName = element.attributes.l?.value;
const name = element.attributes.n?.value;
const author = element.attributes.a?.value;

const download = await fetch(`/api/counters/download/${url}?name=${name} by ${author}`);
const url = new URL(`http://${window.location.host}/api/counters/download/${folderName}`);
url.searchParams.append('name', `${name} by ${author}`);
if (update == true) url.searchParams.append('update', 'true');

const download = await fetch(url);
const json = await download.json();

if (json.error != null) {
Expand All @@ -134,22 +138,26 @@ async function downloadCounter(element, id) {
};

displayNotification({
element: element.parentElement.parentElement.parentElement,
element: element.parentElement.parentElement,
text: `Error while downloading: ${json.error}`,
classes: ['red'],
delay: 3000,
});

setTimeout(() => {
endDownload(element, id, 'Download');
element.classList.remove('disable');
endDownload(element, id, 'Download');
element.classList.remove('disable');
}, 101);
return;
};


let text = `PP Counter downloaded: ${name} by ${author}`;
if (update == true) text = `PP Counter updated: ${name} by ${author}`;

displayNotification({
element: element.parentElement.parentElement.parentElement,
text: `PP Counter downloaded: ${name} by ${author}`,
element: element.parentElement.parentElement,
text: text,
classes: ['green'],
delay: 3000,
});
Expand Down Expand Up @@ -1108,6 +1116,18 @@ window.addEventListener('click', (event) => {
downloadCounter(t, id);
return
};

if (t?.classList.value.includes('update-button')) {
const id = t.attributes.l?.value;
const name = t.attributes.n?.value;
const author = t.attributes.a?.value;
const confirmed = confirm(`Update counter «${name} by ${author}»?`);
if (!confirmed) return;

startDownload(t);
downloadCounter(t, id, true);
return
};
if (t?.classList.value.includes(' delete-button')) return deleteCounter(t);
if (t?.classList.value.includes(' open-button')) return openCounter(t);
if (t?.classList.value.includes(' open-folder-button')) return openCounter(t);
Expand Down
2 changes: 1 addition & 1 deletion packages/server/assets/homepage.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/server/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default function buildBaseApi(server: Server) {

const tempPath = path.join(cacheFolder, `${Date.now()}.zip`);

if (fs.existsSync(folderPath)) {
if (fs.existsSync(folderPath) && req.query.update !== 'true') {
return sendJson(res, {
error: 'Folder already exist'
});
Expand Down
46 changes: 39 additions & 7 deletions packages/server/utils/counters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export function parseTXT(filePath: string) {
else object.resolution = ['Any', 'Any'];

if (object.authorlinks) object.authorlinks = object.authorlinks.split(',');
if (!object.version) object.version = '1.0';

object.settings = Array.isArray(settings) ? settings : [];

Expand Down Expand Up @@ -295,7 +296,12 @@ function rebuildJSON({
: '';

const name = nameHTML
.replace('{NAME}', `${item.name}${externalHasSettings}`)
.replace(
'{NAME}',
item.version
? `${item.name} v${item.version}${externalHasSettings}`
: `${item.name}${externalHasSettings}`
)
.replace('{CLASS}', 'flexer');
const author = authorHTML.replace('{AUTHOR}', item.author);

Expand Down Expand Up @@ -381,9 +387,21 @@ function rebuildJSON({
? `<button class="button settings-button flexer" n="${item.folderName}"><span>Settings</span></button>`
: '';

const button = item.downloadLink
? `<div class="buttons-group indent-left"><button class="button dl-button flexer" l="${item.downloadLink}" n="${item.name}" a="${item.author}"><span>Download</span></button></div>`
: `<div class="buttons-group flexer indent-left">
const updateBtn =
item._updatable === true
? `<button class="button update-button flexer" l="${item.downloadLink}" n="${item.name}" a="${item.author}"><span>Update</span></button>`
: '';
const downloadBtn =
item.downloadLink && item._downloaded !== true
? `<button class="button dl-button flexer" l="${item.downloadLink}" n="${item.name}" a="${item.author}"><span>Download</span></button>`
: `<button class="button open-button flexer" n="${item.name} by ${item.author}"><span>Open Folder</span></button>`;

const externalButtons = `<div class="buttons-group flexer indent-left">
${updateBtn}
${downloadBtn}
</div>`;

const localButtons = `<div class="buttons-group flexer indent-left">
${settingsBuilderBtn}
${settingsBtn}
<button class="button open-button flexer" n="${item.folderName}"><span>Open Folder</span></button>
Expand All @@ -407,12 +425,16 @@ function rebuildJSON({
items += resultItemHTML
.replace(
'{CLASS}',
item._downloaded === true ? ' downloaded' : ''
item._updatable === true
? ' updatable'
: item._downloaded === true
? ' downloaded'
: ''
)
.replace('{NAME}', name)
.replace('{AUTHOR}', author)
.replace('{AUTHOR_LINKS}', links)
.replace('{BUTTONS}', button)
.replace('{BUTTONS}', external ? externalButtons : localButtons)
.replace('{GALLERY}', gallery)
.replace('{FOOTER}', footer);
} catch (error) {
Expand All @@ -424,7 +446,7 @@ function rebuildJSON({
return items;
}

function getLocalCounters() {
function getLocalCounters(): ICounter[] {
try {
const staticPath = getStaticPath();

Expand Down Expand Up @@ -472,6 +494,7 @@ function getLocalCounters() {
.replace(/^(\\\\\\|\\\\|\\|\/|\/\/)/, '')
.replace(/\\/gm, '/'),
name: path.basename(path.dirname(r)),
version: '1.0',
author: 'local',
resolution: [-2, '400'],
authorlinks: [],
Expand Down Expand Up @@ -548,6 +571,15 @@ export async function buildExternalCounters(
const find = exists.find(
(s) => s.name === r.name && s.author === r.author
);

if (
r.version &&
find &&
r.version.toString().toLowerCase() !==
find.version.toString().toLowerCase()
)
r._updatable = true;

if (find) r._downloaded = true;
return r;
});
Expand Down
2 changes: 2 additions & 0 deletions packages/server/utils/counters.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ export type ISettingsType =

export interface ICounter {
_downloaded?: boolean;
_updatable?: boolean;
_settings?: boolean;
folderName: string;
name: string;
author: string;
version: string;
resolution: number[];
authorlinks: string[];
settings: ISettings[];
Expand Down

0 comments on commit a66e98e

Please sign in to comment.