Everything but the LFS

This commit is contained in:
rncwnd 2026-01-05 12:58:28 +00:00
commit 153b188406
Signed by: rncwnd
GPG key ID: 05EF307E0712FDAA
50 changed files with 4161 additions and 0 deletions

View file

@ -0,0 +1,34 @@
defmodule BoosterTutor.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
BoosterTutorWeb.Telemetry,
BoosterTutor.Repo,
{DNSCluster, query: Application.get_env(:booster_tutor, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: BoosterTutor.PubSub},
# Start a worker by calling: BoosterTutor.Worker.start_link(arg)
# {BoosterTutor.Worker, arg},
# Start to serve requests, typically the last entry
BoosterTutorWeb.Endpoint
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: BoosterTutor.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
@impl true
def config_change(changed, _new, removed) do
BoosterTutorWeb.Endpoint.config_change(changed, removed)
:ok
end
end

View file

@ -0,0 +1,71 @@
defmodule BoosterTutor.BoosterGenerator do
require Logger
@type retail_booster_types :: :play | :collector
def string_to_booster_type!(param) do
case param do
"play" -> :play
"collector" -> :collector
_ -> raise("Invalid booster type given #{param}")
end
end
@spec generate_booster_for_set(String, String) :: nil
def generate_booster_for_set(set_code, booster_type) do
booster_type = string_to_booster_type!(booster_type)
generate_retail_booster_for_set(set_code, booster_type)
end
@spec generate_retail_booster_for_set(String, retail_booster_types()) :: :ok
def generate_retail_booster_for_set(set_code, booster_type) do
case booster_type do
:play -> generate_play_booster_for_set(set_code)
:collector -> raise("Not implemented collector boosters yet")
end
end
@spec generate_play_booster_for_set(String) :: map()
def generate_play_booster_for_set(set_code) do
set_json = load_set_file(set_code)
selected_config = select_booster_config(set_json["booster"]["play"]["boosters"])
sheets = set_json["booster"]["play"]["sheets"]
pulls = Enum.reduce(selected_config["contents"], fn({sheet, quantity}, acc) ->
IO.inspect(sheet, label: "sheet")
IO.inspect(quantity, label: "quantity")
selected_sheet = sheets[sheet]
IO.inspect(selected_sheet, label: "selected_sheet")
picks = pick_n_from_sheet(selected_sheet, quantity)
IO.inspect(picks, label: "picks")
Map.put(acc, sheet, picks)
end)
%{message: "OK", pulls: pulls}
end
@spec select_booster_config(list(map())) :: map()
def select_booster_config(booster_configs) do
duplicated_configs = Enum.map(booster_configs, fn config ->
weight = config["weight"]
List.duplicate(config, weight)
end) |> List.flatten()
final_config = List.first(Enum.take_random(duplicated_configs, 1))
Logger.info("Selected a random booster config. #{inspect(final_config, pretty: true)}")
final_config
end
@spec pick_n_from_sheet(map(), integer()) :: list(any())
def pick_n_from_sheet(sheet, quantity) do
# Build our weighted random thingy.
cards = sheet["cards"]
weights = cards |> Enum.map(fn {id, weight} -> {id, weight} end)
pulls = WeightedRandom.take_n(weights, quantity)
pulls
end
def load_set_file(set_code) do
project_root = Application.fetch_env!(:booster_tutor, :project_root)
path = "#{project_root}/priv/card_data/#{set_code}.json"
{:ok, body} = File.read(path)
{:ok, json} = Jason.decode(body)
json
end
end

View file

@ -0,0 +1,3 @@
defmodule BoosterTutor.Mailer do
use Swoosh.Mailer, otp_app: :booster_tutor
end

View file

@ -0,0 +1,5 @@
defmodule BoosterTutor.Repo do
use Ecto.Repo,
otp_app: :booster_tutor,
adapter: Ecto.Adapters.Postgres
end