Преглед на файлове

Add sorting in card explorer

Cadel Watson преди 4 месеца
родител
ревизия
8d74d8a701
променени са 3 файла, в които са добавени 114 реда и са изтрити 18 реда
  1. 64 0
      src/Card.elm
  2. 24 1
      src/Database.elm
  3. 26 17
      src/Main.elm

+ 64 - 0
src/Card.elm

@@ -7,15 +7,18 @@ module Card exposing
     , ManaColor(..)
     , ManaColor(..)
     , ManaCost(..)
     , ManaCost(..)
     , Power(..)
     , Power(..)
+    , Rarity(..)
     , alpa
     , alpa
     , alsa
     , alsa
     , calculatePerformanceDistributions
     , calculatePerformanceDistributions
+    , colorsSortOrder
     , gihwr
     , gihwr
     , iwd
     , iwd
     , manaCostToSymbol
     , manaCostToSymbol
     , parseManaCost
     , parseManaCost
     , parsePower
     , parsePower
     , pickRate
     , pickRate
+    , raritySortOrder
     )
     )
 
 
 import Parser as P exposing ((|.), (|=), Parser, Trailing(..))
 import Parser as P exposing ((|.), (|=), Parser, Trailing(..))
@@ -48,6 +51,14 @@ type Power
     | VariablePower
     | VariablePower
 
 
 
 
+type Rarity
+    = Common
+    | Uncommon
+    | Rare
+    | Mythic
+    | NoRarity
+
+
 type alias CardData =
 type alias CardData =
     { details : CardDetails
     { details : CardDetails
     , performance : CardPerformanceData
     , performance : CardPerformanceData
@@ -66,6 +77,7 @@ type alias CardDetails =
     , rawManaCost : String
     , rawManaCost : String
     , manaCost : Maybe (List ManaCost)
     , manaCost : Maybe (List ManaCost)
     , imageUrl : String
     , imageUrl : String
+    , rarity : Rarity
     }
     }
 
 
 
 
@@ -399,3 +411,55 @@ powerP =
         , constantPlusVariablePowerP
         , constantPlusVariablePowerP
         , constantPowerP
         , constantPowerP
         ]
         ]
+
+
+raritySortOrder : Rarity -> Int
+raritySortOrder r =
+    case r of
+        Mythic ->
+            0
+
+        Rare ->
+            1
+
+        Uncommon ->
+            2
+
+        Common ->
+            3
+
+        NoRarity ->
+            4
+
+
+colorsSortOrder : List ManaColor -> Int
+colorsSortOrder cs =
+    case cs of
+        -- Colorless
+        [] ->
+            5
+
+        -- Monocolor
+        [ x ] ->
+            case x of
+                Red ->
+                    0
+
+                Green ->
+                    1
+
+                Blue ->
+                    2
+
+                Black ->
+                    3
+
+                White ->
+                    4
+
+                Colorless ->
+                    5
+
+        -- Multicolor
+        _ ->
+            6

+ 24 - 1
src/Database.elm

@@ -1,6 +1,6 @@
 module Database exposing (Database, decode, decoder, encode, fromCardData, get, getAll)
 module Database exposing (Database, decode, decoder, encode, fromCardData, get, getAll)
 
 
-import Card exposing (CardData, CardDetails, CardPerformanceData, CardType(..), ManaColor(..), Power(..), parseManaCost, parsePower)
+import Card exposing (CardData, CardDetails, CardPerformanceData, CardType(..), ManaColor(..), Power(..), Rarity(..), parseManaCost, parsePower)
 import Dict exposing (Dict)
 import Dict exposing (Dict)
 import Json.Decode as Decode exposing (Decoder, decodeString)
 import Json.Decode as Decode exposing (Decoder, decodeString)
 import Json.Decode.Pipeline exposing (custom, optional, required)
 import Json.Decode.Pipeline exposing (custom, optional, required)
@@ -278,6 +278,7 @@ decodeCardDetails =
                         )
                         )
                 )
                 )
             |> required "image_uris" (Decode.field "large" Decode.string)
             |> required "image_uris" (Decode.field "large" Decode.string)
+            |> optional "rarity" decodeRarity NoRarity
         )
         )
 
 
 
 
@@ -391,3 +392,25 @@ decodeCardType =
                 else
                 else
                     Other
                     Other
             )
             )
+
+
+decodeRarity : Decoder Rarity
+decodeRarity =
+    Decode.string
+        |> Decode.map
+            (\r ->
+                if String.contains "mythic" r then
+                    Mythic
+
+                else if String.contains "rare" r then
+                    Rare
+
+                else if String.contains "uncommon" r then
+                    Uncommon
+
+                else if String.contains "common" r then
+                    Common
+
+                else
+                    NoRarity
+            )

+ 26 - 17
src/Main.elm

@@ -2,7 +2,7 @@ port module Main exposing (..)
 
 
 import API
 import API
 import Browser exposing (Document)
 import Browser exposing (Document)
-import Card exposing (CardData, CardPerformanceDistributions, calculatePerformanceDistributions, manaCostToSymbol)
+import Card exposing (CardData, CardPerformanceDistributions, CardType(..), calculatePerformanceDistributions, colorsSortOrder, manaCostToSymbol, raritySortOrder)
 import Chart as C
 import Chart as C
 import Chart.Attributes as CA
 import Chart.Attributes as CA
 import Components.Button as Button
 import Components.Button as Button
@@ -791,27 +791,36 @@ viewDraft model =
         ]
         ]
 
 
 
 
-getCardSortFn : SortOrder -> (CardData -> String)
-getCardSortFn sortOrder =
-    case sortOrder of
-        SortOrderDefault ->
-            \c -> c.details.name
-
-        SortOrderRarity ->
-            \c -> c.details.name
-
-        -- todo
-        SortOrderName ->
-            \c -> c.details.name
-
-
 viewAllCards : CardExplorerModel -> Html Msg
 viewAllCards : CardExplorerModel -> Html Msg
 viewAllCards model =
 viewAllCards model =
     let
     let
         allCards : List CardData
         allCards : List CardData
         allCards =
         allCards =
             Database.getAll model.database
             Database.getAll model.database
-                |> List.sortBy (getCardSortFn model.sortOrder)
+                |> (case model.sortOrder of
+                        SortOrderDefault ->
+                            List.sortBy
+                                (\c ->
+                                    ( if
+                                        c.details.cardType
+                                            /= Land
+                                      then
+                                        0
+
+                                      else
+                                        1
+                                    , colorsSortOrder
+                                        c.details.colors
+                                    , raritySortOrder c.details.rarity
+                                    )
+                                )
+
+                        SortOrderRarity ->
+                            List.sortBy (\c -> raritySortOrder c.details.rarity)
+
+                        SortOrderName ->
+                            List.sortBy (\c -> c.details.name)
+                   )
 
 
         viewCard : CardData -> ( String, Html Msg )
         viewCard : CardData -> ( String, Html Msg )
         viewCard card =
         viewCard card =
@@ -849,7 +858,7 @@ viewAllCards model =
           div [ class "flex flex-col space-between col-span-8 " ]
           div [ class "flex flex-col space-between col-span-8 " ]
             [ Keyed.node "div"
             [ Keyed.node "div"
                 [ class "flex-grow grid grid-cols-8 auto-rows-min gap-6 p-6" ]
                 [ class "flex-grow grid grid-cols-8 auto-rows-min gap-6 p-6" ]
-                (List.map viewCard (Database.getAll model.database))
+                (List.map viewCard allCards)
             ]
             ]
         ]
         ]