mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
reorder seed so that themes can be added to seed endgames
This commit is contained in:
+55
-15
@@ -19,35 +19,42 @@ alias Chesstrainer.Repo
|
|||||||
alias Chesstrainer.Endgames.Endgame
|
alias Chesstrainer.Endgames.Endgame
|
||||||
alias Chesstrainer.Themes.Theme
|
alias Chesstrainer.Themes.Theme
|
||||||
alias Chesstrainer.Repo.Seeds.Endgames
|
alias Chesstrainer.Repo.Seeds.Endgames
|
||||||
alias Chesstrainer.Repo.Seeds.Themes
|
alias Chesstrainer.Repo.Seeds.Themes, as: SeedThemes
|
||||||
|
|
||||||
now = DateTime.utc_now() |> DateTime.truncate(:second)
|
now = DateTime.utc_now() |> DateTime.truncate(:second)
|
||||||
|
|
||||||
IO.puts("🌱 Seeding basic endgames into DB...")
|
# Seed maps may list `themes: ["Lucena", ...]`. Names are matched to seeded
|
||||||
|
# themes case-insensitively, and names missing from seeds/themes.ex are created.
|
||||||
endgames = Endgames.all()
|
endgames =
|
||||||
|
Enum.map(Endgames.all(), fn endgame ->
|
||||||
Repo.insert_all(
|
{theme_names, attrs} = Map.pop(endgame, :themes, [])
|
||||||
Endgame,
|
fen = Chesstrainer.FEN.normalize(attrs.fen)
|
||||||
Enum.map(endgames, fn endgame ->
|
|
||||||
fen = Chesstrainer.FEN.normalize(endgame.fen)
|
|
||||||
{:ok, key} = Chesstrainer.Key.from_fen(fen)
|
{:ok, key} = Chesstrainer.Key.from_fen(fen)
|
||||||
|
|
||||||
endgame
|
themes =
|
||||||
|
theme_names
|
||||||
|
|> Enum.map(&String.trim/1)
|
||||||
|
|> Enum.reject(&(&1 == ""))
|
||||||
|
|> Enum.uniq_by(&String.downcase/1)
|
||||||
|
|
||||||
|
row =
|
||||||
|
attrs
|
||||||
|> Map.put(:fen, fen)
|
|> Map.put(:fen, fen)
|
||||||
|> Map.put(:id, Ecto.UUID.generate())
|
|> Map.put(:id, Ecto.UUID.generate())
|
||||||
|> Map.put(:public_code, Endgame.generate_public_code())
|
|> Map.put(:public_code, Endgame.generate_public_code())
|
||||||
|> Map.put(:key, key)
|
|> Map.put(:key, key)
|
||||||
|> Map.merge(%{inserted_at: now, updated_at: now})
|
|> Map.merge(%{inserted_at: now, updated_at: now})
|
||||||
end),
|
|
||||||
on_conflict: :nothing
|
|
||||||
)
|
|
||||||
|
|
||||||
IO.puts("✅ Done seeding endgames.")
|
%{fen: fen, themes: themes, row: row}
|
||||||
|
end)
|
||||||
|
|
||||||
IO.puts("🌱 Seeding endgame themes into DB...")
|
IO.puts("🌱 Seeding endgame themes into DB...")
|
||||||
|
|
||||||
theme_names = Themes.all()
|
theme_names =
|
||||||
|
(SeedThemes.all() ++ Enum.flat_map(endgames, & &1.themes))
|
||||||
|
|> Enum.map(&String.trim/1)
|
||||||
|
|> Enum.reject(&(&1 == ""))
|
||||||
|
|> Enum.uniq_by(&String.downcase/1)
|
||||||
|
|
||||||
Repo.insert_all(
|
Repo.insert_all(
|
||||||
Theme,
|
Theme,
|
||||||
@@ -57,4 +64,37 @@ Repo.insert_all(
|
|||||||
on_conflict: :nothing
|
on_conflict: :nothing
|
||||||
)
|
)
|
||||||
|
|
||||||
|
theme_id_by_name =
|
||||||
|
Theme
|
||||||
|
|> Repo.all()
|
||||||
|
|> Enum.filter(&(&1.category == :endgame))
|
||||||
|
|> Map.new(&{String.downcase(&1.name), &1.id})
|
||||||
|
|
||||||
IO.puts("✅ Done seeding endgame themes.")
|
IO.puts("✅ Done seeding endgame themes.")
|
||||||
|
|
||||||
|
IO.puts("🌱 Seeding basic endgames into DB...")
|
||||||
|
|
||||||
|
Repo.insert_all(Endgame, Enum.map(endgames, & &1.row), on_conflict: :nothing)
|
||||||
|
|
||||||
|
endgame_id_by_fen =
|
||||||
|
Endgame
|
||||||
|
|> Repo.all()
|
||||||
|
|> Map.new(&{&1.fen, &1.id})
|
||||||
|
|
||||||
|
links =
|
||||||
|
Enum.flat_map(endgames, fn %{fen: fen, themes: themes} ->
|
||||||
|
endgame_id = Map.fetch!(endgame_id_by_fen, fen)
|
||||||
|
|
||||||
|
Enum.map(themes, fn name ->
|
||||||
|
%{
|
||||||
|
endgame_id: Ecto.UUID.dump!(endgame_id),
|
||||||
|
theme_id: Map.fetch!(theme_id_by_name, String.downcase(name))
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
if links != [] do
|
||||||
|
Repo.insert_all("endgames_themes", links, on_conflict: :nothing)
|
||||||
|
end
|
||||||
|
|
||||||
|
IO.puts("✅ Done seeding endgames.")
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ defmodule Chesstrainer.Repo.Seeds.Endgames.Win do
|
|||||||
color: :white,
|
color: :white,
|
||||||
message: "Rook vs king endgame",
|
message: "Rook vs king endgame",
|
||||||
notes: "Basic rook endgame",
|
notes: "Basic rook endgame",
|
||||||
result: :win
|
result: :win,
|
||||||
|
themes: ["rook endgame"]
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
fen: "8/8/8/1P4p1/5k2/5p2/P6K/8 b - - 0 0",
|
fen: "8/8/8/1P4p1/5k2/5p2/P6K/8 b - - 0 0",
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
defmodule Chesstrainer.Repo.Seeds.Themes do
|
defmodule Chesstrainer.Repo.Seeds.Themes do
|
||||||
|
# Endgame seed maps may also reference themes with `themes: ["Lucena", ...]`;
|
||||||
|
# names missing from this list are created when the seeds run.
|
||||||
def all do
|
def all do
|
||||||
[
|
[
|
||||||
"mate in 2",
|
"mate in 2",
|
||||||
|
|||||||
Reference in New Issue
Block a user