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 =
let
path =
if id == 0 then
"api/claims"
else
"api/players/" ++ String.fromInt id ++ "/claims"
in
send
{ method = "GET"
, path = "api/players/" ++ String.fromInt id ++ "/claims"
, path = path
, decoder = valueDecoder (D.list claimDecoder)
}

View File

@@ -283,14 +283,14 @@ map func page =
closeAction ( page, cmd ) =
case page of
Home home ->
( page, cmd )
Home from ->
gotoHome page
GroupChest chest ->
( GroupChest (GroupChest.refresh chest), cmd )
GroupChest from ->
gotoGroupChest page
Shop shop ->
( page, cmd )
Shop from ->
gotoShop page
_ ->
( page, cmd )
@@ -302,6 +302,7 @@ update msg page =
-- Capture API messages
( GotHomeMsg (Home.Api apiMsg), Home home, _ ) ->
update (ApiMsg apiMsg) page
|> closeAction
-- Relay others
( GotHomeMsg subMsg, Home home, _ ) ->
@@ -326,6 +327,7 @@ update msg page =
-- Shop page
( GotShopMsg (Shop.Api apiMsg), Shop shop, _ ) ->
update (ApiMsg apiMsg) page
|> closeAction
( GotShopMsg subMsg, Shop 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 Html exposing (..)
@@ -46,6 +46,8 @@ type IntoMode
{-
Dashboard :
* ViewWithClaims (group)
* View
@@ -72,8 +74,32 @@ intoMode newMode =
IntoMode newMode
show : Table.ItemRenderer Item Never -> Chest
show renderItem =
new =
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 (\_ -> [])
@@ -141,7 +167,7 @@ update msg model =
IntoViewWithClaims claims ->
let
isClaimed item =
List.any (\claim -> claim.loot_id == item.id) claims
List.any (\claim_ -> claim_.loot_id == item.id) claims
renderItem item =
[ if isClaimed item then

View File

@@ -39,20 +39,13 @@ type alias NewPlayerForm =
type PlayerConfig
= PlayerConfig Session Mode
= PlayerConfig Session Chest
type AdminConfig
= AdminConfig Session (List Player) NewPlayerForm
type Mode
= PlayerChest Chest
| GroupChest Chest
| Sell Chest
| Add Chest
init : Session -> ( Model, Cmd Msg )
init session =
case Session.user session of
@@ -66,10 +59,11 @@ init session =
PlayerConfig session
(if data.player.id == 0 then
-- TODO: render claimed items
GroupChest Chest.init
Chest.update (Chest.showWithClaims data.claims) Chest.init
|> Tuple.first
else
PlayerChest Chest.init
Chest.init
)
, Cmd.none
)
@@ -90,32 +84,37 @@ buttons bs =
view : Model -> ( Html Msg, List (Html Msg) )
view model =
case model of
Player (PlayerConfig session mode) ->
Player (PlayerConfig session chest) ->
case Session.user session of
Session.Player data ->
Tuple.mapBoth
(Html.map PlayerViewer)
(List.map (Html.map PlayerViewer))
<|
case mode of
PlayerChest chest ->
( modeButton "Vendre" IntoSell
, [ Html.map GotChestMsg <| Chest.view chest data.loot ]
)
let
toShow =
case data.player.id of
0 ->
GotChestMsg <| Chest.showWithClaims data.claims
GroupChest chest ->
( buttons [ modeButton "Vendre" IntoSell, modeButton "Ajouter" IntoAdd ]
, [ Html.map GotChestMsg <| Chest.view chest data.loot ]
)
_ ->
GotChestMsg Chest.show
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 ]
, [ Html.map GotChestMsg <| Chest.view chest data.loot ]
)
_ ->
modeButton "Vendre" (GotChestMsg Chest.sell)
Add chest ->
( buttons [ modeButton "Ok" ConfirmAdd, modeButton "Annuler" IntoView ]
, [ Html.map GotChestMsg <| Chest.view chest [] ]
Chest.Sell _ ->
buttons [ modeButton "Ok" ConfirmSell, modeButton "Annuler" toShow ]
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
= GotChestMsg Chest.Msg
| IntoSell
| IntoAdd
| ConfirmSell
| 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 =
@@ -258,27 +217,23 @@ update msg model =
)
|> Tuple.mapSecond (Cmd.map AdminViewer)
( PlayerViewer ConfirmSell, Player (PlayerConfig session mode) ) ->
( PlayerViewer ConfirmSell, Player (PlayerConfig session chest) ) ->
( model
, Cmd.map Api <|
case Session.user session of
Session.Player data ->
-- TODO: handle list of players when Viewer is group
mapChest
(\chest ->
Chest.confirmSell
data.player.id
chest
data.loot
[]
)
mode
_ ->
Cmd.none
)
( PlayerViewer ConfirmAdd, Player (PlayerConfig session mode) ) ->
( PlayerViewer ConfirmAdd, Player (PlayerConfig session chest) ) ->
( model
, Cmd.map Api <|
case Session.user session of
@@ -287,45 +242,23 @@ update msg model =
sourceName =
"nouveau loot #1"
in
mapChest (\chest -> Chest.confirmAdd 0 sourceName chest) mode
Chest.confirmAdd
0
sourceName
chest
_ ->
Cmd.none
)
( PlayerViewer aMsg, Player (PlayerConfig session mode) ) ->
( PlayerViewer aMsg, Player (PlayerConfig session chest) ) ->
(case aMsg of
GotChestMsg chestMsg ->
mapChest (Chest.update chestMsg) mode
Chest.update chestMsg chest
|> Tuple.mapBoth
(updateChest model)
(\chest_ -> Player (PlayerConfig session chest_))
(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 )
)

View File

@@ -42,7 +42,7 @@ init session =
( Model session
Loading
(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
)
@@ -77,11 +77,7 @@ view model =
button
[ class "button"
, onClick
(GotChestMsg
(Chest.intoMode <|
Chest.IntoClaim (getClaimsFromSession model.session)
)
)
(GotChestMsg <| Chest.showWithClaims (getClaimsFromSession model.session))
]
[ text "Demander" ]

View File

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