@@ -0,0 +1,44 @@
|
||||
defmodule LolAnalytics.Dimensions.Champion.ChampionMetadata do
|
||||
alias LolAnalytics.Dimensions.Champion.ChampionRepo
|
||||
@champions_data_url "https://ddragon.leagueoflegends.com/cdn/14.11.1/data/en_US/champion.json"
|
||||
|
||||
use GenServer
|
||||
|
||||
def update_metadata() do
|
||||
{:ok, %{"data" => data}} = get_champions()
|
||||
|
||||
data
|
||||
|> Enum.each(&save_metadata/1)
|
||||
end
|
||||
|
||||
defp get_champions() do
|
||||
with {:ok, resp} <- HTTPoison.get(@champions_data_url),
|
||||
data <- Poison.decode(resp.body) do
|
||||
data
|
||||
else
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_metadata({champion, info}) do
|
||||
%{
|
||||
"image" => %{
|
||||
"full" => full_image
|
||||
},
|
||||
"name" => name,
|
||||
"key" => key_string
|
||||
} = info
|
||||
|
||||
attrs = %{
|
||||
image: full_image,
|
||||
name: name
|
||||
}
|
||||
|
||||
{champion_id, _} = Integer.parse(key_string)
|
||||
|
||||
ChampionRepo.update(champion_id, attrs)
|
||||
|
||||
info
|
||||
|> IO.inspect()
|
||||
end
|
||||
end
|
||||
@@ -18,6 +18,13 @@ defmodule LolAnalytics.Dimensions.Champion.ChampionRepo do
|
||||
end
|
||||
end
|
||||
|
||||
def update(champion_id, attrs) do
|
||||
get_or_create(champion_id)
|
||||
|> ChampionSchema.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@spec list_champions() :: any()
|
||||
def list_champions() do
|
||||
Repo.all(ChampionSchema)
|
||||
end
|
||||
|
||||
@@ -4,12 +4,14 @@ defmodule LolAnalytics.Dimensions.Champion.ChampionSchema do
|
||||
|
||||
schema "dim_champion" do
|
||||
field :champion_id, :integer
|
||||
field :name, :string
|
||||
field :image, :string
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def changeset(champion = %__MODULE__{}, attrs \\ %{}) do
|
||||
champion
|
||||
|> cast(attrs, [:champion_id])
|
||||
|> cast(attrs, [:champion_id, :name, :image])
|
||||
|> validate_required([:champion_id])
|
||||
end
|
||||
end
|
||||
|
||||
+60
-8
@@ -1,6 +1,7 @@
|
||||
defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo do
|
||||
import Ecto.Query
|
||||
|
||||
alias LolAnalytics.Dimensions.Champion.ChampionSchema
|
||||
alias LolAnalytics.Dimensions.Player.PlayerRepo
|
||||
alias LolAnalytics.Dimensions.Champion.ChampionRepo
|
||||
alias LolAnalytics.Dimensions.Match.MatchRepo
|
||||
@@ -9,19 +10,70 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo do
|
||||
alias LoLAnalytics.Repo
|
||||
|
||||
def insert(attrs) do
|
||||
match = MatchRepo.get_or_create(attrs.match_id)
|
||||
champion = ChampionRepo.get_or_create(attrs.champion_id)
|
||||
player = PlayerRepo.get_or_create(attrs.puuid)
|
||||
IO.puts(">>>>")
|
||||
IO.inspect(attrs)
|
||||
_match = MatchRepo.get_or_create(attrs.match_id)
|
||||
_champion = ChampionRepo.get_or_create(attrs.champion_id)
|
||||
_player = PlayerRepo.get_or_create(attrs.puuid)
|
||||
changeset = ChampionPlayedGameSchema.changeset(%ChampionPlayedGameSchema{}, attrs)
|
||||
|
||||
IO.inspect(changeset)
|
||||
Repo.insert(changeset)
|
||||
# Repo.insert(match)
|
||||
end
|
||||
|
||||
def list_played_matches() do
|
||||
Repo.all(ChampionPlayedGameSchema)
|
||||
end
|
||||
|
||||
def get_win_rates2 do
|
||||
query2 =
|
||||
from m in ChampionPlayedGameSchema,
|
||||
join: c in ChampionSchema,
|
||||
on: c.champion_id == m.champion_id,
|
||||
select: %{
|
||||
wins: count(m.is_win),
|
||||
win_rate:
|
||||
fragment(
|
||||
"
|
||||
((cast(count(CASE WHEN ? THEN 1 END) as float) / cast(count(*) as float)) * 100.0
|
||||
)",
|
||||
m.is_win
|
||||
),
|
||||
id: m.champion_id,
|
||||
name: c.name,
|
||||
image: c.image
|
||||
},
|
||||
group_by: [m.champion_id, c.image, c.name]
|
||||
|
||||
Repo.all(query2)
|
||||
end
|
||||
|
||||
def get_win_rates() do
|
||||
query = """
|
||||
SELECT
|
||||
(cast(count(CASE WHEN is_win THEN 1 END) as float) / cast(count(*) as float)) * 100.0 as win_rate,
|
||||
count(CASE WHEN is_win THEN 1 END) as games_won,
|
||||
count(*) as total_games,
|
||||
champion_id
|
||||
FROM fact_champion_played_game
|
||||
|
||||
GROUP BY champion_id
|
||||
ORDER BY win_rate desc;
|
||||
"""
|
||||
|
||||
case Ecto.Adapters.SQL.query(Repo, query, []) do
|
||||
{:ok, %Postgrex.Result{rows: rows}} ->
|
||||
rows
|
||||
|> Enum.map(fn [win_rate, wins, games, champion_id] ->
|
||||
%{
|
||||
win_rate: win_rate,
|
||||
wins: wins,
|
||||
games: games,
|
||||
champion_id: champion_id
|
||||
}
|
||||
end)
|
||||
|
||||
{:error, _exception} ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
def get_win_rates_by_roles() do
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user