summaryrefslogtreecommitdiff
path: root/src/gemini/Controller/User.php
blob: bc882b02e868b11ffcf65726dca26a26d93de735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php

namespace App\gemini\Controller;

use App\DB;
use App\View;
use GeminiFoundation\Request;
use GeminiFoundation\Response;
use GeminiFoundation\Status;

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::$connection->lastInsertId();

    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::$connection->lastInsertId();

    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);
  }

  public function account(Request $request): Response
  {
    $user = $this->get($request);


    return new Response(body: View::render('account.twig', [
      'user' => $user,
    ]));
  }

  public function accountUsername(Request $request): Response
  {
    $input = $request->get('input');
    if (empty($input)) {
      return new Response(statusCode: Status::INPUT, meta: 'Username');
    }

    $user = $this->get($request);
    DB::query('update users set username=:username where id=:id', ['username' => urldecode($input), 'id' => $user['id']]);


    return new Response(
      statusCode: Status::REDIRECT_TEMPORARY,
      meta: '/account'
    );
  }

  public function accountEmail(Request $request): Response
  {
    $input = $request->get('input');
    if (empty($input)) {
      return new Response(statusCode: Status::INPUT, meta: 'E-Mail');
    }

    $user = $this->get($request);
    DB::query('update users set email=:email where id=:id', ['email' => urldecode($input), 'id' => $user['id']]);


    return new Response(
      statusCode: Status::REDIRECT_TEMPORARY,
      meta: '/account'
    );
  }

  public function accountPassword(Request $request): Response
  {
    $input = $request->get('input');
    if (empty($input)) {
      return new Response(statusCode: Status::SENSITIVE_INPUT, meta: 'Password');
    }

    $password = password_hash(urldecode($input), PASSWORD_DEFAULT);
    $user = $this->get($request);
    DB::query('update users set password=:password where id=:id', ['password' => $password, 'id' => $user['id']]);


    return new Response(
      statusCode: Status::REDIRECT_TEMPORARY,
      meta: '/account'
    );
  }
}