diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Model/Event/BaseEvent.php | 7 | ||||
-rw-r--r-- | src/Model/Event/SendResources.php | 2 | ||||
-rw-r--r-- | src/Model/Event/SendUnits.php | 2 | ||||
-rw-r--r-- | src/Model/Event/TrainUnits.php | 2 | ||||
-rw-r--r-- | src/Model/Event/UpgradeBuilding.php | 52 | ||||
-rw-r--r-- | src/Model/Village.php | 2 | ||||
-rw-r--r-- | src/Support/BuildingType.php | 0 | ||||
-rw-r--r-- | src/http/Controller/Building.php | 10 | ||||
-rw-r--r-- | src/http/Controller/Login.php | 49 | ||||
-rw-r--r-- | src/http/Controller/Map.php | 6 | ||||
-rw-r--r-- | src/http/Controller/User.php | 1 | ||||
-rw-r--r-- | src/http/Controller/Village.php | 33 | ||||
-rw-r--r-- | src/http/Router.php | 1 |
13 files changed, 138 insertions, 29 deletions
diff --git a/src/Model/Event/BaseEvent.php b/src/Model/Event/BaseEvent.php index 2e2564b..0d50458 100644 --- a/src/Model/Event/BaseEvent.php +++ b/src/Model/Event/BaseEvent.php @@ -15,16 +15,15 @@ abstract class BaseEvent public Event $event; + abstract function create(): void; + abstract function cancel(): void; abstract function dbInsert(): void; abstract function dbDelete(): void; - abstract function cancel(): void; - public function getEvent(): Event + public function populateEvent(): void { if (! isset($this->event)) { $this->event = DB::fetch(Event::class, 'select * from events where id=:id', ['id' => $this->eventId])[0]; } - - return $this->event; } } diff --git a/src/Model/Event/SendResources.php b/src/Model/Event/SendResources.php index ea9361c..d8e4f85 100644 --- a/src/Model/Event/SendResources.php +++ b/src/Model/Event/SendResources.php @@ -94,7 +94,7 @@ class SendResources extends BaseEvent public function cancel(): void { - $this->event = $this->getEvent(); + $this->populateEvent(); $cancelTimeDiff = $this->event->createdAt->diff(new \DateTime()); $cancelTime = (new \DateTime())->add($cancelTimeDiff); diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php index c9afd6e..1b04333 100644 --- a/src/Model/Event/SendUnits.php +++ b/src/Model/Event/SendUnits.php @@ -104,7 +104,7 @@ class SendUnits extends BaseEvent public function cancel(): void { - $this->event = $this->getEvent(); + $this->populateEvent(); $cancelTimeDiff = $this->event->createdAt->diff(new \DateTime()); $cancelTime = (new \DateTime())->add($cancelTimeDiff); diff --git a/src/Model/Event/TrainUnits.php b/src/Model/Event/TrainUnits.php index 59b169f..a94a5a8 100644 --- a/src/Model/Event/TrainUnits.php +++ b/src/Model/Event/TrainUnits.php @@ -14,7 +14,7 @@ class TrainUnits extends BaseEvent */ public function __invoke(): void { - $this->getEvent(); + $this->populateEvent(); DB::query( <<<SQL diff --git a/src/Model/Event/UpgradeBuilding.php b/src/Model/Event/UpgradeBuilding.php index a3f3984..5993105 100644 --- a/src/Model/Event/UpgradeBuilding.php +++ b/src/Model/Event/UpgradeBuilding.php @@ -3,17 +3,21 @@ namespace App\Model\Event; use App\DB; +use App\Model\Village; class UpgradeBuilding extends BaseEvent { public string $type; + public int $wood; + public int $clay; + public int $iron; /** * @return void */ public function __invoke(): void { - $this->getEvent(); + $this->populateEvent(); DB::query( <<<SQL @@ -26,8 +30,44 @@ class UpgradeBuilding extends BaseEvent ); } + /*public function populateEvent(): void + { + parent::populateEvent(); + + $event = DB::fetch(UpgradeBuilding::class, 'select * from events_upgrade_building where id=:id', ['id' => $this->id])[0]; + $this->type = $event->type; + $this->wood = $event->wood; + $this->clay = $event->clay; + $this->iron = $event->iron; + }*/ + + public function create(): void + { + $this->populateEvent(); + + // remove resources + $village = Village::get($this->event->villageId); + $village->wood -= $this->wood; + $village->clay -= $this->clay; + $village->iron -= $this->iron; + $village->updateResources(); + + // add to db + $this->dbInsert(); + } + public function cancel(): void { + $this->populateEvent(); + + // add resources + $village = Village::get($this->event->villageId); + $village->wood += $this->wood; + $village->clay += $this->clay; + $village->iron += $this->iron; + $village->updateResources(); + + // remove from db $this->dbDelete(); } @@ -39,8 +79,14 @@ class UpgradeBuilding extends BaseEvent ); DB::query( - 'insert into events_upgrade_building (event_id, type) VALUES (:event_id, :type)', - ['event_id' => DB::$connection->lastInsertId(), 'type' => $this->type] + 'insert into events_upgrade_building (event_id, type, wood, clay, iron) VALUES (:event_id, :type, :wood, :clay, :iron)', + [ + 'event_id' => DB::$connection->lastInsertId(), + 'type' => $this->type, + 'wood' => $this->wood, + 'clay' => $this->clay, + 'iron' => $this->iron, + ] ); } diff --git a/src/Model/Village.php b/src/Model/Village.php index ff0b7e8..8ecee2a 100644 --- a/src/Model/Village.php +++ b/src/Model/Village.php @@ -184,7 +184,7 @@ class Village * * @return array<int, Unit> */ - public static function getUnits(int $villageId, int $fetchFlag = Village::FETCH_UNIT_ALL, int $returnFlag = Village::RETURN_UNIT_EXISTING): array + public static function getUnits(int $villageId, int $fetchFlag = Village::FETCH_UNIT_RESIDENCE, 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]); diff --git a/src/Support/BuildingType.php b/src/Support/BuildingType.php new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/Support/BuildingType.php diff --git a/src/http/Controller/Building.php b/src/http/Controller/Building.php index 4a59f0e..d434fe8 100644 --- a/src/http/Controller/Building.php +++ b/src/http/Controller/Building.php @@ -21,10 +21,7 @@ class Building $building = Model::getByVillage($village->id, $request->get('type')) ?? Model::getEmpty($village->id, $request->get('type')); // resources - foreach ($building->getResourceRequirements() as $resourceType => $resourceValue) { - $village->{$resourceType} -= $resourceValue; - } - $village->updateResources(); + $resourceRequirements = $building->getResourceRequirements(); // event $event = new Event(); @@ -35,7 +32,10 @@ class Building $upgradeBuildingEvent = new UpgradeBuilding(); $upgradeBuildingEvent->event = $event; $upgradeBuildingEvent->type = $building->type; - $upgradeBuildingEvent->dbInsert(); + $upgradeBuildingEvent->wood = $resourceRequirements['wood']; + $upgradeBuildingEvent->clay = $resourceRequirements['clay']; + $upgradeBuildingEvent->iron = $resourceRequirements['iron']; + $upgradeBuildingEvent->create(); return new RedirectResponse( Router::generate( diff --git a/src/http/Controller/Login.php b/src/http/Controller/Login.php index 8c04d85..37680a7 100644 --- a/src/http/Controller/Login.php +++ b/src/http/Controller/Login.php @@ -3,6 +3,7 @@ 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; @@ -26,15 +27,59 @@ class Login 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(); - // TODO: also insert new village at random free coordinates + // 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' => $user['id'], + 'id' => $userId, 'username' => $user['username'], ]; diff --git a/src/http/Controller/Map.php b/src/http/Controller/Map.php index 69d23e1..3b0773f 100644 --- a/src/http/Controller/Map.php +++ b/src/http/Controller/Map.php @@ -10,13 +10,17 @@ use Symfony\Component\Routing\Annotation\Route; class Map { - #[Route(path: '/map/{x}/{y}/{range}', defaults: ['range' => 1], methods: ['GET'])] + #[Route(path: '/map/{x}/{y}/{range}', defaults: ['range' => 0], methods: ['GET'])] public function region(Request $request): Response { $x = $request->get('x'); $y = $request->get('y'); $range = $request->get('range'); + if ($range <= 0) { + $range = $_ENV['MAP_DEFAULT_RANGE']; + } + $statement = DB::query( 'select * from villages where x>=:x1 and x<=:x2 and y>=:y1 and y<=:y2', [ diff --git a/src/http/Controller/User.php b/src/http/Controller/User.php index c47e32e..e38780f 100644 --- a/src/http/Controller/User.php +++ b/src/http/Controller/User.php @@ -21,6 +21,7 @@ class User 'user' => $user, ])); } + #[Route(path: '/account', methods: ['POST'])] public function accountSave(Request $request): Response { diff --git a/src/http/Controller/Village.php b/src/http/Controller/Village.php index 2d6aa39..22713b1 100644 --- a/src/http/Controller/Village.php +++ b/src/http/Controller/Village.php @@ -57,26 +57,32 @@ class Village )->fetchAll(); foreach ($eventsBuilding as $row) { - $events['UpgradeBuilding'][$row['type']][] = DB::convertToModel(UpgradeBuilding::class, $row); + $model = DB::convertToModel(UpgradeBuilding::class, $row); + $model->populateEvent(); + + $events['UpgradeBuilding'][$row['type']][] = $model; } $eventsUnits = DB::query( <<<SQL select * from events_train_units as event left join events on event.event_id = events.id - where village_id=:id + where events.village_id=:id SQL, ['id' => $village->id] )->fetchAll(); foreach ($eventsUnits as $row) { - $events['TrainUnits'][] = DB::convertToModel(TrainUnits::class, $row); + $model = DB::convertToModel(TrainUnits::class, $row); + $model->populateEvent(); + + $events['TrainUnits'][] = $model; } $eventsUnitsSendOwn = DB::query( <<<SQL select * from events_send_units as event left join events on event.event_id = events.id - where village_id=:id + where events.village_id=:id SQL, ['id' => $village->id] )->fetchAll(); @@ -89,14 +95,17 @@ class Village )->fetchAll(); foreach ([...$eventsUnitsSendOwn, ...$eventsUnitsSendOther] as $row) { - $events['SendUnits'][] = DB::convertToModel(SendUnits::class, $row);; + $model = DB::convertToModel(SendUnits::class, $row); + $model->populateEvent(); + + $events['SendUnits'][] = DB::convertToModel(SendUnits::class, $row); } $eventsResourcesSendOwn = DB::query( <<<SQL select * from events_send_resources as event left join events on event.event_id = events.id - where village_id=:id + where events.village_id=:id SQL, ['id' => $village->id] )->fetchAll(); @@ -109,14 +118,17 @@ class Village )->fetchAll(); foreach ([...$eventsResourcesSendOwn, ...$eventsResourcesSendOther] as $row) { - $events['SendResources'][] = DB::convertToModel(SendResources::class, $row);; + $model = DB::convertToModel(SendResources::class, $row); + $model->populateEvent(); + + $events['SendResources'][] = DB::convertToModel(SendResources::class, $row); } $eventsResourcesCarriersSendOwn = DB::query( <<<SQL select * from events_send_resources_carriers as event left join events on event.event_id = events.id - where village_id=:id + where events.village_id=:id SQL, ['id' => $village->id] )->fetchAll(); @@ -129,7 +141,10 @@ class Village )->fetchAll(); foreach ([...$eventsResourcesCarriersSendOwn, ...$eventsResourcesCarriersSendOther] as $row) { - $events['SendResourcesCarriers'][] = DB::convertToModel(SendResourcesCarriers::class, $row);; + $model = DB::convertToModel(SendResourcesCarriers::class, $row); + $model->populateEvent(); + + $events['SendResourcesCarriers'][] = $model; } $buildings = []; diff --git a/src/http/Router.php b/src/http/Router.php index 62f0514..f279109 100644 --- a/src/http/Router.php +++ b/src/http/Router.php @@ -9,7 +9,6 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Generator\UrlGenerator; -use Symfony\Component\Routing\Loader\AnnotationFileLoader; use Symfony\Component\Routing\Loader\AttributeFileLoader; use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RequestContext; |