filtering by roles
ci / docker (push) Waiting to run

This commit is contained in:
2024-05-31 02:40:03 +02:00
parent 22b79f5376
commit 77b292e47e
6 changed files with 111 additions and 95 deletions
@@ -11,7 +11,7 @@
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
</script>
</head>
<body class="bg-white antialiased">
<body class="bg-slate-700 antialiased">
<%= @inner_content %>
</body>
</html>
@@ -0,0 +1,3 @@
defmodule LolAnalyticsWeb.ChampionLive.Champion do
defstruct [:id, :win_rate, :image, :name, :team_position, :wins, :total_games]
end
@@ -1,46 +1,95 @@
defmodule LoLAnalyticsWeb.ChampionLive.Index do
alias ElixirLS.LanguageServer.Providers.Completion.Reducers.Struct
alias LolAnalyticsWeb.ChampionLive.Mapper
use LoLAnalyticsWeb, :live_view
defstruct id: "", win_rate: 0, wins: 0, image: "", name: ""
@roles ["all", "TOP", "JUNGLE", "MIDDLE", "BOTTOM", "UTILITY"]
@roles [
%{title: "All", value: "all"},
%{title: "Top", value: "TOP"},
%{title: "Jungle", value: "JUNGLE"},
%{title: "Mid", value: "MIDDLE"},
%{title: "Bot", value: "BOTTOM"},
%{title: "Support", value: "UTILITY"}
]
@impl true
def mount(_params, _session, socket) do
champs = LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates2()
champs = LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates()
mapped =
champs
|> Enum.map(fn champ ->
Kernel.struct!(%__MODULE__{}, champ)
end)
|> Mapper.map_champs()
|> Enum.sort(&(&1.win_rate >= &2.win_rate))
{:ok,
stream(
socket,
:champions,
mapped
)
|> assign(:form, to_form(%{"name" => ""}))}
roles =
@roles
|> Enum.reduce(%{}, fn role, acc ->
Map.merge(acc, %{"#{role.value}" => false})
end)
|> Map.merge(%{"all" => true})
form =
Map.merge(
%{"name" => ""},
roles
)
socket =
socket
|> stream(
:champions,
mapped
)
|> assign(:form, to_form(form))
|> assign(:roles, @roles)
{:ok, socket}
end
@impl true
def handle_event("filter", params, socket) do
%{"name" => query_name} = params
%{
"name" => query_name,
"all" => all,
"TOP" => top,
"JUNGLE" => jungle,
"MIDDLE" => mid,
"BOTTOM" => bot,
"UTILITY" => utility
} = params
IO.inspect(params)
filter =
if all == "true" do
nil
else
%{
"TOP" => top == "true",
"JUNGLE" => jungle == "true",
"MIDDLE" => mid == "true",
"BOTTOM" => bot == "true",
"UTILITY" => utility == "true"
}
|> Enum.filter(fn {_k, v} -> v end)
|> Enum.map(fn {k, _v} -> k end)
end
IO.puts(">>>>> 3")
IO.inspect(filter)
champs =
LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates2()
LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates()
|> Enum.filter(fn %{name: name} ->
String.downcase(name) |> String.contains?(query_name)
end)
|> Enum.map(fn champ ->
Kernel.struct!(%__MODULE__{}, champ)
|> Enum.filter(fn champ ->
if filter != nil do
Enum.any?(filter, fn f -> f == champ.team_position end)
end
end)
|> Mapper.map_champs()
|> Enum.sort(&(&1.win_rate >= &2.win_rate))
IO.inspect(champs)
{:noreply,
stream(
socket,
@@ -49,8 +98,8 @@ defmodule LoLAnalyticsWeb.ChampionLive.Index do
)}
end
def handle_event("filter_by", %{"name" => name}, socket) do
{:noreply, assign(socket, %{filter_by: name})}
def handle_event("filter", unsigned_params, socket) do
{:noreply, socket}
end
@impl true
@@ -69,12 +118,4 @@ defmodule LoLAnalyticsWeb.ChampionLive.Index do
def handle_info({LoLAnalyticsWeb.ChampionLive.FormComponent, {:saved, champion}}, socket) do
{:noreply, stream_insert(socket, :champions, champion)}
end
# @impl true
# def handle_event("delete", %{"id" => id}, socket) do
# champion = Accounts.get_champion!(id)
# {:ok, _} = Accounts.delete_champion(champion)
# {:noreply, stream_delete(socket, :champions, champion)}
# end
end
@@ -4,42 +4,30 @@
<h1>Champions</h1>
<.form for={@form} phx-change="filter" phx-submit="save">
<.input type="text" field={@form["name"]} />
<.input type="checkbox" field={@form["all_roles"]} />
<div class="flex flex-col justify-between">
<div class="flex flex-row gap-2">
<.input type="checkbox" field={@form["top_role"]} />
<p>Top</p>
</div>
<div class="flex flex-row">
<.input type="checkbox" field={@form["middle_role"]} />
<p>Middle</p>
</div>
<div class="flex flex-row">
<.input type="checkbox" field={@form["jungle_role"]} />
<p>Jungle</p>
</div>
<div class="flex flex-row">
<.input type="checkbox" field={@form["bottom_role"]} />
<p>Bottom</p>
</div>
<div class="flex flex-row">
<.input type="checkbox" field={@form["utility_role"]} />
<p>Utility</p>
<div class="flex flex-col gap-2">
<.input type="text" field={@form["name"]} />
<div class="flex flex-row justify-between">
<%= for role <- @roles do %>
<div class="flex flex-row gap-2 align-middle">
<.input type="checkbox" field={@form[role.value]} />
<p><%= role.title %></p>
</div>
<% end %>
</div>
</div>
<button>Save</button>
<%!-- <button>Save</button> --%>
</.form>
<div id="champions" class="grid grid-cols-4 gap-4">
<%= for {_, champion} <- @streams.champions do %>
<div class="flex flex-col">
<div class="flex flex-col max-w-sm bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700">
<img src={"https://ddragon.leagueoflegends.com/cdn/14.11.1/img/champion/#{champion.image}"} />
<div class="flex-auto flex-col">
<div class="flex-auto flex-col p-4 ">
<p><%= champion.name %></p>
<p><%= champion.id %></p>
<p><%= champion.wins %> / <%= champion.total_games %></p>
<p><%= champion.win_rate %>%</p>
<p><%= champion.team_position %></p>
</div>
</div>
@@ -0,0 +1,13 @@
defmodule LolAnalyticsWeb.ChampionLive.Mapper do
alias LolAnalyticsWeb.ChampionLive.Champion
def map_champs(champs) do
champs
|> Enum.map(fn champ ->
%{
Kernel.struct!(%Champion{}, champ)
| win_rate: :erlang.float_to_binary(champ.win_rate, decimals: 2)
}
end)
end
end