97 lines
3.2 KiB
JavaScript
97 lines
3.2 KiB
JavaScript
/**
|
|
* Game API / Socket routing
|
|
* - Production: same-origin /Game/api (Nginx → Node)
|
|
* - localhost AppServ: production https://srv1361159.hstgr.cloud/Game (ไม่ต้องรัน node บน Windows)
|
|
*/
|
|
(function (g) {
|
|
'use strict';
|
|
|
|
var loc = g.location || {};
|
|
var pageBase = typeof g.appPath === 'function' ? g.appPath('/Game') : '/Game';
|
|
var PROD = 'https://srv1361159.hstgr.cloud';
|
|
var PROD_GAME = PROD + '/Game';
|
|
|
|
function isLocalDevHost() {
|
|
try {
|
|
var h = (loc.hostname || '').toLowerCase();
|
|
return h === 'localhost' || h === '127.0.0.1';
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function useLocalNode() {
|
|
try { return localStorage.getItem('jdUseLocalGameApi') === '1'; } catch (e) { return false; }
|
|
}
|
|
|
|
function localApiBase() {
|
|
var port = '13010';
|
|
try {
|
|
var p = localStorage.getItem('jdGameNodePort');
|
|
if (p) port = String(p).replace(/\D/g, '') || port;
|
|
} catch (e) { /* ignore */ }
|
|
return 'http://127.0.0.1:' + port + '/Game';
|
|
}
|
|
|
|
var localDev = isLocalDevHost();
|
|
var apiBase = pageBase.replace(/\/$/, '');
|
|
var nodeOrigin = '';
|
|
|
|
if (localDev) {
|
|
if (useLocalNode()) {
|
|
apiBase = localApiBase().replace(/\/$/, '');
|
|
nodeOrigin = apiBase.replace(/\/Game\/?$/i, '');
|
|
} else {
|
|
apiBase = PROD_GAME;
|
|
nodeOrigin = PROD;
|
|
}
|
|
}
|
|
|
|
g.GAME_HTTP_BASE = pageBase;
|
|
g.GAME_API_FETCH_BASE = apiBase;
|
|
g.GAME_NODE_ORIGIN = localDev ? nodeOrigin : (String(g.GAME_SERVER || '').replace(/\/$/, '') || loc.origin || '');
|
|
if (localDev) g.GAME_SERVER = nodeOrigin;
|
|
|
|
g.ensureLocalDevLogin = function ensureLocalDevLogin() {
|
|
if (!localDev) return;
|
|
try {
|
|
if (localStorage.getItem('isLoggedIn') !== 'true') {
|
|
localStorage.setItem('isLoggedIn', 'true');
|
|
localStorage.setItem('loginType', 'guest');
|
|
}
|
|
if (!(localStorage.getItem('playerName') || '').trim()) {
|
|
localStorage.setItem('playerName', 'DevTester');
|
|
}
|
|
if (!(localStorage.getItem('roomLobbyDisplayName') || '').trim()) {
|
|
localStorage.setItem('roomLobbyDisplayName', 'DevTester');
|
|
}
|
|
} catch (e) { /* ignore */ }
|
|
};
|
|
|
|
g.resolveGameApiBase = function resolveGameApiBase() {
|
|
if (!localDev) return pageBase.replace(/\/$/, '');
|
|
return String(g.GAME_API_FETCH_BASE || PROD_GAME).replace(/\/$/, '');
|
|
};
|
|
|
|
if (!localDev || g.__GAME_HTTP_FETCH_PATCHED__ || typeof g.fetch !== 'function') return;
|
|
|
|
var nativeFetch = g.fetch.bind(g);
|
|
var pageApiPrefix = pageBase.replace(/\/$/, '') + '/api';
|
|
var directApiBase = apiBase.replace(/\/$/, '');
|
|
var badPort = /https?:\/\/(?:localhost|127\.0\.0\.1):(3004|3001|3010|13010)\/Game/i;
|
|
|
|
g.fetch = function gameHttpFetch(input, init) {
|
|
if (typeof input === 'string') {
|
|
if (badPort.test(input)) {
|
|
input = PROD_GAME + input.slice(input.indexOf('/Game') + 5);
|
|
} else if (input.indexOf(pageApiPrefix) === 0) {
|
|
input = directApiBase + input.slice(pageBase.length);
|
|
} else if (input.indexOf('/Game/api') === 0) {
|
|
input = directApiBase + input.slice('/Game'.length);
|
|
}
|
|
}
|
|
return nativeFetch(input, init);
|
|
};
|
|
g.__GAME_HTTP_FETCH_PATCHED__ = true;
|
|
})(typeof window !== 'undefined' ? window : this);
|