certificate = $certificate; $this->hostname = $hostname; global $_SESSION; $_SESSION = []; DB::init(); View::init(); View::addGlobal('session', $_SESSION); } public function run(): void { $server = new Server($this->certificate, $this->hostname, [ 'client_certificate_support_workaround' => true, ]); $server->onRequest(function (Response $response, Request $request) { new EventRunner(); // auth if ($request->getClientCertificate() === null) { return new Response( statusCode: Status::CLIENT_CERTIFICATE_REQUIRED, meta: 'Attach a client certificate to log in' ); } $userController = new User(); $user = $userController->get($request); if (empty($user)) { $user = $userController->create($request); } global $_SESSION; $_SESSION['user'] = [ 'id' => $user['id'], 'username' => $user['username'], ]; View::addGlobal('session', $_SESSION); // routes if ($request->getPath() == '/villages') { $villageController = new Village(); $response = $villageController->list($request); } else if (preg_match('@village/(\d+)/(\d+)/storage/config/?(\w+)?@', $request->getPath(), $routeMatch)) { $request ->set('x', $routeMatch[1]) ->set('y', $routeMatch[2]); if (isset($routeMatch[3])) { $request->set('type', $routeMatch[3]); } $villageController = new Village(); $response = $villageController->storageConfig($request); } else if (preg_match('@village/(\d+)/(\d+)/building/(\w+)/level-up@', $request->getPath(), $routeMatch)) { $request ->set('x', $routeMatch[1]) ->set('y', $routeMatch[2]) ->set('type', $routeMatch[3]); $buildingController = new Building(); $response = $buildingController->levelUp($request); } else if (preg_match('@village/(\d+)/(\d+)/unit/(\w+)/create@', $request->getPath(), $routeMatch)) { $request ->set('x', $routeMatch[1]) ->set('y', $routeMatch[2]) ->set('type', $routeMatch[3]); $unitController = new Unit(); $response = $unitController->train($request); } else if (preg_match('@village/(\d+)/(\d+)@', $request->getPath(), $routeMatch)) { $request ->set('x', $routeMatch[1]) ->set('y', $routeMatch[2]); $villageController = new Village(); $response = $villageController->show($request); } return $response; }); $server->listen(); } }