diff --git a/src/Events/DoctrineUpdated.php b/src/Events/DoctrineUpdated.php new file mode 100644 index 0000000..3499cc0 --- /dev/null +++ b/src/Events/DoctrineUpdated.php @@ -0,0 +1,22 @@ +doctrine = $doctrine; + } +} diff --git a/src/Events/FittingUpdated.php b/src/Events/FittingUpdated.php new file mode 100644 index 0000000..b96b0a1 --- /dev/null +++ b/src/Events/FittingUpdated.php @@ -0,0 +1,22 @@ +fitting = $fitting; + } +} diff --git a/src/Http/Controllers/FittingController.php b/src/Http/Controllers/FittingController.php index dff8c9e..0a1f89f 100644 --- a/src/Http/Controllers/FittingController.php +++ b/src/Http/Controllers/FittingController.php @@ -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; @@ -11,6 +13,7 @@ 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; @@ -18,7 +21,6 @@ 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 { @@ -29,6 +31,7 @@ class FittingController extends Controller implements CalculateConstants public function getSettings() { $provider = setting('cryptatech_seat_fitting_price_provider', true); + return view('fitting::settings', compact(['provider'])); } @@ -36,10 +39,10 @@ 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'); } @@ -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) @@ -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(); @@ -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: @@ -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 = []; @@ -417,6 +423,8 @@ public function saveDoctrine(DoctrineValidation $request) $doctrine->fittings()->sync($request->selectedFits); } + DoctrineUpdated::dispatch($doctrine); + return redirect()->route('cryptafitting::doctrineview'); } @@ -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); } } @@ -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; } diff --git a/src/Models/Fitting.php b/src/Models/Fitting.php index 127175f..8775855 100644 --- a/src/Models/Fitting.php +++ b/src/Models/Fitting.php @@ -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. @@ -49,7 +48,6 @@ class Fitting extends Model const IMPLANT = 89; const SKILL = 7; - public $timestamps = true; protected $table = 'crypta_tech_seat_fittings'; @@ -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 @@ -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; @@ -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 { @@ -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; @@ -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, @@ -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) { @@ -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; } } @@ -366,7 +365,7 @@ 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 @@ -374,7 +373,6 @@ public static function createFromEve(string $eft, int $existing_id=null): Fittin * 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; diff --git a/src/Models/FittingItem.php b/src/Models/FittingItem.php index 327e28b..e649953 100644 --- a/src/Models/FittingItem.php +++ b/src/Models/FittingItem.php @@ -20,12 +20,12 @@ * 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; /** @@ -33,7 +33,7 @@ * * @package CryptaTech\Seat\Fitting\Models */ -class FittingItem extends Model implements HasTypeID, IPriceable +class FittingItem extends Model implements HasTypeIDWithAmount, IPriceable { /** * @var bool @@ -93,4 +93,4 @@ public function setPrice(float $price): void { $this->price = $price; } -} \ No newline at end of file +}