test
@@ -1447,46 +1447,56 @@ class TribeGenApp
1447
1447
1448
1448
if (required)
1449
1449
{
1450
// check if kobold has item with this id
1451
const hasItem = (kobold.items || []).some(it => it.id === required);
1452
if (hasItem)
1453
{
1454
const requiredStatRaw = injury.stat_required_to_cure ?? injury.statRequiredToCure ?? null;
1455
const requiredStat = Number(requiredStatRaw);
1456
const hasRequiredStat = Number.isFinite(requiredStat);
1450
const requiredStatRaw = injury.stat_required_to_cure ?? injury.statRequiredToCure ?? null;
1451
const requiredStat = Number(requiredStatRaw);
1452
const hasRequiredStat = Number.isFinite(requiredStat);
1457
1453
1458
let hasDoctor = true;
1459
if (hasRequiredStat)
1454
let doctor = null;
1455
if (hasRequiredStat)
1456
{
1457
const others = (this.currentTribe?.kobolds || []).filter(k => k.id !== kobold.id);
1458
const bestOther = others.reduce((best, k) =>
1460
1459
{
1461
const others = (this.currentTribe?.kobolds || []).filter(k => k.id !== kobold.id);
1462
const bestOther = others.reduce((best, k) =>
1460
const med = Number(k.stats?.medicine || 0);
1461
if (med >= requiredStat && (!best || med > Number(best.stats?.medicine || 0)))
1463
1462
{
1464
const med = Number(k.stats?.medicine || 0);
1465
if (med >= requiredStat && (!best || med > Number(best.stats?.medicine || 0)))
1466
{
1467
return k;
1468
}
1469
return best;
1470
}, null);
1463
return k;
1464
}
1465
return best;
1466
}, null);
1471
1467
1472
if (bestOther)
1473
{
1474
hasDoctor = true;
1475
} else
1468
if (bestOther)
1469
{
1470
doctor = bestOther;
1471
} else
1472
{
1473
const selfMed = Number(kobold.stats?.medicine || 0);
1474
if (selfMed >= requiredStat)
1476
1475
{
1477
const selfMed = Number(kobold.stats?.medicine || 0);
1478
hasDoctor = selfMed >= requiredStat;
1476
doctor = kobold;
1479
1477
}
1480
1478
}
1479
} else
1480
{
1481
doctor = kobold;
1482
}
1481
1483
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
1484
if (hasRequiredStat && !doctor)
1485
{
1486
const label = this.getStatLabel(requiredStat);
1487
const note = document.createElement('div');
1488
note.style.color = '#999';
1489
note.textContent = `Requires ${label} doctor`;
1490
actions.appendChild(note);
1491
} else
1492
{
1493
const hasItemSelf = (kobold.items || []).some(it => it.id === required);
1494
const hasItemDoctor = doctor && doctor.id !== kobold.id
1495
? (doctor.items || []).some(it => it.id === required)
1496
: false;
1497
const hasItemStorage = (this.currentTribe?.storage || []).some(it => it.id === required);
1498
1499
if (hasItemSelf || hasItemDoctor || hasItemStorage)
1490
1500
{
1491
1501
const btn = document.createElement('button');
1492
1502
btn.className = 'primary';
@@ -1526,13 +1536,13 @@ class TribeGenApp
1526
1536
}
1527
1537
});
1528
1538
actions.appendChild(btn);
1539
} else
1540
{
1541
const note = document.createElement('div');
1542
note.style.color = '#999';
1543
note.textContent = `Needs: ${required}`;
1544
actions.appendChild(note);
1529
1545
}
1530
} else
1531
{
1532
const note = document.createElement('div');
1533
note.style.color = '#999';
1534
note.textContent = `Needs: ${required}`;
1535
actions.appendChild(note);
1536
1546
}
1537
1547
}
1538
1548
}
@@ -1447,46 +1447,56 @@ class TribeGenApp1447
1447
1448
if (required)1448
if (required)1449
{1449
{1450
// check if kobold has item with this id1450
const requiredStatRaw = injury.stat_required_to_cure ?? injury.statRequiredToCure ?? null;1451
const hasItem = (kobold.items || []).some(it => it.id === required);1451
const requiredStat = Number(requiredStatRaw);1452
if (hasItem)1452
const hasRequiredStat = Number.isFinite(requiredStat);1453
{1454
const requiredStatRaw = injury.stat_required_to_cure ?? injury.statRequiredToCure ?? null;1455
const requiredStat = Number(requiredStatRaw);1456
const hasRequiredStat = Number.isFinite(requiredStat);1457
1453
1458
let hasDoctor = true;1454
let doctor = null;1459
if (hasRequiredStat)1455
if (hasRequiredStat)1456
{1457
const others = (this.currentTribe?.kobolds || []).filter(k => k.id !== kobold.id);1458
const bestOther = others.reduce((best, k) =>1460
{1459
{1461
const others = (this.currentTribe?.kobolds || []).filter(k => k.id !== kobold.id);1460
const med = Number(k.stats?.medicine || 0);1462
const bestOther = others.reduce((best, k) =>1461
if (med >= requiredStat && (!best || med > Number(best.stats?.medicine || 0)))1463
{1462
{1464
const med = Number(k.stats?.medicine || 0);1463
return k;1465
if (med >= requiredStat && (!best || med > Number(best.stats?.medicine || 0)))1464
}1466
{1465
return best;1467
return k;1466
}, null);1468
}1469
return best;1470
}, null);1471
1467
1472
if (bestOther)1468
if (bestOther)1473
{1469
{1474
hasDoctor = true;1470
doctor = bestOther;1475
} else1471
} else1472
{1473
const selfMed = Number(kobold.stats?.medicine || 0);1474
if (selfMed >= requiredStat)1476
{1475
{1477
const selfMed = Number(kobold.stats?.medicine || 0);1476
doctor = kobold;1478
hasDoctor = selfMed >= requiredStat;1479
}1477
}1480
}1478
}1479
} else1480
{1481
doctor = kobold;1482
}1481
1483
1482
if (hasRequiredStat && !hasDoctor)1484
if (hasRequiredStat && !doctor)1483
{1485
{1484
const label = this.getStatLabel(requiredStat);1486
const label = this.getStatLabel(requiredStat);1485
const note = document.createElement('div');1487
const note = document.createElement('div');1486
note.style.color = '#999';1488
note.style.color = '#999';1487
note.textContent = `Requires ${label} doctor`;1489
note.textContent = `Requires ${label} doctor`;1488
actions.appendChild(note);1490
actions.appendChild(note);1489
} else1491
} else1492
{1493
const hasItemSelf = (kobold.items || []).some(it => it.id === required);1494
const hasItemDoctor = doctor && doctor.id !== kobold.id1495
? (doctor.items || []).some(it => it.id === required)1496
: false;1497
const hasItemStorage = (this.currentTribe?.storage || []).some(it => it.id === required);1498
1499
if (hasItemSelf || hasItemDoctor || hasItemStorage)1490
{1500
{1491
const btn = document.createElement('button');1501
const btn = document.createElement('button');1492
btn.className = 'primary';1502
btn.className = 'primary';@@ -1526,13 +1536,13 @@ class TribeGenApp1526
}1536
}1527
});1537
});1528
actions.appendChild(btn);1538
actions.appendChild(btn);1539
} else1540
{1541
const note = document.createElement('div');1542
note.style.color = '#999';1543
note.textContent = `Needs: ${required}`;1544
actions.appendChild(note);1529
}1545
}1530
} else1531
{1532
const note = document.createElement('div');1533
note.style.color = '#999';1534
note.textContent = `Needs: ${required}`;1535
actions.appendChild(note);1536
}1546
}1537
}1547
}1538
}1548
}@@ -152,40 +152,74 @@ if ($method === 'POST') {
152
152
if (!$doctor) {
153
153
throw new \Exception('No kobold has sufficient medicine skill to treat this injury');
154
154
}
155
} else {
156
$doctor = $target;
155
157
}
156
158
157
// check kobold has the item
158
$has = false;
159
159
$itemName = $itemId;
160
$newItems = [];
161
foreach ($target->getItems() ?? [] as $it) {
162
$iid = is_array($it) ? ($it['id'] ?? null) : (is_object($it) && method_exists($it, 'getId') ? $it->getId() : null);
163
if ($iid === $itemId && !$has) {
164
$has = true;
165
// capture name if present
166
if (is_array($it) && isset($it['name']))
167
$itemName = $it['name'];
168
elseif (is_object($it) && method_exists($it, 'getName'))
169
$itemName = $it->getName();
170
// consume (skip adding to newItems)
171
continue;
160
$itemSource = null;
161
$consumeItem = function ($items) use ($itemId, &$itemName) {
162
$found = false;
163
$newItems = [];
164
$picked = null;
165
foreach ($items ?? [] as $it) {
166
$iid = is_array($it) ? ($it['id'] ?? null) : (is_object($it) && method_exists($it, 'getId') ? $it->getId() : null);
167
if ($iid === $itemId && !$found) {
168
$found = true;
169
$picked = $it;
170
// capture name if present
171
if (is_array($it) && isset($it['name']))
172
$itemName = $it['name'];
173
elseif (is_object($it) && method_exists($it, 'getName'))
174
$itemName = $it->getName();
175
continue;
176
}
177
$newItems[] = $it;
178
}
179
return [$found, $newItems, $picked];
180
};
181
182
// try consume from target first
183
[$foundTarget, $newTargetItems, $pickedTarget] = $consumeItem($target->getItems() ?? []);
184
if ($foundTarget) {
185
$itemSource = 'target';
186
$reflection = new ReflectionClass($target);
187
if ($reflection->hasProperty('items')) {
188
$prop = $reflection->getProperty('items');
189
$prop->setValue($target, $newTargetItems);
190
}
191
} else if ($doctor && $doctor->getId() !== $target->getId()) {
192
[$foundDoctor, $newDoctorItems, $pickedDoctor] = $consumeItem($doctor->getItems() ?? []);
193
if ($foundDoctor) {
194
$itemSource = 'doctor';
195
$reflection = new ReflectionClass($doctor);
196
if ($reflection->hasProperty('items')) {
197
$prop = $reflection->getProperty('items');
198
$prop->setValue($doctor, $newDoctorItems);
199
}
200
}
201
}
202
203
if (!$itemSource) {
204
$storage = $tribe->getStorage() ?? [];
205
[$foundStorage, $newStorage, $pickedStorage] = $consumeItem($storage);
206
if ($foundStorage) {
207
$itemSource = 'storage';
208
$refT = new ReflectionClass($tribe);
209
if ($refT->hasProperty('storage')) {
210
$p = $refT->getProperty('storage');
211
$p->setValue($tribe, array_values($newStorage));
212
}
172
213
}
173
$newItems[] = $it;
174
214
}
175
215
176
if (!$has)
177
throw new \Exception('Kobold does not have the required item');
216
if (!$itemSource) {
217
throw new \Exception('No valid source has the required item');
218
}
178
219
179
220
// remove injury
180
221
$target->removeInjury($injuryId);
181
222
182
// set kobold items to newItems (consumed)
183
$reflection = new ReflectionClass($target);
184
if ($reflection->hasProperty('items')) {
185
$prop = $reflection->getProperty('items');
186
$prop->setValue($target, $newItems);
187
}
188
189
223
// save tribe
190
224
$tribe->save();
191
225
@@ -193,10 +227,20 @@ if ($method === 'POST') {
193
227
$injuryName = $injury['injury_type'] ?? ($injury['injuryType'] ?? 'injury');
194
228
195
229
$doctorName = $doctor ? $doctor->getName() : $koboldName;
230
$itemSourceText = 'from their own inventory';
231
if ($itemSource === 'target') {
232
if ($doctor && $doctor->getId() !== $target->getId()) {
233
$itemSourceText = "from {$koboldName}'s inventory";
234
}
235
} elseif ($itemSource === 'doctor') {
236
$itemSourceText = "from {$doctorName}'s inventory";
237
} elseif ($itemSource === 'storage') {
238
$itemSourceText = 'from tribe storage';
239
}
196
240
if ($doctor && $doctor->getId() !== $target->getId()) {
197
$story = "{$doctorName} treated {$koboldName}'s {$injuryName} using the {$itemName}.";
241
$story = "{$doctorName} treated {$koboldName}'s {$injuryName} using the {$itemName} {$itemSourceText}.";
198
242
} else {
199
$story = "{$doctorName} used the {$itemName} to cure the {$injuryName}.";
243
$story = "{$doctorName} used the {$itemName} {$itemSourceText} to cure the {$injuryName}.";
200
244
}
201
245
202
246
echo json_encode(['success' => true, 'story' => $story]);
@@ -152,40 +152,74 @@ if ($method === 'POST') {152
if (!$doctor) {152
if (!$doctor) {153
throw new \Exception('No kobold has sufficient medicine skill to treat this injury');153
throw new \Exception('No kobold has sufficient medicine skill to treat this injury');154
}154
}155
} else {156
$doctor = $target;155
}157
}156
158
157
// check kobold has the item158
$has = false;159
$itemName = $itemId;159
$itemName = $itemId;160
$newItems = [];160
$itemSource = null;161
foreach ($target->getItems() ?? [] as $it) {161
$consumeItem = function ($items) use ($itemId, &$itemName) {162
$iid = is_array($it) ? ($it['id'] ?? null) : (is_object($it) && method_exists($it, 'getId') ? $it->getId() : null);162
$found = false;163
if ($iid === $itemId && !$has) {163
$newItems = [];164
$has = true;164
$picked = null;165
// capture name if present165
foreach ($items ?? [] as $it) {166
if (is_array($it) && isset($it['name']))166
$iid = is_array($it) ? ($it['id'] ?? null) : (is_object($it) && method_exists($it, 'getId') ? $it->getId() : null);167
$itemName = $it['name'];167
if ($iid === $itemId && !$found) {168
elseif (is_object($it) && method_exists($it, 'getName'))168
$found = true;169
$itemName = $it->getName();169
$picked = $it;170
// consume (skip adding to newItems)170
// capture name if present171
continue;171
if (is_array($it) && isset($it['name']))172
$itemName = $it['name'];173
elseif (is_object($it) && method_exists($it, 'getName'))174
$itemName = $it->getName();175
continue;176
}177
$newItems[] = $it;178
}179
return [$found, $newItems, $picked];180
};181
182
// try consume from target first183
[$foundTarget, $newTargetItems, $pickedTarget] = $consumeItem($target->getItems() ?? []);184
if ($foundTarget) {185
$itemSource = 'target';186
$reflection = new ReflectionClass($target);187
if ($reflection->hasProperty('items')) {188
$prop = $reflection->getProperty('items');189
$prop->setValue($target, $newTargetItems);190
}191
} else if ($doctor && $doctor->getId() !== $target->getId()) {192
[$foundDoctor, $newDoctorItems, $pickedDoctor] = $consumeItem($doctor->getItems() ?? []);193
if ($foundDoctor) {194
$itemSource = 'doctor';195
$reflection = new ReflectionClass($doctor);196
if ($reflection->hasProperty('items')) {197
$prop = $reflection->getProperty('items');198
$prop->setValue($doctor, $newDoctorItems);199
}200
}201
}202
203
if (!$itemSource) {204
$storage = $tribe->getStorage() ?? [];205
[$foundStorage, $newStorage, $pickedStorage] = $consumeItem($storage);206
if ($foundStorage) {207
$itemSource = 'storage';208
$refT = new ReflectionClass($tribe);209
if ($refT->hasProperty('storage')) {210
$p = $refT->getProperty('storage');211
$p->setValue($tribe, array_values($newStorage));212
}172
}213
}173
$newItems[] = $it;174
}214
}175
215
176
if (!$has)216
if (!$itemSource) {177
throw new \Exception('Kobold does not have the required item');217
throw new \Exception('No valid source has the required item');218
}178
219
179
// remove injury220
// remove injury180
$target->removeInjury($injuryId);221
$target->removeInjury($injuryId);181
222
182
// set kobold items to newItems (consumed)183
$reflection = new ReflectionClass($target);184
if ($reflection->hasProperty('items')) {185
$prop = $reflection->getProperty('items');186
$prop->setValue($target, $newItems);187
}188
189
// save tribe223
// save tribe190
$tribe->save();224
$tribe->save();191
225
@@ -193,10 +227,20 @@ if ($method === 'POST') {193
$injuryName = $injury['injury_type'] ?? ($injury['injuryType'] ?? 'injury');227
$injuryName = $injury['injury_type'] ?? ($injury['injuryType'] ?? 'injury');194
228
195
$doctorName = $doctor ? $doctor->getName() : $koboldName;229
$doctorName = $doctor ? $doctor->getName() : $koboldName;230
$itemSourceText = 'from their own inventory';231
if ($itemSource === 'target') {232
if ($doctor && $doctor->getId() !== $target->getId()) {233
$itemSourceText = "from {$koboldName}'s inventory";234
}235
} elseif ($itemSource === 'doctor') {236
$itemSourceText = "from {$doctorName}'s inventory";237
} elseif ($itemSource === 'storage') {238
$itemSourceText = 'from tribe storage';239
}196
if ($doctor && $doctor->getId() !== $target->getId()) {240
if ($doctor && $doctor->getId() !== $target->getId()) {197
$story = "{$doctorName} treated {$koboldName}'s {$injuryName} using the {$itemName}.";241
$story = "{$doctorName} treated {$koboldName}'s {$injuryName} using the {$itemName} {$itemSourceText}.";198
} else {242
} else {199
$story = "{$doctorName} used the {$itemName} to cure the {$injuryName}.";243
$story = "{$doctorName} used the {$itemName} {$itemSourceText} to cure the {$injuryName}.";200
}244
}201
245
202
echo json_encode(['success' => true, 'story' => $story]);246
echo json_encode(['success' => true, 'story' => $story]);