moves Modes inside Chest module

This commit is contained in:
2019-12-04 22:03:29 +01:00
parent b97be8c321
commit b00d26e6d4
6 changed files with 115 additions and 206 deletions

View File

@@ -432,9 +432,17 @@ getLoot id =
getClaims id = getClaims id =
let
path =
if id == 0 then
"api/claims"
else
"api/players/" ++ String.fromInt id ++ "/claims"
in
send send
{ method = "GET" { method = "GET"
, path = "api/players/" ++ String.fromInt id ++ "/claims" , path = path
, decoder = valueDecoder (D.list claimDecoder) , decoder = valueDecoder (D.list claimDecoder)
} }

View File

@@ -283,14 +283,14 @@ map func page =
closeAction ( page, cmd ) = closeAction ( page, cmd ) =
case page of case page of
Home home -> Home from ->
( page, cmd ) gotoHome page
GroupChest chest -> GroupChest from ->
( GroupChest (GroupChest.refresh chest), cmd ) gotoGroupChest page
Shop shop -> Shop from ->
( page, cmd ) gotoShop page
_ -> _ ->
( page, cmd ) ( page, cmd )
@@ -302,6 +302,7 @@ update msg page =
-- Capture API messages -- Capture API messages
( GotHomeMsg (Home.Api apiMsg), Home home, _ ) -> ( GotHomeMsg (Home.Api apiMsg), Home home, _ ) ->
update (ApiMsg apiMsg) page update (ApiMsg apiMsg) page
|> closeAction
-- Relay others -- Relay others
( GotHomeMsg subMsg, Home home, _ ) -> ( GotHomeMsg subMsg, Home home, _ ) ->
@@ -326,6 +327,7 @@ update msg page =
-- Shop page -- Shop page
( GotShopMsg (Shop.Api apiMsg), Shop shop, _ ) -> ( GotShopMsg (Shop.Api apiMsg), Shop shop, _ ) ->
update (ApiMsg apiMsg) page update (ApiMsg apiMsg) page
|> closeAction
( GotShopMsg subMsg, Shop shop, _ ) -> ( GotShopMsg subMsg, Shop shop, _ ) ->
Shop.update subMsg shop Shop.update subMsg shop

View File

