BlueGit

blue-star tribegen

require a doctor for cures

timfa Tim Falken · pushed by timfa

Authored 2026-02-05 · 9 months ago

Hash
496a36061602577214d4b9ec3bc1dfb48cfc1a10
Parent
28f2714e
3 files changed +135 −38
web/js/app.js +71 −33
@@ -2,7 +2,7 @@ var doneTyping = true;
2 2 // Main Game App
3 3 class TribeGenApp
4 4 {
5 constructor ()
5 constructor()
6 6 {
7 7 this.currentUser = null;
8 8 this.currentSave = null;
@@ -1451,44 +1451,82 @@ class TribeGenApp
1451 1451 const hasItem = (kobold.items || []).some(it => it.id === required);
1452 1452 if (hasItem)
1453 1453 {
1454 const btn = document.createElement('button');
1455 btn.className = 'primary';
1456 btn.textContent = `Cure using ${required}`;
1457 btn.addEventListener('click', async () =>
1454 const requiredStatRaw = injury.stat_required_to_cure ?? injury.statRequiredToCure ?? null;
1455 const requiredStat = Number(requiredStatRaw);
1456 const hasRequiredStat = Number.isFinite(requiredStat);
1457
1458 let hasDoctor = true;
1459 if (hasRequiredStat)
1458 1460 {
1459 let dataText = "Request failed altogether";
1460 try
1461 const others = (this.currentTribe?.kobolds || []).filter(k => k.id !== kobold.id);
1462 const bestOther = others.reduce((best, k) =>
1461 1463 {
1462 const resp = await fetch('api/game/cureInjury', {
1463 method: 'POST',
1464 headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser?.token}` },
1465 body: JSON.stringify({ userId: this.currentUser.email, saveId: this.currentSave.id, koboldId: kobold.id, injuryId: injury.id, itemId: required })
1466 });
1467 dataText = await resp.text();
1468 const data = JSON.parse(dataText);
1469 if (data.success)
1470 {
1471 this.hideKoboldModal();
1472 typeText(document.getElementById('eventStory'), data.story);
1473 this.showEventModal();
1474 // Refresh game state
1475 await this.loadGame(this.currentSave.id);
1476 // reopen kobold modal after refresh
1477 const refreshed = this.currentTribe.kobolds.find(k => k.id === kobold.id);
1478
1479 } else
1464 const med = Number(k.stats?.medicine || 0);
1465 if (med >= requiredStat && (!best || med > Number(best.stats?.medicine || 0)))
1480 1466 {
1481 alert(data.error || 'Failed to cure injury');
1482 console.error('Cure injury error:', data);
1467 return k;
1483 1468 }
1484 } catch (err)
1469 return best;
1470 }, null);
1471
1472 if (bestOther)
1485 1473 {
1486 console.error(err);
1487 alert('Failed to cure injury');
1488 console.error('Response data:', dataText);
1474 hasDoctor = true;
1475 } else
1476 {
1477 const selfMed = Number(kobold.stats?.medicine || 0);
1478 hasDoctor = selfMed >= requiredStat;
1489 1479 }
1490 });
1491 actions.appendChild(btn);
1480 }
1481
1482 if (hasRequiredStat && !hasDoctor)
1483 {
1484 const label = this.getStatLabel(requiredStat);
1485 const note = document.createElement('div');
1486 note.style.color = '#999';
1487 note.textContent = `Requires ${label} doctor`;
1488 actions.appendChild(note);
1489 } else
1490 {
1491 const btn = document.createElement('button');
1492 btn.className = 'primary';
1493 btn.textContent = `Cure using ${required}`;
1494 btn.addEventListener('click', async () =>
1495 {
1496 let dataText = "Request failed altogether";
1497 try
1498 {
1499 const resp = await fetch('api/game/cureInjury', {
1500 method: 'POST',
1501 headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser?.token}` },
1502 body: JSON.stringify({ userId: this.currentUser.email, saveId: this.currentSave.id, koboldId: kobold.id, injuryId: injury.id, itemId: required })
1503 });
1504 dataText = await resp.text();
1505 const data = JSON.parse(dataText);
1506 if (data.success)
1507 {
1508 this.hideKoboldModal();
1509 typeText(document.getElementById('eventStory'), data.story);
1510 this.showEventModal();
1511 // Refresh game state
1512 await this.loadGame(this.currentSave.id);
1513 // reopen kobold modal after refresh
1514 const refreshed = this.currentTribe.kobolds.find(k => k.id === kobold.id);
1515
1516 } else
1517 {
1518 alert(data.error || 'Failed to cure injury');
1519 console.error('Cure injury error:', data);
1520 }
1521 } catch (err)
1522 {
1523 console.error(err);
1524 alert('Failed to cure injury');
1525 console.error('Response data:', dataText);
1526 }
1527 });
1528 actions.appendChild(btn);
1529 }
1492 1530 } else
1493 1531 {
1494 1532 const note = document.createElement('div');
@@ -2,7 +2,7 @@ var doneTyping = true;
2// Main Game App
2// Main Game App
3class TribeGenApp
3class TribeGenApp
4{
4{
5 constructor ()
5 constructor()
6 {
6 {
7 this.currentUser = null;
7 this.currentUser = null;
8 this.currentSave = null;
8 this.currentSave = null;
@@ -1451,44 +1451,82 @@ class TribeGenApp
1451 const hasItem = (kobold.items || []).some(it => it.id === required);
1451 const hasItem = (kobold.items || []).some(it => it.id === required);
1452 if (hasItem)
1452 if (hasItem)
1453 {
1453 {
1454 const btn = document.createElement('button');
1454 const requiredStatRaw = injury.stat_required_to_cure ?? injury.statRequiredToCure ?? null;
1455 btn.className = 'primary';
1455 const requiredStat = Number(requiredStatRaw);
1456 btn.textContent = `Cure using ${required}`;
1456 const hasRequiredStat = Number.isFinite(requiredStat);
1457 btn.addEventListener('click', async () =>
1457
1458 let hasDoctor = true;
1459 if (hasRequiredStat)
1458 {
1460 {
1459 let dataText = "Request failed altogether";
1461 const others = (this.currentTribe?.kobolds || []).filter(k => k.id !== kobold.id);
1460 try
1462 const bestOther = others.reduce((best, k) =>
1461 {
1463 {
1462 const resp = await fetch('api/game/cureInjury', {
1464 const med = Number(k.stats?.medicine || 0);
1463 method: 'POST',
1465 if (med >= requiredStat && (!best || med > Number(best.stats?.medicine || 0)))
1464 headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser?.token}` },
1465 body: JSON.stringify({ userId: this.currentUser.email, saveId: this.currentSave.id, koboldId: kobold.id, injuryId: injury.id, itemId: required })
1466 });
1467 dataText = await resp.text();
1468 const data = JSON.parse(dataText);
1469 if (data.success)
1470 {
1471 this.hideKoboldModal();
1472 typeText(document.getElementById('eventStory'), data.story);
1473 this.showEventModal();
1474 // Refresh game state
1475 await this.loadGame(this.currentSave.id);
1476 // reopen kobold modal after refresh
1477 const refreshed = this.currentTribe.kobolds.find(k => k.id === kobold.id);
1478
1479 } else
1480 {
1466 {
1481 alert(data.error || 'Failed to cure injury');
1467 return k;
1482 console.error('Cure injury error:', data);
1483 }
1468 }
1484 } catch (err)
1469 return best;
1470 }, null);
1471
1472 if (bestOther)
1485 {
1473 {
1486 console.error(err);
1474 hasDoctor = true;
1487 alert('Failed to cure injury');
1475 } else
1488 console.error('Response data:', dataText);
1476 {
1477 const selfMed = Number(kobold.stats?.medicine || 0);
1478 hasDoctor = selfMed >= requiredStat;
1489 }
1479 }
1490 });
1480 }
1491 actions.appendChild(btn);
1481
1482 if (hasRequiredStat && !hasDoctor)
1483 {
1484 const label = this.getStatLabel(requiredStat);
1485 const note = document.createElement('div');
1486 note.style.color = '#999';
1487 note.textContent = `Requires ${label} doctor`;
1488 actions.appendChild(note);
1489 } else
1490 {
1491 const btn = document.createElement('button');
1492 btn.className = 'primary';
1493 btn.textContent = `Cure using ${required}`;
1494 btn.addEventListener('click', async () =>
1495 {
1496 let dataText = "Request failed altogether";
1497 try
1498 {
1499 const resp = await fetch('api/game/cureInjury', {
1500 method: 'POST',
1501 headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser?.token}` },
1502 body: JSON.stringify({ userId: this.currentUser.email, saveId: this.currentSave.id, koboldId: kobold.id, injuryId: injury.id, itemId: required })
1503 });
1504 dataText = await resp.text();
1505 const data = JSON.parse(dataText);
1506 if (data.success)
1507 {
1508 this.hideKoboldModal();
1509 typeText(document.getElementById('eventStory'), data.story);
1510 this.showEventModal();
1511 // Refresh game state
1512 await this.loadGame(this.currentSave.id);
1513 // reopen kobold modal after refresh
1514 const refreshed = this.currentTribe.kobolds.find(k => k.id === kobold.id);
1515
1516 } else
1517 {
1518 alert(data.error || 'Failed to cure injury');
1519 console.error('Cure injury error:', data);
1520 }
1521 } catch (err)
1522 {
1523 console.error(err);
1524 alert('Failed to cure injury');
1525 console.error('Response data:', dataText);
1526 }
1527 });
1528 actions.appendChild(btn);
1529 }
1492 } else
1530 } else
1493 {
1531 {
1494 const note = document.createElement('div');
1532 const note = document.createElement('div');
web/src/Classes/Injury.php +14 −0
@@ -20,4 +20,18 @@ class Injury
20 20 {
21 21 return self::$cureMap[$category] ?? null;
22 22 }
23
24 public static function getStatRequiredToCure($severity)
25 {
26 if ($severity === null) {
27 return null;
28 }
29
30 $sev = (int) $severity;
31 if ($sev <= 0) {
32 return 0;
33 }
34
35 return $severity * 20;
36 }
23 37 }
@@ -20,4 +20,18 @@ class Injury
20 {
20 {
21 return self::$cureMap[$category] ?? null;
21 return self::$cureMap[$category] ?? null;
22 }
22 }
23
24 public static function getStatRequiredToCure($severity)
25 {
26 if ($severity === null) {
27 return null;
28 }
29
30 $sev = (int) $severity;
31 if ($sev <= 0) {
32 return 0;
33 }
34
35 return $severity * 20;
36 }
23}
37}
web/src/api/game.php +50 −5
@@ -87,7 +87,7 @@ if ($method === 'POST') {
87 87
88 88 // find kobold
89 89 $target = null;
90 foreach ($tribe->getKobolds(true) as $k) {
90 foreach ($tribe->getKobolds(false) as $k) {
91 91 if ($k->getId() === $koboldId) {
92 92 $target = $k;
93 93 break;
@@ -124,6 +124,36 @@ if ($method === 'POST') {
124 124 throw new \Exception('Provided item does not match required medicine');
125 125 }
126 126
127 $requiredStat = Injury::getStatRequiredToCure($injury['severity'] ?? null);
128 $doctor = null;
129 if ($requiredStat !== null) {
130 $bestOther = null;
131 $bestOtherMed = -1;
132 foreach ($tribe->getKobolds(false) as $k) {
133 if ($k->getId() === $koboldId) {
134 continue;
135 }
136 $med = (int) ($k->getStat('medicine') ?? 0);
137 if ($med >= $requiredStat && $med > $bestOtherMed) {
138 $bestOther = $k;
139 $bestOtherMed = $med;
140 }
141 }
142
143 if ($bestOther) {
144 $doctor = $bestOther;
145 } else {
146 $selfMed = (int) ($target->getStat('medicine') ?? 0);
147 if ($selfMed >= $requiredStat) {
148 $doctor = $target;
149 }
150 }
151
152 if (!$doctor) {
153 throw new \Exception('No kobold has sufficient medicine skill to treat this injury');
154 }
155 }
156
127 157 // check kobold has the item
128 158 $has = false;
129 159 $itemName = $itemId;
@@ -162,7 +192,12 @@ if ($method === 'POST') {
162 192 $koboldName = $target->getName();
163 193 $injuryName = $injury['injury_type'] ?? ($injury['injuryType'] ?? 'injury');
164 194
165 $story = "{$koboldName} used the {$itemName} to cure the {$injuryName}.";
195 $doctorName = $doctor ? $doctor->getName() : $koboldName;
196 if ($doctor && $doctor->getId() !== $target->getId()) {
197 $story = "{$doctorName} treated {$koboldName}'s {$injuryName} using the {$itemName}.";
198 } else {
199 $story = "{$doctorName} used the {$itemName} to cure the {$injuryName}.";
200 }
166 201
167 202 echo json_encode(['success' => true, 'story' => $story]);
168 203 } catch (\Exception $e) {
@@ -201,7 +236,7 @@ if ($method === 'POST') {
201 236
202 237 // find kobold
203 238 $target = null;
204 foreach ($tribe->getKobolds(true) as $k) {
239 foreach ($tribe->getKobolds(false) as $k) {
205 240 if ($k->getId() === $koboldId) {
206 241 $target = $k;
207 242 break;
@@ -258,7 +293,7 @@ if ($method === 'POST') {
258 293
259 294 // find kobold
260 295 $target = null;
261 foreach ($tribe->getKobolds(true) as $k) {
296 foreach ($tribe->getKobolds(false) as $k) {
262 297 if ($k->getId() === $koboldId) {
263 298 $target = $k;
264 299 break;
@@ -690,7 +725,17 @@ function getKobolds($tribe, $tribeMembers): array
690 725 if (class_exists('Injury')) {
691 726 $req = Injury::getCureForCategory($inj['category'] ?? null);
692 727 }
693 $inj['required_medicine'] = $req;
728 if ($req !== null) {
729 $inj['required_medicine'] = $req;
730
731 $category = $inj['category'] ?? null;
732 if ($category !== 'malnourished' && $category !== 'old-age') {
733 $statReq = Injury::getStatRequiredToCure($inj['severity'] ?? null);
734 if ($statReq !== null) {
735 $inj['stat_required_to_cure'] = $statReq;
736 }
737 }
738 }
694 739 }
695 740
696 741 $kobolds[] = [
@@ -87,7 +87,7 @@ if ($method === 'POST') {
87
87
88 // find kobold
88 // find kobold
89 $target = null;
89 $target = null;
90 foreach ($tribe->getKobolds(true) as $k) {
90 foreach ($tribe->getKobolds(false) as $k) {
91 if ($k->getId() === $koboldId) {
91 if ($k->getId() === $koboldId) {
92 $target = $k;
92 $target = $k;
93 break;
93 break;
@@ -124,6 +124,36 @@ if ($method === 'POST') {
124 throw new \Exception('Provided item does not match required medicine');
124 throw new \Exception('Provided item does not match required medicine');
125 }
125 }
126
126
127 $requiredStat = Injury::getStatRequiredToCure($injury['severity'] ?? null);
128 $doctor = null;
129 if ($requiredStat !== null) {
130 $bestOther = null;
131 $bestOtherMed = -1;
132 foreach ($tribe->getKobolds(false) as $k) {
133 if ($k->getId() === $koboldId) {
134 continue;
135 }
136 $med = (int) ($k->getStat('medicine') ?? 0);
137 if ($med >= $requiredStat && $med > $bestOtherMed) {
138 $bestOther = $k;
139 $bestOtherMed = $med;
140 }
141 }
142
143 if ($bestOther) {
144 $doctor = $bestOther;
145 } else {
146 $selfMed = (int) ($target->getStat('medicine') ?? 0);
147 if ($selfMed >= $requiredStat) {
148 $doctor = $target;
149 }
150 }
151
152 if (!$doctor) {
153 throw new \Exception('No kobold has sufficient medicine skill to treat this injury');
154 }
155 }
156
127 // check kobold has the item
157 // check kobold has the item
128 $has = false;
158 $has = false;
129 $itemName = $itemId;
159 $itemName = $itemId;
@@ -162,7 +192,12 @@ if ($method === 'POST') {
162 $koboldName = $target->getName();
192 $koboldName = $target->getName();
163 $injuryName = $injury['injury_type'] ?? ($injury['injuryType'] ?? 'injury');
193 $injuryName = $injury['injury_type'] ?? ($injury['injuryType'] ?? 'injury');
164
194
165 $story = "{$koboldName} used the {$itemName} to cure the {$injuryName}.";
195 $doctorName = $doctor ? $doctor->getName() : $koboldName;
196 if ($doctor && $doctor->getId() !== $target->getId()) {
197 $story = "{$doctorName} treated {$koboldName}'s {$injuryName} using the {$itemName}.";
198 } else {
199 $story = "{$doctorName} used the {$itemName} to cure the {$injuryName}.";
200 }
166
201
167 echo json_encode(['success' => true, 'story' => $story]);
202 echo json_encode(['success' => true, 'story' => $story]);
168 } catch (\Exception $e) {
203 } catch (\Exception $e) {
@@ -201,7 +236,7 @@ if ($method === 'POST') {
201
236
202 // find kobold
237 // find kobold
203 $target = null;
238 $target = null;
204 foreach ($tribe->getKobolds(true) as $k) {
239 foreach ($tribe->getKobolds(false) as $k) {
205 if ($k->getId() === $koboldId) {
240 if ($k->getId() === $koboldId) {
206 $target = $k;
241 $target = $k;
207 break;
242 break;
@@ -258,7 +293,7 @@ if ($method === 'POST') {
258
293
259 // find kobold
294 // find kobold
260 $target = null;
295 $target = null;
261 foreach ($tribe->getKobolds(true) as $k) {
296 foreach ($tribe->getKobolds(false) as $k) {
262 if ($k->getId() === $koboldId) {
297 if ($k->getId() === $koboldId) {
263 $target = $k;
298 $target = $k;
264 break;
299 break;
@@ -690,7 +725,17 @@ function getKobolds($tribe, $tribeMembers): array
690 if (class_exists('Injury')) {
725 if (class_exists('Injury')) {
691 $req = Injury::getCureForCategory($inj['category'] ?? null);
726 $req = Injury::getCureForCategory($inj['category'] ?? null);
692 }
727 }
693 $inj['required_medicine'] = $req;
728 if ($req !== null) {
729 $inj['required_medicine'] = $req;
730
731 $category = $inj['category'] ?? null;
732 if ($category !== 'malnourished' && $category !== 'old-age') {
733 $statReq = Injury::getStatRequiredToCure($inj['severity'] ?? null);
734 if ($statReq !== null) {
735 $inj['stat_required_to_cure'] = $statReq;
736 }
737 }
738 }
694 }
739 }
695
740
696 $kobolds[] = [
741 $kobolds[] = [