diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-01-02 20:42:01 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-01-05 12:33:59 +0100 |
commit | b21316248572cb27ed1f504529ad6680a473022e (patch) | |
tree | f8a2f81258cae3b1d2429fb7df5a3287954b683a /src/gemini/Controller/User.php | |
parent | f621d95f89ded05a2e916c5ee363bfe75ea37482 (diff) |
gemini
Diffstat (limited to 'src/gemini/Controller/User.php')
-rw-r--r-- | src/gemini/Controller/User.php | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/gemini/Controller/User.php b/src/gemini/Controller/User.php new file mode 100644 index 0000000..a870b88 --- /dev/null +++ b/src/gemini/Controller/User.php @@ -0,0 +1,118 @@ +<?php + +namespace App\gemini\Controller; + +use App\DB; +use GeminiFoundation\Request; + +class User +{ + public function get(Request $request): array|bool + { + if ($request->getClientCertificate() === null) { + return false; + } + + return DB::query( + <<<SQL + select users.id, username, email from users + join users_gemini on users.id = users_gemini.user_id + where users_gemini.certificate=:fingerprint + SQL, + ['fingerprint' => $request->getClientCertificate()->getFingerprint()] + )->fetch(); + } + + public function create(Request $request): array|bool + { + DB::query( + 'insert into users (username, password, email) values (:username, :fingerprint, :email)', + [ + 'username' => md5($request->getClientCertificate()->getFingerprint()), + 'fingerprint' => $request->getClientCertificate()->getFingerprint(), + 'email' => '(no email)', + ] + ); + $userId = DB::query('select id from users where password=:password', ['password' => $request->getClientCertificate()->getFingerprint()])->fetchColumn(); + + DB::query( + 'insert into users_gemini (certificate, user_id) values (:fingerprint, :userId)', + ['fingerprint' => $request->getClientCertificate()->getFingerprint(), 'userId' => $userId] + ); + + // also insert new village at random free coordinates + DB::query( + 'insert into villages (name, x, y, wood, clay, iron, food, satisfaction) values (:name, :x, :y, :wood, :clay, :iron, :food, :satisfaction)', + [ + 'name' => substr(md5(rand()), 0, 6), + 'x' => rand(0, 100), + 'y' => rand(0, 100), + 'wood' => 500, + 'clay' => 500, + 'iron' => 500, + 'food' => 500, + 'satisfaction' => 100, + ] + ); + $villageId = DB::query('select id from villages order by id desc limit 1')->fetchColumn(); + + DB::query( + 'insert into user_villages (user_id, village_id) values (:userId, :villageId)', + ['userId' => $userId, 'villageId' => $villageId] + ); + + // insert base buildings + DB::query( + 'insert into village_buildings (level, type, village_id) values (:level, :type, :villageId)', + ['level' => 1, 'type' => 'TownHall', 'villageId' => $villageId] + ); + + DB::query( + 'insert into village_buildings (level, type, village_id) values (:level, :type, :villageId)', + ['level' => 1, 'type' => 'Storage', 'villageId' => $villageId] + ); + DB::query( + 'insert into village_storage_config (wood, clay, iron, food, village_id) values (:wood, :clay, :iron, :food, :villageId)', + ['wood' => 25, 'clay' => 25, 'iron' => 25, 'food' => 25, 'villageId' => $villageId] + ); + + DB::query( + 'insert into village_buildings (level, type, village_id) values (:level, :type, :villageId)', + ['level' => 1, 'type' => 'WoodCutter', 'villageId' => $villageId] + ); + DB::query( + 'insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id) values (:amount, :type, false, :villageId, :villageId)', + ['amount' => 1, 'type' => 'WoodCutter', 'villageId' => $villageId] + ); + + DB::query( + 'insert into village_buildings (level, type, village_id) values (:level, :type, :villageId)', + ['level' => 1, 'type' => 'ClayPit', 'villageId' => $villageId] + ); + DB::query( + 'insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id) values (:amount, :type, false, :villageId, :villageId)', + ['amount' => 1, 'type' => 'PitWorker', 'villageId' => $villageId] + ); + + DB::query( + 'insert into village_buildings (level, type, village_id) values (:level, :type, :villageId)', + ['level' => 1, 'type' => 'IronMine', 'villageId' => $villageId] + ); + DB::query( + 'insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id) values (:amount, :type, false, :villageId, :villageId)', + ['amount' => 1, 'type' => 'Miner', 'villageId' => $villageId] + ); + + DB::query( + 'insert into village_buildings (level, type, village_id) values (:level, :type, :villageId)', + ['level' => 1, 'type' => 'Farm', 'villageId' => $villageId] + ); + DB::query( + 'insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id) values (:amount, :type, false, :villageId, :villageId)', + ['amount' => 1, 'type' => 'Farmer', 'villageId' => $villageId] + ); + + + return $this->get($request); + } +} |