UK
games/rob-revisited / tests / RevisitedChestSceneTest.php · commit f9a1307
<?phpnamespace Tests;use App\Revisited\Land\Chests;use App\Revisited\Land\Game;use App\Revisited\Sim\Crafting;use App\Revisited\Sim\Items;use App\Revisited\Ui\ChestView;use App\Revisited\Ui\HudData;use App\Revisited\Ui\SaveScreen;use App\Revisited\Ui\Screens;use App\Revisited\Ui\UiInput;use App\Revisited\Ui\Wheel;use App\Scene\RevisitedScene;use JumPHP\Testing\GameTestCase;/** The chest's transfer screen played through the scene, and the wheel reaching both ends of every list. */final class RevisitedChestSceneTest extends GameTestCase{    private function begin(array $more = []): Game    {        $this->game->start(RevisitedScene::class, ['seed' => 7, 'new' => true, 'help' => false] + $more);        for ($k = 0; $k < 400 && $this->game->peek('mode') !== ($more['menu'] ?? 'play'); $k++) {            $this->game->run(1);        }        return $this->game->peek('game');    }    public function testTheActionOpensTheChestAndTheKeysMoveItemsBothWays(): void    {        $g = $this->begin(['chest' => 'chest_wood']);        self::assertSame('play', $this->game->peek('mode'));        $t = Chests::inZone($g)[0];        self::assertSame('chest', $g->actions()[0]['id'] ?? null, 'the chest is the action in front of Rob');        $this->game->press('rv_act', 2);        self::assertSame('chest', $this->game->peek('mode'), 'the transfer screen is open');        self::assertSame(0, $t->left, 'its lid is up');        $box = Chests::store($t)->all();        $bag = array_sum($g->inv->all());        $this->game->press('confirm', 2);           // one of the first movable item of the bag goes in        self::assertSame($bag - 1, array_sum($g->inv->all()));        self::assertSame(array_sum($box) + 1, array_sum(Chests::store($t)->all()));        $this->game->press('right', 1);        $this->game->press('rv_drop', 2);           // X: the whole stack of the chest's first item comes out        self::assertLessThan(array_sum($box) + 1, array_sum(Chests::store($t)->all()));        $this->game->press('rv_next', 2);           // PgSuiv: everything the bag can carry        self::assertSame([], Chests::store($t)->all());        $this->game->press('rv_prev', 2);           // PgPréc: everything but what Rob wears or holds        self::assertNotEmpty(Chests::store($t)->all());        foreach (array_keys($g->inv->all()) as $id) {            self::assertSame(0, Chests::movable($g->inv, (string) $id), "$id held or worn, or did not fit");        }        $this->game->press('rv_close', 2);        self::assertSame('play', $this->game->peek('mode'));        self::assertSame(1, $t->left, 'the lid comes down');    }    public function testOnAPhoneTheTabsAndTheRowButtonsMoveItems(): void    {        $g = $this->begin(['chest' => 'basket', 'menu' => 'chest', 'touch' => true, 'uiscale' => 0.54]);        self::assertSame('chest', $this->game->peek('mode'));        /** @var ChestView $v */        $v = $this->game->peek('chestView');        self::assertTrue($v->touch);        $t = Chests::opened($g);        self::assertNotNull($t);        $n = array_sum(Chests::store($t)->all());        $this->game->press('rv_prev', 2);        self::assertGreaterThan($n, array_sum(Chests::store($t)->all()));        self::assertLessThanOrEqual(12.0, Chests::store($t)->weight());    }    /** Many wheel events of mixed sizes: one way to the very end, back to the very top. */    private static function wheel(callable $send, int $count, float $dir): void    {        $sizes = [0.1, 0.25, 1.0, 0.4, 3.0, 0.05, 2.0, 0.7];        for ($k = 0; $k < $count; $k++) {            $send($dir * $sizes[$k % count($sizes)]);        }    }    public function testTheWheelReachesTheFirstAndTheLastRowOfEveryList(): void    {        $g = $this->begin();        foreach (Items::all() as $id) {            $g->inv->add($id);        }        $g->inv->setCapacity(9999.0);        foreach (Items::all() as $id) {            $g->inv->add($id);        }        /** @var Screens $s */        $s = $this->game->peek('screens');        $d = new HudData();        $ui = static function (float $wheel): UiInput {            $u = new UiInput();            $u->wheel = $wheel;            return $u;        };        // the inventory grid        $s->open('inventory');        $rows = (int) ceil(count($g->inv->all()) / 6);        self::assertGreaterThan(6, $rows);        self::wheel(fn (float $w) => $s->update($ui($w), $g, $d, 0.016), 120, -1.0);        self::assertSame($rows - 6, $s->invTop, 'the last row of the bag');        self::wheel(fn (float $w) => $s->update($ui($w), $g, $d, 0.016), 120, 1.0);        self::assertSame(0, $s->invTop, 'back to the first row');        // the crafting list        $s->open('craft');        $n = count(Crafting::recipes());        self::wheel(fn (float $w) => $s->update($ui($w), $g, $d, 0.016), 200, -1.0);        self::assertSame($n - 12, $this->game->peek('craftTop', $s));        self::wheel(fn (float $w) => $s->update($ui($w), $g, $d, 0.016), 200, 1.0);        self::assertSame(0, $this->game->peek('craftTop', $s), 'the wheel brings the first recipe back');        // small deltas are not lost        $w = new Wheel();        $sum = 0;        for ($k = 0; $k < 10; $k++) {            $sum += $w->steps(-0.3);        }        self::assertSame(3, $sum, 'ten notches of 0.3 = three rows');        // the chest's lists        $g->inv->setCapacity(9999.0);        $g->give('chest_metal');        Chests::place($g, 'chest_metal');        $t = Chests::inZone($g)[0] ?? null;        self::assertNotNull($t);        Chests::open($g, $t);        $v = new ChestView();        $v->open($g, false);        $box = static function (float $w): UiInput {            $u = new UiInput();            $u->wheel = $w;            $u->mx = 100.0;            return $u;        };        self::wheel(fn (float $w) => $v->update($box($w), $g, 0.016), 300, -1.0);        self::assertSame((float) (count(ChestView::bagList($g)) - 10), $v->scroll['bag']);        self::wheel(fn (float $w) => $v->update($box($w), $g, 0.016), 300, 1.0);        self::assertSame(0.0, $v->scroll['bag'], 'the chest screen comes back to the top');        Chests::close($g);        // the save list        /** @var SaveScreen $ss */        $ss = $this->game->peek('saveScreen');        $this->game->scene();        $p = new \ReflectionProperty($this->game->scene(), 'demoSaves');        $p->setValue($this->game->scene(), 12);        (new \ReflectionMethod($this->game->scene(), 'makeDemoSaves'))->invoke($this->game->scene(), $g);        $this->game->run(2);        $ss = $this->game->peek('saveScreen');        self::wheel(fn (float $w) => $ss->update($ui($w), null, false, 0.016), 80, -1.0);        self::assertGreaterThan(0, $ss->top);        self::wheel(fn (float $w) => $ss->update($ui($w), null, false, 0.016), 80, 1.0);        self::assertSame(0, $ss->top, 'the first save row');    }}