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