From d58f61770463aab2c71464c11f902f0074b49b62 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 15 Jan 2024 15:50:43 +0100 Subject: http catch-up --- public/assets/style.css | 1 + src/Model/Event/SendResourcesCarriers.php | 3 ++ src/View.php | 4 ++ src/http/Controller/Event.php | 36 ++++++++++++++++ src/http/Controller/User.php | 57 ++++++++++++++++++++++++ src/http/Controller/Village.php | 44 ++++++++++++++++++- src/http/Router.php | 3 +- src/http/Support/RouteLoader.php | 13 +++--- views/http/account.twig | 22 ++++++++++ views/http/base.twig | 7 ++- views/http/components/timer.twig | 6 +++ views/http/village.twig | 72 ++++++++++++++++++++++++++++--- 12 files changed, 253 insertions(+), 15 deletions(-) create mode 100644 src/http/Controller/User.php create mode 100644 views/http/account.twig diff --git a/public/assets/style.css b/public/assets/style.css index 300d282..a6b425b 100644 --- a/public/assets/style.css +++ b/public/assets/style.css @@ -19,6 +19,7 @@ button, input[type="submit"] { color: #fff; border: none; padding: 0.25rem 0.75rem; + cursor: pointer; } button:hover:not(:disabled), input[type="submit"]:hover:not(:disabled) { diff --git a/src/Model/Event/SendResourcesCarriers.php b/src/Model/Event/SendResourcesCarriers.php index b6c3772..d129732 100644 --- a/src/Model/Event/SendResourcesCarriers.php +++ b/src/Model/Event/SendResourcesCarriers.php @@ -34,6 +34,9 @@ class SendResourcesCarriers extends BaseEvent } } + public function cancel(): void + {} + public function dbInsert(): void { DB::query( diff --git a/src/View.php b/src/View.php index 10ce9db..761ef62 100644 --- a/src/View.php +++ b/src/View.php @@ -25,6 +25,10 @@ class View // self::$twig->addExtension(new IntlExtension()); self::$twig->addFilter(new TwigFilter('buildTime', function ($buildTime) { + if ($buildTime > 3600*24) { + return @sprintf('%02d:%02d:%02d:%02d', $buildTime / (3600*24), $buildTime / 3600, ($buildTime / 60) % 60, $buildTime % 60); + } + return @sprintf('%02d:%02d:%02d', $buildTime / 3600, ($buildTime / 60) % 60, $buildTime % 60); })); } 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 @@ + $_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( + << $village->id] + )->fetchAll(); + + $eventsResourcesSendOther = DB::query( + << $village->id] + )->fetchAll(); + + foreach ([...$eventsResourcesSendOwn, ...$eventsResourcesSendOther] as $row) { + $events['SendResources'][] = DB::convertToModel(SendResources::class, $row);; + } + + $eventsResourcesCarriersSendOwn = DB::query( + << $village->id] + )->fetchAll(); + + $eventsResourcesCarriersSendOther = DB::query( + << $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; diff --git a/src/http/Router.php b/src/http/Router.php index db75f81..62f0514 100644 --- a/src/http/Router.php +++ b/src/http/Router.php @@ -10,6 +10,7 @@ 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; use Symfony\Component\Routing\RouteCollection; @@ -28,7 +29,7 @@ class Router self::$context->fromRequest($request); self::$routes = new RouteCollection(); - $loader = new AnnotationFileLoader(new FileLocator(), new RouteLoader()); + $loader = new AttributeFileLoader(new FileLocator(), new RouteLoader()); $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/Controller')); foreach ($iterator as $file) { /**@var \SplFileInfo $file*/ diff --git a/src/http/Support/RouteLoader.php b/src/http/Support/RouteLoader.php index b0e74cb..d11011c 100644 --- a/src/http/Support/RouteLoader.php +++ b/src/http/Support/RouteLoader.php @@ -2,10 +2,10 @@ namespace App\http\Support; -use Symfony\Component\Routing\Loader\AnnotationClassLoader; +use Symfony\Component\Routing\Loader\AttributeClassLoader; use Symfony\Component\Routing\Route; -class RouteLoader extends AnnotationClassLoader +class RouteLoader extends AttributeClassLoader { protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annotation) { $route->setDefault('_', compact('class', 'method', 'annotation')); @@ -15,10 +15,9 @@ class RouteLoader extends AnnotationClassLoader { $name = parent::getDefaultRouteName($class, $method); - return str_replace( - '_', - '.', - str_replace('app_controller_', '', $name) - ); + $name = preg_replace('/app_\w+_controller_/', '', $name); + $name = str_replace('_', '.', $name); + + return $name; } } diff --git a/views/http/account.twig b/views/http/account.twig new file mode 100644 index 0000000..8b32feb --- /dev/null +++ b/views/http/account.twig @@ -0,0 +1,22 @@ +{% extends 'base.twig' %} + +{% block main %} +

Account

+ +
+ + + + + +
+{% endblock %} diff --git a/views/http/base.twig b/views/http/base.twig index 15ddafd..bd59207 100644 --- a/views/http/base.twig +++ b/views/http/base.twig @@ -5,7 +5,12 @@
Logged in as {{ session.user.username }}
diff --git a/views/http/components/timer.twig b/views/http/components/timer.twig index ccb31a7..4898a2e 100644 --- a/views/http/components/timer.twig +++ b/views/http/components/timer.twig @@ -14,12 +14,18 @@ document.addEventListener('DOMContentLoaded', function (ev) { window.location.reload(); } + const dd = Math.floor(diff/1000/60/60/24); + diff -= dd*1000*60*60*24; const hh = Math.floor(diff/1000/60/60); diff -= hh*1000*60*60; const mm = Math.floor(diff/1000/60); diff -= mm*1000*60; const ss = Math.floor(diff/1000); + timer.innerHTML = `${('00' + hh).slice(-2)}:${('00' + mm).slice(-2)}:${('00' + ss).slice(-2)}`; + if (dd > 0) { + timer.innerHTML = `${('00' + dd).slice(-2)}:${timer.innerHTML}`; + } } setTime(); }); diff --git a/views/http/village.twig b/views/http/village.twig index c157a04..a498ea6 100644 --- a/views/http/village.twig +++ b/views/http/village.twig @@ -132,7 +132,7 @@ {% endif %} {% if events['SendUnits'] %} -

Send Resources / Units

+

Send Units

@@ -172,6 +172,68 @@
{% endif %} + + {% if events['SendResources'] %} +

Send Resources

+ + + + + + + + + + + + {% for event in events['SendResources'] %} + + + + + + + + {% endfor %} + +
SourceDestinationResourcesTime
{{ village.get(event.source).name }}{{ village.get(event.destination).name }}Resources + {% include 'components/timer.twig' with { 'time': event.event.time|date('c') } %} + + {% if event.isCanceled %} + Canceled + {% else %} + {% if event.event.villageId == village.id %} +
+ +
+ {% endif %} + {% endif %} +
+ {% endif %} + + {% if events['SendResourcesCarriers'] %} +

Send Resources Carriers

+ + + + + + + + + + {% for event in events['SendResourcesCarriers'] %} + + + + + + {% endfor %} + +
SourceDestinationTime
{{ village.get(event.source).name }}{{ village.get(event.destination).name }} + {% include 'components/timer.twig' with { 'time': event.event.time|date('c') } %} +
+ {% endif %}
@@ -344,10 +406,10 @@ {% if village.getBuilding(village.id, 'PostOffice') %}

Send Resources

- - - - + + + +