From b21316248572cb27ed1f504529ad6680a473022e Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 2 Jan 2024 20:42:01 +0100 Subject: gemini --- src/gemini/Gemini.php | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/gemini/Gemini.php (limited to 'src/gemini/Gemini.php') diff --git a/src/gemini/Gemini.php b/src/gemini/Gemini.php new file mode 100644 index 0000000..41b8716 --- /dev/null +++ b/src/gemini/Gemini.php @@ -0,0 +1,120 @@ +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(); + } +} -- cgit v1.2.3