app/Models/Card.php: in getBySettings() implement support to include X number of jackpot cards in the result.
This commit is contained in:
parent
0a80dbeb5e
commit
020645dbca
2 changed files with 37 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue