Skip to content

Commit

Permalink
Introduce deck meta
Browse files Browse the repository at this point in the history
  • Loading branch information
lsocrate committed Oct 18, 2021
1 parent 37883be commit c0afee2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
57 changes: 41 additions & 16 deletions client/src/Deck.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Deck exposing
( Deck
, Decklist
, Faction
, copiesInDeck
, decoder
Expand All @@ -23,13 +24,26 @@ import Shared exposing (Collection)


type alias Deck =
{ decklist : Decklist
, meta : Meta
}


type alias Decklist =
{ agenda : Agenda
, haven : Haven
, faction : Faction
, library : Library
}


type alias Meta =
{ id : String
, name : String
, owner : String
}


type alias Agenda =
Maybe Cards.Agenda

Expand All @@ -46,7 +60,7 @@ type alias Library =
Dict Cards.Id ( Cards.Library, Int )


empty : Deck
empty : Decklist
empty =
{ agenda = Nothing
, haven = Nothing
Expand All @@ -55,7 +69,7 @@ empty =
}


isLegal : Deck -> Bool
isLegal : Decklist -> Bool
isLegal deck =
case ( deck.agenda, deck.haven ) of
( Just _, Just _ ) ->
Expand All @@ -66,7 +80,7 @@ isLegal deck =
False


setCard : Deck -> ( Cards.Card, Int ) -> Deck
setCard : Decklist -> ( Cards.Card, Int ) -> Decklist
setCard deck entry =
case entry of
( Cards.AgendaCard _, 0 ) ->
Expand Down Expand Up @@ -97,7 +111,7 @@ setCard deck entry =
deck


copiesInDeck : Deck -> Cards.Card -> Int
copiesInDeck : Decklist -> Cards.Card -> Int
copiesInDeck deck card =
case card of
Cards.AgendaCard { id } ->
Expand Down Expand Up @@ -135,30 +149,30 @@ copiesInDeck deck card =
Dict.get id deck.library |> Maybe.map Tuple.second |> Maybe.withDefault 0


isLeader : Deck -> Cards.Faction -> Bool
isLeader : Decklist -> Cards.Faction -> Bool
isLeader deck character =
Dict.get character.id deck.faction |> Maybe.map Tuple.second |> Maybe.withDefault False


leader : Deck -> Maybe Cards.Faction
leader : Decklist -> Maybe Cards.Faction
leader deck =
Dict.values deck.faction |> List.filter Tuple.second |> List.head |> Maybe.map Tuple.first


setLeader : Deck -> Cards.Faction -> Deck
setLeader : Decklist -> Cards.Faction -> Decklist
setLeader deck newLeader =
{ deck | faction = Dict.map (\_ ( char, _ ) -> ( char, char.id == newLeader.id )) deck.faction }


isValidFaction : Deck -> Bool
isValidFaction : Decklist -> Bool
isValidFaction deck =
Dict.isEmpty deck.faction
|| ((leader deck |> Maybe.map (always True) |> Maybe.withDefault False)
&& (Dict.size deck.faction == 7)
)


isValidLibrary : Deck -> Bool
isValidLibrary : Decklist -> Bool
isValidLibrary deck =
let
deckSize =
Expand All @@ -167,7 +181,7 @@ isValidLibrary deck =
(deckSize == 0) || (deckSize >= 40 && deckSize <= 60)


demoDeck : Collection -> Deck
demoDeck : Collection -> Decklist
demoDeck collection =
let
agenda =
Expand Down Expand Up @@ -241,7 +255,7 @@ demoDeck collection =
----------


encode : String -> Deck -> Maybe Encode.Value
encode : String -> Decklist -> Maybe Encode.Value
encode name deck =
case ( deck.agenda, deck.haven ) of
( Just agenda, Just haven ) ->
Expand Down Expand Up @@ -270,17 +284,28 @@ encode name deck =

decoder : Collection -> Decoder Deck
decoder collection =
Decode.map4 Deck
Decode.map2 Deck
(decklistDecoder collection)
metaDecoder


metaDecoder : Decoder Meta
metaDecoder =
Decode.map3 Meta
(Decode.field "id" Decode.string)
(Decode.field "name" Decode.string)
(Decode.field "creatorId" Decode.string)


decklistDecoder : Collection -> Decoder Decklist
decklistDecoder collection =
Decode.map4 Decklist
(agendaDecoder collection)
(havenDecoder collection)
(factionDecoder collection)
(libraryDecoder collection)



-- (Decode.field "libraryDeck" Decode.string)


agendaDecoder : Collection -> Decoder Agenda
agendaDecoder collection =
decodeCardId "agenda" |> getInCollection collection |> mapToAgenda
Expand Down
14 changes: 7 additions & 7 deletions client/src/Pages/Build.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Pages.Build exposing (Model, Msg, page)

import API.Decklist
import Cards exposing (Card)
import Deck exposing (Deck)
import Deck exposing (Decklist)
import Dict
import Effect exposing (Effect)
import Gen.Params.Build exposing (Params)
Expand Down Expand Up @@ -45,7 +45,7 @@ type alias Model =
, textFilter : Maybe String
, showAllFilters : Bool
, showCollectionImages : Bool
, deck : Deck
, deck : Decklist
, deckName : DeckName
}

Expand Down Expand Up @@ -397,7 +397,7 @@ viewClansInFaction faction =
)


viewFactionList : Deck -> Html Msg
viewFactionList : Decklist -> Html Msg
viewFactionList deck =
let
characters =
Expand Down Expand Up @@ -442,7 +442,7 @@ cardCount =
List.foldl (\( _, n ) sum -> sum + n) 0


viewLibraryList : Deck -> Html Msg
viewLibraryList : Decklist -> Html Msg
viewLibraryList deck =
let
{ actions, combat, other } =
Expand Down Expand Up @@ -480,7 +480,7 @@ viewLibraryList deck =
]


groupLibraryCards : Deck -> { actions : List ( Cards.Library, Int ), combat : List ( Cards.Library, Int ), other : List ( Cards.Library, Int ) }
groupLibraryCards : Decklist -> { actions : List ( Cards.Library, Int ), combat : List ( Cards.Library, Int ), other : List ( Cards.Library, Int ) }
groupLibraryCards deck =
let
groups =
Expand Down Expand Up @@ -523,7 +523,7 @@ viewCardList collection model =
]


viewCardListRow : Deck -> Card -> Html Msg
viewCardListRow : Decklist -> Card -> Html Msg
viewCardListRow deck card =
li [ class "deckbldr-collectionitem--row" ]
[ span [ class "deckbldr-rowpiece_quant--row" ] [ viewQuantityPicker card (Deck.copiesInDeck deck card) ]
Expand Down Expand Up @@ -593,7 +593,7 @@ viewCardListImages collection model =
]


viewCardListImage : Deck -> Card -> Html Msg
viewCardListImage : Decklist -> Card -> Html Msg
viewCardListImage deck card =
li [ class "deckbldr-collectionitem--image" ]
[ UI.Card.lazy card
Expand Down
14 changes: 7 additions & 7 deletions client/src/Pages/View/Id_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Pages.View.Id_ exposing (Model, Msg, page)

import API.Decklist
import Cards
import Deck exposing (Deck)
import Deck exposing (Decklist)
import Dict
import Effect exposing (Effect)
import Gen.Params.View.Id_ exposing (Params)
Expand Down Expand Up @@ -38,7 +38,7 @@ type Model


type alias Xyz =
{ deck : Deck
{ deck : Decklist
, deckName : String
}

Expand All @@ -61,8 +61,8 @@ update msg model =
FromShared subMsg ->
( model, Effect.fromShared subMsg )

FetchedDecklist (Ok res) ->
( Viewing { deck = res, deckName = "Some name" }, Effect.none )
FetchedDecklist (Ok deck) ->
( Viewing { deck = deck.decklist, deckName = deck.meta.name }, Effect.none )

FetchedDecklist (Err _) ->
( model, Effect.none )
Expand Down Expand Up @@ -185,7 +185,7 @@ viewClansInFaction faction =
)


viewFactionList : Deck -> Html Msg
viewFactionList : Decklist -> Html Msg
viewFactionList deck =
let
characters =
Expand Down Expand Up @@ -229,7 +229,7 @@ cardCount =
List.foldl (\( _, n ) sum -> sum + n) 0


viewLibraryList : Deck -> Html Msg
viewLibraryList : Decklist -> Html Msg
viewLibraryList deck =
let
{ actions, combat, other } =
Expand Down Expand Up @@ -267,7 +267,7 @@ viewLibraryList deck =
]


groupLibraryCards : Deck -> { actions : List ( Cards.Library, Int ), combat : List ( Cards.Library, Int ), other : List ( Cards.Library, Int ) }
groupLibraryCards : Decklist -> { actions : List ( Cards.Library, Int ), combat : List ( Cards.Library, Int ), other : List ( Cards.Library, Int ) }
groupLibraryCards deck =
let
groups =
Expand Down

0 comments on commit c0afee2

Please sign in to comment.