| 1 | // Main Game App |
| 2 | class TribeGenApp |
| 3 | { |
| 4 | constructor () |
| 5 | { |
| 6 | this.currentUser = null; |
| 7 | this.currentSave = null; |
| 8 | this.currentTribe = null; |
| 9 | this.init(); |
| 10 | } |
| 11 | |
| 12 | async init () |
| 13 | { |
| 14 | this.setupEventListeners(); |
| 15 | this.checkAuth(); |
| 16 | } |
| 17 | |
| 18 | setupEventListeners () |
| 19 | { |
| 20 | // Auth Events |
| 21 | document.getElementById('loginForm').addEventListener('submit', (e) => this.handleLogin(e)); |
| 22 | document.getElementById('registerForm').addEventListener('submit', (e) => this.handleRegister(e)); |
| 23 | document.getElementById('logoutBtn').addEventListener('click', () => this.handleLogout()); |
| 24 | |
| 25 | // Tab switching |
| 26 | document.querySelectorAll('.tab-btn').forEach(btn => |
| 27 | { |
| 28 | btn.addEventListener('click', (e) => this.switchTab(e.target.dataset.tab)); |
| 29 | }); |
| 30 | |
| 31 | // Main Menu |
| 32 | document.getElementById('newGameBtn').addEventListener('click', () => this.startGameSetup()); |
| 33 | |
| 34 | // Game Setup |
| 35 | document.getElementById('startGenerationBtn').addEventListener('click', () => this.generateKobolds()); |
| 36 | document.getElementById('selectLeaderBtn').addEventListener('click', () => this.selectLeader()); |
| 37 | document.getElementById('finishTribeBtn').addEventListener('click', () => this.finishTribe()); |
| 38 | |
| 39 | // Game Screen |
| 40 | document.getElementById('endMonthBtn').addEventListener('click', () => this.endMonth()); |
| 41 | |
| 42 | // Modal |
| 43 | document.querySelector('.modal-close').addEventListener('click', () => this.hideEventModal()); |
| 44 | document.getElementById('eventContinueBtn').addEventListener('click', () => this.hideEventModal()); |
| 45 | } |
| 46 | |
| 47 | switchTab (tabName) |
| 48 | { |
| 49 | document.querySelectorAll('.tab-content').forEach(tab => tab.classList.remove('active')); |
| 50 | document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active')); |
| 51 | document.getElementById(tabName + 'Tab').classList.add('active'); |
| 52 | event.target.classList.add('active'); |
| 53 | } |
| 54 | |
| 55 | async checkAuth () |
| 56 | { |
| 57 | // Check if user is logged in |
| 58 | const savedUser = localStorage.getItem('currentUser'); |
| 59 | if (savedUser) |
| 60 | { |
| 61 | this.currentUser = JSON.parse(savedUser); |
| 62 | this.showMainMenu(); |
| 63 | } else |
| 64 | { |
| 65 | this.showAuthScreen(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | async handleLogin (e) |
| 70 | { |
| 71 | e.preventDefault(); |
| 72 | const email = document.querySelector('#loginForm input[name="email"]').value; |
| 73 | const password = document.querySelector('#loginForm input[name="password"]').value; |
| 74 | |
| 75 | try |
| 76 | { |
| 77 | const response = await fetch('api/auth/login', { |
| 78 | method: 'POST', |
| 79 | headers: { 'Content-Type': 'application/json' }, |
| 80 | body: JSON.stringify({ email, password }) |
| 81 | }); |
| 82 | |
| 83 | const dataJson = await response; |
| 84 | const dataText = await dataJson.text(); |
| 85 | const data = JSON.parse(dataText); |
| 86 | |
| 87 | if (data.success) |
| 88 | { |
| 89 | this.currentUser = data.user; |
| 90 | localStorage.setItem('currentUser', JSON.stringify(data.user)); |
| 91 | this.showMainMenu(); |
| 92 | } else |
| 93 | { |
| 94 | this.showAuthError(data.error); |
| 95 | } |
| 96 | } catch (error) |
| 97 | { |
| 98 | this.showAuthError('Login failed'); |
| 99 | console.error(error); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | async handleRegister (e) |
| 104 | { |
| 105 | e.preventDefault(); |
| 106 | const email = document.querySelector('#registerForm input[name="email"]').value; |
| 107 | const password = document.querySelector('#registerForm input[name="password"]').value; |
| 108 | const passwordConfirm = document.querySelector('#registerForm input[name="passwordConfirm"]').value; |
| 109 | |
| 110 | if (password !== passwordConfirm) |
| 111 | { |
| 112 | this.showAuthError('Passwords do not match'); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | try |
| 117 | { |
| 118 | const response = await fetch('api/auth/register', { |
| 119 | method: 'POST', |
| 120 | headers: { 'Content-Type': 'application/json' }, |
| 121 | body: JSON.stringify({ email, password }) |
| 122 | }); |
| 123 | |
| 124 | const dataRaw = await response; |
| 125 | const dataText = await dataRaw.text(); |
| 126 | const data = JSON.parse(dataText); |
| 127 | |
| 128 | if (data.success) |
| 129 | { |
| 130 | this.currentUser = data.user; |
| 131 | localStorage.setItem('currentUser', JSON.stringify(data.user)); |
| 132 | this.showMainMenu(); |
| 133 | } else |
| 134 | { |
| 135 | this.showAuthError(data.error); |
| 136 | } |
| 137 | } catch (error) |
| 138 | { |
| 139 | this.showAuthError('Registration failed'); |
| 140 | console.error(error); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | handleLogout () |
| 145 | { |
| 146 | this.currentUser = null; |
| 147 | localStorage.removeItem('currentUser'); |
| 148 | this.showAuthScreen(); |
| 149 | } |
| 150 | |
| 151 | showAuthError (error) |
| 152 | { |
| 153 | const errorDiv = document.getElementById('authError'); |
| 154 | errorDiv.textContent = error; |
| 155 | errorDiv.classList.add('show'); |
| 156 | setTimeout(() => errorDiv.classList.remove('show'), 5000); |
| 157 | } |
| 158 | |
| 159 | showAuthScreen () |
| 160 | { |
| 161 | this.hideAll(); |
| 162 | document.getElementById('authScreen').classList.remove('hidden'); |
| 163 | } |
| 164 | |
| 165 | showMainMenu () |
| 166 | { |
| 167 | this.hideAll(); |
| 168 | document.getElementById('mainMenuScreen').classList.remove('hidden'); |
| 169 | document.getElementById('userEmail').textContent = this.currentUser.email; |
| 170 | this.loadSaves(); |
| 171 | } |
| 172 | |
| 173 | async loadSaves () |
| 174 | { |
| 175 | try |
| 176 | { |
| 177 | const response = await fetch('api/game/list', { |
| 178 | method: 'GET', |
| 179 | headers: { 'Authorization': `Bearer ${this.currentUser.token}` } |
| 180 | }); |
| 181 | |
| 182 | const data = await response.json(); |
| 183 | |
| 184 | const savesList = document.getElementById('savesList'); |
| 185 | savesList.innerHTML = ''; |
| 186 | |
| 187 | if (data.saves && data.saves.length > 0) |
| 188 | { |
| 189 | data.saves.forEach(save => |
| 190 | { |
| 191 | const card = document.createElement('div'); |
| 192 | card.className = 'save-card'; |
| 193 | card.innerHTML = ` |
| 194 | <h3>${save.name}</h3> |
| 195 | <p>Month: ${save.current_month}</p> |
| 196 | <p>Season: ${save.current_season}</p> |
| 197 | `; |
| 198 | card.addEventListener('click', () => this.loadGame(save.id)); |
| 199 | savesList.appendChild(card); |
| 200 | }); |
| 201 | } |
| 202 | } catch (error) |
| 203 | { |
| 204 | console.error('Failed to load saves:', error); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | startGameSetup () |
| 209 | { |
| 210 | this.hideAll(); |
| 211 | document.getElementById('gameSetupScreen').classList.remove('hidden'); |
| 212 | document.getElementById('setupStep1').classList.remove('hidden'); |
| 213 | document.getElementById('setupStep2').classList.add('hidden'); |
| 214 | document.getElementById('setupStep3').classList.add('hidden'); |
| 215 | } |
| 216 | |
| 217 | async generateKobolds () |
| 218 | { |
| 219 | const tribeName = document.getElementById('tribeName').value; |
| 220 | |
| 221 | if (!tribeName) |
| 222 | { |
| 223 | alert('Please enter a tribe name'); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | try |
| 228 | { |
| 229 | const response = await fetch('api/game/generation', { |
| 230 | method: 'POST', |
| 231 | headers: { |
| 232 | 'Content-Type': 'application/json', |
| 233 | 'Authorization': `Bearer ${this.currentUser.token}` |
| 234 | }, |
| 235 | body: JSON.stringify({ |
| 236 | saveName: tribeName, |
| 237 | userId: this.currentUser.id |
| 238 | }) |
| 239 | }); |
| 240 | |
| 241 | const dataText = await response.text(); |
| 242 | const data = JSON.parse(dataText); |
| 243 | |
| 244 | if (data.success) |
| 245 | { |
| 246 | this.currentSave = data.save; |
| 247 | this.displayKobolds(data.kobolds, 'leader'); |
| 248 | |
| 249 | document.getElementById('setupStep1').classList.add('hidden'); |
| 250 | document.getElementById('setupStep2').classList.remove('hidden'); |
| 251 | } |
| 252 | } catch (error) |
| 253 | { |
| 254 | alert('Failed to generate kobolds'); |
| 255 | console.error(error); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | displayKobolds (kobolds, type) |
| 260 | { |
| 261 | const container = type === 'leader' |
| 262 | ? document.getElementById('leaderSelectionGrid') |
| 263 | : document.getElementById('memberSelectionGrid'); |
| 264 | |
| 265 | container.innerHTML = ''; |
| 266 | |
| 267 | kobolds.forEach((kobold, index) => |
| 268 | { |
| 269 | const card = document.createElement('div'); |
| 270 | card.className = 'kobold-card'; |
| 271 | |
| 272 | const spriteUrl = `api/game/sprite?userId=${this.currentUser.id}&koboldId=${kobold.id}&age=${kobold.ageCategory}`; |
| 273 | |
| 274 | card.innerHTML = ` |
| 275 | <img src="${spriteUrl}" alt="Kobold sprite" class="kobold-sprite"> |
| 276 | <div class="kobold-name">${kobold.age} years</div> |
| 277 | <div class="kobold-stats"> |
| 278 | <div>Vitality: ${kobold.stats.vitality}</div> |
| 279 | </div> |
| 280 | `; |
| 281 | |
| 282 | if (type === 'member') |
| 283 | { |
| 284 | card.addEventListener('click', () => this.toggleMemberSelection(card, index)); |
| 285 | } else |
| 286 | { |
| 287 | card.addEventListener('click', () => this.selectLeaderCard(card, index)); |
| 288 | } |
| 289 | |
| 290 | container.appendChild(card); |
| 291 | }); |
| 292 | } |
| 293 | |
| 294 | selectLeaderCard (card, index) |
| 295 | { |
| 296 | document.querySelectorAll('#leaderSelectionGrid .kobold-card').forEach(c => c.classList.remove('selected')); |
| 297 | card.classList.add('selected'); |
| 298 | document.getElementById('selectLeaderBtn').dataset.leaderIndex = index; |
| 299 | } |
| 300 | |
| 301 | toggleMemberSelection (card, index) |
| 302 | { |
| 303 | card.classList.toggle('selected'); |
| 304 | |
| 305 | const selected = document.querySelectorAll('#memberSelectionGrid .kobold-card.selected').length; |
| 306 | document.getElementById('memberCount').textContent = `${selected} / 5 selected`; |
| 307 | |
| 308 | if (selected === 5) |
| 309 | { |
| 310 | document.getElementById('finishTribeBtn').disabled = false; |
| 311 | } else |
| 312 | { |
| 313 | document.getElementById('finishTribeBtn').disabled = true; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | async selectLeader () |
| 318 | { |
| 319 | const leaderIndex = document.getElementById('selectLeaderBtn').dataset.leaderIndex; |
| 320 | const leaderName = document.getElementById('leaderName').value; |
| 321 | |
| 322 | if (!leaderName) |
| 323 | { |
| 324 | alert('Please enter a leader name'); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | // Proceed to member selection |
| 329 | document.getElementById('setupStep2').classList.add('hidden'); |
| 330 | document.getElementById('setupStep3').classList.remove('hidden'); |
| 331 | |
| 332 | // For now, just display remaining kobolds (in real implementation, filter out leader) |
| 333 | } |
| 334 | |
| 335 | async finishTribe () |
| 336 | { |
| 337 | const selectedMembers = document.querySelectorAll('#memberSelectionGrid .kobold-card.selected'); |
| 338 | const memberIndices = Array.from(selectedMembers).map(card => |
| 339 | { |
| 340 | return Array.from(card.parentElement.children).indexOf(card); |
| 341 | }); |
| 342 | |
| 343 | // Load the game |
| 344 | try |
| 345 | { |
| 346 | const response = await fetch('api/game/save', { |
| 347 | method: 'POST', |
| 348 | headers: { |
| 349 | 'Content-Type': 'application/json', |
| 350 | 'Authorization': `Bearer ${this.currentUser.token}` |
| 351 | }, |
| 352 | body: JSON.stringify({ |
| 353 | saveId: this.currentSave.id, |
| 354 | leaderName: document.getElementById('leaderName').value, |
| 355 | memberIndices: memberIndices |
| 356 | }) |
| 357 | }); |
| 358 | |
| 359 | const data = await response.json(); |
| 360 | |
| 361 | if (data.success) |
| 362 | { |
| 363 | this.loadGame(this.currentSave.id); |
| 364 | } |
| 365 | } catch (error) |
| 366 | { |
| 367 | alert('Failed to finish tribe setup'); |
| 368 | console.error(error); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | async loadGame (saveId) |
| 373 | { |
| 374 | try |
| 375 | { |
| 376 | const response = await fetch(`api/game/load?saveId=${saveId}`, { |
| 377 | method: 'GET', |
| 378 | headers: { 'Authorization': `Bearer ${this.currentUser.token}` } |
| 379 | }); |
| 380 | |
| 381 | const data = await response.json(); |
| 382 | |
| 383 | if (data.success) |
| 384 | { |
| 385 | this.currentSave = data.save; |
| 386 | this.currentTribe = data.tribe; |
| 387 | this.showGameScreen(); |
| 388 | } |
| 389 | } catch (error) |
| 390 | { |
| 391 | alert('Failed to load game'); |
| 392 | console.error(error); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | showGameScreen () |
| 397 | { |
| 398 | this.hideAll(); |
| 399 | document.getElementById('gameScreen').classList.remove('hidden'); |
| 400 | this.renderGame(); |
| 401 | } |
| 402 | |
| 403 | renderGame () |
| 404 | { |
| 405 | document.getElementById('tribeName').textContent = this.currentSave.name; |
| 406 | document.getElementById('monthDisplay').textContent = `Month ${this.currentSave.current_month}`; |
| 407 | document.getElementById('seasonDisplay').textContent = this.capitalizeFirst(this.currentSave.current_season); |
| 408 | |
| 409 | this.renderTribeList(); |
| 410 | this.renderEvents(); |
| 411 | } |
| 412 | |
| 413 | renderTribeList () |
| 414 | { |
| 415 | const list = document.getElementById('tribeList'); |
| 416 | list.innerHTML = ''; |
| 417 | |
| 418 | this.currentTribe.kobolds.forEach(kobold => |
| 419 | { |
| 420 | const item = document.createElement('div'); |
| 421 | item.className = 'tribe-item'; |
| 422 | item.innerHTML = ` |
| 423 | <div> |
| 424 | <div class="tribe-item-name">${kobold.name}</div> |
| 425 | <div class="tribe-item-age">${kobold.age} years</div> |
| 426 | </div> |
| 427 | `; |
| 428 | list.appendChild(item); |
| 429 | }); |
| 430 | } |
| 431 | |
| 432 | renderEvents () |
| 433 | { |
| 434 | const list = document.getElementById('eventsList'); |
| 435 | list.innerHTML = ''; |
| 436 | |
| 437 | const events = ['Hunting', 'Exploration', 'Farming']; |
| 438 | |
| 439 | events.forEach(event => |
| 440 | { |
| 441 | const item = document.createElement('div'); |
| 442 | item.className = 'event-item'; |
| 443 | item.textContent = event; |
| 444 | item.addEventListener('click', () => this.executeEvent(event.toLowerCase())); |
| 445 | list.appendChild(item); |
| 446 | }); |
| 447 | } |
| 448 | |
| 449 | async executeEvent (eventType) |
| 450 | { |
| 451 | try |
| 452 | { |
| 453 | const response = await fetch('api/game/event', { |
| 454 | method: 'POST', |
| 455 | headers: { |
| 456 | 'Content-Type': 'application/json', |
| 457 | 'Authorization': `Bearer ${this.currentUser.token}` |
| 458 | }, |
| 459 | body: JSON.stringify({ |
| 460 | saveId: this.currentSave.id, |
| 461 | eventType: eventType |
| 462 | }) |
| 463 | }); |
| 464 | |
| 465 | const data = await response.json(); |
| 466 | |
| 467 | if (data.success) |
| 468 | { |
| 469 | document.getElementById('eventStory').textContent = data.story; |
| 470 | this.showEventModal(); |
| 471 | } |
| 472 | } catch (error) |
| 473 | { |
| 474 | alert('Failed to execute event'); |
| 475 | console.error(error); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | async endMonth () |
| 480 | { |
| 481 | try |
| 482 | { |
| 483 | const response = await fetch('api/game/endMonth', { |
| 484 | method: 'POST', |
| 485 | headers: { |
| 486 | 'Content-Type': 'application/json', |
| 487 | 'Authorization': `Bearer ${this.currentUser.token}` |
| 488 | }, |
| 489 | body: JSON.stringify({ saveId: this.currentSave.id }) |
| 490 | }); |
| 491 | |
| 492 | const data = await response.json(); |
| 493 | |
| 494 | if (data.success) |
| 495 | { |
| 496 | this.currentSave = data.save; |
| 497 | document.getElementById('eventStory').textContent = data.monthStory; |
| 498 | this.showEventModal(); |
| 499 | } |
| 500 | } catch (error) |
| 501 | { |
| 502 | alert('Failed to end month'); |
| 503 | console.error(error); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | showEventModal () |
| 508 | { |
| 509 | document.getElementById('eventModal').classList.remove('hidden'); |
| 510 | } |
| 511 | |
| 512 | hideEventModal () |
| 513 | { |
| 514 | document.getElementById('eventModal').classList.add('hidden'); |
| 515 | this.renderGame(); |
| 516 | } |
| 517 | |
| 518 | hideAll () |
| 519 | { |
| 520 | document.querySelectorAll('.screen').forEach(screen => screen.classList.add('hidden')); |
| 521 | } |
| 522 | |
| 523 | capitalizeFirst (str) |
| 524 | { |
| 525 | return str.charAt(0).toUpperCase() + str.slice(1); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // Initialize app when DOM is ready |
| 530 | document.addEventListener('DOMContentLoaded', () => |
| 531 | { |
| 532 | window.app = new TribeGenApp(); |
| 533 | }); |