summaryrefslogtreecommitdiff
path: root/src/http/Controller/Login.php
blob: 37680a70482e4602346ef1bde84234f6df86d5a0 (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
<?php

namespace App\http\Controller;

use App\DB;
use App\Model\Building\Farm;
use App\View;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class Login
{
  #[Route(path: '/login', methods: ['GET'])]
  public function form(Request $request): Response
  {
    return new Response(View::render('login.twig'));
  }

  #[Route(path: '/login', methods: ['POST'])]
  public function login(Request $request): Response
  {
    $email = $request->get('email');
    $user = DB::query('select id,username,password from users where email=:email or username=:email', ['email' => $email])->fetch();

    if (empty($user)) {
      $password = password_hash($request->get('password'), PASSWORD_DEFAULT);
      DB::query('insert into users (username, password, email) values (:username, :password, :email)', ['username' => $email, 'password' => $password, 'email' => $email]);
      
      $userId = DB::$connection->lastInsertId();

      // insert new village at random free coordinates
      # TODO: check if coords are free
      DB::query(
        'insert into villages (name, x, y, wood, clay, iron, food, satisfaction) values (:name, :x, :y, 100, 100, 100, 100, 100)',
        [
          'name' => $email,
          'x' => random_int(-10, 10),
          'y' => random_int(-10, 10),
        ]
      );
      $villageId = DB::$connection->lastInsertId();

      DB::query('insert into user_villages (user_id, village_id) values (:user_id, :village_id)', [
        'user_id' => $userId,
        'village_id' => $villageId,
      ]);

      DB::query('insert into village_storage_config (wood, clay, iron, food, village_id) values (:wood, :clay, :iron, :food, :village_id)', [
        'wood' => 25,
        'clay' => 25,
        'iron' => 25,
        'food' => 25,
        'village_id' => $village->id,
       ]);

      $initialBuildings = ['TownHall', 'Storage', 'WoodCutter', 'ClayPit', 'IronMine', 'Farm'];
      foreach ($initialBuildings as $buildingType) {
         DB::query('insert into village_buildings (level, type, village_id) values (:level, :type, :village_id)', [
          'level' => 1,
          'type' => $buildingType,
          'village_id' => $villageId,
        ]);       
      }

      $initialUnits = ['WoodCutter', 'PitWorker', 'Miner', 'Farmer'];
      foreach ($initialUnits as $unitType) {
        DB::query('insert into village_units (amount, type, home_village_id, residence_village_id) values (:amount, :type, :village_id, :village_id)', [
          'amount' => 1,
          'type' => $unitType,
          'village_id' => $villageId,
        ]);
      }
    } else {
      $password = $user['password'];
      $userId = $user['id'];
    }

    if (password_verify($request->get('password'), $password)) {
      $_SESSION['user'] = [
        'id' => $userId,
        'username' => $user['username'],
      ];

      return new RedirectResponse('/villages');
    }

    return new RedirectResponse('/login');
  }

  #[Route(path: '/logout', methods: ['GET'])]
  public function logout(Request $request): Response
  {
    session_unset();
    session_destroy();

    return new RedirectResponse('/login');
  }
}