Rendering champions with win rates
ci / docker (push) Waiting to run

This commit is contained in:
2024-05-31 00:45:22 +02:00
parent 520c234a94
commit 22b79f5376
19 changed files with 853 additions and 9 deletions
@@ -0,0 +1,90 @@
defmodule LoLAnalyticsWeb.ChampionLive.FormComponent do
use LoLAnalyticsWeb, :live_component
alias LoLAnalytics.Accounts
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage champion records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="champion-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<:actions>
<.button phx-disable-with="Saving...">Save Champion</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{champion: champion} = assigns, socket) do
changeset = Accounts.change_champion(champion)
{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
end
@impl true
def handle_event("validate", %{"champion" => champion_params}, socket) do
changeset =
socket.assigns.champion
|> Accounts.change_champion(champion_params)
|> Map.put(:action, :validate)
{:noreply, assign_form(socket, changeset)}
end
def handle_event("save", %{"champion" => champion_params}, socket) do
save_champion(socket, socket.assigns.action, champion_params)
end
defp save_champion(socket, :edit, champion_params) do
case Accounts.update_champion(socket.assigns.champion, champion_params) do
{:ok, champion} ->
notify_parent({:saved, champion})
{:noreply,
socket
|> put_flash(:info, "Champion updated successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp save_champion(socket, :new, champion_params) do
case Accounts.create_champion(champion_params) do
{:ok, champion} ->
notify_parent({:saved, champion})
{:noreply,
socket
|> put_flash(:info, "Champion created successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end
@@ -0,0 +1,80 @@
defmodule LoLAnalyticsWeb.ChampionLive.Index do
alias ElixirLS.LanguageServer.Providers.Completion.Reducers.Struct
use LoLAnalyticsWeb, :live_view
defstruct id: "", win_rate: 0, wins: 0, image: "", name: ""
@roles ["all", "TOP", "JUNGLE", "MIDDLE", "BOTTOM", "UTILITY"]
@impl true
def mount(_params, _session, socket) do
champs = LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates2()
mapped =
champs
|> Enum.map(fn champ ->
Kernel.struct!(%__MODULE__{}, champ)
end)
|> Enum.sort(&(&1.win_rate >= &2.win_rate))
{:ok,
stream(
socket,
:champions,
mapped
)
|> assign(:form, to_form(%{"name" => ""}))}
end
def handle_event("filter", params, socket) do
%{"name" => query_name} = params
champs =
LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates2()
|> Enum.filter(fn %{name: name} ->
String.downcase(name) |> String.contains?(query_name)
end)
|> Enum.map(fn champ ->
Kernel.struct!(%__MODULE__{}, champ)
end)
|> Enum.sort(&(&1.win_rate >= &2.win_rate))
IO.inspect(champs)
{:noreply,
stream(
socket,
:champions,
champs
)}
end
def handle_event("filter_by", %{"name" => name}, socket) do
{:noreply, assign(socket, %{filter_by: name})}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Champions")
# |> assign(:champion, nil)
end
@impl true
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
@@ -0,0 +1,66 @@
<.header>
Listing Champions
</.header>
<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>
</div>
<button>Save</button>
</.form>
<div id="champions" class="grid grid-cols-4 gap-4">
<%= for {_, champion} <- @streams.champions do %>
<div class="flex flex-col">
<img src={"https://ddragon.leagueoflegends.com/cdn/14.11.1/img/champion/#{champion.image}"} />
<div class="flex-auto flex-col">
<p><%= champion.name %></p>
<p><%= champion.id %></p>
<p><%= champion.win_rate %>%</p>
</div>
</div>
<div class="sr-only">
<.link navigate={~p"/champions/#{champion}"}>Show</.link>
</div>
<% end %>
</div>
<.modal
:if={@live_action in [:new, :edit]}
id="champion-modal"
show
on_cancel={JS.patch(~p"/champions")}
>
<.live_component
module={LoLAnalyticsWeb.ChampionLive.FormComponent}
id={@champion.champion_id || :new}
title={@page_title}
action={@live_action}
champion={@champion}
patch={~p"/champions"}
/>
</.modal>
@@ -0,0 +1,21 @@
defmodule LoLAnalyticsWeb.ChampionLive.Show do
use LoLAnalyticsWeb, :live_view
alias LoLAnalytics.Accounts
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:champion, Accounts.get_champion!(id))}
end
defp page_title(:show), do: "Show Champion"
defp page_title(:edit), do: "Edit Champion"
end
@@ -0,0 +1,25 @@
<.header>
Champion <%= @champion.id %>
<:subtitle>This is a champion record from your database.</:subtitle>
<:actions>
<.link patch={~p"/champions/#{@champion}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit champion</.button>
</.link>
</:actions>
</.header>
<.list>
</.list>
<.back navigate={~p"/champions"}>Back to champions</.back>
<.modal :if={@live_action == :edit} id="champion-modal" show on_cancel={JS.patch(~p"/champions/#{@champion}")}>
<.live_component
module={LoLAnalyticsWeb.ChampionLive.FormComponent}
id={@champion.id}
title={@page_title}
action={@live_action}
champion={@champion}
patch={~p"/champions/#{@champion}"}
/>
</.modal>
@@ -0,0 +1,90 @@
defmodule LoLAnalyticsWeb.RoleLive.FormComponent do
use LoLAnalyticsWeb, :live_component
alias LoLAnalytics.Accounts
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage role records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="role-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<:actions>
<.button phx-disable-with="Saving...">Save Role</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{role: role} = assigns, socket) do
changeset = Accounts.change_role(role)
{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
end
@impl true
def handle_event("validate", %{"role" => role_params}, socket) do
changeset =
socket.assigns.role
|> Accounts.change_role(role_params)
|> Map.put(:action, :validate)
{:noreply, assign_form(socket, changeset)}
end
def handle_event("save", %{"role" => role_params}, socket) do
save_role(socket, socket.assigns.action, role_params)
end
defp save_role(socket, :edit, role_params) do
case Accounts.update_role(socket.assigns.role, role_params) do
{:ok, role} ->
notify_parent({:saved, role})
{:noreply,
socket
|> put_flash(:info, "Role updated successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp save_role(socket, :new, role_params) do
case Accounts.create_role(role_params) do
{:ok, role} ->
notify_parent({:saved, role})
{:noreply,
socket
|> put_flash(:info, "Role created successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end
@@ -0,0 +1,37 @@
defmodule LoLAnalyticsWeb.RoleLive.Index do
use LoLAnalyticsWeb, :live_view
alias LoLAnalytics.Accounts
alias LoLAnalytics.Accounts.Role
@roles ["ALL", "TOP", "MIDDLE", "JUNGLE", "UTILITY", "BOTTOM"]
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :roles, @roles)}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Roles")
|> assign(:role, nil)
end
@impl true
def handle_info({LoLAnalyticsWeb.RoleLive.FormComponent, {:saved, role}}, socket) do
{:noreply, stream_insert(socket, :roles, role)}
end
# @impl true
# def handle_event("delete", %{"id" => id}, socket) do
# role = Accounts.get_role!(id)
# {:ok, _} = Accounts.delete_role(role)
# {:noreply, stream_delete(socket, :roles, role)}
# end
end
@@ -0,0 +1,40 @@
<.header>
Listing Roles
<:actions>
<.link patch={~p"/roles/new"}>
<.button>New Role</.button>
</.link>
</:actions>
</.header>
<.table
id="roles"
rows={@streams.roles}
row_click={fn {_id, role} -> JS.navigate(~p"/roles/#{role}") end}
>
<:action :let={{_id, role}}>
<div class="sr-only">
<.link navigate={~p"/roles/#{role}"}>Show</.link>
</div>
<.link patch={~p"/roles/#{role}/edit"}>Edit</.link>
</:action>
<:action :let={{id, role}}>
<.link
phx-click={JS.push("delete", value: %{id: role.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
<.modal :if={@live_action in [:new, :edit]} id="role-modal" show on_cancel={JS.patch(~p"/roles")}>
<.live_component
module={LoLAnalyticsWeb.RoleLive.FormComponent}
id={@role.id || :new}
title={@page_title}
action={@live_action}
role={@role}
patch={~p"/roles"}
/>
</.modal>
@@ -0,0 +1,21 @@
defmodule LoLAnalyticsWeb.RoleLive.Show do
use LoLAnalyticsWeb, :live_view
alias LoLAnalytics.Accounts
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:role, Accounts.get_role!(id))}
end
defp page_title(:show), do: "Show Role"
defp page_title(:edit), do: "Edit Role"
end
@@ -0,0 +1,25 @@
<.header>
Role <%= @role.id %>
<:subtitle>This is a role record from your database.</:subtitle>
<:actions>
<.link patch={~p"/roles/#{@role}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit role</.button>
</.link>
</:actions>
</.header>
<.list>
</.list>
<.back navigate={~p"/roles"}>Back to roles</.back>
<.modal :if={@live_action == :edit} id="role-modal" show on_cancel={JS.patch(~p"/roles/#{@role}")}>
<.live_component
module={LoLAnalyticsWeb.RoleLive.FormComponent}
id={@role.id}
title={@page_title}
action={@live_action}
role={@role}
patch={~p"/roles/#{@role}"}
/>
</.modal>
@@ -18,6 +18,12 @@ defmodule LoLAnalyticsWeb.Router do
pipe_through :browser
get "/", PageController, :home
live "/champions", ChampionLive.Index, :index
live "/champions/new", ChampionLive.Index, :new
live "/champions/:id/edit", ChampionLive.Index, :edit
live "/champions/:id", ChampionLive.Show, :show
live "/champions/:id/show/edit", ChampionLive.Show, :edit
end
# Other scopes may use custom stacks.