67 lines
2.9 KiB
JavaScript
67 lines
2.9 KiB
JavaScript
// ==================== КОНФИГУРАЦИЯ СЕРВЕРА ====================
|
||
// Возможность переопределить сервер через query string
|
||
const urlParams = new URLSearchParams(window.location.search);
|
||
export const SERVER_URL = urlParams.get('server') || 'https://apigrech.mkn8n.ru';
|
||
|
||
// Защита от mixed content
|
||
if (location.protocol === 'https:' && SERVER_URL.startsWith('http://')) {
|
||
console.warn('⚠️ Mixed content warning: page is HTTPS but server URL is HTTP');
|
||
alert('⚠️ Предупреждение: страница загружена по HTTPS, но сервер использует HTTP. Это может вызвать проблемы.');
|
||
}
|
||
|
||
// ==================== WORLD ID И ИГРОКА ====================
|
||
let worldId = null;
|
||
let playerName = localStorage.getItem('minegrechka_playerName') || null;
|
||
|
||
// Запрашиваем имя игрока, если его нет
|
||
if (!playerName) {
|
||
playerName = prompt('Введите ваше имя для игры:') || 'Игрок';
|
||
localStorage.setItem('minegrechka_playerName', playerName);
|
||
console.log('Player name set:', playerName);
|
||
}
|
||
|
||
// Берём worldId из URL или генерируем новый
|
||
console.log('Current URL:', window.location.href);
|
||
const worldParam = urlParams.get('world');
|
||
console.log('world param:', worldParam);
|
||
|
||
// Проверяем на null, undefined или пустую строку
|
||
worldId = (worldParam && worldParam.trim() !== '') ? worldParam : null;
|
||
|
||
console.log('worldId after params:', worldId, 'type:', typeof worldId);
|
||
|
||
// Если worldId отсутствует - генерируем новый и записываем в URL
|
||
if (!worldId) {
|
||
worldId = Math.random().toString(36).substring(2, 10);
|
||
console.log('Generated worldId:', worldId);
|
||
|
||
try {
|
||
const newUrl = new URL(window.location.href);
|
||
newUrl.searchParams.set('world', worldId);
|
||
const newUrlString = newUrl.toString();
|
||
console.log('New URL to set:', newUrlString);
|
||
|
||
// Проверяем, поддерживается ли history API
|
||
if (typeof window.history !== 'undefined' && typeof window.history.replaceState === 'function') {
|
||
window.history.replaceState(null, '', newUrlString);
|
||
console.log('URL after replaceState:', window.location.href);
|
||
console.log('URL after replaceState (direct check):', window.location.search);
|
||
} else {
|
||
console.error('History API not supported!');
|
||
}
|
||
} catch (e) {
|
||
console.error('Error updating URL:', e);
|
||
}
|
||
|
||
console.log('Generated new worldId for browser:', worldId);
|
||
}
|
||
|
||
console.log('Final worldId:', worldId, 'Player name:', playerName);
|
||
|
||
console.log(`Server URL: ${SERVER_URL}, World ID: ${worldId}`);
|
||
|
||
// Setters для изменения worldId/playerName из других модулей
|
||
export function setWorldId(id) { worldId = id; }
|
||
export function setPlayerName(name) { playerName = name; }
|
||
|
||
export { worldId, playerName }; |