@@ -1,4 +1,4 @@
module Page.Chest exposing (Chest(..), IntoMode(..), Msg, confirmAdd, confirmBuy, confirmGrab, confirmSell, init, intoMode, update, view) module Page.Chest exposing (..)
import Api exposing (Claims, Item, Loot) import Api exposing (Claims, Item, Loot)
import Html exposing (..) import Html exposing (..)
@@ -46,6 +46,8 @@ type IntoMode
{- {-
Dashboard : Dashboard :
* ViewWithClaims (group) * ViewWithClaims (group)
* View * View
@@ -72,8 +74,32 @@ intoMode newMode =
IntoMode newMode IntoMode newMode
show : Table.ItemRenderer Item Never -> Chest new =
show renderItem = intoMode IntoAdd
show =
intoMode IntoView
showWithClaims claims =
intoMode (IntoViewWithClaims claims)
buy =
intoMode IntoBuy
sell =
intoMode IntoSell
claim initialClaims =
intoMode (IntoClaim initialClaims)
showWith : Table.ItemRenderer Item Never -> Chest
showWith renderItem =
View <| Table.renderRowLevel renderItem (\_ -> []) View <| Table.renderRowLevel renderItem (\_ -> [])
@@ -141,7 +167,7 @@ update msg model =
IntoViewWithClaims claims -> IntoViewWithClaims claims ->
let let
isClaimed item = isClaimed item =
List.any (\claim -> claim.loot_id == item.id) claims List.any (\claim_ -> claim_.loot_id == item.id) claims
renderItem item = renderItem item =
[ if isClaimed item then [ if isClaimed item then

View File

@@ -39,20 +39,13 @@ type alias NewPlayerForm =
type PlayerConfig type PlayerConfig
= PlayerConfig Session Mode = PlayerConfig Session Chest
type AdminConfig type AdminConfig
= AdminConfig Session (List Player) NewPlayerForm = AdminConfig Session (List Player) NewPlayerForm
type Mode
= PlayerChest Chest
| GroupChest Chest
| Sell Chest
| Add Chest
init : Session -> ( Model, Cmd Msg ) init : Session -> ( Model, Cmd Msg )
init session = init session =
case Session.user session of case Session.user session of
@@ -66,10 +59,11 @@ init session =
PlayerConfig session PlayerConfig session
(if data.player.id == 0 then (if data.player.id == 0 then
-- TODO: render claimed items -- TODO: render claimed items
GroupChest Chest.init Chest.update (Chest.showWithClaims data.claims) Chest.init
|> Tuple.first
else else
PlayerChest Chest.init Chest.init
) )
, Cmd.none , Cmd.none
) )
@@ -90,32 +84,37 @@ buttons bs =
view : Model -> ( Html Msg, List (Html Msg) ) view : Model -> ( Html Msg, List (Html Msg) )
view model = view model =
case model of case model of
Player (PlayerConfig session mode) -> Player (PlayerConfig session chest) ->
case Session.user session of case Session.user session of
Session.Player data -> Session.Player data ->
Tuple.mapBoth let
(Html.map PlayerViewer) toShow =
(List.map (Html.map PlayerViewer)) case data.player.id of
<| 0 ->
case mode of GotChestMsg <| Chest.showWithClaims data.claims
PlayerChest chest ->
( modeButton "Vendre" IntoSell
, [ Html.map GotChestMsg <| Chest.view chest data.loot ]
)
GroupChest chest -> _ ->
( buttons [ modeButton "Vendre" IntoSell, modeButton "Ajouter" IntoAdd ] GotChestMsg Chest.show
, [ Html.map GotChestMsg <| Chest.view chest data.loot ] in
) ( Html.map PlayerViewer <|
case chest of
Chest.View _ ->
case data.player.id of
0 ->
buttons [ modeButton "Vendre" (GotChestMsg Chest.sell), modeButton "Ajouter" (GotChestMsg Chest.new) ]
Sell chest -> _ ->
( buttons [ modeButton "Ok" ConfirmSell, modeButton "Annuler" IntoView ] modeButton "Vendre" (GotChestMsg Chest.sell)
, [ Html.map GotChestMsg <| Chest.view chest data.loot ]
)
Add chest -> Chest.Sell _ ->
( buttons [ modeButton "Ok" ConfirmAdd, modeButton "Annuler" IntoView ] buttons [ modeButton "Ok" ConfirmSell, modeButton "Annuler" toShow ]
, [ Html.map GotChestMsg <| Chest.view chest [] ]
Chest.New _ ->
buttons [ modeButton "Ok" ConfirmAdd, modeButton "Annuler" toShow ]
_ ->
text ""
, [ Html.map (PlayerViewer << GotChestMsg) <| Chest.view chest data.loot ]
) )
_ -> _ ->
@@ -198,48 +197,8 @@ type AdminMsg
type PlayerMsg type PlayerMsg
= GotChestMsg Chest.Msg = GotChestMsg Chest.Msg
| IntoSell
| IntoAdd
| ConfirmSell | ConfirmSell
| ConfirmAdd | ConfirmAdd
| IntoView
mapChest : (Chest -> a) -> Mode -> a
mapChest fn mode =
case mode of
PlayerChest chest ->
fn chest
GroupChest chest ->
fn chest
Add chest ->
fn chest
Sell chest ->
fn chest
updateChest : Model -> Chest -> Model
updateChest model new =
case model of
Admin _ ->
model
Player (PlayerConfig s mode) ->
case mode of
PlayerChest _ ->
Player (PlayerConfig s (PlayerChest new))
GroupChest _ ->
Player (PlayerConfig s (GroupChest new))
Add _ ->
Player (PlayerConfig s (Add new))
Sell _ ->
Player (PlayerConfig s (Sell new))
update msg model = update msg model =
@@ -258,27 +217,23 @@ update msg model =
) )
|> Tuple.mapSecond (Cmd.map AdminViewer) |> Tuple.mapSecond (Cmd.map AdminViewer)
( PlayerViewer ConfirmSell, Player (PlayerConfig session mode) ) -> ( PlayerViewer ConfirmSell, Player (PlayerConfig session chest) ) ->
( model ( model
, Cmd.map Api <| , Cmd.map Api <|
case Session.user session of case Session.user session of
Session.Player data -> Session.Player data ->
-- TODO: handle list of players when Viewer is group -- TODO: handle list of players when Viewer is group
mapChest
(\chest ->
Chest.confirmSell Chest.confirmSell
data.player.id data.player.id
chest chest
data.loot data.loot
[] []
)
mode
_ -> _ ->
Cmd.none Cmd.none
) )
( PlayerViewer ConfirmAdd, Player (PlayerConfig session mode) ) -> ( PlayerViewer ConfirmAdd, Player (PlayerConfig session chest) ) ->
( model ( model
, Cmd.map Api <| , Cmd.map Api <|
case Session.user session of case Session.user session of
@@ -287,45 +242,23 @@ update msg model =
sourceName = sourceName =
"nouveau loot #1" "nouveau loot #1"
in in
mapChest (\chest -> Chest.confirmAdd 0 sourceName chest) mode Chest.confirmAdd
0
sourceName
chest
_ -> _ ->
Cmd.none Cmd.none
) )
( PlayerViewer aMsg, Player (PlayerConfig session mode) ) -> ( PlayerViewer aMsg, Player (PlayerConfig session chest) ) ->
(case aMsg of (case aMsg of
GotChestMsg chestMsg -> GotChestMsg chestMsg ->
mapChest (Chest.update chestMsg) mode Chest.update chestMsg chest
|> Tuple.mapBoth |> Tuple.mapBoth
(updateChest model) (\chest_ -> Player (PlayerConfig session chest_))
(Cmd.map GotChestMsg) (Cmd.map GotChestMsg)
IntoSell ->
( Player (PlayerConfig session (Sell <| Chest.initSelection Nothing True)), Cmd.none )
IntoAdd ->
( Player (PlayerConfig session (Add Chest.initCreate)), Cmd.none )
IntoView ->
let
userChest =
case Session.user session of
Session.Player data ->
if data.player.id == 0 then
GroupChest
else
PlayerChest
-- TODO: this seems not right
-- there should be a better way
-- to handle this
_ ->
PlayerChest
in
( Player (PlayerConfig session (userChest Chest.init)), Cmd.none )
_ -> _ ->
( model, Cmd.none ) ( model, Cmd.none )
) )

