Skip to content

Commit

Permalink
Allow to ignore auction sell in calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Tychuk committed Aug 22, 2021
1 parent 51fdfa5 commit bfb5e06
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 17 deletions.
31 changes: 31 additions & 0 deletions migrations/Version20210822090029.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210822090029 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE user_calculation ADD is_auction_sell_allowed TINYINT(1) NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE user_calculation DROP is_auction_sell_allowed');
}
}
12 changes: 9 additions & 3 deletions src/Controller/CalculatorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ public function inventory(): Response
$calculatorParams = $this->calculatorParamsFactory->createParamsForInventory(
$materials,
$devices,
$userInventory->getMaterialQtys()
$userInventory->getMaterialQtys(),
$userCalculation->getIsAuctionSellAllowed()
);

$deals = $this->calculatorService->calculateForInventory(
Expand Down Expand Up @@ -216,7 +217,8 @@ public function mining(): Response
$calculatorParams = $this->calculatorParamsFactory->createParamsForMining(
$materials,
$devices,
$userMining->getAcceptableMaterialIds()
$userMining->getAcceptableMaterialIds(),
$userCalculation->getIsAuctionSellAllowed()
);

$deals = $this->calculatorService->calculateForMining(
Expand Down Expand Up @@ -264,7 +266,11 @@ public function trade(): Response
if (!$viewModel->hasErrors()) {
$this->getDoctrine()->getManager()->flush();

$calculatorParams = $this->calculatorParamsFactory->createParamsForTrade($materials, $devices);
$calculatorParams = $this->calculatorParamsFactory->createParamsForTrade(
$materials,
$devices,
$userCalculation->getIsAuctionSellAllowed()
);

$deals = $this->calculatorService->calculateForTrade(
$calculatorParams,
Expand Down
17 changes: 17 additions & 0 deletions src/Entity/Calculator/UserCalculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class UserCalculation
*/
private $maximizationParamCode;

/**
* @ORM\Column(type="boolean")
*/
private $isAuctionSellAllowed;

public function __construct()
{
$this->maximizationParamCode = UserCalculation::CREDIT_CODE;
Expand Down Expand Up @@ -64,4 +69,16 @@ public function setMaximizationParamCode(?string $maximizationParamCode): self

return $this;
}

public function getIsAuctionSellAllowed(): ?bool
{
return $this->isAuctionSellAllowed;
}

public function setIsAuctionSellAllowed(bool $isAuctionSellAllowed): self
{
$this->isAuctionSellAllowed = $isAuctionSellAllowed;

return $this;
}
}
43 changes: 29 additions & 14 deletions src/Service/Calculator/CalculatorParamsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,73 @@ class CalculatorParamsFactory
* @param Material[] $materials
* @param Device[] $devices
* @param int[] $inventoryQtys
* @param bool $isAuctionSellAllowed
* @return CalculatorParams
*/
public function createParamsForInventory(
array $materials,
array $devices,
array $inventoryQtys
array $inventoryQtys,
bool $isAuctionSellAllowed = true
): CalculatorParams {
return $this->createParams($materials, $devices, $inventoryQtys);
return $this->createParams($materials, $devices, $inventoryQtys, [], $isAuctionSellAllowed);
}

/**
* @param Material[] $materials
* @param Device[] $devices
* @param int[] $miningAcceptableIds
* @param bool $isAuctionSellAllowed
* @return CalculatorParams
*/
public function createParamsForMining(
array $materials,
array $devices,
array $miningAcceptableIds
array $miningAcceptableIds,
bool $isAuctionSellAllowed = true
): CalculatorParams {
return $this->createParams($materials, $devices, [], $miningAcceptableIds);
return $this->createParams($materials, $devices, [], $miningAcceptableIds, $isAuctionSellAllowed);
}

/**
* @param Material[] $materials
* @param Device[] $devices
* @param bool $isAuctionSellAllowed
* @return CalculatorParams
*/
public function createParamsForTrade(
array $materials,
array $devices
array $devices,
bool $isAuctionSellAllowed = true
): CalculatorParams {
return $this->createParams($materials, $devices);
return $this->createParams($materials, $devices, [], [], $isAuctionSellAllowed);
}

/**
* @param Material[] $materials
* @param Device[] $devices
* @param int[] $inventoryQtys
* @param int[] $miningAcceptableIds
* @param bool $isAuctionSellAllowed
* @return CalculatorParams
*/
public function createParams(
array $materials,
array $devices,
array $inventoryQtys = [],
array $miningAcceptableIds = []
array $miningAcceptableIds = [],
bool $isAuctionSellAllowed = true
): CalculatorParams {
$params = new CalculatorParams();

$params->setMaterialStockItems($this->createMaterialStockItems(
$materials,
$inventoryQtys,
$miningAcceptableIds
$miningAcceptableIds,
$isAuctionSellAllowed
));

$params->setDeviceStockItems($this->createDeviceStockItems($devices));
$params->setDeviceStockItems($this->createDeviceStockItems($devices, $isAuctionSellAllowed));

return $params;
}
Expand All @@ -84,10 +93,15 @@ public function createParams(
* @param Material[] $materials
* @param int[] $inventoryQtys
* @param int[] $miningAcceptableIds
* @param bool $isAuctionSellAllowed
* @return MaterialStockItem[]
*/
private function createMaterialStockItems(array $materials, array $inventoryQtys, array $miningAcceptableIds): array
{
private function createMaterialStockItems(
array $materials,
array $inventoryQtys,
array $miningAcceptableIds,
bool $isAuctionSellAllowed
): array {
/** @var MaterialStockItem[] $materialItems */
$materialItems = [];

Expand Down Expand Up @@ -121,7 +135,7 @@ private function createMaterialStockItems(array $materials, array $inventoryQtys
$destinations[] = $this->createMarketplaceDestination($marketplacePrice);
}

if ($auctionPrice !== null) {
if ($auctionPrice !== null && $isAuctionSellAllowed) {
$destinations[] = $this->createAuctionDestination($auctionPrice);
}

Expand All @@ -136,9 +150,10 @@ private function createMaterialStockItems(array $materials, array $inventoryQtys

/**
* @param Device[] $devices
* @param bool $isAuctionSellAllowed
* @return DeviceStockItem[]
*/
private function createDeviceStockItems(array $devices): array
private function createDeviceStockItems(array $devices, bool $isAuctionSellAllowed): array
{
/** @var DeviceStockItem[] $deviceItems */
$deviceItems = [];
Expand Down Expand Up @@ -169,7 +184,7 @@ private function createDeviceStockItems(array $devices): array
$destinations[] = $this->createMarketplaceDestination($marketplacePrice);
}

if ($auctionPrice !== null) {
if ($auctionPrice !== null && $isAuctionSellAllowed) {
$destinations[] = $this->createAuctionDestination($auctionPrice);
}

Expand Down
25 changes: 25 additions & 0 deletions src/ViewModel/Calculator/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Inventory extends AbstractViewModel
*/
private $maximizationParamList;

/**
* @var bool
*/
private $isAuctionSellAllowed = false;

/**
* @var DealGrid
*/
Expand Down Expand Up @@ -61,6 +66,8 @@ public function fillFromRequest(Request $request): void
$this->maximizationParamList->fillFromRequest($request);
$this->addErrors($this->maximizationParamList->getErrors());

$this->isAuctionSellAllowed = (bool)$request->request->get('auction-sell-allowed');

$this->inventoryMaterialGrid->fillFromRequest($request);
}

Expand All @@ -71,6 +78,7 @@ public function fillFromRequest(Request $request): void
public function fillFromUser(UserCalculation $userCalculation, UserInventory $userInventory): void
{
$this->maximizationParamList->fillFromUserCalculation($userCalculation);
$this->isAuctionSellAllowed = $userCalculation->getIsAuctionSellAllowed();
$this->inventoryMaterialGrid->fillFromModels($userInventory->getMaterials()->toArray());
}

Expand All @@ -90,6 +98,7 @@ public function fillFromDeals(array $deals): void
public function fillUser(UserCalculation $userCalculation, UserInventory $userInventory): void
{
$this->maximizationParamList->fillUserCalculation($userCalculation);
$userCalculation->setIsAuctionSellAllowed($this->isAuctionSellAllowed);
$this->inventoryMaterialGrid->fillModels($userInventory->getMaterials()->toArray(), $userInventory);
}

Expand Down Expand Up @@ -156,4 +165,20 @@ public function setHasCalculationResult(bool $hasCalculationResult): void
{
$this->hasCalculationResult = $hasCalculationResult;
}

/**
* @return bool
*/
public function isAuctionSellAllowed(): bool
{
return $this->isAuctionSellAllowed;
}

/**
* @param bool $isAuctionSellAllowed
*/
public function setIsAuctionSellAllowed(bool $isAuctionSellAllowed): void
{
$this->isAuctionSellAllowed = $isAuctionSellAllowed;
}
}
25 changes: 25 additions & 0 deletions src/ViewModel/Calculator/Mining.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Mining extends AbstractViewModel
*/
private $maximizationParamList;

/**
* @var bool
*/
private $isAuctionSellAllowed = false;

/**
* @var DealGrid
*/
Expand Down Expand Up @@ -61,6 +66,8 @@ public function fillFromRequest(Request $request): void
$this->maximizationParamList->fillFromRequest($request);
$this->addErrors($this->maximizationParamList->getErrors());

$this->isAuctionSellAllowed = (bool)$request->request->get('auction-sell-allowed');

$this->miningMaterialGrid->fillFromRequest($request);
}

Expand All @@ -71,6 +78,7 @@ public function fillFromRequest(Request $request): void
public function fillFromUser(UserCalculation $userCalculation, UserMining $userMining): void
{
$this->maximizationParamList->fillFromUserCalculation($userCalculation);
$this->isAuctionSellAllowed = $userCalculation->getIsAuctionSellAllowed();
$this->miningMaterialGrid->fillFromModels($userMining->getMaterials()->toArray());
}

Expand All @@ -90,6 +98,7 @@ public function fillFromDeals(array $deals): void
public function fillUser(UserCalculation $userCalculation, UserMining $userMining): void
{
$this->maximizationParamList->fillUserCalculation($userCalculation);
$userCalculation->setIsAuctionSellAllowed($this->isAuctionSellAllowed);
$this->miningMaterialGrid->fillModels($userMining->getMaterials()->toArray(), $userMining);
}

Expand Down Expand Up @@ -156,4 +165,20 @@ public function setHasCalculationResult(bool $hasCalculationResult): void
{
$this->hasCalculationResult = $hasCalculationResult;
}

/**
* @return bool
*/
public function isAuctionSellAllowed(): bool
{
return $this->isAuctionSellAllowed;
}

/**
* @param bool $isAuctionSellAllowed
*/
public function setIsAuctionSellAllowed(bool $isAuctionSellAllowed): void
{
$this->isAuctionSellAllowed = $isAuctionSellAllowed;
}
}
25 changes: 25 additions & 0 deletions src/ViewModel/Calculator/Trade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class Trade extends AbstractViewModel
*/
private $maximizationParamList;

/**
* @var bool
*/
private $isAuctionSellAllowed = false;

/**
* @var DealGrid
*/
Expand Down Expand Up @@ -46,6 +51,8 @@ public function fillFromRequest(Request $request): void
{
$this->maximizationParamList->fillFromRequest($request);
$this->addErrors($this->maximizationParamList->getErrors());

$this->isAuctionSellAllowed = (bool)$request->request->get('auction-sell-allowed');
}

/**
Expand All @@ -54,6 +61,7 @@ public function fillFromRequest(Request $request): void
public function fillFromUser(UserCalculation $userCalculation): void
{
$this->maximizationParamList->fillFromUserCalculation($userCalculation);
$this->isAuctionSellAllowed = $userCalculation->getIsAuctionSellAllowed();
}

/**
Expand All @@ -71,6 +79,7 @@ public function fillFromDeals(array $deals): void
public function fillUser(UserCalculation $userCalculation): void
{
$this->maximizationParamList->fillUserCalculation($userCalculation);
$userCalculation->setIsAuctionSellAllowed($this->isAuctionSellAllowed);
}

/**
Expand Down Expand Up @@ -120,4 +129,20 @@ public function setHasCalculationResult(bool $hasCalculationResult): void
{
$this->hasCalculationResult = $hasCalculationResult;
}

/**
* @return bool
*/
public function isAuctionSellAllowed(): bool
{
return $this->isAuctionSellAllowed;
}

/**
* @param bool $isAuctionSellAllowed
*/
public function setIsAuctionSellAllowed(bool $isAuctionSellAllowed): void
{
$this->isAuctionSellAllowed = $isAuctionSellAllowed;
}
}
Loading

0 comments on commit bfb5e06

Please sign in to comment.