79 lines
2.8 KiB
JavaScript
79 lines
2.8 KiB
JavaScript
/**
|
|
* AppServ localhost — ใช้ Game API + Socket บน production
|
|
* ไม่ต้องรัน node server.js บน Windows (หลีกเลี่ยง EACCES port 3001/3010)
|
|
* ปิดโหมดนี้: localStorage.setItem('jdUseLocalGameApi','1') แล้วรัน node ที่พอร์ต 13010
|
|
*/
|
|
(function (g) {
|
|
'use strict';
|
|
var loc = g.location || {};
|
|
var h = (loc.hostname || '').toLowerCase();
|
|
if (h !== 'localhost' && h !== '127.0.0.1') return;
|
|
|
|
var PROD = 'https://srv1361159.hstgr.cloud';
|
|
var PROD_GAME = PROD + '/Game';
|
|
|
|
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 apiBase = useLocalNode() ? localApiBase() : PROD_GAME;
|
|
var nodeOrigin = useLocalNode()
|
|
? apiBase.replace(/\/Game\/?$/, '')
|
|
: PROD;
|
|
|
|
g.__JD_GAME_API_BASE__ = apiBase;
|
|
g.GAME_API_FETCH_BASE = apiBase;
|
|
g.GAME_NODE_ORIGIN = nodeOrigin;
|
|
g.GAME_SERVER = nodeOrigin;
|
|
|
|
g.ensureLocalDevLogin = function () {
|
|
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.ensureLocalDevLogin();
|
|
|
|
g.resolveGameApiBase = function () {
|
|
return String(g.__JD_GAME_API_BASE__ || apiBase).replace(/\/$/, '');
|
|
};
|
|
|
|
if (g.__JD_LOCAL_DEV_FETCH_PATCHED__ || typeof g.fetch !== 'function') return;
|
|
var nativeFetch = g.fetch.bind(g);
|
|
var badPort = /https?:\/\/(?:localhost|127\.0\.0\.1):(3004|3001|3010|13010)\/Game/i;
|
|
|
|
g.fetch = function jdLocalDevFetch(input, init) {
|
|
if (typeof input === 'string') {
|
|
if (badPort.test(input)) {
|
|
input = PROD_GAME + input.slice(input.indexOf('/Game') + 5);
|
|
} else if (!useLocalNode() && input.indexOf('/Game/api') >= 0 && input.indexOf('://') < 0) {
|
|
var base = typeof g.appPath === 'function' ? g.appPath('/Game') : '/Game';
|
|
if (input.indexOf(base + '/api') === 0) {
|
|
input = PROD_GAME + input.slice(base.length);
|
|
} else if (input.indexOf('/Game/api') === 0) {
|
|
input = PROD_GAME + input.slice('/Game'.length);
|
|
}
|
|
}
|
|
}
|
|
return nativeFetch(input, init);
|
|
};
|
|
g.__JD_LOCAL_DEV_FETCH_PATCHED__ = true;
|
|
})(typeof window !== 'undefined' ? window : this);
|