View File

@@ -42,7 +42,7 @@ init session =
( Model session ( Model session
Loading Loading
(Tuple.first <| (Tuple.first <|
Chest.update (Chest.intoMode (Chest.IntoViewWithClaims <| getClaimsFromSession session)) Chest.init Chest.update (Chest.showWithClaims <| getClaimsFromSession session) Chest.init
) )
, Cmd.map Internal <| Api.fetchLoot GotLoot Api.OfGroup , Cmd.map Internal <| Api.fetchLoot GotLoot Api.OfGroup
) )
@@ -77,11 +77,7 @@ view model =
button button
[ class "button" [ class "button"
, onClick , onClick
(GotChestMsg (GotChestMsg <| Chest.showWithClaims (getClaimsFromSession model.session))
(Chest.intoMode <|
Chest.IntoClaim (getClaimsFromSession model.session)
)
)
] ]
[ text "Demander" ] [ text "Demander" ]

View File

@@ -15,7 +15,7 @@ import Table
type alias Model = type alias Model =
{ session : Session { session : Session
, loot : Status Loot , loot : Status Loot
, chest : Mode , chest : Chest
} }
@@ -25,26 +25,8 @@ type Status a
| Loaded a | Loaded a
type Mode
= View Chest
| Buy Chest
| Refresh Chest
getChest mode =
case mode of
View c ->
c
Buy c ->
c
Refresh c ->
c
init session = init session =
( Model session Loading <| View Chest.init, fetchShopItems ) ( Model session Loading Chest.init, fetchShopItems )
fetchShopItems = fetchShopItems =
@@ -52,12 +34,12 @@ fetchShopItems =
|> Cmd.map Internal |> Cmd.map Internal
btn : String -> Msg -> Html Msg btn : String -> msg -> Html msg
btn t msg = btn t msg =
button [ class "button", onClick msg ] [ text t ] button [ class "button", onClick msg ] [ text t ]
buttons : List (Html Msg) -> Html Msg buttons : List (Html msg) -> Html msg
buttons bs = buttons bs =
div [ class "buttons" ] bs div [ class "buttons" ] bs
@@ -78,24 +60,25 @@ view model =
Loaded loot -> Loaded loot ->
let let
controls = controls =
Html.map Internal <|
case ( model.chest, Session.user model.session ) of case ( model.chest, Session.user model.session ) of
( View chest, Session.Admin ) -> ( Chest.View _, Session.Admin ) ->
btn "Remplacer" (Internal IntoRefresh) btn "Remplacer" (GotChestMsg Chest.new)
( View chest, Session.Player _ ) -> ( Chest.View _, Session.Player _ ) ->
btn "Acheter" (Internal IntoBuy) btn "Acheter" (GotChestMsg Chest.buy)
( Buy chest, Session.Player _ ) -> ( Chest.Buy _, Session.Player _ ) ->
buttons [ btn "Ok" (Internal ConfirmBuy), btn "Annuler" (Internal IntoView) ] buttons [ btn "Ok" ConfirmBuy, btn "Annuler" (GotChestMsg Chest.show) ]
( Refresh chest, Session.Admin ) -> ( Chest.New _, Session.Admin ) ->
buttons [ btn "Ok" (Internal ConfirmRefresh), btn "Annuler" (Internal IntoView) ] buttons [ btn "Ok" ConfirmRefresh, btn "Annuler" (GotChestMsg Chest.show) ]
_ -> _ ->
text "" text ""
in in
( controls ( controls
, [ Chest.view (getChest model.chest) loot |> Html.map (Internal << GotChestMsg) ] , [ Chest.view model.chest loot |> Html.map (Internal << GotChestMsg) ]
) )
@@ -110,31 +93,13 @@ type Msg
type ShopMsg type ShopMsg
= GotLoot Api.ToChest (HttpResult Loot) = GotLoot Api.ToChest (HttpResult Loot)
| IntoRefresh
| ConfirmRefresh | ConfirmRefresh
| GotRefreshResult (Maybe ()) | GotRefreshResult (Maybe ())
| IntoBuy
| ConfirmBuy | ConfirmBuy
| GotBuyResult | GotBuyResult
| IntoView
| GotChestMsg Chest.Msg | GotChestMsg Chest.Msg
updateChest model chest =
{ model
| chest =
case model.chest of
Buy _ ->
Buy chest
Refresh _ ->
Refresh chest
View _ ->
View chest
}
-- GotRefreshResult (Maybe ()) -- GotRefreshResult (Maybe ())
--| GotBuyMsg Selection.Msg --| GotBuyMsg Selection.Msg
@@ -146,11 +111,11 @@ update msg model =
case msg of case msg of
Internal ConfirmBuy -> Internal ConfirmBuy ->
case ( Session.user (getSession model), model.loot, model.chest ) of case ( Session.user (getSession model), model.loot, model.chest ) of
( Session.Player data, Loaded loot, Buy chest ) -> ( Session.Player data, Loaded loot, Chest.Buy _ ) ->
( model ( model
, Chest.confirmBuy , Chest.confirmBuy
data.player.id data.player.id
chest model.chest
loot loot
|> Cmd.map Api |> Cmd.map Api
) )
@@ -181,15 +146,6 @@ update msg model =
Err e -> Err e ->
( { model | loot = LoadError <| Debug.toString e }, Cmd.none ) ( { model | loot = LoadError <| Debug.toString e }, Cmd.none )
-- Refresh mode
IntoRefresh ->
case Session.user (getSession model) of
Session.Admin ->
( { model | chest = Refresh Chest.initCreate }, Cmd.none )
_ ->
( model, Cmd.none )
ConfirmRefresh -> ConfirmRefresh ->
case Session.user (getSession model) of case Session.user (getSession model) of
Session.Admin -> Session.Admin ->
@@ -206,22 +162,10 @@ update msg model =
in in
( model, Cmd.none ) ( model, Cmd.none )
-- Buy mode
IntoBuy ->
case Session.user (getSession model) of
Session.Player _ ->
( { model | chest = Buy <| Chest.initSelection Nothing True }, Cmd.none )
_ ->
( model, Cmd.none )
IntoView ->
( { model | chest = View Chest.init }, Cmd.none )
GotChestMsg subMsg -> GotChestMsg subMsg ->
Chest.update subMsg (getChest model.chest) Chest.update subMsg model.chest
|> Tuple.mapBoth |> Tuple.mapBoth
(updateChest model) (\c -> { model | chest = c })
(Cmd.map GotChestMsg) (Cmd.map GotChestMsg)
_ -> _ ->