dont make api calls when no server is connected in local (#1847)

* dont make api calls in local without a server connected and fix fusionLuck not set by default
This commit is contained in:
Matthew 2024-06-05 21:24:47 -04:00 committed by GitHub
parent 6d35399c31
commit c5689dfc96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 5 deletions

View File

@ -211,8 +211,8 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
this.generateFusionSpecies();
}
}
this.luck = (this.shiny ? this.variant + 1 : 0) + (this.fusionShiny ? this.fusionVariant + 1 : 0);
this.fusionLuck = this.luck;
}
this.generateName();

View File

@ -29,6 +29,7 @@ export class LoadingScene extends SceneBase {
}
preload() {
Utils.localPing();
this.load["manifest"] = this.game["manifest"];
if (!isMobile()) {

View File

@ -263,6 +263,8 @@ export const isLocal = (
// Set the server URL based on whether it's local or not
export const serverUrl = isLocal ? `${window.location.hostname}:${window.location.port}` : "";
export const apiUrl = isLocal ? serverUrl : "https://api.pokerogue.net";
// used to disable api calls when isLocal is true and a server is not found
export let isLocalServerConnected = false;
export function setCookie(cName: string, cValue: string): void {
const expiration = new Date();
@ -285,8 +287,22 @@ export function getCookie(cName: string): string {
return "";
}
/**
* When locally running the game, "pings" the local server
* with a GET request to verify if a server is running,
* sets isLocalServerConnected based on results
*/
export function localPing() {
if (isLocal) {
apiFetch("game/titlestats")
.then(resolved => isLocalServerConnected = true,
rejected => isLocalServerConnected = false
);
}
}
export function apiFetch(path: string, authed: boolean = false): Promise<Response> {
return new Promise((resolve, reject) => {
return (isLocal && isLocalServerConnected) || !isLocal ? new Promise((resolve, reject) => {
const request = {};
if (authed) {
const sId = getCookie(sessionIdKey);
@ -297,11 +313,11 @@ export function apiFetch(path: string, authed: boolean = false): Promise<Respons
fetch(`${apiUrl}/${path}`, request)
.then(response => resolve(response))
.catch(err => reject(err));
});
}) : new Promise(() => {});
}
export function apiPost(path: string, data?: any, contentType: string = "application/json", authed: boolean = false): Promise<Response> {
return new Promise((resolve, reject) => {
return (isLocal && isLocalServerConnected) || !isLocal ? new Promise((resolve, reject) => {
const headers = {
"Accept": contentType,
"Content-Type": contentType,
@ -315,7 +331,7 @@ export function apiPost(path: string, data?: any, contentType: string = "applica
fetch(`${apiUrl}/${path}`, { method: "POST", headers: headers, body: data })
.then(response => resolve(response))
.catch(err => reject(err));
});
}) : new Promise(() => {});
}
export class BooleanHolder {