BlueGit

blue-star tribegen

event selection ui

timfa Tim Falken · pushed by timfa

Authored 2026-01-28 · 9 months ago

Hash
c37c9e3f64442eeb81ffe84da9fb73e8758719ed
Parent
146dd342
4 files changed +222 −21
web/css/style.css +80 −0
@@ -809,6 +809,86 @@ button {
809 809 cursor: pointer;
810 810 }
811 811
812 /* Participant Selection */
813 .participants-list {
814 display: flex;
815 flex-direction: column;
816 gap: 10px;
817 margin: 20px 0;
818 max-height: 400px;
819 overflow-y: auto;
820 border: 1px solid #ddd;
821 padding: 10px;
822 border-radius: 5px;
823 }
824
825 .participant-row {
826 display: flex;
827 align-items: center;
828 padding: 10px;
829 border: 1px solid #e0e0e0;
830 border-radius: 4px;
831 background: #fafafa;
832 }
833
834 .participant-row.eligible {
835 background: #f0f8ff;
836 border-color: #667eea;
837 }
838
839 .participant-row.eligible label {
840 cursor: pointer;
841 flex: 1;
842 display: flex;
843 align-items: center;
844 gap: 10px;
845 }
846
847 .participant-row.eligible input[type="checkbox"] {
848 cursor: pointer;
849 width: 18px;
850 height: 18px;
851 }
852
853 .participant-row.ineligible {
854 background: #f5f5f5;
855 border-color: #ccc;
856 opacity: 0.6;
857 }
858
859 .participant-disabled {
860 display: flex;
861 justify-content: space-between;
862 align-items: center;
863 width: 100%;
864 flex: 1;
865 }
866
867 .disabled-reason {
868 font-size: 0.85em;
869 color: #999;
870 font-style: italic;
871 margin-left: 10px;
872 }
873
874 .participant-footer {
875 display: flex;
876 justify-content: space-between;
877 align-items: center;
878 margin-top: 20px;
879 gap: 10px;
880 }
881
882 .participant-footer .counter {
883 font-size: 0.9em;
884 color: #666;
885 }
886
887 .participant-footer button {
888 flex: 1;
889 max-width: 150px;
890 }
891
812 892 /* Responsive */
813 893 @media (max-width: 768px) {
814 894 .game-main {
@@ -809,6 +809,86 @@ button {
809 cursor: pointer;
809 cursor: pointer;
810}
810}
811
811
812/* Participant Selection */
813.participants-list {
814 display: flex;
815 flex-direction: column;
816 gap: 10px;
817 margin: 20px 0;
818 max-height: 400px;
819 overflow-y: auto;
820 border: 1px solid #ddd;
821 padding: 10px;
822 border-radius: 5px;
823}
824
825.participant-row {
826 display: flex;
827 align-items: center;
828 padding: 10px;
829 border: 1px solid #e0e0e0;
830 border-radius: 4px;
831 background: #fafafa;
832}
833
834.participant-row.eligible {
835 background: #f0f8ff;
836 border-color: #667eea;
837}
838
839.participant-row.eligible label {
840 cursor: pointer;
841 flex: 1;
842 display: flex;
843 align-items: center;
844 gap: 10px;
845}
846
847.participant-row.eligible input[type="checkbox"] {
848 cursor: pointer;
849 width: 18px;
850 height: 18px;
851}
852
853.participant-row.ineligible {
854 background: #f5f5f5;
855 border-color: #ccc;
856 opacity: 0.6;
857}
858
859.participant-disabled {
860 display: flex;
861 justify-content: space-between;
862 align-items: center;
863 width: 100%;
864 flex: 1;
865}
866
867.disabled-reason {
868 font-size: 0.85em;
869 color: #999;
870 font-style: italic;
871 margin-left: 10px;
872}
873
874.participant-footer {
875 display: flex;
876 justify-content: space-between;
877 align-items: center;
878 margin-top: 20px;
879 gap: 10px;
880}
881
882.participant-footer .counter {
883 font-size: 0.9em;
884 color: #666;
885}
886
887.participant-footer button {
888 flex: 1;
889 max-width: 150px;
890}
891
812/* Responsive */
892/* Responsive */
813@media (max-width: 768px) {
893@media (max-width: 768px) {
814 .game-main {
894 .game-main {
web/indexHtml.html +14 −0
@@ -177,6 +177,20 @@
177 177 </div>
178 178 </div>
179 179 </div>
180
181 <!-- Participant Selection Modal -->
182 <div id="participantModal" class="modal hidden">
183 <div class="modal-content">
184 <button class="modal-close">&times;</button>
185 <h3 id="participantModalTitle">Select Participants</h3>
186 <div id="participantsList" class="participants-list"></div>
187 <div class="participant-footer">
188 <div id="participantCount" class="counter">0 selected</div>
189 <button id="cancelParticipantsBtn" class="secondary">Cancel</button>
190 <button id="startEventBtn" class="primary" disabled>Start Event</button>
191 </div>
192 </div>
193 </div>
180 194 </div>
181 195
182 196 <!-- Config Screen -->
@@ -177,6 +177,20 @@
177 </div>
177 </div>
178 </div>
178 </div>
179 </div>
179 </div>
180
181 <!-- Participant Selection Modal -->
182 <div id="participantModal" class="modal hidden">
183 <div class="modal-content">
184 <button class="modal-close">&times;</button>
185 <h3 id="participantModalTitle">Select Participants</h3>
186 <div id="participantsList" class="participants-list"></div>
187 <div class="participant-footer">
188 <div id="participantCount" class="counter">0 selected</div>
189 <button id="cancelParticipantsBtn" class="secondary">Cancel</button>
190 <button id="startEventBtn" class="primary" disabled>Start Event</button>
191 </div>
192 </div>
193 </div>
180 </div>
194 </div>
181
195
182 <!-- Config Screen -->
196 <!-- Config Screen -->
web/js/app.js +127 −20
@@ -464,44 +464,152 @@ class TribeGenApp
464 464
465 465 const events = ['Hunting', 'Exploration', 'Farming'];
466 466
467 events.forEach(event =>
467 events.forEach(evt =>
468 468 {
469 469 const item = document.createElement('div');
470 470 item.className = 'event-item';
471 item.textContent = event;
472 item.addEventListener('click', () => this.executeEvent(event));
471 item.textContent = evt;
472 item.addEventListener('click', () => this.showParticipantModal(evt));
473 473 list.appendChild(item);
474 474 });
475 475 }
476 476
477 477 async executeEvent (eventType)
478 478 {
479 // Deprecated direct execution; use participant modal instead
480 this.showParticipantModal(eventType);
481 }
482
483 showParticipantModal (eventType)
484 {
485 this.currentEventType = eventType;
486 const modal = document.getElementById('participantModal');
487 document.getElementById('participantModalTitle').textContent = `Select participants for ${eventType}`;
488
489 const list = document.getElementById('participantsList');
490 list.innerHTML = '';
491
492 // Default limits and minimum age (server also enforces). Adjust per event if needed.
493 const eventConfig = {
494 'Exploration': { min: 1, max: 3, minAge: 0 },
495 'Hunting': { min: 1, max: 3, minAge: 0 },
496 'Farming': { min: 1, max: 3, minAge: 0 }
497 };
498 const config = eventConfig[eventType] || { min: 1, max: 3, minAge: 0 };
499 this.participantMin = config.min;
500 this.participantMax = config.max;
501 this.participantMinAge = config.minAge;
502
503 // Helper: check if kobold is eligible
504 const isEligible = (kobold) => {
505 const ageCategory = (kobold.ageCategory || '').toLowerCase();
506 if (ageCategory === 'egg') return false; // eggs cannot participate
507 if (!kobold.isAlive && kobold.isDead) return false; // dead cannot participate (if available from backend)
508 if (kobold.age < this.participantMinAge) return false; // too young
509 return true;
510 };
511
512 // Render kobolds with eligibility status
513 this.currentTribe.kobolds.forEach(kobold =>
514 {
515 const ageCategory = (kobold.ageCategory || '').toLowerCase();
516 const eligible = isEligible(kobold);
517 let reason = '';
518
519 if (ageCategory === 'egg')
520 reason = '(eggs cannot participate)';
521 else if (kobold.age < this.participantMinAge)
522 reason = `(too young, needs age ${this.participantMinAge}+)`;
523
524 const row = document.createElement('div');
525 row.className = `participant-row ${eligible ? 'eligible' : 'ineligible'}`;
526
527 if (eligible)
528 {
529 row.innerHTML = `
530 <label>
531 <input type="checkbox" class="participant-checkbox" data-id="${kobold.id}">
532 <strong>${kobold.name}</strong> (${kobold.age}y - ${kobold.ageCategory})
533 </label>
534 `;
535 const cb = row.querySelector('.participant-checkbox');
536 cb.addEventListener('change', () => this._updateParticipantButtons());
537 } else
538 {
539 row.innerHTML = `
540 <div class="participant-disabled">
541 <span>${kobold.name} (${kobold.age}y - ${kobold.ageCategory})</span>
542 <span class="disabled-reason">${reason}</span>
543 </div>
544 `;
545 }
546
547 list.appendChild(row);
548 });
549
550 document.getElementById('participantCount').textContent = `0 selected`;
551
552 // cancel/start handlers
553 const cancelBtn = document.getElementById('cancelParticipantsBtn');
554 const startBtn = document.getElementById('startEventBtn');
555
556 cancelBtn.onclick = () => { modal.classList.add('hidden'); };
557 startBtn.onclick = async () => { await this.startEventWithParticipants(); };
558
559 // initial state
560 this._updateParticipantButtons();
561
562 modal.classList.remove('hidden');
563 }
564
565 _updateParticipantButtons ()
566 {
567 const list = document.getElementById('participantsList');
568 const selected = Array.from(list.querySelectorAll('.participant-checkbox:checked')).map(cb => cb.dataset.id);
569 const count = selected.length;
570 document.getElementById('participantCount').textContent = `${count} selected`;
571
572 const startBtn = document.getElementById('startEventBtn');
573 if (count >= (this.participantMin || 1) && count <= (this.participantMax || 3))
574 {
575 startBtn.disabled = false;
576 } else
577 {
578 startBtn.disabled = true;
579 }
580 }
581
582 async startEventWithParticipants ()
583 {
584 const modal = document.getElementById('participantModal');
585 const list = document.getElementById('participantsList');
586 const selected = Array.from(list.querySelectorAll('.participant-checkbox:checked')).map(cb => cb.dataset.id);
587
588 if (!selected || selected.length === 0) return;
589
479 590 try
480 591 {
481 const response = await fetch('api/game/event', {
592 const resp = await fetch('api/game/event', {
482 593 method: 'POST',
483 headers: {
484 'Content-Type': 'application/json',
485 'Authorization': `Bearer ${this.currentUser.token}`
486 },
487 body: JSON.stringify({
488 userId: this.currentUser.id,
489 saveId: this.currentSave.id,
490 eventType: eventType
491 })
594 headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser.token}` },
595 body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, eventType: this.currentEventType, participants: selected })
492 596 });
493 597
494 const data = await response.json();
495
598 const data = await resp.json();
496 599 if (data.success)
497 600 {
498 601 typeText(document.getElementById('eventStory'), data.story);
499 602 this.showEventModal();
603 modal.classList.add('hidden');
604 await this.loadGame(this.currentSave.id);
605 } else
606 {
607 alert(data.error || 'Failed to start event');
500 608 }
501 } catch (error)
609 } catch (err)
502 610 {
503 alert('Failed to execute event');
504 console.error(error);
611 console.error(err);
612 alert('Failed to start event');
505 613 }
506 614 }
507 615
@@ -716,8 +824,7 @@ class TribeGenApp
716 824 statEl.className = 'stat-item';
717 825 statEl.innerHTML = `
718 826 <div class="stat-name">${stat}</div>
719 <div class="stat-value">${value}</div>
720 <div class="stat-label">${label}</div>
827 <div class="stat-value">${label}</div>
721 828 `;
722 829 statsGrid.appendChild(statEl);
723 830 });
@@ -464,44 +464,152 @@ class TribeGenApp
464
464
465 const events = ['Hunting', 'Exploration', 'Farming'];
465 const events = ['Hunting', 'Exploration', 'Farming'];
466
466
467 events.forEach(event =>
467 events.forEach(evt =>
468 {
468 {
469 const item = document.createElement('div');
469 const item = document.createElement('div');
470 item.className = 'event-item';
470 item.className = 'event-item';
471 item.textContent = event;
471 item.textContent = evt;
472 item.addEventListener('click', () => this.executeEvent(event));
472 item.addEventListener('click', () => this.showParticipantModal(evt));
473 list.appendChild(item);
473 list.appendChild(item);
474 });
474 });
475 }
475 }
476
476
477 async executeEvent (eventType)
477 async executeEvent (eventType)
478 {
478 {
479 // Deprecated direct execution; use participant modal instead
480 this.showParticipantModal(eventType);
481 }
482
483 showParticipantModal (eventType)
484 {
485 this.currentEventType = eventType;
486 const modal = document.getElementById('participantModal');
487 document.getElementById('participantModalTitle').textContent = `Select participants for ${eventType}`;
488
489 const list = document.getElementById('participantsList');
490 list.innerHTML = '';
491
492 // Default limits and minimum age (server also enforces). Adjust per event if needed.
493 const eventConfig = {
494 'Exploration': { min: 1, max: 3, minAge: 0 },
495 'Hunting': { min: 1, max: 3, minAge: 0 },
496 'Farming': { min: 1, max: 3, minAge: 0 }
497 };
498 const config = eventConfig[eventType] || { min: 1, max: 3, minAge: 0 };
499 this.participantMin = config.min;
500 this.participantMax = config.max;
501 this.participantMinAge = config.minAge;
502
503 // Helper: check if kobold is eligible
504 const isEligible = (kobold) => {
505 const ageCategory = (kobold.ageCategory || '').toLowerCase();
506 if (ageCategory === 'egg') return false; // eggs cannot participate
507 if (!kobold.isAlive && kobold.isDead) return false; // dead cannot participate (if available from backend)
508 if (kobold.age < this.participantMinAge) return false; // too young
509 return true;
510 };
511
512 // Render kobolds with eligibility status
513 this.currentTribe.kobolds.forEach(kobold =>
514 {
515 const ageCategory = (kobold.ageCategory || '').toLowerCase();
516 const eligible = isEligible(kobold);
517 let reason = '';
518
519 if (ageCategory === 'egg')
520 reason = '(eggs cannot participate)';
521 else if (kobold.age < this.participantMinAge)
522 reason = `(too young, needs age ${this.participantMinAge}+)`;
523
524 const row = document.createElement('div');
525 row.className = `participant-row ${eligible ? 'eligible' : 'ineligible'}`;
526
527 if (eligible)
528 {
529 row.innerHTML = `
530 <label>
531 <input type="checkbox" class="participant-checkbox" data-id="${kobold.id}">
532 <strong>${kobold.name}</strong> (${kobold.age}y - ${kobold.ageCategory})
533 </label>
534 `;
535 const cb = row.querySelector('.participant-checkbox');
536 cb.addEventListener('change', () => this._updateParticipantButtons());
537 } else
538 {
539 row.innerHTML = `
540 <div class="participant-disabled">
541 <span>${kobold.name} (${kobold.age}y - ${kobold.ageCategory})</span>
542 <span class="disabled-reason">${reason}</span>
543 </div>
544 `;
545 }
546
547 list.appendChild(row);
548 });
549
550 document.getElementById('participantCount').textContent = `0 selected`;
551
552 // cancel/start handlers
553 const cancelBtn = document.getElementById('cancelParticipantsBtn');
554 const startBtn = document.getElementById('startEventBtn');
555
556 cancelBtn.onclick = () => { modal.classList.add('hidden'); };
557 startBtn.onclick = async () => { await this.startEventWithParticipants(); };
558
559 // initial state
560 this._updateParticipantButtons();
561
562 modal.classList.remove('hidden');
563 }
564
565 _updateParticipantButtons ()
566 {
567 const list = document.getElementById('participantsList');
568 const selected = Array.from(list.querySelectorAll('.participant-checkbox:checked')).map(cb => cb.dataset.id);
569 const count = selected.length;
570 document.getElementById('participantCount').textContent = `${count} selected`;
571
572 const startBtn = document.getElementById('startEventBtn');
573 if (count >= (this.participantMin || 1) && count <= (this.participantMax || 3))
574 {
575 startBtn.disabled = false;
576 } else
577 {
578 startBtn.disabled = true;
579 }
580 }
581
582 async startEventWithParticipants ()
583 {
584 const modal = document.getElementById('participantModal');
585 const list = document.getElementById('participantsList');
586 const selected = Array.from(list.querySelectorAll('.participant-checkbox:checked')).map(cb => cb.dataset.id);
587
588 if (!selected || selected.length === 0) return;
589
479 try
590 try
480 {
591 {
481 const response = await fetch('api/game/event', {
592 const resp = await fetch('api/game/event', {
482 method: 'POST',
593 method: 'POST',
483 headers: {
594 headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser.token}` },
484 'Content-Type': 'application/json',
595 body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, eventType: this.currentEventType, participants: selected })
485 'Authorization': `Bearer ${this.currentUser.token}`
486 },
487 body: JSON.stringify({
488 userId: this.currentUser.id,
489 saveId: this.currentSave.id,
490 eventType: eventType
491 })
492 });
596 });
493
597
494 const data = await response.json();
598 const data = await resp.json();
495
496 if (data.success)
599 if (data.success)
497 {
600 {
498 typeText(document.getElementById('eventStory'), data.story);
601 typeText(document.getElementById('eventStory'), data.story);
499 this.showEventModal();
602 this.showEventModal();
603 modal.classList.add('hidden');
604 await this.loadGame(this.currentSave.id);
605 } else
606 {
607 alert(data.error || 'Failed to start event');
500 }
608 }
501 } catch (error)
609 } catch (err)
502 {
610 {
503 alert('Failed to execute event');
611 console.error(err);
504 console.error(error);
612 alert('Failed to start event');
505 }
613 }
506 }
614 }
507
615
@@ -716,8 +824,7 @@ class TribeGenApp
716 statEl.className = 'stat-item';
824 statEl.className = 'stat-item';
717 statEl.innerHTML = `
825 statEl.innerHTML = `
718 <div class="stat-name">${stat}</div>
826 <div class="stat-name">${stat}</div>
719 <div class="stat-value">${value}</div>
827 <div class="stat-value">${label}</div>
720 <div class="stat-label">${label}</div>
721 `;
828 `;
722 statsGrid.appendChild(statEl);
829 statsGrid.appendChild(statEl);
723 });
830 });
web/src/Events/ExplorationEvent.php +1 −1
@@ -35,7 +35,7 @@ class ExplorationEvent extends Event
35 35
36 36 $this->story .= "A group of " . count($explorers) . " intrepid explorers set out:\n";
37 37 foreach ($explorers as $explorer) {
38 $this->story .= "- " . $explorer->getName() . " (Intelligence: " . getStatLabel($explorer->getStat('intelligence')) . ")\n";
38 $this->story .= "- " . $explorer->getName() . " (Intelligence: " . GameHelper::getStatLabel($explorer->getStat('intelligence')) . ")\n";
39 39 }
40 40 $this->story .= "\n";
41 41
@@ -35,7 +35,7 @@ class ExplorationEvent extends Event
35
35
36 $this->story .= "A group of " . count($explorers) . " intrepid explorers set out:\n";
36 $this->story .= "A group of " . count($explorers) . " intrepid explorers set out:\n";
37 foreach ($explorers as $explorer) {
37 foreach ($explorers as $explorer) {
38 $this->story .= "- " . $explorer->getName() . " (Intelligence: " . getStatLabel($explorer->getStat('intelligence')) . ")\n";
38 $this->story .= "- " . $explorer->getName() . " (Intelligence: " . GameHelper::getStatLabel($explorer->getStat('intelligence')) . ")\n";
39 }
39 }
40 $this->story .= "\n";
40 $this->story .= "\n";
41
41