fix events, item giving
@@ -43,6 +43,8 @@ if (strpos($path, '/api/') === 0) {
43
43
include __DIR__ . '/src/api/game.php';
44
44
} elseif ($apiPath === 'game/endMonth') {
45
45
include __DIR__ . '/src/api/game.php';
46
} elseif ($apiPath === 'game/giveItem') {
47
include __DIR__ . '/src/api/game.php';
46
48
} else {
47
49
http_response_code(404);
48
50
echo json_encode(['error' => 'Not found']);
@@ -43,6 +43,8 @@ if (strpos($path, '/api/') === 0) {43
include __DIR__ . '/src/api/game.php';43
include __DIR__ . '/src/api/game.php';44
} elseif ($apiPath === 'game/endMonth') {44
} elseif ($apiPath === 'game/endMonth') {45
include __DIR__ . '/src/api/game.php';45
include __DIR__ . '/src/api/game.php';46
} elseif ($apiPath === 'game/giveItem') {47
include __DIR__ . '/src/api/game.php';46
} else {48
} else {47
http_response_code(404);49
http_response_code(404);48
echo json_encode(['error' => 'Not found']);50
echo json_encode(['error' => 'Not found']);@@ -188,7 +188,8 @@ class TribeGenApp
188
188
headers: { 'Authorization': `Bearer ${this.currentUser.token}` }
189
189
});
190
190
191
const data = await response.json();
191
const dataText = await response.text()
192
const data = JSON.parse(dataText);
192
193
193
194
const savesList = document.getElementById('savesList');
194
195
savesList.innerHTML = '';
@@ -382,7 +383,8 @@ class TribeGenApp
382
383
})
383
384
});
384
385
385
const data = await response.json();
386
const dataText = await response.text()
387
const data = JSON.parse(dataText);
386
388
387
389
if (data.success)
388
390
{
@@ -404,7 +406,8 @@ class TribeGenApp
404
406
headers: { 'Authorization': `Bearer ${this.currentUser.token}` }
405
407
});
406
408
407
const data = await response.json();
409
const dataText = await response.text()
410
const data = JSON.parse(dataText);
408
411
409
412
if (data.success)
410
413
{
@@ -501,7 +504,8 @@ class TribeGenApp
501
504
this.participantMinAge = config.minAge;
502
505
503
506
// Helper: check if kobold is eligible
504
const isEligible = (kobold) => {
507
const isEligible = (kobold) =>
508
{
505
509
const ageCategory = (kobold.ageCategory || '').toLowerCase();
506
510
if (ageCategory === 'egg') return false; // eggs cannot participate
507
511
if (!kobold.isAlive && kobold.isDead) return false; // dead cannot participate (if available from backend)
@@ -595,7 +599,9 @@ class TribeGenApp
595
599
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, eventType: this.currentEventType, participants: selected })
596
600
});
597
601
598
const data = await resp.json();
602
const dataText = await resp.text()
603
const data = JSON.parse(dataText);
604
599
605
if (data.success)
600
606
{
601
607
typeText(document.getElementById('eventStory'), data.story);
@@ -753,7 +759,8 @@ class TribeGenApp
753
759
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, koboldId: koboldId, itemId: item.id })
754
760
});
755
761
756
const data = await resp.json();
762
const dataText = await resp.text()
763
const data = JSON.parse(dataText);
757
764
if (data.success)
758
765
{
759
766
typeText(document.getElementById('eventStory'), data.story);
@@ -900,7 +907,8 @@ class TribeGenApp
900
907
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser?.token}` },
901
908
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, koboldId: kobold.id, injuryId: injury.id, itemId: required })
902
909
});
903
const data = await resp.json();
910
const dataText = await resp.text()
911
const data = JSON.parse(dataText);
904
912
if (data.success)
905
913
{
906
914
typeText(document.getElementById('eventStory'), data.story);
@@ -988,7 +996,8 @@ class TribeGenApp
988
996
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, koboldId: kobold.id, itemId: item.id })
989
997
});
990
998
991
const data = await resp.json();
999
const dataText = await resp.text()
1000
const data = JSON.parse(dataText);
992
1001
if (data.success)
993
1002
{
994
1003
typeText(document.getElementById('eventStory'), data.story);
@@ -188,7 +188,8 @@ class TribeGenApp188
headers: { 'Authorization': `Bearer ${this.currentUser.token}` }188
headers: { 'Authorization': `Bearer ${this.currentUser.token}` }189
});189
});190
190
191
const data = await response.json();191
const dataText = await response.text()192
const data = JSON.parse(dataText);192
193
193
const savesList = document.getElementById('savesList');194
const savesList = document.getElementById('savesList');194
savesList.innerHTML = '';195
savesList.innerHTML = '';@@ -382,7 +383,8 @@ class TribeGenApp382
})383
})383
});384
});384
385
385
const data = await response.json();386
const dataText = await response.text()387
const data = JSON.parse(dataText);386
388
387
if (data.success)389
if (data.success)388
{390
{@@ -404,7 +406,8 @@ class TribeGenApp404
headers: { 'Authorization': `Bearer ${this.currentUser.token}` }406
headers: { 'Authorization': `Bearer ${this.currentUser.token}` }405
});407
});406
408
407
const data = await response.json();409
const dataText = await response.text()410
const data = JSON.parse(dataText);408
411
409
if (data.success)412
if (data.success)410
{413
{@@ -501,7 +504,8 @@ class TribeGenApp501
this.participantMinAge = config.minAge;504
this.participantMinAge = config.minAge;502
505
503
// Helper: check if kobold is eligible506
// Helper: check if kobold is eligible504
const isEligible = (kobold) => {507
const isEligible = (kobold) =>508
{505
const ageCategory = (kobold.ageCategory || '').toLowerCase();509
const ageCategory = (kobold.ageCategory || '').toLowerCase();506
if (ageCategory === 'egg') return false; // eggs cannot participate510
if (ageCategory === 'egg') return false; // eggs cannot participate507
if (!kobold.isAlive && kobold.isDead) return false; // dead cannot participate (if available from backend)511
if (!kobold.isAlive && kobold.isDead) return false; // dead cannot participate (if available from backend)@@ -595,7 +599,9 @@ class TribeGenApp595
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, eventType: this.currentEventType, participants: selected })599
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, eventType: this.currentEventType, participants: selected })596
});600
});597
601
598
const data = await resp.json();602
const dataText = await resp.text()603
const data = JSON.parse(dataText);604
599
if (data.success)605
if (data.success)600
{606
{601
typeText(document.getElementById('eventStory'), data.story);607
typeText(document.getElementById('eventStory'), data.story);@@ -753,7 +759,8 @@ class TribeGenApp753
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, koboldId: koboldId, itemId: item.id })759
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, koboldId: koboldId, itemId: item.id })754
});760
});755
761
756
const data = await resp.json();762
const dataText = await resp.text()763
const data = JSON.parse(dataText);757
if (data.success)764
if (data.success)758
{765
{759
typeText(document.getElementById('eventStory'), data.story);766
typeText(document.getElementById('eventStory'), data.story);@@ -900,7 +907,8 @@ class TribeGenApp900
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser?.token}` },907
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.currentUser?.token}` },901
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, koboldId: kobold.id, injuryId: injury.id, itemId: required })908
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, koboldId: kobold.id, injuryId: injury.id, itemId: required })902
});909
});903
const data = await resp.json();910
const dataText = await resp.text()911
const data = JSON.parse(dataText);904
if (data.success)912
if (data.success)905
{913
{906
typeText(document.getElementById('eventStory'), data.story);914
typeText(document.getElementById('eventStory'), data.story);@@ -988,7 +996,8 @@ class TribeGenApp988
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, koboldId: kobold.id, itemId: item.id })996
body: JSON.stringify({ userId: this.currentUser.id, saveId: this.currentSave.id, koboldId: kobold.id, itemId: item.id })989
});997
});990
998
991
const data = await resp.json();999
const dataText = await resp.text()1000
const data = JSON.parse(dataText);992
if (data.success)1001
if (data.success)993
{1002
{994
typeText(document.getElementById('eventStory'), data.story);1003
typeText(document.getElementById('eventStory'), data.story);@@ -430,7 +430,7 @@ class Kobold
430
430
return $roll <= $stat;
431
431
}
432
432
433
public function addInjury($injuryType, $category, $severity = 1)
433
public function addInjury($injuryType)
434
434
{
435
435
// If an injury file exists in assets, load it for full data
436
436
$injData = null;
@@ -438,26 +438,11 @@ class Kobold
438
438
$maybe = GameHelper::loadInjury($injuryType);
439
439
if ($maybe) {
440
440
$injData = $maybe;
441
// override severity and category if provided explicitly
442
$injData['severity'] = $severity ?? ($injData['severity'] ?? 1);
443
if ($category)
444
$injData['category'] = $category;
445
441
// ensure kobold id
446
442
$injData['kobold_id'] = $this->id;
447
443
$this->injuries[] = $injData;
448
444
return;
449
445
}
450
451
// Fallback: create a minimal injury array
452
$id = uniqid('inj_');
453
$this->injuries[] = [
454
'id' => $id,
455
'kobold_id' => $this->id,
456
'injury_type' => $injuryType,
457
'category' => $category,
458
'severity' => $severity,
459
'stat_modifications' => []
460
];
461
446
}
462
447
463
448
public function removeInjury($injuryId)
@@ -430,7 +430,7 @@ class Kobold430
return $roll <= $stat;430
return $roll <= $stat;431
}431
}432
432
433
public function addInjury($injuryType, $category, $severity = 1)433
public function addInjury($injuryType)434
{434
{435
// If an injury file exists in assets, load it for full data435
// If an injury file exists in assets, load it for full data436
$injData = null;436
$injData = null;@@ -438,26 +438,11 @@ class Kobold438
$maybe = GameHelper::loadInjury($injuryType);438
$maybe = GameHelper::loadInjury($injuryType);439
if ($maybe) {439
if ($maybe) {440
$injData = $maybe;440
$injData = $maybe;441
// override severity and category if provided explicitly442
$injData['severity'] = $severity ?? ($injData['severity'] ?? 1);443
if ($category)444
$injData['category'] = $category;445
// ensure kobold id441
// ensure kobold id446
$injData['kobold_id'] = $this->id;442
$injData['kobold_id'] = $this->id;447
$this->injuries[] = $injData;443
$this->injuries[] = $injData;448
return;444
return;449
}445
}450
451
// Fallback: create a minimal injury array452
$id = uniqid('inj_');453
$this->injuries[] = [454
'id' => $id,455
'kobold_id' => $this->id,456
'injury_type' => $injuryType,457
'category' => $category,458
'severity' => $severity,459
'stat_modifications' => []460
];461
}446
}462
447
463
public function removeInjury($injuryId)448
public function removeInjury($injuryId)@@ -58,47 +58,17 @@ class ExplorationEvent extends Event
58
58
}
59
59
60
60
// Chance for injury (large chance for testing) - someone trips on loose stone
61
$injuryChance = rand(1, 100) <= 70; // higher for testing
61
$injuryChance = rand(1, 100) <= 99; // higher for testing
62
62
if ($injuryChance && count($explorers) > 0) {
63
// Vitality affects injury chance - higher vitality = less likely to be injured
64
$avgVitality = array_sum(array_map(function ($k) {
65
return $k->getStat('vitality');
66
}, $explorers)) / count($explorers);
67
$injuryProbability = (100 - $avgVitality) / 100;
68
69
if (rand(1, 100) / 100 <= $injuryProbability) {
70
// Select a random explorer to get injured
71
$injuredKobold = $explorers[array_rand($explorers)];
72
73
$this->story .= "Disaster! " . $injuredKobold->getName() . " tripped over a loose stone!\n";
74
75
// Try to load a predefined injury (brokenLeg) from assets
76
$inj = GameHelper::loadInjury('brokenLeg');
77
if ($inj) {
78
$inj['kobold_id'] = $injuredKobold->getId();
79
$inj['id'] = uniqid('inj_');
80
$injuries = $injuredKobold->getInjuries();
81
$injuries[] = $inj;
82
$injuredKobold->setInjuries($injuries);
83
84
$this->story .= $injuredKobold->getName() . " suffered a " . ($inj['injury_type'] ?? 'broken leg') . " (severity: " . ($inj['severity'] ?? 1) . "/10).\n";
85
} else {
86
// Fallback to simple injury
87
$newInjury = [
88
'id' => uniqid('inj_'),
89
'kobold_id' => $injuredKobold->getId(),
90
'injury_type' => 'broken_leg',
91
'category' => 'fracture',
92
'severity' => rand(3, 7),
93
'created_at' => date('Y-m-d H:i:s')
94
];
95
$injuries = $injuredKobold->getInjuries();
96
$injuries[] = $newInjury;
97
$injuredKobold->setInjuries($injuries);
98
99
$this->story .= $injuredKobold->getName() . " suffered a broken leg (severity: " . $newInjury['severity'] . "/10).\n";
100
}
101
}
63
64
// Select a random explorer to get injured
65
$injuredKobold = $explorers[array_rand($explorers)];
66
67
$this->story .= "Disaster! " . $injuredKobold->getName() . " tripped over a loose stone!\n";
68
69
$injuredKobold->addInjury("brokenLeg");
70
71
$this->story .= $injuredKobold->getName() . " broke a leg!\n";
102
72
}
103
73
104
74
// For testing: add a 'bone-setting' item to tribe storage so player can cure fractures
@@ -58,47 +58,17 @@ class ExplorationEvent extends Event58
}58
}59
59
60
// Chance for injury (large chance for testing) - someone trips on loose stone60
// Chance for injury (large chance for testing) - someone trips on loose stone61
$injuryChance = rand(1, 100) <= 70; // higher for testing61
$injuryChance = rand(1, 100) <= 99; // higher for testing62
if ($injuryChance && count($explorers) > 0) {62
if ($injuryChance && count($explorers) > 0) {63
// Vitality affects injury chance - higher vitality = less likely to be injured63
64
$avgVitality = array_sum(array_map(function ($k) {64
// Select a random explorer to get injured65
return $k->getStat('vitality');65
$injuredKobold = $explorers[array_rand($explorers)];66
}, $explorers)) / count($explorers);66
67
$injuryProbability = (100 - $avgVitality) / 100;67
$this->story .= "Disaster! " . $injuredKobold->getName() . " tripped over a loose stone!\n";68
68
69
if (rand(1, 100) / 100 <= $injuryProbability) {69
$injuredKobold->addInjury("brokenLeg");70
// Select a random explorer to get injured70
71
$injuredKobold = $explorers[array_rand($explorers)];71
$this->story .= $injuredKobold->getName() . " broke a leg!\n";72
73
$this->story .= "Disaster! " . $injuredKobold->getName() . " tripped over a loose stone!\n";74
75
// Try to load a predefined injury (brokenLeg) from assets76
$inj = GameHelper::loadInjury('brokenLeg');77
if ($inj) {78
$inj['kobold_id'] = $injuredKobold->getId();79
$inj['id'] = uniqid('inj_');80
$injuries = $injuredKobold->getInjuries();81
$injuries[] = $inj;82
$injuredKobold->setInjuries($injuries);83
84
$this->story .= $injuredKobold->getName() . " suffered a " . ($inj['injury_type'] ?? 'broken leg') . " (severity: " . ($inj['severity'] ?? 1) . "/10).\n";85
} else {86
// Fallback to simple injury87
$newInjury = [88
'id' => uniqid('inj_'),89
'kobold_id' => $injuredKobold->getId(),90
'injury_type' => 'broken_leg',91
'category' => 'fracture',92
'severity' => rand(3, 7),93
'created_at' => date('Y-m-d H:i:s')94
];95
$injuries = $injuredKobold->getInjuries();96
$injuries[] = $newInjury;97
$injuredKobold->setInjuries($injuries);98
99
$this->story .= $injuredKobold->getName() . " suffered a broken leg (severity: " . $newInjury['severity'] . "/10).\n";100
}101
}102
}72
}103
73
104
// For testing: add a 'bone-setting' item to tribe storage so player can cure fractures74
// For testing: add a 'bone-setting' item to tribe storage so player can cure fractures@@ -254,7 +254,7 @@ class GameHelper
254
254
* @param string $injuryId filename without extension (e.g. 'brokenLeg')
255
255
* @return array|null normalized injury array or null if not found
256
256
*/
257
public static function loadInjury($injuryId)
257
public static function loadInjury(string $injuryId)
258
258
{
259
259
$injuryPath = __DIR__ . '/../../assets/injuries/' . $injuryId . '.json';
260
260
@@ -269,7 +269,7 @@ class GameHelper
269
269
270
270
// Normalize to snake_case keys matching internal storage
271
271
$inj = [];
272
$inj['id'] = uniqid('inj_');
272
$inj['id'] = $injuryId;
273
273
$inj['injury_type'] = $data['injuryType'] ?? ($data['injury_type'] ?? $injuryId);
274
274
$inj['description'] = $data['description'] ?? '';
275
275
$inj['category'] = $data['category'] ?? null;
@@ -254,7 +254,7 @@ class GameHelper254
* @param string $injuryId filename without extension (e.g. 'brokenLeg')254
* @param string $injuryId filename without extension (e.g. 'brokenLeg')255
* @return array|null normalized injury array or null if not found255
* @return array|null normalized injury array or null if not found256
*/256
*/257
public static function loadInjury($injuryId)257
public static function loadInjury(string $injuryId)258
{258
{259
$injuryPath = __DIR__ . '/../../assets/injuries/' . $injuryId . '.json';259
$injuryPath = __DIR__ . '/../../assets/injuries/' . $injuryId . '.json';260
260
@@ -269,7 +269,7 @@ class GameHelper269
269
270
// Normalize to snake_case keys matching internal storage270
// Normalize to snake_case keys matching internal storage271
$inj = [];271
$inj = [];272
$inj['id'] = uniqid('inj_');272
$inj['id'] = $injuryId;273
$inj['injury_type'] = $data['injuryType'] ?? ($data['injury_type'] ?? $injuryId);273
$inj['injury_type'] = $data['injuryType'] ?? ($data['injury_type'] ?? $injuryId);274
$inj['description'] = $data['description'] ?? '';274
$inj['description'] = $data['description'] ?? '';275
$inj['category'] = $data['category'] ?? null;275
$inj['category'] = $data['category'] ?? null;