Skip to content

Commit

Permalink
fix:switch
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklikOf13 committed Jun 11, 2024
1 parent 2c5a42f commit 09b1ef1
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions client/src/scripts/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ export async function setUpUI(game: Game): Promise<void> {
$("#locked-info").on("click", () => $("#locked-tooltip").fadeToggle(250));

const pad = (n: number): string | number => n < 10 ? `0${n}` : n;
const updateSwitchTime = (): void => {
async function updateSwitchTime(){

Check warning on line 137 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing return type on function

Check failure on line 137 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing space before opening brace

Check warning on line 137 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing return type on function

Check failure on line 137 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing space before opening brace
if (!selectedRegion?.nextSwitchTime) {
$("#locked-time").text("--:--:--");
return;
}
const millis = selectedRegion.nextSwitchTime - Date.now();
if (millis < 0) {
location.reload();
return;
$("#locked-time").text("--:--:--");
await updateServerSelectors()

Check failure on line 145 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon

Check failure on line 145 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}
const hours = Math.floor(millis / 3600000) % 24;
const minutes = Math.floor(millis / 60000) % 60;
Expand Down Expand Up @@ -173,63 +173,63 @@ export async function setUpUI(game: Game): Promise<void> {
}

// Get player counts + find server w/ best ping
let bestPing = Number.MAX_VALUE;
let bestRegion: string | undefined;
for (const [regionID, region] of regionMap) {
const listItem = $(`.server-list-item[data-region=${regionID}]`);
try {
const pingStartTime = Date.now();

Check failure on line 176 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed

Check failure on line 177 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

More than 1 blank line not allowed
interface ServerInfo {
readonly protocolVersion: number
readonly playerCount: number
readonly maxTeamSize: TeamSize
readonly nextSwitchTime: number
};
async function updateServerSelectors(){

Check warning on line 178 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing return type on function

Check failure on line 178 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing space before opening brace

Check warning on line 178 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing return type on function
let bestPing = Number.MAX_VALUE;
let bestRegion: string | undefined;

const serverInfo = await (
await fetch(`${region.mainAddress}/api/serverInfo`, { signal: AbortSignal.timeout(5000) })
)?.json() as ServerInfo;
for (const [regionID, region] of regionMap) {
const listItem = $(`.server-list-item[data-region=${regionID}]`);
try {
const pingStartTime = Date.now();

const ping = Date.now() - pingStartTime;
interface ServerInfo {
readonly protocolVersion: number
readonly playerCount: number
readonly maxTeamSize: TeamSize
readonly nextSwitchTime: number
};

if (serverInfo.protocolVersion !== GameConstants.protocolVersion) {
console.error(`Protocol version mismatch for region ${regionID}. Expected ${GameConstants.protocolVersion}, got ${serverInfo.protocolVersion}`);
continue;
}
const serverInfo = await (
await fetch(`${region.mainAddress}/api/serverInfo`, { signal: AbortSignal.timeout(5000) })
)?.json() as ServerInfo;

regionInfo[regionID] = {
...region,
...serverInfo,
ping
};
const ping = Date.now() - pingStartTime;

if (serverInfo.protocolVersion !== GameConstants.protocolVersion) {
console.error(`Protocol version mismatch for region ${regionID}. Expected ${GameConstants.protocolVersion}, got ${serverInfo.protocolVersion}`);
continue;
}

listItem.find(".server-player-count").text(serverInfo.playerCount ?? "-");
// listItem.find(".server-ping").text(typeof playerCount === "string" ? ping : "-");
regionInfo[regionID] = {
...region,
...serverInfo,
ping
};

if (ping < bestPing) {
bestPing = ping;
bestRegion = regionID;
listItem.find(".server-player-count").text(serverInfo.playerCount ?? "-");
// listItem.find(".server-ping").text(typeof playerCount === "string" ? ping : "-");

if (ping < bestPing) {
bestPing = ping;
bestRegion = regionID;
}
} catch (e) {
console.error(`Failed to load server info for region ${regionID}. Details:`, e);
}
} catch (e) {
console.error(`Failed to load server info for region ${regionID}. Details:`, e);
}
}

const updateServerSelectors = (): void => {
if (!selectedRegion) { // Handle invalid region
selectedRegion = regionInfo[Config.defaultRegion];
game.console.setBuiltInCVar("cv_region", "");
}
selectedRegion = regionInfo[(game.console.getBuiltInCVar("cv_region") || bestRegion) ?? Config.defaultRegion];

$("#server-name").text(selectedRegion.name);
$("#server-player-count").text(selectedRegion.playerCount ?? "-");
// $("#server-ping").text(selectedRegion.ping && selectedRegion.ping > 0 ? selectedRegion.ping : "-");
updateSwitchTime();
resetPlayButtons();
};

selectedRegion = regionInfo[(game.console.getBuiltInCVar("cv_region") || bestRegion) ?? Config.defaultRegion];
updateServerSelectors();
await updateServerSelectors();
updateSwitchTime();

Check failure on line 232 in client/src/scripts/ui.ts

View workflow job for this annotation

GitHub Actions / Lint

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

serverList.children("li.server-list-item").on("click", function(this: HTMLLIElement) {
const region = this.getAttribute("data-region");
Expand Down

0 comments on commit 09b1ef1

Please sign in to comment.