summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-01-15 15:50:43 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-01-15 15:50:43 +0100
commitd58f61770463aab2c71464c11f902f0074b49b62 (patch)
tree97f55098caa342c1146f4e0bdc9f299affb62739 /src
parent2d0da0b920035c90d56db5dbec6d62f5b50ba0c2 (diff)
http catch-upHEADmain
Diffstat (limited to 'src')
-rw-r--r--src/Model/Event/SendResourcesCarriers.php3
-rw-r--r--src/View.php4
-rw-r--r--src/http/Controller/Event.php36
-rw-r--r--src/http/Controller/User.php57
-rw-r--r--src/http/Controller/Village.php44
-rw-r--r--src/http/Router.php3
-rw-r--r--src/http/Support/RouteLoader.php13
7 files changed, 151 insertions, 9 deletions
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 @@
+<?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;
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;
}
}