Skip to content

Commit

Permalink
add events when fitting and doctrines change (#3)
Browse files Browse the repository at this point in the history
seat-inventory needs to update some stuff when a fitting changes. I
tried to use an observer for this, but: The plugin first saves the
fitting and only afterwards save the items of the fit, meaning the
observer fires before the items of a fitting have been saved.

This PR adds a laravel event that fires after a fitting has been saved.

The same has been done with doctrines for the same reason.

This PR depends on eveseat/services#180
  • Loading branch information
Crypta-Eve authored Apr 3, 2024
2 parents b198cf6 + 3907759 commit 7d078dd
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 32 deletions.
22 changes: 22 additions & 0 deletions src/Events/DoctrineUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace CryptaTech\Seat\Fitting\Events;

use CryptaTech\Seat\Fitting\Models\Doctrine;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class DoctrineUpdated
{
use Dispatchable, SerializesModels;

public Doctrine $doctrine;

/**
* @param Doctrine $doctrine
*/
public function __construct(Doctrine $doctrine)
{
$this->doctrine = $doctrine;
}
}
22 changes: 22 additions & 0 deletions src/Events/FittingUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace CryptaTech\Seat\Fitting\Events;

use CryptaTech\Seat\Fitting\Models\Fitting;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class FittingUpdated
{
use Dispatchable, SerializesModels;

public Fitting $fitting;

/**
* @param Fitting $fitting
*/
public function __construct(Fitting $fitting)
{
$this->fitting = $fitting;
}
}
38 changes: 23 additions & 15 deletions src/Http/Controllers/FittingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CryptaTech\Seat\Fitting\Http\Controllers;

use CryptaTech\Seat\Fitting\Events\DoctrineUpdated;
use CryptaTech\Seat\Fitting\Events\FittingUpdated;
use CryptaTech\Seat\Fitting\Helpers\CalculateConstants;
use CryptaTech\Seat\Fitting\Helpers\CalculateEft;
use CryptaTech\Seat\Fitting\Models\Doctrine;
Expand All @@ -11,14 +13,14 @@
use CryptaTech\Seat\Fitting\Validation\FittingValidation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use RecursiveTree\Seat\PricesCore\Facades\PriceProviderSystem;
use Seat\Eveapi\Models\Alliances\Alliance;
use Seat\Eveapi\Models\Character\CharacterAffiliation;
use Seat\Eveapi\Models\Character\CharacterInfo;
use Seat\Eveapi\Models\Corporation\CorporationInfo;
use Seat\Eveapi\Models\Sde\DgmTypeAttribute;
use Seat\Eveapi\Models\Sde\InvType;
use Seat\Web\Http\Controllers\Controller;
use RecursiveTree\Seat\PricesCore\Facades\PriceProviderSystem;

class FittingController extends Controller implements CalculateConstants
{
Expand All @@ -29,17 +31,18 @@ class FittingController extends Controller implements CalculateConstants
public function getSettings()
{
$provider = setting('cryptatech_seat_fitting_price_provider', true);

return view('fitting::settings', compact(['provider']));
}

public function saveSettings(Request $request)
{

$request->validate([
"price_source" => "required|integer"
'price_source' => 'required|integer',
]);

setting(["cryptatech_seat_fitting_price_provider", $request->price_source], true);
setting(['cryptatech_seat_fitting_price_provider', $request->price_source], true);

return redirect()->back()->with('success', 'Updated settings');
}
Expand Down Expand Up @@ -220,18 +223,18 @@ public function getFittingCostById($id)

// $eft = implode("\n", $fit->eftfitting);
try {
PriceProviderSystem::getPrices($provider,$items);
PriceProviderSystem::getPrices($provider, $items);
} catch (PriceProviderException $e) {
$message = $e->getMessage();
return redirect()->back()->with("error", "Failed to get prices from price provider: $message");

return redirect()->back()->with('error', "Failed to get prices from price provider: $message");
}

$total = $items->sum(function(FittingItem $v){
$total = $items->sum(function (FittingItem $v) {
return $v->getPrice();
});


return response()->json(json_encode(["total" => $total, "ship" => $ship->getPrice()]));
return response()->json(json_encode(['total' => $total, 'ship' => $ship->getPrice()]));
}

public function getFittingById($id)
Expand Down Expand Up @@ -289,7 +292,10 @@ public function saveFitting(FittingValidation $request)
$fit = Fitting::createFromEve($request->eftfitting, $request->fitSelection);
} else {
$fit = Fitting::createFromEve($request->eftfitting);
}
}

// dispatch an event so other plugins know that a fitting has updated
FittingUpdated::dispatch($fit);

$fitlist = $this->getFittingList();

Expand All @@ -312,7 +318,7 @@ private function fittingParser($fit)
$jsfit['fitname'] = $fit->name;
$jsfit['dronebay'] = []; // Lets load fighters in here too xD
foreach ($fit->items as $ls) {

switch ($ls->flag){
case Fitting::BAY_DRONE:
case Fitting::BAY_FIGHTER:
Expand All @@ -330,12 +336,12 @@ private function fittingParser($fit)
$jsfit[$ls->invFlag->flagName] = ['id' => $ls->type_id, 'name' => $ls->type->typeName];
break;
}

}

return $jsfit;
}


