summaryrefslogtreecommitdiff
path: root/src/gemini/Controller/User.php
blob: a870b881ec5ad1051dc3852bdca89c2c830a2016 (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
<?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);
  }
}