Skip to content

Commit

Permalink
fix server browser crashing due to no skin info
Browse files Browse the repository at this point in the history
  • Loading branch information
TsFreddie committed Jan 16, 2025
1 parent 6579237 commit 30e42c1
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/ddnet/MapLink.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<Link
data-sveltekit-preload-data="tap"
href={`/ddnet/m?n=${encodeAsciiURIComponent(map)}`}
href={`/ddnet/maps/${encodeAsciiURIComponent(map)}`}
{...rest}
{className}>{@render children()}</Link
>
2 changes: 1 addition & 1 deletion src/lib/components/ddnet/PlayerLink.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<Link
data-sveltekit-preload-data="tap"
href={`/ddnet/p?n=${encodeAsciiURIComponent(player)}`}
href={`/ddnet/players/${encodeAsciiURIComponent(player)}`}
{...rest}
{className}>{@render children()}</Link
>
7 changes: 7 additions & 0 deletions src/lib/ddnet/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ export const primaryAddress = (addresses: string[]) => {
return addresses[0];
};

export const isAddressValid = (addresses: string[]) => {
// ignore non-tw protocols
return addresses.some(
(address) => address.startsWith('tw-0.6+udp://') || address.startsWith('tw-0.7+udp://')
);
};

const REGION_MAP: { [key: string]: string } = {
'as:cn': '中国',
as: '亚洲',
Expand Down
4 changes: 2 additions & 2 deletions src/routes/ddnet/maps/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@
class="relative mt-2 aspect-map h-auto w-full overflow-hidden rounded-md border border-slate-600 hover:border-blue-500 active:border-blue-300"
style="background-image: url({map.thumbnail}); background-size: cover; background-repeat: no-repeat; background-position: center;"
onmousedown={() => {
preloadData(`/ddnet/m?n=${encodeAsciiURIComponent(map.name)}`);
preloadData(`/ddnet/maps/${encodeAsciiURIComponent(map.name)}`);
}}
onclick={() => {
goto(`/ddnet/m?n=${encodeAsciiURIComponent(map.name)}`);
goto(`/ddnet/maps/${encodeAsciiURIComponent(map.name)}`);
}}
aria-label={map.name}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import { basename } from 'path';
import { decodeAsciiURIComponent, encodeAsciiURIComponent } from '$lib/link';
import { uaIsStrict } from '$lib/helpers';

export const load = (async ({ url, parent }) => {
export const load = (async ({ url, parent, params }) => {
const parentData = await parent();

const query = url.searchParams.get('n');
if (!query) {
const param = params.name;
if (!param) {
return redirect(302, '/ddnet/maps');
}

if (!uaIsStrict(parentData.ua) && query.startsWith('!!')) {
if (!uaIsStrict(parentData.ua) && param.startsWith('!!')) {
// redirect to the non-stamped version
return redirect(302, `/ddnet/m?n=${encodeAsciiURIComponent(decodeAsciiURIComponent(query))}`);
return redirect(302, `/ddnet/maps/${encodeAsciiURIComponent(decodeAsciiURIComponent(param))}`);
}

const name = decodeAsciiURIComponent(query);
const name = decodeAsciiURIComponent(param);
let data;

try {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/routes/ddnet/players/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
let lastQueryTime = 0;
function gotoName(name: string) {
goto(`/ddnet/p?n=${encodeAsciiURIComponent(name)}`);
goto(`/ddnet/players/${encodeAsciiURIComponent(name)}`);
}
async function query() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ interface MapData {
pending_points?: number;
}

export const load = (async ({ fetch, url, parent }) => {
export const load = (async ({ fetch, url, parent, params }) => {
const parentData = await parent();
const param = params.name;

const query = url.searchParams.get('n');
if (!query) {
return redirect(302, '/ddnet/players');
}

if (!uaIsStrict(parentData.ua) && query.startsWith('!!')) {
if (!uaIsStrict(parentData.ua) && param.startsWith('!!')) {
// redirect to the non-stamped version
return redirect(302, `/ddnet/p?n=${encodeAsciiURIComponent(decodeAsciiURIComponent(query))}`);
return redirect(
302,
`/ddnet/players/${encodeAsciiURIComponent(decodeAsciiURIComponent(param))}`
);
}

const name = decodeAsciiURIComponent(query);
const name = decodeAsciiURIComponent(param);
const fetchPlayer = fetch(`https://ddnet.org/players/?json2=${encodeURIComponent(name)}`);
const fetchMaps = maps.fetch();
const fetchSkins = skins.fetch();
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/routes/ddnet/servers/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { goto, invalidate } from '$app/navigation';
import Breadcrumbs from '$lib/components/Breadcrumbs.svelte';
import Modal from '$lib/components/Modal.svelte';
import { ddnetColorToRgb, joinViaDDNet, joinViaSteam, showScore, sortPlayers } from '$lib/ddnet/helpers';
import { joinViaDDNet, joinViaSteam, showScore, sortPlayers } from '$lib/ddnet/helpers';
import serverSearch, { type SortKey } from '$lib/stores/server-search.js';
import Fa from 'svelte-fa';
import VirtualScroll from 'svelte-virtual-scroll-list';
Expand All @@ -17,7 +17,7 @@
const { data } = $props();
const checkMapName = async (map: string) => {
const url = `/ddnet/m?n=${encodeURIComponent(map)}`;
const url = `/ddnet/maps/${encodeURIComponent(map)}`;
const response = await fetch(url, { method: 'HEAD' });
if (response.ok && selectedServer?.info?.map?.name == map) {
mapLink = url;
Expand Down Expand Up @@ -437,7 +437,7 @@
{showScore(client.score, selectedServer.info.client_score_kind)}
</div>
<TeeRender
name={client.skin.name}
name={client.skin ? client.skin.name : ''}
className="w-8 h-8 mr-2"
useDefault
body={client.skin ? client.skin.color_body : null}
Expand Down
38 changes: 20 additions & 18 deletions src/routes/ddnet/servers/+page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { primaryAddress, region } from '$lib/ddnet/helpers';
import { isAddressValid, primaryAddress, region } from '$lib/ddnet/helpers';
import type { GameInfo, ServerInfo } from '$lib/server/fetches/servers';
import type { PageLoad } from './$types';

Expand All @@ -20,23 +20,25 @@ export const load = (async ({ fetch, parent }) => {
const kog = gameInfo.communities.find((community) => community.id == 'kog');
if (kog) kog.icon.servers = data.gameInfo['servers-kog'];

const servers = data.servers.map((server) => {
const ip = server.addresses[0].split('://')[1];
const community = gameInfo.communities.find((community) =>
community.icon.servers
?.flatMap((server) => Object.values(server.servers).flatMap((server) => server))
.includes(ip)
);
return {
key: primaryAddress(server.addresses),
...server,
region: region(server.location),
community: community?.name,
community_icon: community?.icon.url,
searchText:
`${region(server.location)}|${server.info.name}|${server.info.game_type}|${server.info.map.name}|${server.location}|${server.info.clients.map((client) => client.name + '|' + client.clan).join('|')}`.toLowerCase()
};
});
const servers = data.servers
.filter((server) => isAddressValid(server.addresses))
.map((server) => {
const ip = server.addresses[0].split('://')[1];
const community = gameInfo.communities.find((community) =>
community.icon.servers
?.flatMap((server) => Object.values(server.servers).flatMap((server) => server))
.includes(ip)
);
return {
key: primaryAddress(server.addresses),
...server,
region: region(server.location),
community: community?.name,
community_icon: community?.icon.url,
searchText:
`${region(server.location)}|${server.info.name}|${server.info.game_type}|${server.info.map.name}|${server.location}|${server.info.clients.map((client) => client.name + '|' + client.clan).join('|')}`.toLowerCase()
};
});

return { servers, gameInfo, ...(await parent()) };
}) satisfies PageLoad;
4 changes: 2 additions & 2 deletions src/routes/goto/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
if (hash == 'm') target = `/ddnet/maps`;
else if (hash == 'p') target = `/ddnet/players`;
else if (hash == 's') target = `/ddnet/servers`;
else if (hash.startsWith('m')) target = `/ddnet/m?n=${hash.slice(1)}`;
else if (hash.startsWith('p')) target = `/ddnet/p?n=${hash.slice(1)}`;
else if (hash.startsWith('m')) target = `/ddnet/maps/${hash.slice(1)}`;
else if (hash.startsWith('p')) target = `/ddnet/players/${hash.slice(1)}`;
else if (hash.startsWith('s')) target = `/ddnet/servers#${hash.slice(1)}`;
}
Expand Down

0 comments on commit 30e42c1

Please sign in to comment.