import { DateTime } from 'luxon';
import * as semver from 'semver';
import { fetchJson } from '../api.js';
import type { StoredData } from '../types.js';
import { BASE_URL } from '../utils/constants.js';
export async function renderResources(container: HTMLElement) {
const platforms = ['Windows', 'Android', 'iOS', 'PlayStation'];
const targets = [
{ region: 'os', channel: 6 },
{ region: 'cn', channel: 1 },
];
for (const target of targets) {
const section = document.createElement('div');
section.className = 'mb-5';
section.innerHTML = `
${target.region === 'cn' ? 'China' : 'Global'}
`;
const accordion = document.createElement('div');
accordion.className = 'accordion';
accordion.id = `accordion-res-${target.region}-${target.channel}`;
let itemIndex = 0;
for (const platform of platforms) {
const url = `${BASE_URL}/akEndfield/launcher/game_resources/${target.channel}/${platform}/all.json`;
try {
const data = await fetchJson[]>(url);
// Group by res_version
const resVersionMap = new Map; versions: Set }>();
for (const e of data) {
const resVer = e.rsp.res_version;
if (!resVersionMap.has(resVer)) {
resVersionMap.set(resVer, { rsp: e, versions: new Set() });
}
resVersionMap.get(resVer)!.versions.add(e.req.version);
}
const resVersionSet = Array.from(resVersionMap.values()).map((d) => ({
resVersion: d.rsp.rsp.res_version,
rsp: d.rsp,
versions: Array.from(d.versions).sort(semver.rcompare),
}));
const sortedSet = resVersionSet.reverse();
let rows = '';
for (let i = 0; i < sortedSet.length; i++) {
const item = sortedSet[i]!;
const nextItem = sortedSet[i + 1];
// Newest first
const currentDate = DateTime.fromISO(item.rsp.updatedAt);
const dateStr = currentDate.toFormat('yyyy/MM/dd HH:mm:ss');
const intervalStr = (() => {
if (nextItem) {
const nextDate = DateTime.fromISO(nextItem.rsp.updatedAt);
const diff = currentDate.diff(nextDate);
return diff.toFormat('dd:hh:mm:ss');
}
return '-';
})();
const initialRes = item.rsp.rsp.resources.find((e: any) => e.name === 'initial');
const mainRes = item.rsp.rsp.resources.find((e: any) => e.name === 'main');
const isKick = JSON.parse(item.rsp.rsp.configs).kick_flag === true;
rows += `
| ${dateStr} |
${intervalStr} |
${initialRes.version} |
${mainRes.version} |
${isKick ? '✅' : ''} |
${item.versions.join(', ')} |
`;
}
const itemId = `res-${target.region}-${target.channel}-${platform}`;
const isExpanded = false;
itemIndex++;
const item = document.createElement('div');
item.className = 'accordion-item';
item.innerHTML = `
| Date |
Interval |
Initial |
Main |
Kick |
Game version |
${rows}
`;
accordion.appendChild(item);
} catch (err) {
// Ignore
}
}
if (accordion.childElementCount > 0) {
section.appendChild(accordion);
container.appendChild(section);
}
}
}