Значительно изменил бд и генерацию так же добавил роли

This commit is contained in:
2026-03-25 20:44:28 +05:00
parent 0aeffffa56
commit d16af289fe
14 changed files with 485 additions and 67 deletions

View File

@@ -0,0 +1,71 @@
# frozen_string_literal: true
require "json"
module ClashDeckGenerator2
module Services
class CardRolesRepository
ROLES_PATH = File.expand_path("../../config/cards/roles.json", __dir__)
def all
@all ||= load_roles
end
def cards
all.fetch("cards", {})
end
def roles_for(card_name)
cards.fetch(card_name, [])
end
def has_role?(card_name, role)
roles_for(card_name).include?(role)
end
def meta
all.fetch("_meta", {})
end
private
def load_roles
raise "roles.json not found: #{ROLES_PATH}" unless File.exist?(ROLES_PATH)
parsed = JSON.parse(File.read(ROLES_PATH))
unless parsed.is_a?(Hash)
raise "roles.json must contain a JSON object at root"
end
unless parsed.key?("cards")
raise 'roles.json must contain "cards" key'
end
cards = parsed["cards"]
unless cards.is_a?(Hash)
raise '"cards" must be a JSON object'
end
cards.each do |card_name, roles|
unless card_name.is_a?(String) && !card_name.strip.empty?
raise "Invalid card name in roles.json: #{card_name.inspect}"
end
unless roles.is_a?(Array)
raise "Roles for #{card_name} must be an array"
end
roles.each do |role|
unless role.is_a?(String) && !role.strip.empty?
raise "Invalid role for #{card_name}: #{role.inspect}"
end
end
end
parsed
end
end
end
end