Open sidebar
// Controller <?php namespace App\Http\Controllers\City; use App\Commands\Trade\All; use App\Commands\Trade\Delete; use App\Commands\Trade\Get; use App\Http\Requests\TradeRequest; use App\Http\Controllers\Controller; use Illuminate\Bus\Dispatcher; use App\Commands\Trade\create; /** * Description of TradeController * * @Controller(prefix="trades") * @Middleware("auth") * * @author Christian */ class TradeController extends Controller { private $bus; public function __construct(Dispatcher $bus) { $this->bus = $bus; } /** * Check the Users Town and get the Form * @GET("/", as="trades.index") * * @return Response */ public function index() { return view('trade.index'); } /** * Check the Users Town and get the Form * @GET("/all", as="trades.all") * * @return Response */ public function getAllTrades() { $all = $this->bus->dispatch(new All()); return view('trade.all', compact('all')); } /** * Check the Users Town and get the Form * @GET("/single/{id}", as="trades.single") * * @return Response */ public function getSingleTrade($id) { return $this->bus->dispatch(new Get( $id)); } /** * Check the Users Town and get the Form * @GET("/create", as="trades.create") * * @return Response */ public function createTrade() { return view('trade.create'); } /** * Check the Users Town and get the Form * @POST("/create", as="trades.doCreate") * * @return Response */ public function postTrade(TradeRequest $request) { return $this->bus->dispatch(new Create($request)); } /** * Check the Users Town and get the Form * @GET("/delete/{id}", as="trades.delete") * * @return Response */ public function deleteTrade($id) { /** * Todo */ #return $this->bus->dispatch(new Delete($id)); } /** * Check the Users Town and get the Form * @POST("/filter", as="trades.doFilter") * * @return Response */ public function filterTrades() { } } // Command <?php namespace App\Commands\Trade; use App\Commands\Command; use Illuminate\Queue\SerializesModels; class Get extends Command { public $id; /** * Create a new command instance. * * @return void */ public function __construct($id = 0) { $this->id = $id; } } // Handler <?php namespace App\Handlers\Commands\Trade; use App\Commands\Trade\Get; use App\Database\Repository\CityRepository; use App\Database\Repository\TradeRepository; use App\Services\TradeService; use Illuminate\Contracts\Auth\Guard; use Illuminate\Http\RedirectResponse; use Illuminate\Queue\InteractsWithQueue; class GetHandler { private $trade, $city, $auth, $service; /** * Create the command handler. * * @return void */ public function __construct(Guard $auth, TradeRepository $trade, CityRepository $city, TradeService $service) { $this->auth = $auth; $this->service = $service; $this->trade = $trade; $this->city = $city; } /** * Handle the command. * * @param Get $command * @return void */ public function handle(Get $command) { $cities = $this->city->findWithChecked('user_id', $this->auth->user()->id, ['*'], ['units', 'materials', 'maps', 'actions']); $elementsUnit = $this->service->getJsonAction($cities->units->elements); $trades = $this->trade->find($command->id, ['*'], ['maps']); // Handel $elementsTrade = $this->service->getJsonAction($trades->elements); // Handel Elemente $check = $this->service->checkMaterial($cities->materials->{$elementsTrade['search_resource']}, $elementsTrade['search']); if($check instanceof RedirectResponse) { return $check; } $elementsUnit['lkw'] = $this->service->checkUnits($elementsUnit['lkw'], $elementsTrade['search_lkw']); if($elementsUnit['lkw'] instanceof RedirectResponse) { return $check; } $time = $this->service->setTimeFromCoords( ['x' => $trades->maps->xcoords, 'y' => $trades->maps->ycoords], ['x' => $cities->maps->xcoords, 'y' => $cities->maps->ycoords], 'game.units.lkw.1.speed' ); $cities->materials->update( [ $elementsTrade['search_resource'] => bcsub( (string)$cities->materials->{$elementsTrade['search_resource']}, (string)$elementsTrade['search'], 3) ] ); $cities->units->update( [ 'elements' => json_encode($elementsUnit), ] ); $cities->actions->update( [ 'elements' => $this->service->setJsonAction( $this->service->getJsonAction($cities->actions->elements), $elementsTrade, $time), ] //dbAction ); if((bool)$trades->npc !== true) { $citiesOther = $this->city->find($trades->cities_id, ['*'], ['actions']); $citiesOther->actions->update( [ 'elements' => $this->service->setJsonAction( $this->service->getJsonAction($citiesOther->actions->elements), $elementsTrade, $time), ] //$dbActionsOther ); $this->trade->delete($command->id); } return redirect()->route('trades.all')->with('msg', 'Handel erfolgreich angenommen'); } } // Service <?php namespace App\Services; use Carbon\Carbon; /** * Created by PhpStorm. * User: Christian * Date: 12.06.2015 * Time: 22:30 */ class TradeService { /** * @param $resource * @param $bulk * @return bool|\Illuminate\Http\RedirectResponse */ public function checkMaterial($resource, $bulk) { if($resource < $bulk) { return redirect()->route('trades.all')->with('msg', 'Deine vorhandenen Rohstoffe sind zu gering'); } return true; } /** * @param $bulkAvailable * @param $bulkRequired * @return \Illuminate\Http\RedirectResponse|string */ public function checkUnits($bulkAvailable, $bulkRequired) { if($bulkAvailable < $bulkRequired) { return redirect()->route('trades.all')->with('msg', 'Deine vorhandenen Einheiten sind zu gering.'); } return bcsub((string)$bulkAvailable , (string)$bulkRequired, 0); } /** * @param array $firstPoint * @param array $lastPoint * @return string */ public function setTimeFromCoords(array $firstPoint, array $lastPoint, $config) { $x = bcsub((string)$firstPoint['x'], (string)$lastPoint['x'], 3); $x = ($x < 0) ? bcmul((string)$x, '-1', 3) : $x; $y = bcsub((string)$firstPoint['y'], (string)$lastPoint['y'], 3); $y = ($y < 0) ? bcmul((string)$y, '-1', 3) : $y; return bcmul( (string)config($config), (string)bcsqrt( (string)bcadd( (string)bcpow( (string)$x, '2', 3), (string)bcpow( (string)$y, '2', 3) ), 3), 0); } /** * @param $action * @param bool $array * @return mixed */ public function getJsonAction($action, $array = true) { return json_decode($action, $array); } /** * @param $action * @param $elements * @param $time * @return string */ public function setJsonAction($action, $elements, $time) { $i = 1; if (!is_null($action)) { if (is_array($action)) { if (array_key_exists('trades', $action) === true) { list($keys) = array_divide($action['trades']); $i = last($keys) + 1; } } } array_set($action, 'trades.' . $i . '.name', $elements['give_resource']); array_set($action, 'trades.' . $i . '.add', $elements['give']); array_set($action, 'trades.' . $i . '.unit', $elements['give_lkw']); array_set($action, 'trades.' . $i . '.time', bcadd((string)Carbon::now()->timestamp, (string)$time, 0)); return json_encode($action); } }
Close sidebar
Back
Please note that all pasted data is publicly available.
Twitter
GitHub
Use setting
Back
Please note that all pasted data is publicly available.
Twitter
GitHub
Use setting