wip champion picked summoner spell
All checks were successful
ci / docker (push) Successful in 3m45s

This commit is contained in:
Álvaro Girona Arias 2024-06-06 19:03:30 +02:00
parent 9b955641d0
commit fe3d040978
6 changed files with 186 additions and 3 deletions

View File

@ -18,12 +18,10 @@ defmodule LolAnalytics.Analyzer.ChampionAnalyzer do
@doc """ @doc """
iex> LolAnalytics.Analyzer.ChampionAnalyzer.analyze(:url, "http://localhost:9000/ranked/14.9.580.2108/EUW1_6923309745.json") iex> LolAnalytics.Analyzer.ChampionAnalyzer.analyze(:url, "http://localhost:9000/ranked/14.9.580.2108/EUW1_6923309745.json")
""" """
@spec analyze(any(), any()) :: none()
@impl true @impl true
def analyze(:url, path) do def analyze(:url, path) do
data = HTTPoison.get!(path) data = HTTPoison.get!(path)
analyze(:data, data.body) analyze(:data, data.body)
:ok
end end
@impl true @impl true

View File

@ -1,9 +1,12 @@
defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo do defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo do
import Ecto.Query
alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellSchema alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellSchema
alias LoLAnalytics.Repo alias LoLAnalytics.Repo
def get_or_create(spell_id) do def get_or_create(spell_id) do
spell = Repo.get(SummonerSpellSchema, spell_id: spell_id) query = from s in SummonerSpellSchema, where: s.spell_id == ^spell_id
spell = Repo.one(query)
case spell do case spell do
nil -> nil ->

View File

@ -0,0 +1,52 @@
defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.FactProcessor do
alias LolAnalytics.Facts.ChampionPickedSummonerSpell
def process_game_at_url(url) do
data = HTTPoison.get!(url)
process_game_data(data.body)
end
defp process_game_data(data) do
decoded_match = Poison.decode!(data, as: %LoLAPI.Model.MatchResponse{})
participants = decoded_match.info.participants
version = extract_game_version(decoded_match)
participants
|> Enum.each(fn participant = %LoLAPI.Model.Participant{} ->
if participant.teamPosition != "" do
attrs_spell_1 = %{
champion_id: participant.championId,
match_id: decoded_match.metadata.matchId,
is_win: participant.win,
summoner_spell_id: participant.summoner1Id,
game_length_seconds: decoded_match.info.gameDuration,
queue_id: decoded_match.info.queueId,
puuid: participant.puuid,
team_position: participant.teamPosition,
patch_number: version
}
attrs_spell_2 = %{
champion_id: participant.championId,
match_id: decoded_match.metadata.matchId,
is_win: participant.win,
summoner_spell_id: participant.summoner2Id,
game_length_seconds: decoded_match.info.gameDuration,
queue_id: decoded_match.info.queueId,
puuid: participant.puuid,
team_position: participant.teamPosition,
patch_number: version
}
ChampionPickedSummonerSpell.Repo.insert(attrs_spell_1)
ChampionPickedSummonerSpell.Repo.insert(attrs_spell_2)
end
end)
end
defp extract_game_version(game_data) do
game_data.info.gameVersion
|> String.split(".")
|> Enum.take(2)
|> Enum.join(".")
end
end

View File

@ -0,0 +1,65 @@
defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
import Ecto.Query
alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo
alias LolAnalytics.Dimensions.Champion.ChampionSchema
alias LolAnalytics.Facts.ChampionPickedSummonerSpell.Schema
alias LolAnalytics.Dimensions.Player.PlayerRepo
alias LolAnalytics.Dimensions.Champion.ChampionRepo
alias LolAnalytics.Dimensions.Match.MatchRepo
alias LoLAnalytics.Repo
@type insert_attrs :: %{
match_id: String.t(),
champion_id: String.t(),
puuid: String.t(),
summoner_spell_id: :integer
}
@spec insert(insert_attrs()) :: any()
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)
_spell = SummonerSpellRepo.get_or_create(attrs.summoner_spell_id)
changeset = Schema.changeset(%Schema{}, attrs)
IO.inspect(attrs)
Repo.insert(changeset)
|> IO.inspect()
end
def get_champion_picked_summoners() do
query =
from f in Schema,
join: c in ChampionSchema,
on: c.champion_id == f.champion_id,
select: %{
wins: fragment("count(CASE WHEN ? THEN 1 END)", f.is_win),
win_rate:
fragment(
"
((cast(count(CASE WHEN ? THEN 1 END) as float) / cast(count(*) as float)) * 100.0
)",
f.is_win
),
id: f.champion_id,
spell_id: f.spell_id,
name: c.name,
image: c.image,
team_position: f.team_position,
total_games: count("*")
},
group_by: [f.champion_id, f.spell_id, c.image, c.name, f.team_position]
Repo.all(query)
end
def list_facts() do
Repo.all(Schema)
end
end

View File

@ -0,0 +1,34 @@
defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Schema do
use Ecto.Schema
import Ecto.Changeset
@params [
:champion_id,
:summoner_spell_id,
:match_id,
:is_win,
:game_length_seconds,
:queue_id,
:team_position,
:puuid
]
schema "fact_champion_picked_summoner_spell" do
field :champion_id, :integer
field :summoner_spell_id, :integer
field :match_id, :string
field :is_win, :boolean
field :game_length_seconds, :integer
field :queue_id, :integer
field :team_position, :string
field :puuid, :string
end
def changeset(fact = %__MODULE__{}, attrs \\ %{}) do
fact
|> cast(attrs, @params)
|> validate_required(@params)
|> unique_constraint([:puuid, :match_id, :summoner_spell_id])
end
end

View File

@ -0,0 +1,31 @@
defmodule LoLAnalytics.Repo.Migrations.SummonerSpellWinRate do
use Ecto.Migration
def change do
create table("fact_champion_picked_summoner_spell") do
add :champion_id, references("dim_champion", column: :champion_id, type: :integer)
add :summoner_spell_id, references("dim_summoner_spell", column: :spell_id, type: :integer)
add :match_id, references("dim_match", column: :match_id, type: :string)
add :is_win, :boolean
add :game_length_seconds, :integer
add :queue_id, :integer
add :team_position, :string
add :puuid, references("dim_player", column: :puuid, type: :string)
add :patch_number, references("dim_patch", column: :patch_number, type: :string)
end
create index(
"fact_champion_picked_summoner_spell",
[:puuid, :match_id, :summoner_spell_id],
unique: true
)
create index("fact_champion_picked_summoner_spell", [
:is_win,
:team_position,
:queue_id,
:summoner_spell_id,
:patch_number
])
end
end