Skip to content

Commit 2bbc893

Browse files
Refactor manual download version selector logic (#144)
* Refactor manual download version selector logic * typo Signed-off-by: TheBossMagnus <[email protected]> * minor fix Signed-off-by: TheBossMagnus <[email protected]> * fix valid version detection Signed-off-by: TheBossMagnus <[email protected]> * ctrl+h mistake Signed-off-by: TheBossMagnus <[email protected]> * fix display order Signed-off-by: TheBossMagnus <[email protected]> * lints and fixes Signed-off-by: TheBossMagnus <[email protected]> * fix fetured alpha/beta not gettting showed Signed-off-by: TheBossMagnus <[email protected]> * fix display order again Signed-off-by: TheBossMagnus <[email protected]> * spli the manual downloader js from the html Signed-off-by: TheBossMagnus <[email protected]> * lints Signed-off-by: TheBossMagnus <[email protected]> * remove useless blank line Signed-off-by: TheBossMagnus <[email protected]> * spotless Signed-off-by: TheBossMagnus <[email protected]> * beautify a little Signed-off-by: TheBossMagnus <[email protected]> * Compact and improve reliability of version type detection * Reorder and scope js to ensure it works * Whitespace stuff * Reverted unnecessary change --------- Signed-off-by: TheBossMagnus <[email protected]> Co-authored-by: Madis <[email protected]>
1 parent 221a4bc commit 2bbc893

File tree

2 files changed

+209
-228
lines changed

2 files changed

+209
-228
lines changed

public/assets/js/vanillaEmbed.js

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
async function loadVersionsFromURL(url) {
2+
try {
3+
const response = await fetch(url);
4+
const data = await response.json();
5+
return data;
6+
} catch (error) {
7+
console.error('Error loading data:', error);
8+
return null;
9+
}
10+
}
11+
12+
function compareSemver(versionA, versionB) {
13+
const partsA = versionA.split('.').map(Number);
14+
const partsB = versionB.split('.').map(Number);
15+
16+
const maxLength = Math.max(partsA.length, partsB.length);
17+
18+
for (let i = 0; i < maxLength; i++) {
19+
const numA = partsA[i] || 0;
20+
const numB = partsB[i] || 0;
21+
22+
if (numA > numB) return 1;
23+
if (numA < numB) return -1;
24+
}
25+
26+
return 0; // Versions are equal
27+
}
28+
29+
function isVersionAllowed(version, minVersion = '1.19.4') {
30+
return compareSemver(version, minVersion) >= 0;
31+
}
32+
33+
function groupByGameVersions(data) {
34+
const groupedVersion = {};
35+
36+
data.forEach(item => {
37+
item.game_versions.forEach(version => {
38+
if (isVersionAllowed(version)) {
39+
if (!groupedVersion[version]) {
40+
groupedVersion[version] = [];
41+
}
42+
groupedVersion[version].push(item);
43+
}
44+
});
45+
});
46+
47+
return groupedVersion;
48+
}
49+
50+
function getFirstVersionItem(versionItems, type) {
51+
for (let i = 0; i < versionItems.length; i++) {
52+
if (versionItems[i].version_type === type) {
53+
return { item: versionItems[i], index: i };
54+
}
55+
}
56+
return { item: null, index: -1 };
57+
}
58+
59+
function getVersionToDisplay(groupedVersion) {
60+
const versionsToDisplay = [];
61+
62+
for (const version in groupedVersion) {
63+
if (groupedVersion[version].length > 0) {
64+
const versionItems = groupedVersion[version];
65+
66+
const firstRelease = getFirstVersionItem(versionItems, 'release');
67+
const firstBeta = getFirstVersionItem(versionItems, 'beta');
68+
const firstAlpha = getFirstVersionItem(versionItems, 'alpha');
69+
70+
if (firstRelease.item) {
71+
versionsToDisplay.push(firstRelease.item);
72+
if (firstBeta.item.featured) {
73+
versionsToDisplay.push(firstBeta.item);
74+
} else if (firstAlpha.item.featured) {
75+
versionsToDisplay.push(firstAlpha.item);
76+
}
77+
} else if (firstBeta.item) {
78+
versionsToDisplay.push(firstBeta.item);
79+
if (firstAlpha.item.featured) {
80+
versionsToDisplay.push(firstAlpha.item);
81+
}
82+
} else if (firstAlpha.item) versionsToDisplay.push(firstAlpha.item);
83+
}
84+
}
85+
86+
// Sort versionsToDisplay by game_version field, to make newer versions appear first
87+
versionsToDisplay.sort((a, b) => compareSemver(b.game_versions[0], a.game_versions[0]));
88+
89+
return versionsToDisplay;
90+
}
91+
92+
function populateDropdownAndSetupButton(versions) {
93+
const versionsDropdown = document.getElementById('versionsDropdown');
94+
95+
versions.forEach(version => {
96+
const option = document.createElement('option');
97+
option.textContent = version.name;
98+
option.value = version.files[0].url;
99+
versionsDropdown.appendChild(option);
100+
});
101+
102+
const specialOption = document.createElement('option');
103+
specialOption.textContent = '4.6.1, 4.5.7, ...';
104+
specialOption.value = 'CurseForge';
105+
versionsDropdown.appendChild(specialOption);
106+
107+
// Set up the "Go" button to download the selected pack or open the URL
108+
const goButton = document.getElementById('goButton');
109+
goButton.onclick = () => {
110+
const selectedURL = versionsDropdown.value;
111+
if (selectedURL === 'CurseForge') {
112+
window.open("https://www.curseforge.com/minecraft/modpacks/fabulously-optimized/files?showAlphaFiles=show", '_blank');
113+
} else {
114+
downloadPack(selectedURL);
115+
}
116+
};
117+
}
118+
119+
const apiUrl = 'https://api.modrinth.com/v2/project/1KVo5zza/version';
120+
const downloadParam = urlParams.get('download'); //urlParams from converter.js
121+
122+
loadVersionsFromURL(apiUrl)
123+
.then(data => {
124+
if (data) {
125+
const groupedVersion = groupByGameVersions(data);
126+
const versionsToDisplay = getVersionToDisplay(groupedVersion);
127+
populateDropdownAndSetupButton(versionsToDisplay);
128+
if (downloadParam === 'latest') {
129+
// Download the first (newest) item
130+
downloadPack(versionsToDisplay[0].files[0].url);
131+
} else if (downloadParam) {
132+
// Download specific version based on query parameter
133+
const foundVersion = versionsToDisplay.find(version => version.game_versions[0] === downloadParam);
134+
if (foundVersion) {
135+
downloadPack(foundVersion.files[0].url);
136+
} else {
137+
alert('Requested version not found');
138+
}
139+
}
140+
} else {
141+
console.error('Failed to load data');
142+
}
143+
})
144+
.catch(error => console.error('Error:', error));

0 commit comments

Comments
 (0)