Skip to content

Commit

Permalink
add guaranteed categories
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed May 24, 2024
1 parent 0df85d5 commit 01fe163
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 61 deletions.
8 changes: 4 additions & 4 deletions app/Http/Controllers/Users/PairingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function getPairings(Request $request) {
}

$user_items = $user->items()->whereNull('deleted_at')->where('count', '>', 0)->get()->pluck('id')->toArray();
$user_pairing_items = UserItem::whereIn('item_id', $user_items)->whereIn('item_id', Item::whereRelation('tags', 'tag', 'pairing')->pluck('id')->toArray())->get();
$user_boost_items = UserItem::whereIn('item_id', $user_items)->whereIn('item_id', Item::whereRelation('tags', 'tag', 'boost')->pluck('id')->toArray())->get();
$user_pairing_items = UserItem::where('user_id', $user->id)->whereIn('item_id', $user_items)->whereIn('item_id', Item::whereRelation('tags', 'tag', 'pairing')->pluck('id')->toArray())->get();
$user_boost_items = UserItem::where('user_id', $user->id)->whereIn('item_id', $user_items)->whereIn('item_id', Item::whereRelation('tags', 'tag', 'boost')->pluck('id')->toArray())->get();

return view('home.pairings', [
'characters' => Character::visible()->myo(0)->orderBy('number', 'DESC')->get()->pluck('fullName', 'slug')->toArray(),
Expand All @@ -72,11 +72,11 @@ public function createPairings(Request $request, PairingManager $service) {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}
return redirect()->back();
} else {
flash('Pairing created successfully!')->success();
return redirect()->to('characters/pairings?type=waiting');
}

return redirect()->back();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions app/Services/Item/PairingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Services\Item;

use App\Models\Feature\Feature;
use App\Models\Feature\FeatureCategory;
use App\Models\Item\Item;
use App\Models\Species\Species;
use App\Models\Species\Subtype;
Expand All @@ -27,6 +28,7 @@ class PairingService extends Service {
public function getEditData() {
return [
'features' => Feature::orderBy('name')->pluck('name', 'id'),
'categories' => FeatureCategory::orderBy('name')->pluck('name', 'id'),
'specieses' => Species::orderBy('name')->pluck('name', 'id'),
'subtypes' => Subtype::orderBy('name')->pluck('name', 'id'),
];
Expand Down Expand Up @@ -68,6 +70,7 @@ public function updateData($tag, $data) {
$specieses = isset($data['illegal_species_ids']) ? array_filter($data['illegal_species_ids']) : [];
$features = isset($data['illegal_feature_ids']) ? array_filter($data['illegal_feature_ids']) : [];
$subtypes = isset($data['illegal_subtype_ids']) ? array_filter($data['illegal_subtype_ids']) : [];
$featureCategories = isset($data['feature_category_ids']) ? array_filter($data['feature_category_ids']) : [];

if (isset($data['feature_id'])) {
$pairingData['feature_id'] = $data['feature_id'];
Expand Down Expand Up @@ -100,6 +103,13 @@ public function updateData($tag, $data) {
$pairingData['illegal_subtype_ids'] = $subtypes;
}

foreach ($featureCategories as $key=>$category) {
$pairingData['guaranteed_feature_categories'][] = [
'id' => $category,
'number' => $data['feature_category_number'][$key] ?? 1,
];
}

DB::beginTransaction();

try {
Expand Down
26 changes: 25 additions & 1 deletion app/Services/PairingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ private function getChosenFeatures($tag, $characters, $feature_pool, $boosts) {
$boosted_chance_by_rarity = [];
// sort boosts by rarityid for easier access
foreach ($boosts as $boost) {
if ($boost->tag('boost') != null) {
if ($boost->tag('boost')) {
$boost_tag = $boost->tag('boost');
if (isset($boost_tag->getData()['rarity_id'])) {
$boosted_chance_by_rarity[$boost_tag->getData()['rarity_id']] =
Expand All @@ -881,12 +881,36 @@ private function getChosenFeatures($tag, $characters, $feature_pool, $boosts) {

//sort features by category
$features_by_category = $feature_pool->groupBy('feature_category_id');
// check if there are any guaranteed categories and add them to the features_by_category array keys
$guaranteed_feature_categories = $tag->getData()['guaranteed_feature_categories'] ?? null;
if ($guaranteed_feature_categories) {
foreach ($guaranteed_feature_categories as $category) {
if (!$features_by_category->has($category['id'])) {
// get the lowest rarity features in the category
$features_by_category[$category['id']] = Feature::where('feature_category_id', $category['id'])->orderBy('rarity_id', 'asc')->get();
}
}
}
$chosen_features = [];
foreach ($features_by_category as $categoryId=>$features) {
//if no category is set, make min inheritable 0 and max inheritable 100 (basically, unlimited)
$min_inheritable = $features->first()->category->min_inheritable ?? 0;
$max_inheritable = $features->first()->category->max_inheritable ?? 100;

// check if the guaranteed category is set and check the min and max inheritable values
if (isset($guaranteed_feature_categories) && collect($guaranteed_feature_categories)->where('id', $categoryId)->first()) {
// get the guaranteed_feature_categories where the guaranteed_feature_categories['id'] == $categoryId
$tagData = collect($guaranteed_feature_categories)->where('id', $categoryId)->first();
// check if tagData['number'] contains a '-' and split it into min and max values
if (strpos($tagData['number'], '-') !== false) {
$min_max = explode('-', $tagData['number']);
$min_inheritable = $min_max[0];
$max_inheritable = $min_max[1];
} else {
$min_inheritable = $tagData['number'];
}
}

$features_calculated = 0;
$features_chosen = 0;
$i = 0;
Expand Down
Loading

0 comments on commit 01fe163

Please sign in to comment.