public function postSkills(FittingValidation $request)
{
$skillsToons = [];
Expand Down Expand Up @@ -417,6 +423,8 @@ public function saveDoctrine(DoctrineValidation $request)
$doctrine->fittings()->sync($request->selectedFits);
}

DoctrineUpdated::dispatch($doctrine);

return redirect()->route('cryptafitting::doctrineview');
}

Expand All @@ -429,7 +437,7 @@ public function viewDoctrineReport()
$allids = [];

foreach ($corps as $corp) {
if (!is_null($corp->alliance_id)) {
if (! is_null($corp->alliance_id)) {
array_push($allids, $corp->alliance_id);
}
}
Expand Down Expand Up @@ -518,10 +526,10 @@ public function runReport($alliance_id, $corp_id, $doctrine_id)
}
}

if (!isset($data['totals'][$fit['name']]['ship'])) {
if (! isset($data['totals'][$fit['name']]['ship'])) {
$data['totals'][$fit['name']]['ship'] = 0;
}
if (!isset($data['totals'][$fit['name']]['fit'])) {
if (! isset($data['totals'][$fit['name']]['fit'])) {
$data['totals'][$fit['name']]['fit'] = 0;
}

Expand Down
24 changes: 11 additions & 13 deletions src/Models/Fitting.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@

namespace CryptaTech\Seat\Fitting\Models;

use Illuminate\Database\Eloquent\Model;
use Seat\Eveapi\Models\Sde\InvType;
use CryptaTech\Seat\Fitting\Models\FittingItem;
use CryptaTech\Seat\Fitting\Models\Sde\DgmTypeEffect;
use Illuminate\Database\Eloquent\Model;
use Seat\Eveapi\Models\Sde\DgmTypeAttribute;
use Seat\Eveapi\Models\Sde\InvType;

/**
* Class Fitting.
Expand All @@ -49,7 +48,6 @@ class Fitting extends Model
const IMPLANT = 89;
const SKILL = 7;


public $timestamps = true;

protected $table = 'crypta_tech_seat_fittings';
Expand Down Expand Up @@ -254,7 +252,7 @@ public function toEve()
/**
* @return Fitting
*/
public static function createFromEve(string $eft, int $existing_id=null): Fitting
public static function createFromEve(string $eft, int $existing_id = null): Fitting
{

// Normalise all the line endings to \n
Expand All @@ -280,13 +278,11 @@ public static function createFromEve(string $eft, int $existing_id=null): Fittin
$fit->ship_type_id = $ship->typeID;

$fit->save();


// This is our current parser state
$state = STATE::LOWS;
// $state = STATE::from($state->value +1); <-- Used to increment states


$index = 0;
foreach ($data as $line) {
if (empty($line)) continue;
Expand All @@ -299,7 +295,7 @@ public static function createFromEve(string $eft, int $existing_id=null): Fittin

// Here is where we update our state machine to the next state if required.
$solved = false;
while (!$solved && ($state != $state->nextState())) {
while (! $solved && ($state != $state->nextState())) {
if ($state->validInvType($module)) {
$solved = true;
} else {
Expand All @@ -313,7 +309,7 @@ public static function createFromEve(string $eft, int $existing_id=null): Fittin
'fitting_id' => $fit->fitting_id,
'type_id' => $module->typeID,
'flag' => $state->getFlag($index),
'quantity' => isset($modu[1]) ? $modu[1] : 1
'quantity' => isset($modu[1]) ? $modu[1] : 1,
]);
$index += 1;

Expand All @@ -322,7 +318,7 @@ public static function createFromEve(string $eft, int $existing_id=null): Fittin
// we have something split for qty
$chg = explode(' x', trim($mod[1]));
$charge = InvType::where('typeName', $chg[0])->first();
if (!empty($charge)) {
if (! empty($charge)) {
FittingItem::create([
'fitting_id' => $fit->fitting_id,
'type_id' => $charge->typeID,
Expand All @@ -334,7 +330,7 @@ public static function createFromEve(string $eft, int $existing_id=null): Fittin
}

// Now, if this is a ship that has a fighter bay, do a pass back over and move fighters from cargo to the fighter bay.
// There is probably more efficient ways to do this all in the DB in one update, but cbf as it wont run often.
// There is probably more efficient ways to do this all in the DB in one update, but cbf as it wont run often.
// Though may be more of an issue if people depend on observers....... But this whole serial approach falls apart then anyway.
$fighterHangarCapacity = DgmTypeAttribute::where('typeID', $fit->ship_type_id)->where('attributeID', 2055)->value('valueFloat');
if ($fighterHangarCapacity) {
Expand All @@ -348,6 +344,9 @@ public static function createFromEve(string $eft, int $existing_id=null): Fittin
}
}

// laravel caches relations, meaning that with the way we add items to the fitting, the cache isn't invalidated automatically
$fit->unsetRelations();

return $fit;
}
}
Expand All @@ -366,15 +365,14 @@ public static function createFromEve(string $eft, int $existing_id=null): Fittin
* Now the hard one.... Fighters... I think that because cargo is the catch all and last.. I need to do a pass later
* that for all ships with fighter bays, these get moved from cargo to the fighter bay.
* NOT DOING VOLUME CHECKS!
*
*
* So how am I goign to parse this mess... Well its not technically correct but a state machine
* We know the header is first so that is easy.
* From there I am going to ignore blank lines (HEATHEN!) and populate slots in order from top to bottom based on the order they appear
* So if the first item we come across is a gun, then skip the lows and mediums.
* Then if all the slots (not based on hull but max possible) are full then we assume its in cargo. (ie 4 Rigs means 3 fit and 1 in cargo)
* Charges in weapons are automatically loaded into cargo
*/

enum STATE: int
{
case LOWS = 1;
Expand Down
8 changes: 4 additions & 4 deletions src/Models/FittingItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

namespace CryptaTech\Seat\Fitting\Models;
namespace CryptaTech\Seat\Fitting\Models;

use CryptaTech\Seat\Fitting\Models\Sde\InvFlag;
use Illuminate\Database\Eloquent\Model;
use Seat\Eveapi\Models\Sde\InvType;
use Seat\Services\Contracts\HasTypeID;
use Seat\Services\Contracts\HasTypeIDWithAmount;
use Seat\Services\Contracts\IPriceable;

/**
* Class FittingItem.
*
* @package CryptaTech\Seat\Fitting\Models
*/
class FittingItem extends Model implements HasTypeID, IPriceable
class FittingItem extends Model implements HasTypeIDWithAmount, IPriceable
{
/**
* @var bool
Expand Down Expand Up @@ -93,4 +93,4 @@ public function setPrice(float $price): void
{
$this->price = $price;
}
}
}

0 comments on commit 7d078dd

Please sign in to comment.