diff options
Diffstat (limited to 'src/gemini/Gemini.php')
-rw-r--r-- | src/gemini/Gemini.php | 120 |
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(); + } +} |