isCanceled) { // TODO: switch destination and source // TODO: add resources back to "destination" // TODO: add merchants back to "destination" } else { // 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, ] ); $source = Village::get($this->source); $destination = Village::get($this->destination); $event = new Event(); $event->time = (new \DateTime())->add( \DateInterval::createFromDateString( Unit::getTravelTime(new Merchant(), Village::getDistance($source->x, $source->y, $destination->x, $destination->y)) . ' seconds' ) ); $event->villageId = $this->source; $sendResourcesMerchants = new SendResourcesMerchants(); $sendResourcesMerchants->dbInsert($this->id); // TODO: add resources to destination // (TODO: add foreign merchants to destination)? // TODO: create SendUnits event with merchants back to source } } 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( << 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 = Merchant::getResourceCapabilities($this->event->villageId); $resourcesTotal = $this->wood + $this->clay + $this->iron + $this->food; $necessaryMerchants = ceil($resourcesTotal / $resourceCapabilities); $merchantsAccountedFor = 0; while ($merchantsAccountedFor < $necessaryMerchants) { $merchants = DB::fetch( Merchant::class, 'select * from village_units where type=:type and residence_village_id=:villageId and is_traveling=false', ['type' => 'Merchant', 'villageId' => $this->source] ); foreach ($merchants as $merchant) { /**@type Merchant $merchant*/ $currentlyNecessaryMerchants = $necessaryMerchants - $merchantsAccountedFor; $currentlyUseableMerchants = $currentlyNecessaryMerchants - $merchant->amount < 0 ? $currentlyNecessaryMerchants : $merchant->amount; $merchantsAccountedFor += $currentlyUseableMerchants; DB::query( << $currentlyUseableMerchants, 'type' => 'Merchant', 'home' => $merchant->homeVillageId, 'residence' => $merchant->residenceVillageId, ] ); DB::query('update village_units set amount=amount-:amount where id=:unitId', ['amount' => $necessaryMerchants, 'unitId' => $merchant->id]); DB::query( << $sendResourcesEventId, 'unit_id' => $merchant->id, 'amount' => $necessaryMerchants, ] ); } } // TODO: remove resources from 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]); #DB::query('delete from events_send_resources_merchants where event_id=:id', ['id' => $this->id]); } }