summaryrefslogtreecommitdiff
path: root/src/Model/Village.php
blob: 019d9ba91836f2cfc7f29916d33dfac9fd9344cd (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php

namespace App\Model;

use App\DB;
use App\Model\Building\Marketplace;
use App\Model\Building\Storage;
use App\Model\Village\StorageConfig;

class Village
{
  public int $id;

  public string $name;

  public int $x;
  public int $y;

  public int $wood;
  public int $clay;
  public int $iron;
  public int $food;

  public int $satisfaction;

  public string $createdAt;
  public string $updatedAt;

  public static function canBuild(Village $village, Building $building): bool
  {
    if ($building->level >= $building->maxLevel) {
      return false;
    }

    $resourceRequirements = $building->getResourceRequirements();
    foreach ($resourceRequirements as $resourceType => $requirement) {
      if ($village->$resourceType < $requirement) {
        return false;
      }
    }

    return true;
  }

  public static function canTrain(Village $village, Unit $unit, int $amount): bool
  {
    $resourceRequirements = Unit::getResourceRequirements($unit, $amount);
    foreach ($resourceRequirements as $resourceType => $requirement) {
      if ($village->$resourceType < $requirement) {
        return false;
      }
    }

    return true;
  }

  public static function canSendResources(Village $village): bool
  {
    $marketplace = Village::getBuilding($village->id, 'Marketplace');
    if (! $marketplace) {
      return false;
    }

    $merchants = Village::getUnit($village, $marketplace->unitType, Village::FETCH_UNIT_RESIDENCE, Village::RETURN_UNIT_EXISTING);
    if (! $merchants || $merchants->amount === 0) {
      return false;
    }

    return true;
  }

  /* DB - Actions */

  public static function get(int $id): ?Village
  {
    return DB::fetch(Village::class, 'select * from villages where id=:id', ['id' => $id])[0] ?? null;
  }

  public static function getByCoordinates(int $x, int $y): ?Village
  {
    return DB::fetch(Village::class, 'select * from villages where x=:x and y=:y', ['x' => $x, 'y' => $y])[0] ?? null;
  }

  public function updateResources(): mixed
  {
    return DB::query(
      'update villages set wood=:wood,clay=:clay,iron=:iron,food=:food where id=:id',
      ['wood' => $this->wood, 'clay' => $this->clay, 'iron' => $this->iron, 'food' => $this->food, 'id' => $this->id]
    );
  }

  public static function getDistance(int $x, int $y, int $dx, int $dy): int
  {
    return abs($x - $dx) + abs($y - $dy);
  }

  /* DB - Relations */

  public static function getBuildings(int $villageId, bool $withEmpty = false): array
  {
    $buildings = DB::fetch(Building::class, 'select * from village_buildings where village_id=:id', ['id' => $villageId]);

    if ($withEmpty) {
      $nonBuiltBuildings = array_diff(
        ['TownHall', 'Embassy', 'Marketplace', 'Storage', 'WoodCutter', 'ClayPit', 'IronMine', 'Farm'],
        array_column($buildings, 'type'),
      );

      foreach ($nonBuiltBuildings as $type) {
        $buildings[] = Building::getEmpty($villageId, $type);
      }
    }

    return array_map(function (Building $building) {
      return $building->cast();
    }, $buildings);
  }

  public static function getBuilding(int $villageId, string $buildingType): ?Building
  {
    $results = DB::fetch(
      Building::resolveType($buildingType),
      'select * from village_buildings where village_id=:id and type=:type',
      ['id' => $villageId, 'type' => $buildingType]
    );

    return isset($results[0]) ? $results[0]->cast() : null;
  }

  public static function getStorage(int $villageId): ?Storage
  {
    return Village::getBuilding($villageId, 'Storage');
  }

  public static function getStorageConfig(int $villageId): ?StorageConfig
  {
    $results = DB::fetch(
      StorageConfig::class,
      'select * from village_storage_config where village_id=:id',
      ['id' => $villageId]
    );

    return $results[0] ?? null;
  }

  public const FETCH_UNIT_HOME_AT_HOME = 1;
  public const FETCH_UNIT_HOME_AT_SUPPORT = 2;
  public const FETCH_UNIT_SUPPORT_AT_HOME = 3;
  public const FETCH_UNIT_RESIDENCE = 4;

  public const RETURN_UNIT_EXISTING = 1;
  public const RETURN_UNIT_ALL = 2;
  public const RETURN_UNIT_TRAINABLE = 3;

  public static function getUnit(Village $village, string $unitType, int $fetchFlag = Village::FETCH_UNIT_RESIDENCE, int $returnFlag = Village::RETURN_UNIT_EXISTING): ?Unit
  {
    if ($fetchFlag == Village::FETCH_UNIT_RESIDENCE) {
      $query = 'select * from village_units where residence_village_id=:id and type=:type and is_traveling=false';
    }

    $results = DB::fetch(Unit::class, $query, ['id' => $village->id, 'type' => $unitType]);
    if (isset($results[0])) {
      $unit = $results[0]->cast();
    } else {
      return null;
    }

    return $unit;
  }

  /**
   * @param int $fetchFlag
   *
   * @return array<int, Unit>
   */
  public static function getUnits(int $villageId, int $fetchFlag = Village::FETCH_UNIT_ALL, int $returnFlag = Village::RETURN_UNIT_EXISTING): array
  {
    if ($fetchFlag == Village::FETCH_UNIT_HOME_AT_HOME) {
      $units = DB::fetch(Unit::class, 'select * from village_units where home_village_id=:id and residence_village_id=:id', ['id' => $villageId]);
    }
    else if ($fetchFlag == Village::FETCH_UNIT_HOME_AT_SUPPORT) {
      $units = DB::fetch(Unit::class, 'select * from village_units where home_village_id=:id and residence_village_id!=:id', ['id' => $villageId]);
    }
    else if ($fetchFlag == Village::FETCH_UNIT_SUPPORT_AT_HOME) {
      $units = DB::fetch(Unit::class, 'select * from village_units where home_village_id!=:id and residence_village_id=:id', ['id' => $villageId]);
    }
    else if ($fetchFlag == Village::FETCH_UNIT_RESIDENCE) {
      $units = DB::fetch(Unit::class, 'select * from village_units where residence_village_id=:id', ['id' => $villageId]);
    }

    if ($returnFlag == Village::RETURN_UNIT_ALL || $returnFlag == Village::RETURN_UNIT_TRAINABLE) {
      $nonExistingUnits = array_diff(
        ['WoodCutter', 'PitWorker', 'Miner', 'Farmer', 'Merchant', 'Diplomat'],
        array_column($units, 'type'),
      );

      foreach ($nonExistingUnits as $type) {
        $units[] = Unit::getEmpty($villageId, $type);
      }

      if ($returnFlag == Village::RETURN_UNIT_TRAINABLE) {
        $units = array_filter($units, function (Unit $unit) {
          return !! $unit->cast()->getBuilding();
        });
      }
    }
    else if ($returnFlag == Village::RETURN_UNIT_EXISTING) {
      $units = array_filter($units, function (Unit $unit) {
        return ! $unit->isTraveling;
      });
    }

    return array_map(function (Unit $unit) {
      return $unit->cast();
    }, $units);
  }
}