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+)/unit/(\w+)/location/(\d+)/(\d+)/recall@', $request->getPath(), $routeMatch)) { $request ->set('x', $routeMatch[1]) ->set('y', $routeMatch[2]) ->set('type', $routeMatch[3]) ->set('lx', $routeMatch[4]) ->set('ly', $routeMatch[5]); $unitController = new Unit(); $response = $unitController->recall($request); } else if (preg_match('@village/(\d+)/(\d+)/unit/(\w+)/location/(\d+)/(\d+)/send-back@', $request->getPath(), $routeMatch)) { $request ->set('x', $routeMatch[1]) ->set('y', $routeMatch[2]) ->set('type', $routeMatch[3]) ->set('lx', $routeMatch[4]) ->set('ly', $routeMatch[5]); $unitController = new Unit(); $response = $unitController->sendBack($request); } else if (preg_match('@village/(\d+)/(\d+)/send-units@', $request->getPath(), $routeMatch)) { $request ->set('x', $routeMatch[1]) ->set('y', $routeMatch[2]); if (preg_match('@village/(\d+)/(\d+)/send-units/type/(\w+)$@', $request->getPath(), $routeMatch)) { $request->set('selectedUnit', $routeMatch[3]); } else if (preg_match('@village/(\d+)/(\d+)/send-units/type/(\w+)/village/(\d+)/(\d+)$@', $request->getPath(), $routeMatch)) { $request ->set('selectedUnit', $routeMatch[3]) ->set('selectedVillageX', $routeMatch[4]) ->set('selectedVillageY', $routeMatch[5]); } else if (preg_match('@village/(\d+)/(\d+)/send-units/type/(\w+)/village/(\d+)/(\d+)/action/(\w+)$@', $request->getPath(), $routeMatch)) { $request ->set('selectedUnit', $routeMatch[3]) ->set('selectedVillageX', $routeMatch[4]) ->set('selectedVillageY', $routeMatch[5]) ->set('selectedAction', $routeMatch[6]); } $unitController = new Unit(); $response = $unitController->sendUnits($request); } else if (preg_match('@village/(\d+)/(\d+)/send-resources@', $request->getPath(), $routeMatch)) { $request ->set('x', $routeMatch[1]) ->set('y', $routeMatch[2]); if (preg_match('@village/(\d+)/(\d+)/send-resources/type/(\w+)$@', $request->getPath(), $routeMatch)) { $request->set('selectedResourceType', $routeMatch[3]); } else if (preg_match('@village/(\d+)/(\d+)/send-resources/type/(\w+)/village/(\d+)/(\d+)$@', $request->getPath(), $routeMatch)) { $request ->set('selectedResourceType', $routeMatch[3]) ->set('selectedVillageX', $routeMatch[4]) ->set('selectedVillageY', $routeMatch[5]); } $villageController = new Village(); $response = $villageController->sendResources($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); } else if (preg_match('@event/(\d+)/cancel@', $request->getPath(), $routeMatch)) { $request->set('id', $routeMatch[1]); $eventController = new Event(); $response = $eventController->cancel($request); } return $response; }); $server->listen(); } }