summaryrefslogtreecommitdiff
path: root/src/Model/Event/SendResources.php
blob: ea9361cea707c5d57f2786f47b23bc6531822191 (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
<?php

namespace App\Model\Event;

use App\DB;
use App\Model\Event;
use App\Model\Unit;
use App\Model\Unit\MailCarrier;
use App\Model\Village;

class SendResources extends BaseEvent
{
  public int $wood = 0;
  public int $clay = 0;
  public int $iron = 0;
  public int $food = 0;

  public int $source;
  public int $destination;

  public bool $isCanceled = false;

  /**
   * @return void
   */
  public function __invoke(): void
  {
    if ($this->isCanceled) {
      // add resources back to "destination"
      DB::query(
        'update villages set wood=wood+:wood, clay=clay+:clay, iron=iron+:iron, food=food+:food where id=:id',
        [
          'wood' => $this->wood,
          'clay' => $this->clay,
          'iron' => $this->iron,
          'food' => $this->food,
          'id' => $this->source,
        ]
      );

      // add mailCarriers back to "destination"
      $meta = DB::query('select amount, unit_id from events_meta_send_resources_carriers where event_id=:eventId', ['eventId' => $this->id])->fetchAll();
      foreach ($meta as $m) {
        $unit = DB::query('select * from village_units where id=:unitId', ['unitId' => $m['unit_id']])->fetch();

        DB::query('update village_units set amount=amount+:amount where id=:unitId', ['amount' => $m['amount'], 'unitId' => $unit['id']]);
        DB::query(
          'update village_units set amount=amount-:amount where type=:unitType and home_village_id=:homeVillageId and residence_village_id=:residenceVillageId and is_traveling=true',
          [
            'amount' => $m['amount'],
            'unitType' => $unit['type'],
            'homeVillageId' => $unit['home_village_id'],
            'residenceVillageId' => $unit['residence_village_id'],
          ]
        );
      }
    }

    else {
      // add resources to destination
      // TODO: account for storage capacity
      DB::query(
        'update villages set wood=wood+:wood, clay=clay+:clay, iron=iron+:iron, food=food+:food where id=:id',
        [
          'wood' => $this->wood,
          'clay' => $this->clay,
          'iron' => $this->iron,
          'food' => $this->food,
          'id' => $this->destination,
        ]
      );

      // create SendResourcesCarriers event with mailCarriers back to source
      $source = Village::get($this->destination);
      $destination = Village::get($this->source);

      $event = new Event();
      $event->time = (new \DateTime())->add(
        \DateInterval::createFromDateString(
          Unit::getTravelTime(new MailCarrier(), Village::getDistance($source->x, $source->y, $destination->x, $destination->y))
            . ' seconds'
        )
      );
      $event->villageId = $this->source;

      $sendResourcesMailCarriers = new SendResourcesCarriers();
      $sendResourcesMailCarriers->event = $event;
      $sendResourcesMailCarriers->sendResourcesEventId = $this->id;
      $sendResourcesMailCarriers->source = $source->id;
      $sendResourcesMailCarriers->destination = $destination->id;
      $sendResourcesMailCarriers->dbInsert();
    }
  }

  public function cancel(): void
  {
    $this->event = $this->getEvent();

    $cancelTimeDiff = $this->event->createdAt->diff(new \DateTime());
    $cancelTime = (new \DateTime())->add($cancelTimeDiff);

    $this->isCanceled = true;

    DB::query(
      'update events set time=:time where id=:id',
      ['time' => $cancelTime->format('c'), 'id' => $this->event->id]
    );
    DB::query(
      'update events_send_resources set is_canceled=:is_canceled where id=:id',
      ['is_canceled' => $this->isCanceled, 'id' => $this->id]
    );
  }

  public function dbInsert(): void
  {
    DB::query(
      'insert into events (time, village_id) VALUES (:time, :village_id)',
      ['time' => $this->event->time->format('c'), 'village_id' => $this->event->villageId]
    );

    DB::query(
      <<<SQL
      insert into events_send_resources (event_id, wood, clay, iron, food, source, destination, is_canceled)
      VALUES (:event_id, :wood, :clay, :iron, :food, :source, :destination, :is_canceled)
      SQL,
      [
        'event_id' => DB::$connection->lastInsertId(),
        'wood' => $this->wood,
        'clay' => $this->clay,
        'iron' => $this->iron,
        'food' => $this->food,
        'source' => $this->source, 'destination' => $this->destination,
        'is_canceled' => $this->isCanceled ?: 0, // @see https://www.php.net/manual/de/pdostatement.execute.php#126013
      ]
    );
    $sendResourcesEventId = DB::$connection->lastInsertId();

    $resourceCapabilities = MailCarrier::getResourceCapabilities($this->event->villageId);
    $resourcesTotal = $this->wood + $this->clay + $this->iron + $this->food;
    $necessaryMailCarriers = ceil($resourcesTotal / $resourceCapabilities);

    $mailCarriersAccountedFor = 0;
    while ($mailCarriersAccountedFor < $necessaryMailCarriers) {
      $mailCarriers = DB::fetch(
        MailCarrier::class,
        'select * from village_units where type=:type and residence_village_id=:villageId and is_traveling=false',
        ['type' => 'MailCarrier', 'villageId' => $this->source]
      );
      foreach ($mailCarriers as $mailCarrier) {
        /**@type MailCarrier $mailCarrier*/

        $currentlyNecessaryMailCarriers = $necessaryMailCarriers - $mailCarriersAccountedFor;
        $currentlyUseableMailCarriers = $currentlyNecessaryMailCarriers - $mailCarrier->amount < 0 ? $currentlyNecessaryMailCarriers : $mailCarrier->amount;
        $mailCarriersAccountedFor += $currentlyUseableMailCarriers;

        DB::query(
          <<<SQL
          insert into village_units (amount, type, home_village_id, residence_village_id, is_traveling)
          VALUES (:amount, :type, :home, :residence, true)
            on conflict (type, home_village_id, residence_village_id, is_traveling)
            do update set amount = village_units.amount+:amount
          SQL,
          [
            'amount' => $currentlyUseableMailCarriers,
            'type' => 'MailCarrier',
            'home' => $mailCarrier->homeVillageId,
            'residence' => $mailCarrier->residenceVillageId,
          ]
        );
        DB::query('update village_units set amount=amount-:amount where id=:unitId', ['amount' => $necessaryMailCarriers, 'unitId' => $mailCarrier->id]);

        DB::query(
          <<<SQL
          insert into events_meta_send_resources_carriers (event_id, unit_id, amount)
          VALUES (:event_id, :unit_id, :amount)
          SQL,
          [
            'event_id' => $sendResourcesEventId,
            'unit_id' => $mailCarrier->id,
            'amount' => $necessaryMailCarriers,
          ]
        );
      }
    }

    // remove resources from source
    DB::query(
      'update villages set wood=wood-:wood, clay=clay-:clay, iron=iron-:iron, food=food-:food where id=:id',
      [
        'wood' => $this->wood,
        'clay' => $this->clay,
        'iron' => $this->iron,
        'food' => $this->food,
        'id' => $this->source,
      ]
    );
  }

  public function dbDelete(): void
  {
    DB::query('delete from events where id=:id', ['id' => $this->eventId]);
    DB::query('delete from events_send_resources where id=:id', ['id' => $this->id]);
  }
}