56 lines
1.6 KiB
Ruby
56 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module ClashDeckGenerator2
|
|
module Actions
|
|
module Decks
|
|
class Generate < ClashDeckGenerator2::Action
|
|
include Deps[view: "views.home.index"]
|
|
|
|
def handle(_req, res)
|
|
res.headers.delete("Content-Security-Policy")
|
|
|
|
roles_repo = ClashDeckGenerator2::Services::CardRolesRepository.new
|
|
cards_repo = ClashDeckGenerator2::Repos::CardsRepo.new
|
|
|
|
generator = ClashDeckGenerator2::Services::DeckGenerator.new(
|
|
cards_repo: cards_repo,
|
|
roles_repo: roles_repo
|
|
)
|
|
|
|
deck = unwrap(generator.call)
|
|
|
|
res.render(
|
|
view,
|
|
deck: deck,
|
|
roles_repo: roles_repo,
|
|
stats: build_stats(deck)
|
|
)
|
|
end
|
|
|
|
private
|
|
|
|
def unwrap(result)
|
|
if result.is_a?(Array) && result.first == :cards
|
|
result.last
|
|
else
|
|
result
|
|
end
|
|
end
|
|
|
|
def build_stats(deck)
|
|
{
|
|
cards_count: deck.size,
|
|
average_elixir: (deck.sum(&:elixir_cost).to_f / deck.size).round(2),
|
|
spells_count: deck.count { |c| c.type == "spell" },
|
|
troops_count: deck.count { |c| c.type == "troop" },
|
|
buildings_count: deck.count { |c| c.type == "building" },
|
|
champions_count: deck.count { |c| c.rarity == "champion" },
|
|
heroes_count: deck.count { |c| c.rarity == "hero" },
|
|
evolutions_count: deck.count { |c| c.rarity == "evolution" },
|
|
meta_cards_count: deck.count { |c| c.is_meta == 1 }
|
|
}
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end |