DE
games/dmmotor / tests / ServerWadsTest.php · commit 9d248a4
<?phpnamespace Tests;use App\Dm\Wad;use App\Scene\PlayScene;use App\Scene\TitleScene;use App\Service\DmGame;use JumPHP\Externals\Externals;use JumPHP\Kernel\Engine;use JumPHP\Testing\GameTestCase;/** * Game tests of the WADs shared by the playground (jumphp/externals, folder dmmotor/wads): listed on the first * screen, looked into by packets, downloaded by blocks when chosen, played. A local folder stands for the server. */final class ServerWadsTest extends GameTestCase{    private function dm(): DmGame    {        /** @var DmGame $game */        $game = $this->game->peek('game');        return $game;    }    /** A WAD made of E1M1 of Phase 1 renamed $map, of type $type, in a fresh "server" folder. */    private function server(string $file, string $type, string $map): string    {        $iwad = Wad::fromParts(Engine::get()->config->path('assets/wads/freedoom1.wad'));        $start = $iwad->check('E1M1');        $lumps = [[$map, '']];        for ($i = 1; $i <= 10; $i++) {            $lumps[] = [$iwad->name($start + $i), $iwad->lumpNum($start + $i)];        }        $data = '';        $dir = '';        $pos = 12;        foreach ($lumps as [$name, $bytes]) {            $dir .= pack('VV', $pos, strlen($bytes)) . str_pad($name, 8, "\0");            $data .= $bytes;            $pos += strlen($bytes);        }        $folder = sys_get_temp_dir() . '/dmmotor-server-' . getmypid() . '-' . $file;        @mkdir($folder, 0777, true);        file_put_contents("$folder/$file", $type . pack('VV', count($lumps), $pos) . $data . $dir);        return $folder;    }    private function openTitleWith(string $folder): TitleScene    {        $this->game->start(TitleScene::class);        /** @var Externals $ext */        $ext = $this->game->peek('externals');        $ext->configure(['local' => ['dmmotor/wads' => $folder], 'block' => 4096, 'persist' => false]);        $this->game->start(TitleScene::class);   // enter() lists the server        /** @var TitleScene $scene */        $scene = $this->game->scene();        return $scene;    }    public function testAModOfTheServerIsInspectedDownloadedAndPlayed(): void    {        $scene = $this->openTitleWith($this->server('netmod.wad', 'PWAD', 'E1M4'));        $ids = $this->game->peek('ids', $scene);        self::assertContains('net/netmod', $ids);        $this->game->peek('game', $scene)->catalog->inspect('net/netmod', $this->game->peek('externals', $scene));        $catalog = $this->dm()->catalog;        self::assertSame('ready', $catalog->remoteState('net/netmod'));        self::assertSame(['E1M4'], $catalog->analysis('net/netmod')['maps']);        self::assertFalse($catalog->isDownloaded('net/netmod'), 'looked into, not downloaded');        // choose it: downloaded, then the title of the game        $index = array_search('net/netmod', $ids, true);        $current = $this->game->peek('choice', $scene);        for ($i = 0; $i < count($ids) && $current !== $index; $i++) {            $this->game->press('down', 2);            $current = $this->game->peek('choice', $scene);        }        $this->game->run(2);        $this->game->shot('server-wad');        $this->game->press('confirm', 5);        self::assertTrue($catalog->isDownloaded('net/netmod'));        self::assertSame('net/netmod', $this->dm()->iwad);        self::assertSame([1, 4], $this->dm()->firstMap);        $this->game->start(PlayScene::class, ['iwad' => 'net/netmod', 'episode' => 1, 'map' => 4, 'skill' => 2, 'newGame' => true]);        self::assertEqualsWithDelta(-416, $this->dm()->world->player->mo->x, 0.01);    }    public function testTheWadsOfASharedSubFolderAreListed(): void    {        // the administrator shared dmmotor/wads/extra only, not dmmotor/wads (its parent answers 403)        $this->game->start(TitleScene::class);        /** @var Externals $ext */        $ext = $this->game->peek('externals');        $ext->configure(['local' => ['dmmotor/wads/extra' => $this->server('submod.wad', 'PWAD', 'E1M2')], 'block' => 4096, 'persist' => false]);        $this->game->start(TitleScene::class);        self::assertContains('net/extra/submod', $this->game->peek('ids', $this->game->scene()));    }    public function testAGameOfTheServerThatIsNotFreeIsNotPlayed(): void    {        $scene = $this->openTitleWith($this->server('other.wad', 'IWAD', 'E1M1'));        $catalog = $this->dm()->catalog;        $catalog->inspect('net/other', $this->game->peek('externals', $scene));        self::assertSame('ready', $catalog->remoteState('net/other'));        self::assertFalse($catalog->isFree('net/other'), 'an IWAD without the FREEDOOM lump');        self::assertSame('NOT A FREE GAME: NOT PLAYED', $catalog->summary('net/other'));    }    /**     * A name from the commercial list in force (WadCatalog::COMMERCIAL), read from the constant itself: the test     * keeps working whatever list the developer is testing.     */    private function commercialName(): string    {        $pattern = (new \ReflectionClassConstant(\App\Service\WadCatalog::class, 'COMMERCIAL'))->getValue();        preg_match('~\^\(([^|)$]+)~', $pattern, $m);        return $m[1] ?? 'doom';    }    public function testACommercialGameIsNeverPlayedEvenAsAMod(): void    {        $name = $this->commercialName();        $scene = $this->openTitleWith($this->server("$name.wad", 'PWAD', 'E1M1'));        $catalog = $this->dm()->catalog;        $catalog->inspect("net/$name", $this->game->peek('externals', $scene));        self::assertFalse($catalog->isFree("net/$name"), "$name is commercial");    }}