Browse Source

Trigger fetch

Cadel Watson 1 năm trước cách đây
mục cha
commit
b0357858c8
4 tập tin đã thay đổi với 94 bổ sung21 xóa
  1. 18 0
      server/main.py
  2. 15 1
      src/API.elm
  3. 15 16
      src/Database.elm
  4. 46 4
      src/Main.elm

+ 18 - 0
server/main.py

@@ -62,3 +62,21 @@ def read_item(set_name: str):
         card_data = json.load(f)
 
     return {"data": card_data}
+
+
+@app.get("/sets/{set_name}")
+def read_item(set_name: str):
+    if set_name not in discover_sets():
+        return {"error": "Set not found"}
+
+    card_ratings_path = os.path.join("data", "sets", set_name, "card-ratings-all.json")
+
+    with open(card_ratings_path) as f:
+        card_data = json.load(f)
+
+    card_ratings_path = os.path.join("data", "sets", set_name, "card-ratings-all.json")
+
+    with open(card_ratings_path) as f:
+        ratings_data = json.load(f)
+
+    return {"code": set_name, "data": {"cards": card_data, "ratings": ratings_data}}

+ 15 - 1
src/API.elm

@@ -1,5 +1,6 @@
-module API exposing (getSets)
+module API exposing (getSetData, getSets)
 
+import Database exposing (Database)
 import Http
 import Json.Decode as Decode
 
@@ -26,6 +27,19 @@ getSets onSuccess =
         }
 
 
+getSetData : String -> (Result String ( String, Maybe Database ) -> msg) -> Cmd msg
+getSetData setCode onSuccess =
+    Http.request
+        { method = "GET"
+        , headers = []
+        , url = makeApiUrl [ "sets", setCode ]
+        , body = Http.emptyBody
+        , expect = Http.expectJson (Result.mapError httpErrorToString >> onSuccess) Database.decoder
+        , timeout = Nothing
+        , tracker = Just setCode
+        }
+
+
 httpErrorToString : Http.Error -> String
 httpErrorToString error =
     case error of

+ 15 - 16
src/Database.elm

@@ -1,4 +1,4 @@
-module Database exposing (Database, decode, get, getAll)
+module Database exposing (Database, decode, decoder, get, getAll)
 
 import Card exposing (CardData, CardDetails, CardPerformanceData, CardType(..), ManaColor(..), Power(..), parseManaCost)
 import Dict exposing (Dict)
@@ -23,23 +23,23 @@ getAll (Database db) =
 
 decode : String -> Result String ( String, Maybe Database )
 decode setData =
-    let
-        decoder : Decoder ( String, Maybe Database )
-        decoder =
-            Decode.map2 pair
-                (Decode.field "code" Decode.string)
-                (Decode.field "data"
-                    (Decode.nullable
-                        (Decode.map2 createDatabase
-                            (Decode.field "ratings" decodePerformanceData)
-                            (Decode.field "cards" decodeSetData)
-                        )
-                    )
-                )
-    in
     decodeString decoder setData |> Result.mapError Decode.errorToString
 
 
+decoder : Decoder ( String, Maybe Database )
+decoder =
+    Decode.map2 pair
+        (Decode.field "code" Decode.string)
+        (Decode.field "data"
+            (Decode.nullable
+                (Decode.map2 createDatabase
+                    (Decode.field "ratings" decodePerformanceData)
+                    (Decode.field "cards" decodeSetData)
+                )
+            )
+        )
+
+
 createDatabase : Dict String CardPerformanceData -> Dict String CardDetails -> Database
 createDatabase performanceData detailsData =
     Dict.merge
@@ -56,7 +56,6 @@ decodePerformanceData : Decoder (Dict String CardPerformanceData)
 decodePerformanceData =
     Decode.list decodeCardPerformance
         |> Decode.map Dict.fromList
-        |> Decode.map (Debug.log "perf")
 
 
 decodeSetData : Decoder (Dict String CardDetails)

