module Api.Elo exposing ( EloStat , HealthCheck , Leaderboard , Match , Participant , Players , getHealth , getLeaderboard , getMatches , getPlayers ) {-| # ELO API This module contains functions to address the API of the ELO tracker. -} import Http import Json.Decode as D type alias EloStat = { draws : Int , elo : Int , losses : Int , name : String , url : String , version : Maybe String , wins : Int } type alias HealthCheck = { ok : Bool, periodicMatches : Bool } type alias Leaderboard = List { name : String, players : Players } type alias Match = { name : String , participants : List Participant , timestamp : String } type alias Participant = { name : String , result : String , url : String , version : Maybe String } type alias Players = List EloStat eloStatDecoder : D.Decoder EloStat eloStatDecoder = D.map7 EloStat (D.field "draws" D.int) (D.field "elo" D.int) (D.field "losses" D.int) (D.field "name" D.string) (D.field "url" D.string) (D.maybe <| D.field "version" D.string) (D.field "wins" D.int) getHealth : { baseUrl : String , toMsg : Result Http.Error HealthCheck -> msg } -> Cmd msg getHealth data = Http.get { url = data.baseUrl ++ "/health" , expect = Http.expectJson data.toMsg healthCheckDecoder } getLeaderboard : { baseUrl : String , toMsg : Result Http.Error Leaderboard -> msg } -> Cmd msg getLeaderboard data = Http.get { url = data.baseUrl ++ "/leaderboard" , expect = Http.expectJson data.toMsg leaderboardDecoder } getMatches : { baseUrl : String , toMsg : Result Http.Error (List Match) -> msg } -> Cmd msg getMatches data = Http.get { url = data.baseUrl ++ "/matches" , expect = Http.expectJson data.toMsg (D.list matchDecoder) } getPlayers : { baseUrl : String , toMsg : Result Http.Error Players -> msg } -> Cmd msg getPlayers data = Http.get { url = data.baseUrl ++ "/players" , expect = Http.expectJson data.toMsg playersDecoder } healthCheckDecoder : D.Decoder HealthCheck healthCheckDecoder = D.map2 HealthCheck (D.field "ok" D.bool) (D.field "periodic_matches" D.bool) leaderboardDecoder : D.Decoder Leaderboard leaderboardDecoder = D.map2 (\name players -> { name = name, players = players }) (D.field "name" D.string) (D.field "players" playersDecoder) |> D.list matchDecoder : D.Decoder Match matchDecoder = D.map3 Match (D.field "name" D.string) (D.field "participants" <| D.list participantDecoder) (D.field "timestamp" D.string) participantDecoder : D.Decoder Participant participantDecoder = D.map4 Participant (D.field "name" D.string) (D.field "result" D.string) (D.field "url" D.string) (D.maybe <| D.field "version" D.string) playersDecoder : D.Decoder Players playersDecoder = D.list eloStatDecoder