summaryrefslogtreecommitdiff
path: root/src/gemini/Gemini.php
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-01-02 20:42:01 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-01-05 12:33:59 +0100
commitb21316248572cb27ed1f504529ad6680a473022e (patch)
treef8a2f81258cae3b1d2429fb7df5a3287954b683a /src/gemini/Gemini.php
parentf621d95f89ded05a2e916c5ee363bfe75ea37482 (diff)
gemini
Diffstat (limited to 'src/gemini/Gemini.php')
-rw-r--r--src/gemini/Gemini.php120
1 files changed, 120 insertions, 0 deletions
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 @@
+<?php
+
+namespace App\gemini;
+
+use App\DB;
+use App\EventRunner;
+use App\View;
+use App\gemini\Controller\Building;
+use App\gemini\Controller\Unit;
+use App\gemini\Controller\User;
+use App\gemini\Controller\Village;
+use GeminiFoundation\Request;
+use GeminiFoundation\Response;
+use GeminiFoundation\Server;
+use GeminiFoundation\Status;
+
+class Gemini
+{
+ private array $certificate;
+ private string $hostname;
+
+ public function __construct(array $certificate, string $hostname)
+ {
+ $this->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();
+ }
+}