+ 46 - 4
src/Main.elm

@@ -58,9 +58,9 @@ type alias ToolboxAccordion =
 
 type SetLoadStatus
     = CheckingLocalData
+    | NoLocalData
     | HasLocalData Database.Database
     | FetchingRemoteData
-    | SavingRemoteData
 
 
 type alias ChooseSetModel =
@@ -114,6 +114,8 @@ type Msg
     | ToggleDeckList
     | SetDeckSortMethod Deck.DeckSortMethod
     | IOGotSets (Result String (List String))
+    | IOFetchSetData String
+    | IOGotSetData (Result String ( String, Maybe Database.Database ))
     | PortReceiveDoesSetHaveLocalData String
 
 
@@ -185,18 +187,25 @@ update msg model =
 
                         markSetDataNotAvailable : String -> Dict String SetLoadStatus -> Dict String SetLoadStatus
                         markSetDataNotAvailable setCode =
-                            Dict.insert setCode FetchingRemoteData
+                            Dict.insert setCode NoLocalData
                     in
                     case Database.decode unparsedData of
                         Ok ( setCode, Just db ) ->
                             ( ChooseSet { mdl | sets = Maybe.map (markSetDataLoaded setCode db) mdl.sets }, Cmd.none )
 
                         Ok ( setCode, Nothing ) ->
-                            ( ChooseSet { mdl | sets = Maybe.map (markSetDataNotAvailable setCode) mdl.sets }, Cmd.none )
+                            ( ChooseSet { mdl | sets = Maybe.map (markSetDataNotAvailable setCode) mdl.sets }
+                            , Cmd.none
+                            )
 
                         Err e ->
                             ( Error { error = "Error decoding local set data (" ++ e ++ ")" }, Cmd.none )
 
+                IOFetchSetData setCode ->
+                    ( ChooseSet { mdl | sets = Maybe.map (Dict.insert setCode FetchingRemoteData) mdl.sets }
+                    , API.getSetData setCode IOGotSetData
+                    )
+
                 _ ->
                     ( ChooseSet mdl, Cmd.none )
 
@@ -260,6 +269,12 @@ update msg model =
                 IOGotSets _ ->
                     ( Ready mdl, Cmd.none )
 
+                IOGotSetData _ ->
+                    ( Ready mdl, Cmd.none )
+
+                IOFetchSetData _ ->
+                    ( Ready mdl, Cmd.none )
+
                 PortReceiveDoesSetHaveLocalData _ ->
                     ( Ready mdl, Cmd.none )
 
@@ -286,11 +301,38 @@ view model =
 
 viewChooseSet : ChooseSetModel -> Html Msg
 viewChooseSet model =
+    let
+        viewLoadStatus : String -> SetLoadStatus -> Html Msg
+        viewLoadStatus setCode s =
+            case s of
+                FetchingRemoteData ->
+                    text "Loading..."
+
+                CheckingLocalData ->
+                    text "Loading..."
+
+                NoLocalData ->
+                    button [ onClick (IOFetchSetData setCode) ] [ text "Download" ]
+
+                HasLocalData database ->
+                    button [] [ text "Open" ]
+    in
     div [ class "w-full h-full bg-slate-100 flex justify-center items-center" ]
         [ div [ class "max-w-2xl max-h-2xl" ]
             [ case model.sets of
                 Just sets ->
-                    ul [] (List.map (\s -> li [] [ text s ]) (Dict.keys sets))
+                    ul []
+                        (List.map
+                            (\s ->
+                                case Dict.get s sets of
+                                    Just setStatus ->
+                                        li [] [ text s, viewLoadStatus s setStatus ]
+
+                                    Nothing ->
+                                        p [] [ text "Loading..." ]
+                            )
+                            (Dict.keys sets)
+                        )
 
                 Nothing ->
                     p [] [ text "Loading..." ]