Добавил генерацию колоды и улучшил бд

This commit is contained in:
2026-03-21 00:15:28 +05:00
parent b99c3f19bc
commit 0aeffffa56
5 changed files with 153 additions and 17 deletions

View File

@@ -0,0 +1,36 @@
# frozen_string_literal: true
module ClashDeckGenerator2
module Services
class DeckGenerator
DECK_SIZE = 8
def initialize(cards_repo:)
@cards_repo = cards_repo
end
def call(only_meta: true)
pool = only_meta ? @cards_repo.meta_cards : @cards_repo.all_cards
deck = sample_unique(pool, DECK_SIZE)
{
cards: deck,
avg_elixir: average_elixir(deck)
}
end
private
def sample_unique(pool, n)
raise "Not enough cards in pool (need #{n}, have #{pool.size})" if pool.size < n
pool.sample(n)
end
def average_elixir(deck)
sum = deck.sum { |c| c[:elixir_cost].to_i }
sum.fdiv(deck.size).round(2)
end
end
end
end