Ver Fonte

Add sorting in card explorer

Cadel Watson há 4 meses atrás
pai
commit
8d74d8a701
3 ficheiros alterados com 114 adições e 18 exclusões
  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(..)
     , ManaCost(..)
     , Power(..)
+    , Rarity(..)
     , alpa
     , alsa
     , calculatePerformanceDistributions
+    , colorsSortOrder
     , gihwr
     , iwd
     , manaCostToSymbol
     , parseManaCost
     , parsePower
     , pickRate
+    , raritySortOrder
     )
 
 import Parser as P exposing ((|.), (|=), Parser, Trailing(..))
@@ -48,6 +51,14 @@ type Power
     | VariablePower
 
 
+type Rarity
+    = Common
+    | Uncommon
+    | Rare
+    | Mythic
+    | NoRarity
+
+
 type alias CardData =
     { details : CardDetails
     , performance : CardPerformanceData
@@ -66,6 +77,7 @@ type alias CardDetails =
     , rawManaCost : String
     , manaCost : Maybe (List ManaCost)
     , imageUrl : String
+    , rarity : Rarity
     }
 
 
@@ -399,3 +411,55 @@ powerP =
         , constantPlusVariablePowerP
         , 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)
 
-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 Json.Decode as Decode exposing (Decoder, decodeString)
 import Json.Decode.Pipeline exposing (custom, optional, required)
@@ -278,6 +278,7 @@ decodeCardDetails =
                         )
                 )
             |> required "image_uris" (Decode.field "large" Decode.string)
+            |> optional "rarity" decodeRarity NoRarity
         )
 
 
@@ -391,3 +392,25 @@ decodeCardType =
                 else
                     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 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.Attributes as CA
 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 model =
     let
         allCards : List CardData
         allCards =
             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 card =
@@ -849,7 +858,7 @@ viewAllCards model =
           div [ class "flex flex-col space-between col-span-8 " ]
             [ Keyed.node "div"
                 [ 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)
             ]
         ]