diff options
Diffstat (limited to 'src/http/Controller')
-rw-r--r-- | src/http/Controller/Event.php | 36 | ||||
-rw-r--r-- | src/http/Controller/User.php | 57 | ||||
-rw-r--r-- | src/http/Controller/Village.php | 44 |
3 files changed, 136 insertions, 1 deletions
diff --git a/src/http/Controller/Event.php b/src/http/Controller/Event.php index 8b9b92a..264fa26 100644 --- a/src/http/Controller/Event.php +++ b/src/http/Controller/Event.php @@ -8,7 +8,10 @@ use App\Model\Event\SendResources; use App\Model\Event\SendUnits; use App\Model\Event\TrainUnits; use App\Model\Event\UpgradeBuilding; +use App\Model\Unit; +use App\Model\Unit\MailCarrier; use App\Model\Village; +use App\View; use App\http\Router; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -20,6 +23,39 @@ class Event #[Route(path: '/village/{x}/{y}/send-resources', methods: ['POST'])] public function sendResources(Request $request): Response { + $village = Village::getByCoordinates($request->get('x'), $request->get('y')); + + $totalAmount = $request->get('wood') + $request->get('clay') + $request->get('iron') + $request->get('food'); + + $resourceCapabilities = MailCarrier::getResourceCapabilities($village); + $necessaryMailCarriers = ceil($totalAmount / $resourceCapabilities); + $mailCarriers = DB::fetch(MailCarrier::class, 'select sum(amount) as amount from village_units where type=:type and residence_village_id=:villageId and is_traveling=false', ['villageId' => $village->id, 'type' => 'MailCarrier'])[0]->amount ?? 0; + if ($mailCarriers === 0 || $totalAmount > $resourceCapabilities) { + return new Response(View::render('error.twig', ['message' => 'Insufficient Mail Carriers']), 403); + } + + $destination = Village::get($request->get('village')); + + // event + $event = new Model(); + $event->time = (new \DateTime())->add( + \DateInterval::createFromDateString( + Unit::getTravelTime(new MailCarrier(), Village::getDistance($village->x, $village->y, $destination->x, $destination->y)) + . ' seconds' + ) + ); + $event->villageId = $village->id; + $sendResourcesEvent = new SendResources(); + $sendResourcesEvent->event = $event; + $sendResourcesEvent->wood = $request->get('wood'); + $sendResourcesEvent->clay = $request->get('clay'); + $sendResourcesEvent->iron = $request->get('iron'); + $sendResourcesEvent->food = $request->get('food'); + $sendResourcesEvent->source = $village->id; + $sendResourcesEvent->destination = $destination->id; + $sendResourcesEvent->dbInsert(); + + return new RedirectResponse( Router::generate( 'village.show', diff --git a/src/http/Controller/User.php b/src/http/Controller/User.php new file mode 100644 index 0000000..c47e32e --- /dev/null +++ b/src/http/Controller/User.php @@ -0,0 +1,57 @@ +<?php + +namespace App\http\Controller; + +use App\DB; +use App\View; +use App\http\Router; +use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Attribute\Route; + +class User +{ + #[Route(path: '/account', methods: ['GET'])] + public function account(Request $request): Response + { + $user = DB::query('select username,email from users where id=:id', ['id' => $_SESSION['user']['id']])->fetch(); + + return new Response(View::render('account.twig', [ + 'user' => $user, + ])); + } + #[Route(path: '/account', methods: ['POST'])] + public function accountSave(Request $request): Response + { + $username = $request->get('username'); + $email = $request->get('email'); + + if ($request->get('password')) { + $password = password_hash($request->get('password'), PASSWORD_DEFAULT); + DB::query( + 'update users set username=:username, email=:email, password=:password where id=:id', + [ + 'username' => $username, + 'email' => $email, + 'password' => $password, + 'id' => $_SESSION['user']['id'], + ] + ); + } else { + DB::query( + 'update users set username=:username, email=:email where id=:id', + [ + 'username' => $username, + 'email' => $email, + 'id' => $_SESSION['user']['id'], + ] + ); + } + + $_SESSION['user']['username'] = $request->get('username'); + + + return new RedirectResponse(Router::generate('user.account')); + } +} diff --git a/src/http/Controller/Village.php b/src/http/Controller/Village.php index 7b1f227..2d6aa39 100644 --- a/src/http/Controller/Village.php +++ b/src/http/Controller/Village.php @@ -4,6 +4,8 @@ namespace App\http\Controller; use App\DB; use App\Guard; +use App\Model\Event\SendResources; +use App\Model\Event\SendResourcesCarriers; use App\Model\Event\SendUnits; use App\Model\Event\TrainUnits; use App\Model\Event\UpgradeBuilding; @@ -27,7 +29,7 @@ class Village join user_villages on villages.id = user_villages.village_id where user_villages.user_id=:id SQL, - ['id' => $_SESSION['user']['id']] + ['id' => $_SESSION['user']['id'] ?? -1] ); return new Response(View::render('villages.twig', [ @@ -90,6 +92,46 @@ class Village $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 + SQL, ['id' => $village->id] + )->fetchAll(); + + $eventsResourcesSendOther = DB::query( + <<<SQL + select * from events_send_resources as event + left join events on event.event_id = events.id + where (destination=:id or source=:id) and village_id!=:id and is_canceled=false + SQL, ['id' => $village->id] + )->fetchAll(); + + foreach ([...$eventsResourcesSendOwn, ...$eventsResourcesSendOther] as $row) { + $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 + SQL, ['id' => $village->id] + )->fetchAll(); + + $eventsResourcesCarriersSendOther = DB::query( + <<<SQL + select * from events_send_resources_carriers as event + left join events on event.event_id = events.id + where (destination=:id or source=:id) and village_id!=:id + SQL, ['id' => $village->id] + )->fetchAll(); + + foreach ([...$eventsResourcesCarriersSendOwn, ...$eventsResourcesCarriersSendOther] as $row) { + $events['SendResourcesCarriers'][] = DB::convertToModel(SendResourcesCarriers::class, $row);; + } + $buildings = []; foreach (Model::getBuildings($village->id, true) as $building) { $buildings[$building->type] = $building; |