// Main Game App class TribeGenApp { constructor () { this.currentUser = null; this.currentSave = null; this.currentTribe = null; this.init(); } async init () { this.setupEventListeners(); this.checkAuth(); } setupEventListeners () { // Auth Events document.getElementById('loginForm').addEventListener('submit', (e) => this.handleLogin(e)); document.getElementById('registerForm').addEventListener('submit', (e) => this.handleRegister(e)); document.getElementById('logoutBtn').addEventListener('click', () => this.handleLogout()); // Tab switching document.querySelectorAll('.tab-btn').forEach(btn => { btn.addEventListener('click', (e) => this.switchTab(e.target.dataset.tab)); }); // Main Menu document.getElementById('newGameBtn').addEventListener('click', () => this.startGameSetup()); // Game Setup document.getElementById('startGenerationBtn').addEventListener('click', () => this.generateKobolds()); document.getElementById('selectLeaderBtn').addEventListener('click', () => this.selectLeader()); document.getElementById('finishTribeBtn').addEventListener('click', () => this.finishTribe()); // Game Screen document.getElementById('endMonthBtn').addEventListener('click', () => this.endMonth()); // Modal document.querySelector('.modal-close').addEventListener('click', () => this.hideEventModal()); document.getElementById('eventContinueBtn').addEventListener('click', () => this.hideEventModal()); } switchTab (tabName) { document.querySelectorAll('.tab-content').forEach(tab => tab.classList.remove('active')); document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active')); document.getElementById(tabName + 'Tab').classList.add('active'); event.target.classList.add('active'); } async checkAuth () { // Check if user is logged in const savedUser = localStorage.getItem('currentUser'); if (savedUser) { this.currentUser = JSON.parse(savedUser); this.showMainMenu(); } else { this.showAuthScreen(); } } async handleLogin (e) { e.preventDefault(); const email = document.querySelector('#loginForm input[name="email"]').value; const password = document.querySelector('#loginForm input[name="password"]').value; try { const response = await fetch('api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }) }); const dataJson = await response; const dataText = await dataJson.text(); const data = JSON.parse(dataText); if (data.success) { this.currentUser = data.user; localStorage.setItem('currentUser', JSON.stringify(data.user)); this.showMainMenu(); } else { this.showAuthError(data.error); } } catch (error) { this.showAuthError('Login failed'); console.error(error); } } async handleRegister (e) { e.preventDefault(); const email = document.querySelector('#registerForm input[name="email"]').value; const password = document.querySelector('#registerForm input[name="password"]').value; const passwordConfirm = document.querySelector('#registerForm input[name="passwordConfirm"]').value; if (password !== passwordConfirm) { this.showAuthError('Passwords do not match'); return; } try { const response = await fetch('api/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }) }); const dataRaw = await response; const dataText = await dataRaw.text(); const data = JSON.parse(dataText); if (data.success) { this.currentUser = data.user; localStorage.setItem('currentUser', JSON.stringify(data.user)); this.showMainMenu(); } else { this.showAuthError(data.error); } } catch (error) { this.showAuthError('Registration failed'); console.error(error); } } handleLogout () { this.currentUser = null; localStorage.removeItem('currentUser'); this.showAuthScreen(); } showAuthError (error) { const errorDiv = document.getElementById('authError'); errorDiv.textContent = error; errorDiv.classList.add('show'); setTimeout(() => errorDiv.classList.remove('show'), 5000); } showAuthScreen () { this.hideAll(); document.getElementById('authScreen').classList.remove('hidden'); } showMainMenu () { this.hideAll(); document.getElementById('mainMenuScreen').classList.remove('hidden'); document.getElementById('userEmail').textContent = this.currentUser.email; this.loadSaves(); } async loadSaves () { try { const response = await fetch('api/game/list', { method: 'GET', headers: { 'Authorization': `Bearer ${this.currentUser.token}` } }); const data = await response.json(); const savesList = document.getElementById('savesList'); savesList.innerHTML = ''; if (data.saves && data.saves.length > 0) { data.saves.forEach(save => { const card = document.createElement('div'); card.className = 'save-card'; card.innerHTML = `
Month: ${save.current_month}
Season: ${save.current_season}
`; card.addEventListener('click', () => this.loadGame(save.id)); savesList.appendChild(card); }); } } catch (error) { console.error('Failed to load saves:', error); } } startGameSetup () { this.hideAll(); document.getElementById('gameSetupScreen').classList.remove('hidden'); document.getElementById('setupStep1').classList.remove('hidden'); document.getElementById('setupStep2').classList.add('hidden'); document.getElementById('setupStep3').classList.add('hidden'); } async generateKobolds () { const tribeName = document.getElementById('tribeName').value; if (!tribeName) { alert('Please enter a tribe name'); return; } try { const response = await fetch('api/game/generation', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser.token}` }, body: JSON.stringify({ saveName: tribeName, userId: this.currentUser.id }) }); const dataText = await response.text(); const data = JSON.parse(dataText); if (data.success) { this.currentSave = data.save; this.displayKobolds(data.kobolds, 'leader'); document.getElementById('setupStep1').classList.add('hidden'); document.getElementById('setupStep2').classList.remove('hidden'); } } catch (error) { alert('Failed to generate kobolds'); console.error(error); } } displayKobolds (kobolds, type) { const container = type === 'leader' ? document.getElementById('leaderSelectionGrid') : document.getElementById('memberSelectionGrid'); container.innerHTML = ''; kobolds.forEach((kobold, index) => { const card = document.createElement('div'); card.className = 'kobold-card'; const spriteUrl = `api/game/sprite?userId=${this.currentUser.id}&koboldId=${kobold.id}&age=${kobold.ageCategory}`; card.innerHTML = `