From 020645dbcad984d202a3e0495284cc35d948e9a5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 19 Feb 2023 17:11:46 +0100 Subject: [PATCH] app/Models/Card.php: in getBySettings() implement support to include X number of jackpot cards in the result. --- app/Models/Card.php | 25 +++++++++++++++++++------ tests/Feature/Models/CardTest.php | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/Models/Card.php b/app/Models/Card.php index 1c4adc1..5487b73 100644 --- a/app/Models/Card.php +++ b/app/Models/Card.php @@ -78,17 +78,30 @@ class Card extends Model /** * Get cards depending on settings. */ - public static function getBySettings(GameSettings $settings, ?int $max = null) + public static function getBySettings(GameSettings $settings, ?int $max = null, int $num_jackpot = 0) { $query = self::settingsQuery($settings) - ->inRandomOrder(); + ->where('jackpot', '=', 0); - if ($max !== null) { - $query->limit($max); + $jitems = \collect([]); + if ($num_jackpot > 0) { + $jitems = self::settingsQuery($settings) + ->where('jackpot', '!=', 0) + ->limit($num_jackpot) + ->get(); } - return $query->get() - ->makeHidden(['character', 'class', 'deleted_at']); + if ($max !== null) { + $query->limit($max - count($jitems)); + } + + if (count($jitems) > 0) { + $items = $query->get()->merge($jitems)->shuffle(); + } else { + $items = $query->inRandomOrder()->get(); + } + + return $items->makeHidden(['character', 'class', 'deleted_at']); } protected static function settingsQuery(GameSettings $settings): Builder diff --git a/tests/Feature/Models/CardTest.php b/tests/Feature/Models/CardTest.php index 48df935..9f61dd0 100644 --- a/tests/Feature/Models/CardTest.php +++ b/tests/Feature/Models/CardTest.php @@ -155,4 +155,22 @@ class CardTest extends TestCase $this->assertContains($card->role, $expected); } } + + public function test_get_with_jackpot_cards() + { + Card::factory()->count(10)->create(['jackpot' => 1]); + + $cards = Card::getBySettings(new GameSettings(), 4, 2); + + $this->assertSame(4, count($cards)); + + $jackpot_count = 0; + foreach ($cards as $card) { + if ($card->jackpot != 0) { + $jackpot_count++; + } + } + + $this->assertEquals(2, $jackpot_count); + } }