game-village-politics-backend/app/Websocket/Controllers/RallyController.php

83 lines
2.7 KiB
PHP

<?php
namespace App\Websocket\Controllers;
use App\Actions\CancelMovement;
use App\Actions\PlanMarch;
use App\Actions\SendSupportHome;
use App\Actions\SendTroops;
use App\Actions\SimulateBattle;
use App\Actions\WithdrawSupport;
use App\Presenters\VillagePresenter;
use App\Websocket\GameController;
/**
* rally.preview {village_id, x, y, units, mode}: target, distance and travel time; rally.simulate: a battle on paper.
* rally.send {village_id, type, x, y, units, mode, catapult_target}, rally.cancel {village_id, movement_id},
* rally.sendHome {village_id, home_village_id}, rally.withdraw {village_id, host_village_id}:
* each answers with the fresh village state.
*/
class RallyController extends GameController
{
public function preview()
{
return $this->play(fn () => app(PlanMarch::class)(
auth()->user(),
(int) request('village_id'),
(int) request('x'),
(int) request('y'),
(array) request('units', []),
request('mode'),
));
}
/** rally.simulate {world_id, attack, defense, wall, morale, luck, night, building}: a battle on paper. */
public function simulate()
{
return $this->play(fn () => app(SimulateBattle::class)(
(int) request('world_id'),
(array) request('attack', []),
(array) request('defense', []),
(int) request('wall', 0),
(float) (request('morale') ?? 100),
(float) request('luck', 0),
(bool) request('night', false),
(int) request('building', 0),
));
}
public function send()
{
return $this->village(fn () => app(SendTroops::class)(
auth()->user(),
(int) request('village_id'),
(string) request('type'),
(int) request('x'),
(int) request('y'),
(array) request('units', []),
request('mode'),
request('catapult_target'),
));
}
public function cancel()
{
return $this->village(fn () => app(CancelMovement::class)(auth()->user(), (int) request('village_id'), (int) request('movement_id')));
}
public function sendHome()
{
return $this->village(fn () => app(SendSupportHome::class)(auth()->user(), (int) request('village_id'), (int) request('home_village_id')));
}
public function withdraw()
{
return $this->village(fn () => app(WithdrawSupport::class)(auth()->user(), (int) request('village_id'), (int) request('host_village_id')));
}
private function village(\Closure $action)
{
return $this->play(fn () => VillagePresenter::present($action(), now()->toImmutable()));
}
}