<?phpnamespace Tests;use App\Revisited\Land\Game;use App\Revisited\Sim\Body;use App\Revisited\Sim\Environment;use App\Revisited\Sim\Inventory;use PHPUnit\Framework\TestCase;/** Rob's long-term fitness (Sim\Body): endurance and force build up with regular effort and fade when idle. */final class RevisitedFitnessTest extends TestCase{ private static function env(float $effort, float $strain = 0.0, bool $sleeping = false): Environment { $e = new Environment(); $e->effort = $effort; $e->strain = $strain; $e->sleeping = $sleeping; $e->sheltered = true; return $e; } /** Fed, rested, healthy again (through a save: the fitness must survive it). */ private static function refresh(Body $b): Body { return Body::fromArray(array_merge($b->toArray(), ['hunger' => 90.0, 'thirst' => 90.0, 'fatigue' => 90.0, 'health' => 100.0])); } /** One day: $run minutes of running, $carry minutes with a heavy load, the rest walking a little, 8 h of sleep. */ private static function day(Body $b, float $run, float $carry = 0.0): Body { $b->tick($run, self::env(1.0)); $b->tick($carry, self::env(0.3, 1.0)); $b->tick(960 - $run - $carry, self::env(0.05)); $b->tick(480, self::env(0.0, 0.0, true)); return self::refresh($b); } public function testRunningEveryDayRaisesTheEnduranceSlowlyOverDays(): void { $b = new Body(); $b = self::day($b, 90); $after1 = $b->fitness()['endurance']; self::assertGreaterThan(0.3, $after1, 'a day of running gives a little'); self::assertLessThan(2.0, $after1, 'but only a little: it is a long-term stat'); for ($d = 0; $d < 9; $d++) { $b = self::day($b, 90); } $f = $b->fitness(); self::assertGreaterThan(5.0, $f['endurance'], 'ten days of regular running: more than +5 %'); self::assertLessThan(15.0, $f['endurance']); self::assertGreaterThan(3.0, $f['enduranceTrend'], 'the 7-day trend goes up'); self::assertLessThan(0.0, $f['force'], 'nothing heavy carried: the force fades a little'); } public function testMoreRunningInOneDayGivesLessAndLess(): void { $short = self::day(new Body(), 30)->fitness()['endurance']; $long = self::day(new Body(), 300)->fitness()['endurance']; self::assertGreaterThan($short, $long); self::assertLessThan($short * 3.0, $long, 'ten times the running is not ten times the gain (daily cap)'); self::assertLessThanOrEqual(Body::FIT_GAIN['endurance'], $long); } public function testASedentaryRobLosesEnduranceAndForce(): void { $b = new Body(); $b->setFitness(20.0, 10.0); for ($d = 0; $d < 10; $d++) { $b = self::day($b, 0); } $f = $b->fitness(); self::assertLessThan(14.0, $f['endurance'], 'ten idle days: several points lost'); self::assertLessThan(5.0, $f['force']); self::assertLessThan(-3.0, $f['enduranceTrend'], 'the trend goes down'); // from 0, idle: toward the floor, slowly $c = new Body(); for ($d = 0; $d < 10; $d++) { $c = self::day($c, 0); } self::assertLessThan(-1.0, $c->fitness()['endurance']); self::assertGreaterThan(Body::FIT_MIN, $c->fitness()['endurance']); } public function testCarryingHeavyLoadsChoppingAndFightingBuildForce(): void { $b = new Body(); for ($d = 0; $d < 8; $d++) { $b = self::day($b, 0, 120); $b->train('force', 24.0); // two trees chopped } self::assertGreaterThan(4.0, $b->fitness()['force']); } public function testHungerAndWoundsSlowTheGains(): void { $fed = new Body(); $fed->tick(90, self::env(1.0)); $hungry = Body::fromArray(array_merge((new Body())->toArray(), ['hunger' => 10.0])); $hungry->tick(90, self::env(1.0)); self::assertLessThan($fed->fitness()['endurance'] * 0.5, $hungry->fitness()['endurance']); $hurt = new Body(); $hurt->wound('rleg', 'fracture', 0.8); $hurt->tick(90, self::env(1.0)); self::assertLessThan($fed->fitness()['endurance'], $hurt->fitness()['endurance']); } public function testEnduranceSavesBreathAndForceCarriesAndHitsHarder(): void { $fit = new Body(); $fit->setFitness(40.0, 40.0); $soft = new Body(); $soft->setFitness(-20.0, -20.0); $fit->tick(30, self::env(1.0)); $soft->tick(30, self::env(1.0)); self::assertGreaterThan($soft->vitals()['fatigue'] + 1.0, $fit->vitals()['fatigue'], 'running tires the fit body less'); self::assertLessThan($soft->sprintThreshold(), $fit->sprintThreshold(), 'the fit body sprints longer'); self::assertGreaterThan($soft->recoveryFactor(), $fit->recoveryFactor(), 'and gets its breath back faster'); // climbing: the same effort costs less $a = new Body(); $a->setFitness(40.0, 40.0); $z = new Body(); $a->exert(20.0); $z->exert(20.0); self::assertGreaterThan($z->vitals()['fatigue'], $a->vitals()['fatigue']); // force: the bag holds more, the blows hit harder self::assertEqualsWithDelta(1.2, $fit->carryFactor(), 0.002); self::assertEqualsWithDelta(0.9, $soft->carryFactor(), 0.002); $inv = new Inventory(); $inv->carry = $fit->carryFactor(); self::assertEqualsWithDelta(Inventory::DEFAULT_CAPACITY * 1.2, $inv->capacity(), 0.05); self::assertGreaterThan(1.2, $fit->strengthFactor()); self::assertLessThan(1.0, $soft->strengthFactor()); } public function testEachFiveStepIsToldOnceAndGoingDownToo(): void { $b = new Body(); $b->setFitness(4.8, 0.0); $b->train('endurance', 60.0); $news = $b->fitnessNews(); self::assertContains('Tu te sens plus endurant : +5 %', $news); self::assertSame([], $b->fitnessNews(), 'told once'); $b->train('endurance', 1.0); self::assertSame([], $b->fitnessNews(), 'no flicker on the line'); // the game says it in the log and as a toast $g = Game::newGame(7); $g->body->setFitness(0.0, 4.95); $g->body->train('force', 30.0); $g->passTime(1); self::assertContains('Tu te sens plus fort : +5 %', $g->takeNotices()); self::assertContains('Tu te sens plus fort : +5 %', $g->messages()); // idle days: the endurance goes under 0, said once $i = new Body(); $news = []; for ($d = 0; $d < 6; $d++) { $i->tick(1440, self::env(0.05)); $news = array_merge($news, $i->fitnessNews()); $i = self::refresh($i); } $down = array_values(array_filter($news, static fn (string $n) => str_starts_with($n, 'Ton endurance baisse'))); self::assertSame(['Ton endurance baisse : -1 %'], $down); } public function testFitnessSurvivesASaveAndOldSavesStartAtZero(): void { $b = self::day(new Body(), 120, 60); $f = $b->fitness(); $c = Body::fromArray(json_decode((string) json_encode($b->toArray()), true)); self::assertEqualsWithDelta($f['endurance'], $c->fitness()['endurance'], 0.01); self::assertEqualsWithDelta($f['force'], $c->fitness()['force'], 0.01); self::assertEqualsWithDelta($f['enduranceTrend'], $c->fitness()['enduranceTrend'], 0.01); $old = $b->toArray(); unset($old['fit']); $o = Body::fromArray($old); self::assertSame(0.0, $o->fitness()['endurance']); self::assertSame(0.0, $o->fitness()['force']); // a whole game round trip $g = Game::newGame(7); $g->body->setFitness(12.0, -4.0); $h = Game::fromArray(json_decode((string) json_encode($g->toArray()), true)); self::assertEqualsWithDelta(12.0, $h->body->fitness()['endurance'], 0.01); self::assertEqualsWithDelta(-4.0, $h->body->fitness()['force'], 0.01); }}