adds claims for group chest view
This commit is contained in:
58
src/Api.elm
58
src/Api.elm
@@ -10,9 +10,9 @@ module Api exposing
|
|||||||
, Update(..)
|
, Update(..)
|
||||||
, checkList
|
, checkList
|
||||||
, confirmAction
|
, confirmAction
|
||||||
, fetchClaimsOf
|
|
||||||
, fetchLoot
|
, fetchLoot
|
||||||
, fetchSession
|
, fetchSession
|
||||||
|
, getClaims
|
||||||
, getLoot
|
, getLoot
|
||||||
, printError
|
, printError
|
||||||
, replaceShopItems
|
, replaceShopItems
|
||||||
@@ -22,6 +22,7 @@ import Api.Player exposing (Player, Wealth)
|
|||||||
import Http
|
import Http
|
||||||
import Json.Decode as D exposing (Decoder, field, int, string, succeed)
|
import Json.Decode as D exposing (Decoder, field, int, string, succeed)
|
||||||
import Json.Encode as E
|
import Json.Encode as E
|
||||||
|
import Task exposing (Task)
|
||||||
|
|
||||||
|
|
||||||
type alias HttpResult a =
|
type alias HttpResult a =
|
||||||
@@ -82,12 +83,15 @@ itemEncoder item =
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- LOOT
|
||||||
|
|
||||||
|
|
||||||
type alias Loot =
|
type alias Loot =
|
||||||
List Item
|
List Item
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- LOOT
|
|
||||||
-- Location of a loot
|
-- Location of a loot
|
||||||
|
|
||||||
|
|
||||||
@@ -144,26 +148,6 @@ claimDecoder =
|
|||||||
(D.field "loot_id" int)
|
(D.field "loot_id" int)
|
||||||
|
|
||||||
|
|
||||||
fetchClaimsOf : (Result Http.Error Claims -> msg) -> Int -> Cmd msg
|
|
||||||
fetchClaimsOf toMsg playerId =
|
|
||||||
let
|
|
||||||
url =
|
|
||||||
case playerId of
|
|
||||||
-- The 'group' need to see all claims
|
|
||||||
0 ->
|
|
||||||
"http://localhost:8088/api/claims"
|
|
||||||
|
|
||||||
id ->
|
|
||||||
"http://localhost:8088/api/players/" ++ String.fromInt playerId ++ "/claims"
|
|
||||||
in
|
|
||||||
Http.get
|
|
||||||
{ url = url
|
|
||||||
, expect =
|
|
||||||
valueDecoder (D.list claimDecoder)
|
|
||||||
|> Http.expectJson toMsg
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- Retrieves items from a list of names
|
-- Retrieves items from a list of names
|
||||||
|
|
||||||
@@ -423,25 +407,35 @@ replaceShopItems toMsg loot =
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fetchSession =
|
send : { method : String, path : String, decoder : Decoder a } -> Task Http.Error a
|
||||||
|
send { method, path, decoder } =
|
||||||
Http.task
|
Http.task
|
||||||
{ method = "GET"
|
{ method = method
|
||||||
, url = "http://localhost:8088/session"
|
, url = "http://localhost:8088/" ++ path
|
||||||
, headers = []
|
, headers = []
|
||||||
, body = Http.emptyBody
|
, body = Http.emptyBody
|
||||||
, resolver = Http.stringResolver <| handleJsonResponse Api.Player.playerDecoder
|
, resolver = Http.stringResolver <| handleJsonResponse decoder
|
||||||
, timeout = Nothing
|
, timeout = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fetchSession =
|
||||||
|
send { method = "GET", path = "session", decoder = Api.Player.playerDecoder }
|
||||||
|
|
||||||
|
|
||||||
getLoot id =
|
getLoot id =
|
||||||
Http.task
|
send
|
||||||
{ method = "GET"
|
{ method = "GET"
|
||||||
, url = "http://localhost:8088/api/players/" ++ String.fromInt id ++ "/loot"
|
, path = "api/players/" ++ String.fromInt id ++ "/loot"
|
||||||
, headers = []
|
, decoder = valueDecoder lootDecoder
|
||||||
, body = Http.emptyBody
|
}
|
||||||
, resolver = Http.stringResolver <| handleJsonResponse (valueDecoder lootDecoder)
|
|
||||||
, timeout = Nothing
|
|
||||||
|
getClaims id =
|
||||||
|
send
|
||||||
|
{ method = "GET"
|
||||||
|
, path = "api/players/" ++ String.fromInt id ++ "/claims"
|
||||||
|
, decoder = valueDecoder (D.list claimDecoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
89
src/Page.elm
89
src/Page.elm
@@ -82,8 +82,8 @@ view page =
|
|||||||
case maybeSession page of
|
case maybeSession page of
|
||||||
Just session ->
|
Just session ->
|
||||||
case Session.user session of
|
case Session.user session of
|
||||||
Session.Player player _ _ ->
|
Session.Player data ->
|
||||||
player.name
|
data.player.name
|
||||||
|
|
||||||
Session.Admin ->
|
Session.Admin ->
|
||||||
"Administration"
|
"Administration"
|
||||||
@@ -95,13 +95,13 @@ view page =
|
|||||||
case maybeSession page of
|
case maybeSession page of
|
||||||
Just session ->
|
Just session ->
|
||||||
case Session.user session of
|
case Session.user session of
|
||||||
Session.Player player _ _ ->
|
Session.Player data ->
|
||||||
let
|
let
|
||||||
linkWithGem =
|
linkWithGem =
|
||||||
navLink "fas fa-gem"
|
navLink "fas fa-gem"
|
||||||
in
|
in
|
||||||
[ navLink "fas fa-store-alt" "Marchand" "/marchand"
|
[ navLink "fas fa-store-alt" "Marchand" "/marchand"
|
||||||
, if player.id /= 0 then
|
, if data.player.id /= 0 then
|
||||||
linkWithGem "Coffre de groupe" "/coffre"
|
linkWithGem "Coffre de groupe" "/coffre"
|
||||||
|
|
||||||
else
|
else
|
||||||
@@ -144,12 +144,13 @@ viewSessionBar session controls =
|
|||||||
Nothing ->
|
Nothing ->
|
||||||
[ text "" ]
|
[ text "" ]
|
||||||
|
|
||||||
Just (Session.Player player wealth _) ->
|
Just (Session.Player data) ->
|
||||||
Wealth.view player.wealth wealth
|
-- TODO: Urgh ! When will this Wealth.Model move out of session !
|
||||||
++ (if player.debt > 0 then
|
Wealth.view data.player.wealth data.wealth
|
||||||
|
++ (if data.player.debt > 0 then
|
||||||
[ div [ class "level-item" ]
|
[ div [ class "level-item" ]
|
||||||
[ p [ class "heading is-size-4 has-text-danger" ]
|
[ p [ class "heading is-size-4 has-text-danger" ]
|
||||||
[ text ("Dette : " ++ String.fromInt player.debt ++ "po") ]
|
[ text ("Dette : " ++ String.fromInt data.player.debt ++ "po") ]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -222,6 +223,25 @@ map func page =
|
|||||||
page
|
page
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Restores the page after an action has be resolved (either success or error)
|
||||||
|
|
||||||
|
|
||||||
|
closeAction ( page, cmd ) =
|
||||||
|
case page of
|
||||||
|
Dashboard home ->
|
||||||
|
( page, cmd )
|
||||||
|
|
||||||
|
GroupChest chest ->
|
||||||
|
( GroupChest (GroupChest.refresh chest), cmd )
|
||||||
|
|
||||||
|
Shop shop ->
|
||||||
|
( page, cmd )
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
( page, cmd )
|
||||||
|
|
||||||
|
|
||||||
update msg page =
|
update msg page =
|
||||||
case ( msg, page, maybeSession page ) of
|
case ( msg, page, maybeSession page ) of
|
||||||
-- Dashboard page
|
-- Dashboard page
|
||||||
@@ -240,6 +260,7 @@ update msg page =
|
|||||||
-- Group chest
|
-- Group chest
|
||||||
( GotGroupChestMsg (GroupChest.Api apiMsg), GroupChest _, _ ) ->
|
( GotGroupChestMsg (GroupChest.Api apiMsg), GroupChest _, _ ) ->
|
||||||
update (ApiMsg apiMsg) page
|
update (ApiMsg apiMsg) page
|
||||||
|
|> closeAction
|
||||||
|
|
||||||
( GotGroupChestMsg subMsg, GroupChest chest, _ ) ->
|
( GotGroupChestMsg subMsg, GroupChest chest, _ ) ->
|
||||||
GroupChest.update subMsg chest
|
GroupChest.update subMsg chest
|
||||||
@@ -266,16 +287,16 @@ update msg page =
|
|||||||
Session.wealth session
|
Session.wealth session
|
||||||
in
|
in
|
||||||
case Session.user session of
|
case Session.user session of
|
||||||
Session.Player player aModel _ ->
|
Session.Player data ->
|
||||||
let
|
let
|
||||||
( newWealth, maybeEdit ) =
|
( newWealth, maybeEdit ) =
|
||||||
Wealth.update wealthMsg aModel
|
Wealth.update wealthMsg data.wealth
|
||||||
in
|
in
|
||||||
( map (Session.updateWealth newWealth) page
|
( map (Session.updateWealth newWealth) page
|
||||||
, case maybeEdit of
|
, case maybeEdit of
|
||||||
Just amount ->
|
Just amount ->
|
||||||
Api.confirmAction
|
Api.confirmAction
|
||||||
(String.fromInt (.id player))
|
(String.fromInt (.id data.player))
|
||||||
(Api.WealthPayload amount)
|
(Api.WealthPayload amount)
|
||||||
|> Cmd.map ApiMsg
|
|> Cmd.map ApiMsg
|
||||||
|
|
||||||
@@ -346,44 +367,46 @@ applyUpdate u user =
|
|||||||
in
|
in
|
||||||
{- Note: DbUpdates always refer to the active player -}
|
{- Note: DbUpdates always refer to the active player -}
|
||||||
case user of
|
case user of
|
||||||
Session.Player player wealthModel loot ->
|
Session.Player data ->
|
||||||
case u of
|
case u of
|
||||||
Api.ItemRemoved item ->
|
Api.ItemRemoved item ->
|
||||||
Session.Player player wealthModel <|
|
Session.Player
|
||||||
List.filter
|
{ data
|
||||||
(\i -> i.id /= item.id)
|
| loot =
|
||||||
loot
|
List.filter
|
||||||
|
(\i -> i.id /= item.id)
|
||||||
|
data.loot
|
||||||
|
}
|
||||||
|
|
||||||
Api.ItemAdded item ->
|
Api.ItemAdded item ->
|
||||||
Session.Player player wealthModel (item :: loot)
|
Session.Player { data | loot = item :: data.loot }
|
||||||
|
|
||||||
Api.WealthUpdated diff ->
|
Api.WealthUpdated diff ->
|
||||||
let
|
let
|
||||||
|
player =
|
||||||
|
data.player
|
||||||
|
|
||||||
wealth =
|
wealth =
|
||||||
player.wealth
|
player.wealth
|
||||||
|
|
||||||
_ =
|
|
||||||
Debug.log "updatePlayerWealth" diff
|
|
||||||
in
|
in
|
||||||
Session.Player
|
Session.Player
|
||||||
{ player
|
{ data
|
||||||
| wealth =
|
| player =
|
||||||
Api.Player.Wealth
|
{ player
|
||||||
(wealth.cp + diff.cp)
|
| wealth =
|
||||||
(wealth.sp + diff.sp)
|
Api.Player.Wealth
|
||||||
(wealth.gp + diff.gp)
|
(wealth.cp + diff.cp)
|
||||||
(wealth.pp + diff.pp)
|
(wealth.sp + diff.sp)
|
||||||
|
(wealth.gp + diff.gp)
|
||||||
|
(wealth.pp + diff.pp)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
wealthModel
|
|
||||||
loot
|
|
||||||
|
|
||||||
Api.ClaimAdded claim ->
|
Api.ClaimAdded claim ->
|
||||||
-- { model | claims = claim :: model.claims }
|
Session.Player { data | claims = claim :: data.claims }
|
||||||
user
|
|
||||||
|
|
||||||
Api.ClaimRemoved claim ->
|
Api.ClaimRemoved claim ->
|
||||||
-- { model | claims = List.filter (\c -> c.id /= claim.id) model.claims }
|
Session.Player { data | claims = List.filter (\c -> c.id /= claim.id) data.claims }
|
||||||
user
|
|
||||||
|
|
||||||
Session.Admin ->
|
Session.Admin ->
|
||||||
user
|
user
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
module Page.Chest exposing (Chest, Msg, confirmAdd, confirmBuy, confirmGrab, confirmSell, init, initCreate, initSelection, update, view)
|
module Page.Chest exposing (Chest, Msg, confirmAdd, confirmBuy, confirmGrab, confirmSell, init, initCreate, initSelection, show, update, view)
|
||||||
|
|
||||||
import Api exposing (Item, Loot)
|
import Api exposing (Item, Loot)
|
||||||
import Html exposing (..)
|
import Html exposing (..)
|
||||||
@@ -27,7 +27,7 @@ type alias RowRenderer msg =
|
|||||||
|
|
||||||
|
|
||||||
type Chest
|
type Chest
|
||||||
= View
|
= View (Item -> Html Never)
|
||||||
| Selection Selection.Model
|
| Selection Selection.Model
|
||||||
| Create NewFromInventory.Model
|
| Create NewFromInventory.Model
|
||||||
|
|
||||||
@@ -46,22 +46,27 @@ type Chest
|
|||||||
|
|
||||||
|
|
||||||
init =
|
init =
|
||||||
View
|
View Table.name
|
||||||
|
|
||||||
|
|
||||||
|
show : Table.ItemRenderer Item Never -> Chest
|
||||||
|
show renderItem =
|
||||||
|
View <| Table.renderRowLevel renderItem (\_ -> [])
|
||||||
|
|
||||||
|
|
||||||
initCreate =
|
initCreate =
|
||||||
Create NewFromInventory.init
|
Create NewFromInventory.init
|
||||||
|
|
||||||
|
|
||||||
initSelection =
|
initSelection maybeInitial =
|
||||||
Selection Selection.init
|
Selection <| Selection.init maybeInitial
|
||||||
|
|
||||||
|
|
||||||
view : Chest -> Loot -> Html Msg
|
view : Chest -> Loot -> Html Msg
|
||||||
view model loot =
|
view model loot =
|
||||||
case model of
|
case model of
|
||||||
View ->
|
View renderItem ->
|
||||||
Table.view Table.name loot
|
Table.view renderItem loot
|
||||||
|> Html.map GotViewMsg
|
|> Html.map GotViewMsg
|
||||||
|
|
||||||
Selection subModel ->
|
Selection subModel ->
|
||||||
|
|||||||
@@ -1076,66 +1076,7 @@ update msg model =
|
|||||||
Just <|
|
Just <|
|
||||||
Api.AddPayload
|
Api.AddPayload
|
||||||
(Maybe.withDefault
|
(Maybe.withDefault
|
||||||
"nouveau loot"
|
Claims (Ok claims) ->
|
||||||
model.state.sourceName
|
|
||||||
)
|
|
||||||
(selectContent model)
|
|
||||||
|
|
||||||
Buy ->
|
|
||||||
let
|
|
||||||
modList =
|
|
||||||
List.map
|
|
||||||
(\item ->
|
|
||||||
Dict.get item.id model.state.priceModifiers
|
|
||||||
|> Maybe.map (\i -> toFloatingMod i)
|
|
||||||
)
|
|
||||||
items
|
|
||||||
in
|
|
||||||
Just <| Api.BuyPayload items Nothing modList
|
|
||||||
|
|
||||||
Sell ->
|
|
||||||
let
|
|
||||||
modList =
|
|
||||||
List.map
|
|
||||||
(\item ->
|
|
||||||
Dict.get item.id model.state.priceModifiers
|
|
||||||
|> Maybe.map (\i -> toFloatingMod i)
|
|
||||||
)
|
|
||||||
items
|
|
||||||
in
|
|
||||||
Just <| Api.SellPayload items Nothing modList []
|
|
||||||
|
|
||||||
Grab ->
|
|
||||||
Just <| Api.GrabPayload items
|
|
||||||
|
|
||||||
View ->
|
|
||||||
Nothing
|
|
||||||
in
|
|
||||||
( model
|
|
||||||
, case maybeData of
|
|
||||||
Just data ->
|
|
||||||
Cmd.map ApiMsg <|
|
|
||||||
Api.confirmAction
|
|
||||||
(String.fromInt model.state.player.id)
|
|
||||||
data
|
|
||||||
|
|
||||||
Nothing ->
|
|
||||||
Cmd.none
|
|
||||||
)
|
|
||||||
|
|
||||||
ClearNotification ->
|
|
||||||
( setNotification Nothing model, Cmd.none )
|
|
||||||
|
|
||||||
SwitchSelectionState id ->
|
|
||||||
( { model | selection = switchSelectionState id model.selection }, Cmd.none )
|
|
||||||
|
|
||||||
SetSelection new ->
|
|
||||||
( { model | selection = new }, Cmd.none )
|
|
||||||
|
|
||||||
SearchTextChanged search ->
|
|
||||||
( { model | searchText = search }, Cmd.none )
|
|
||||||
|
|
||||||
GotClaims (Ok claims) ->
|
|
||||||
( { model | claims = claims }, Cmd.none )
|
( { model | claims = claims }, Cmd.none )
|
||||||
|
|
||||||
GotClaims (Err error) ->
|
GotClaims (Err error) ->
|
||||||
|
|||||||
@@ -21,8 +21,17 @@ type Model
|
|||||||
= Model Selection (Data Int)
|
= Model Selection (Data Int)
|
||||||
|
|
||||||
|
|
||||||
init =
|
init : Maybe (List Int) -> Model
|
||||||
Model Set.empty Dict.empty
|
init maybeInitial =
|
||||||
|
Model
|
||||||
|
(case maybeInitial of
|
||||||
|
Just initial ->
|
||||||
|
Set.fromList initial
|
||||||
|
|
||||||
|
Nothing ->
|
||||||
|
Set.empty
|
||||||
|
)
|
||||||
|
Dict.empty
|
||||||
|
|
||||||
|
|
||||||
view : Model -> Loot -> Html Msg
|
view : Model -> Loot -> Html Msg
|
||||||
|
|||||||
@@ -61,10 +61,10 @@ init session =
|
|||||||
, Player.list (AdminViewer << GotPlayers)
|
, Player.list (AdminViewer << GotPlayers)
|
||||||
)
|
)
|
||||||
|
|
||||||
Session.Player player wealth loot ->
|
Session.Player data ->
|
||||||
( Player <|
|
( Player <|
|
||||||
PlayerConfig session
|
PlayerConfig session
|
||||||
(if player.id == 0 then
|
(if data.player.id == 0 then
|
||||||
-- TODO: render claimed items
|
-- TODO: render claimed items
|
||||||
GroupChest Chest.init
|
GroupChest Chest.init
|
||||||
|
|
||||||
@@ -92,7 +92,7 @@ view model =
|
|||||||
case model of
|
case model of
|
||||||
Player (PlayerConfig session mode) ->
|
Player (PlayerConfig session mode) ->
|
||||||
case Session.user session of
|
case Session.user session of
|
||||||
Session.Player player _ loot ->
|
Session.Player data ->
|
||||||
Tuple.mapBoth
|
Tuple.mapBoth
|
||||||
(Html.map PlayerViewer)
|
(Html.map PlayerViewer)
|
||||||
(List.map (Html.map PlayerViewer))
|
(List.map (Html.map PlayerViewer))
|
||||||
@@ -100,17 +100,17 @@ view model =
|
|||||||
case mode of
|
case mode of
|
||||||
PlayerChest chest ->
|
PlayerChest chest ->
|
||||||
( modeButton "Vendre" IntoSell
|
( modeButton "Vendre" IntoSell
|
||||||
, [ Html.map GotChestMsg <| Chest.view chest loot ]
|
, [ Html.map GotChestMsg <| Chest.view chest data.loot ]
|
||||||
)
|
)
|
||||||
|
|
||||||
GroupChest chest ->
|
GroupChest chest ->
|
||||||
( buttons [ modeButton "Vendre" IntoSell, modeButton "Ajouter" IntoAdd ]
|
( buttons [ modeButton "Vendre" IntoSell, modeButton "Ajouter" IntoAdd ]
|
||||||
, [ Html.map GotChestMsg <| Chest.view chest loot ]
|
, [ Html.map GotChestMsg <| Chest.view chest data.loot ]
|
||||||
)
|
)
|
||||||
|
|
||||||
Sell chest ->
|
Sell chest ->
|
||||||
( buttons [ modeButton "Ok" ConfirmSell, modeButton "Annuler" IntoView ]
|
( buttons [ modeButton "Ok" ConfirmSell, modeButton "Annuler" IntoView ]
|
||||||
, [ Html.map GotChestMsg <| Chest.view chest loot ]
|
, [ Html.map GotChestMsg <| Chest.view chest data.loot ]
|
||||||
)
|
)
|
||||||
|
|
||||||
Add chest ->
|
Add chest ->
|
||||||
@@ -262,9 +262,17 @@ update msg model =
|
|||||||
( model
|
( model
|
||||||
, Cmd.map Api <|
|
, Cmd.map Api <|
|
||||||
case Session.user session of
|
case Session.user session of
|
||||||
Session.Player player _ loot ->
|
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 player.id chest loot []) mode
|
mapChest
|
||||||
|
(\chest ->
|
||||||
|
Chest.confirmSell
|
||||||
|
data.player.id
|
||||||
|
chest
|
||||||
|
data.loot
|
||||||
|
[]
|
||||||
|
)
|
||||||
|
mode
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
Cmd.none
|
Cmd.none
|
||||||
@@ -274,7 +282,7 @@ update msg model =
|
|||||||
( model
|
( model
|
||||||
, Cmd.map Api <|
|
, Cmd.map Api <|
|
||||||
case Session.user session of
|
case Session.user session of
|
||||||
Session.Player player _ _ ->
|
Session.Player _ ->
|
||||||
let
|
let
|
||||||
sourceName =
|
sourceName =
|
||||||
"nouveau loot #1"
|
"nouveau loot #1"
|
||||||
@@ -294,7 +302,7 @@ update msg model =
|
|||||||
(Cmd.map GotChestMsg)
|
(Cmd.map GotChestMsg)
|
||||||
|
|
||||||
IntoSell ->
|
IntoSell ->
|
||||||
( Player (PlayerConfig session (Sell Chest.initSelection)), Cmd.none )
|
( Player (PlayerConfig session (Sell <| Chest.initSelection Nothing)), Cmd.none )
|
||||||
|
|
||||||
IntoAdd ->
|
IntoAdd ->
|
||||||
( Player (PlayerConfig session (Add Chest.initCreate)), Cmd.none )
|
( Player (PlayerConfig session (Add Chest.initCreate)), Cmd.none )
|
||||||
@@ -303,8 +311,8 @@ update msg model =
|
|||||||
let
|
let
|
||||||
userChest =
|
userChest =
|
||||||
case Session.user session of
|
case Session.user session of
|
||||||
Session.Player player _ _ ->
|
Session.Player data ->
|
||||||
if player.id == 0 then
|
if data.player.id == 0 then
|
||||||
GroupChest
|
GroupChest
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
module Page.GroupChest exposing (Model, Msg(..), init, update, view)
|
module Page.GroupChest exposing (Model, Msg(..), init, refresh, update, view)
|
||||||
|
|
||||||
import Api exposing (HttpResult, Loot)
|
import Api exposing (HttpResult, Loot)
|
||||||
import Html exposing (..)
|
import Html exposing (..)
|
||||||
import Html.Attributes exposing (..)
|
import Html.Attributes exposing (..)
|
||||||
import Html.Events exposing (..)
|
import Html.Events exposing (..)
|
||||||
import Page.Chest as Chest exposing (Chest)
|
import Page.Chest as Chest exposing (Chest)
|
||||||
import Session exposing (Session)
|
import Session exposing (Session, User(..))
|
||||||
|
import Set
|
||||||
import Table
|
import Table
|
||||||
|
|
||||||
|
|
||||||
@@ -36,8 +37,22 @@ type State
|
|||||||
| Loaded Loot
|
| Loaded Loot
|
||||||
|
|
||||||
|
|
||||||
|
getClaimsFromSession session =
|
||||||
|
case Session.user session of
|
||||||
|
Session.Player data ->
|
||||||
|
if data.player.id /= 0 then
|
||||||
|
data.claims
|
||||||
|
-- TODO: The group and admin case should be impossible !
|
||||||
|
|
||||||
|
else
|
||||||
|
[]
|
||||||
|
|
||||||
|
Session.Admin ->
|
||||||
|
[]
|
||||||
|
|
||||||
|
|
||||||
init session =
|
init session =
|
||||||
( Model session Loading (View Chest.init), Cmd.map Internal <| Api.fetchLoot GotLoot Api.OfGroup )
|
( Model session Loading (View <| showClaims (getClaimsFromSession session)), Cmd.map Internal <| Api.fetchLoot GotLoot Api.OfGroup )
|
||||||
|
|
||||||
|
|
||||||
view : Model -> ( Html Msg, List (Html Msg) )
|
view : Model -> ( Html Msg, List (Html Msg) )
|
||||||
@@ -55,30 +70,29 @@ view model =
|
|||||||
|
|
||||||
Loaded loot ->
|
Loaded loot ->
|
||||||
( Html.map Internal <|
|
( Html.map Internal <|
|
||||||
|
let
|
||||||
|
( isPlayer, isGroup ) =
|
||||||
|
case Session.user model.session of
|
||||||
|
Session.Admin ->
|
||||||
|
( False, False )
|
||||||
|
|
||||||
|
Session.Player data ->
|
||||||
|
( True, data.player.id == 0 )
|
||||||
|
in
|
||||||
case model.mode of
|
case model.mode of
|
||||||
View _ ->
|
View _ ->
|
||||||
case Session.user model.session of
|
if isPlayer && not isGroup then
|
||||||
Session.Admin ->
|
button [ class "button", onClick IntoGrab ] [ text "Demander" ]
|
||||||
text ""
|
|
||||||
|
|
||||||
Session.Player p _ _ ->
|
else
|
||||||
if p.id == 0 then
|
text ""
|
||||||
text ""
|
|
||||||
|
|
||||||
else
|
|
||||||
button [ class "button", onClick IntoGrab ] [ text "Demander" ]
|
|
||||||
|
|
||||||
Grab _ ->
|
Grab _ ->
|
||||||
case Session.user model.session of
|
if isPlayer && not isGroup then
|
||||||
Session.Admin ->
|
button [ class "button", onClick ConfirmGrab ] [ text "Valider" ]
|
||||||
text ""
|
|
||||||
|
|
||||||
Session.Player p _ _ ->
|
else
|
||||||
if p.id == 0 then
|
text ""
|
||||||
text ""
|
|
||||||
|
|
||||||
else
|
|
||||||
button [ class "button", onClick ConfirmGrab ] [ text "Valider" ]
|
|
||||||
, [ mapChest (\c -> Chest.view c loot) model.mode
|
, [ mapChest (\c -> Chest.view c loot) model.mode
|
||||||
|> Html.map (Internal << GotChestMsg)
|
|> Html.map (Internal << GotChestMsg)
|
||||||
]
|
]
|
||||||
@@ -98,6 +112,31 @@ type InnerMsg
|
|||||||
| ConfirmGrab
|
| ConfirmGrab
|
||||||
|
|
||||||
|
|
||||||
|
showClaims claims =
|
||||||
|
let
|
||||||
|
itemClaimed item =
|
||||||
|
List.any (\c -> c.loot_id == item.id) claims
|
||||||
|
in
|
||||||
|
Chest.show
|
||||||
|
(\item ->
|
||||||
|
[ p []
|
||||||
|
[ text <|
|
||||||
|
(if itemClaimed item then
|
||||||
|
"C"
|
||||||
|
|
||||||
|
else
|
||||||
|
""
|
||||||
|
)
|
||||||
|
++ item.name
|
||||||
|
]
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
refresh model =
|
||||||
|
{ model | mode = View <| showClaims (getClaimsFromSession model.session) }
|
||||||
|
|
||||||
|
|
||||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||||
update msg model =
|
update msg model =
|
||||||
case msg of
|
case msg of
|
||||||
@@ -106,10 +145,10 @@ update msg model =
|
|||||||
|
|
||||||
Internal ConfirmGrab ->
|
Internal ConfirmGrab ->
|
||||||
case ( Session.user model.session, model.loot, model.mode ) of
|
case ( Session.user model.session, model.loot, model.mode ) of
|
||||||
( Session.Player player _ _, Loaded loot, Grab chest ) ->
|
( Session.Player data, Loaded loot, Grab chest ) ->
|
||||||
( { model | mode = View Chest.init }
|
( model
|
||||||
, Chest.confirmGrab
|
, Chest.confirmGrab
|
||||||
player.id
|
data.player.id
|
||||||
loot
|
loot
|
||||||
chest
|
chest
|
||||||
|> Cmd.map Api
|
|> Cmd.map Api
|
||||||
@@ -131,10 +170,19 @@ update msg model =
|
|||||||
|> updateChest model
|
|> updateChest model
|
||||||
|
|
||||||
IntoGrab ->
|
IntoGrab ->
|
||||||
( { model | mode = Grab Chest.initSelection }, Cmd.none )
|
let
|
||||||
|
claimedIds =
|
||||||
|
case Session.user model.session of
|
||||||
|
Player data ->
|
||||||
|
List.map .loot_id data.claims
|
||||||
|
|
||||||
|
Admin ->
|
||||||
|
[]
|
||||||
|
in
|
||||||
|
( { model | mode = Grab <| Chest.initSelection (Just claimedIds) }, Cmd.none )
|
||||||
|
|
||||||
IntoView ->
|
IntoView ->
|
||||||
( { model | mode = View Chest.init }, Cmd.none )
|
( refresh model, Cmd.none )
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
( model, Cmd.none )
|
( model, Cmd.none )
|
||||||
|
|||||||
@@ -82,10 +82,10 @@ view model =
|
|||||||
( View chest, Session.Admin ) ->
|
( View chest, Session.Admin ) ->
|
||||||
btn "Remplacer" (Internal IntoRefresh)
|
btn "Remplacer" (Internal IntoRefresh)
|
||||||
|
|
||||||
( View chest, Session.Player _ _ _ ) ->
|
( View chest, Session.Player _ ) ->
|
||||||
btn "Acheter" (Internal IntoBuy)
|
btn "Acheter" (Internal IntoBuy)
|
||||||
|
|
||||||
( Buy chest, Session.Player p _ _ ) ->
|
( Buy chest, Session.Player _ ) ->
|
||||||
buttons [ btn "Ok" (Internal ConfirmBuy), btn "Annuler" (Internal IntoView) ]
|
buttons [ btn "Ok" (Internal ConfirmBuy), btn "Annuler" (Internal IntoView) ]
|
||||||
|
|
||||||
( Refresh chest, Session.Admin ) ->
|
( Refresh chest, Session.Admin ) ->
|
||||||
@@ -146,10 +146,10 @@ 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 player _ _, Loaded loot, Buy chest ) ->
|
( Session.Player data, Loaded loot, Buy chest ) ->
|
||||||
( model
|
( model
|
||||||
, Chest.confirmBuy
|
, Chest.confirmBuy
|
||||||
player.id
|
data.player.id
|
||||||
chest
|
chest
|
||||||
loot
|
loot
|
||||||
|> Cmd.map Api
|
|> Cmd.map Api
|
||||||
@@ -209,8 +209,8 @@ update msg model =
|
|||||||
-- Buy mode
|
-- Buy mode
|
||||||
IntoBuy ->
|
IntoBuy ->
|
||||||
case Session.user (getSession model) of
|
case Session.user (getSession model) of
|
||||||
Session.Player _ _ _ ->
|
Session.Player _ ->
|
||||||
( { model | chest = Buy Chest.initSelection }, Cmd.none )
|
( { model | chest = Buy <| Chest.initSelection Nothing }, Cmd.none )
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
( model, Cmd.none )
|
( model, Cmd.none )
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module Session exposing (Session, User(..), error, getSession, init, key, notification, updateNotifications, updateUser, updateWealth, user, wealth)
|
module Session exposing (Session, User(..), error, getSession, init, key, notification, updateNotifications, updateUser, updateWealth, user, wealth)
|
||||||
|
|
||||||
import Api exposing (Loot)
|
import Api exposing (Claims, Loot)
|
||||||
import Api.Player as Player exposing (Player)
|
import Api.Player as Player exposing (Player)
|
||||||
import Browser.Navigation as Nav
|
import Browser.Navigation as Nav
|
||||||
import Http
|
import Http
|
||||||
@@ -10,7 +10,12 @@ import Wealth
|
|||||||
|
|
||||||
|
|
||||||
type User
|
type User
|
||||||
= Player Player Wealth.Model Loot
|
= Player
|
||||||
|
{ player : Player
|
||||||
|
, wealth : Wealth.Model
|
||||||
|
, loot : Loot
|
||||||
|
, claims : Claims
|
||||||
|
}
|
||||||
| Admin
|
| Admin
|
||||||
|
|
||||||
|
|
||||||
@@ -25,17 +30,22 @@ type Session
|
|||||||
init : (Result String Session -> msg) -> Nav.Key -> Cmd msg
|
init : (Result String Session -> msg) -> Nav.Key -> Cmd msg
|
||||||
init toMsg navKey =
|
init toMsg navKey =
|
||||||
let
|
let
|
||||||
toSession : Result String ( Player, Loot ) -> msg
|
toSession : Result String ( Player, Loot, Claims ) -> msg
|
||||||
toSession result =
|
toSession result =
|
||||||
case result of
|
case result of
|
||||||
Ok ( player, loot ) ->
|
Ok ( player, loot, claims ) ->
|
||||||
toMsg <|
|
toMsg <|
|
||||||
Ok
|
Ok
|
||||||
(Session
|
(Session
|
||||||
navKey
|
navKey
|
||||||
( Nothing, Nothing )
|
( Nothing, Nothing )
|
||||||
<|
|
(Player
|
||||||
Player player Wealth.init loot
|
{ player = player
|
||||||
|
, wealth = Wealth.init
|
||||||
|
, loot = loot
|
||||||
|
, claims = claims
|
||||||
|
}
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
Err e ->
|
Err e ->
|
||||||
@@ -44,10 +54,11 @@ init toMsg navKey =
|
|||||||
Task.attempt toSession initFullSession
|
Task.attempt toSession initFullSession
|
||||||
|
|
||||||
|
|
||||||
initFullSession : Task String ( Player, Loot )
|
initFullSession : Task String ( Player, Loot, Claims )
|
||||||
initFullSession =
|
initFullSession =
|
||||||
Api.fetchSession
|
Api.fetchSession
|
||||||
|> Task.andThen wrapLoot
|
|> Task.andThen wrapLoot
|
||||||
|
|> Task.andThen wrapClaims
|
||||||
|> Task.mapError Api.printError
|
|> Task.mapError Api.printError
|
||||||
|
|
||||||
|
|
||||||
@@ -57,6 +68,12 @@ wrapLoot player =
|
|||||||
|> Task.andThen (\loot -> Task.succeed ( player, loot ))
|
|> Task.andThen (\loot -> Task.succeed ( player, loot ))
|
||||||
|
|
||||||
|
|
||||||
|
wrapClaims : ( Player, Loot ) -> Task Http.Error ( Player, Loot, Claims )
|
||||||
|
wrapClaims ( player, loot ) =
|
||||||
|
Api.getClaims player.id
|
||||||
|
|> Task.andThen (\claims -> Task.succeed ( player, loot, claims ))
|
||||||
|
|
||||||
|
|
||||||
getSession : { r | session : Session } -> Session
|
getSession : { r | session : Session } -> Session
|
||||||
getSession r =
|
getSession r =
|
||||||
.session r
|
.session r
|
||||||
@@ -92,8 +109,8 @@ updateUser newUser model =
|
|||||||
wealth : Session -> Maybe Wealth.Model
|
wealth : Session -> Maybe Wealth.Model
|
||||||
wealth session =
|
wealth session =
|
||||||
case user session of
|
case user session of
|
||||||
Player _ model _ ->
|
Player data ->
|
||||||
Just model
|
Just data.wealth
|
||||||
|
|
||||||
Admin ->
|
Admin ->
|
||||||
Nothing
|
Nothing
|
||||||
@@ -105,8 +122,8 @@ setWealth wealthModel session =
|
|||||||
session
|
session
|
||||||
in
|
in
|
||||||
case isUser of
|
case isUser of
|
||||||
Player p _ loot ->
|
Player data ->
|
||||||
Session navKey notifications (Player p wealthModel loot)
|
Session navKey notifications (Player { data | wealth = wealthModel })
|
||||||
|
|
||||||
Admin ->
|
Admin ->
|
||||||
Session navKey notifications Admin
|
Session navKey notifications Admin
|
||||||
@@ -119,13 +136,18 @@ updateWealth newWealthModel model =
|
|||||||
model
|
model
|
||||||
in
|
in
|
||||||
case loggedUser of
|
case loggedUser of
|
||||||
Player player _ loot ->
|
Player data ->
|
||||||
Session navKey notifications (Player player newWealthModel loot)
|
Session navKey notifications (Player { data | wealth = newWealthModel })
|
||||||
|
|
||||||
Admin ->
|
Admin ->
|
||||||
Session navKey notifications Admin
|
Session navKey notifications Admin
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Retrieve the notification or Nothing
|
||||||
|
|
||||||
|
|
||||||
|
notification : Session -> Maybe String
|
||||||
notification session =
|
notification session =
|
||||||
let
|
let
|
||||||
(Session _ ( maybeNotification, _ ) _) =
|
(Session _ ( maybeNotification, _ ) _) =
|
||||||
@@ -134,6 +156,11 @@ notification session =
|
|||||||
maybeNotification
|
maybeNotification
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Retrieve the error or Nothing
|
||||||
|
|
||||||
|
|
||||||
|
error : Session -> Maybe String
|
||||||
error session =
|
error session =
|
||||||
let
|
let
|
||||||
(Session _ ( _, maybeError ) _) =
|
(Session _ ( _, maybeError ) _) =
|
||||||
@@ -142,6 +169,7 @@ error session =
|
|||||||
maybeError
|
maybeError
|
||||||
|
|
||||||
|
|
||||||
|
setError : Maybe String -> Session -> Session
|
||||||
setError maybeError session =
|
setError maybeError session =
|
||||||
let
|
let
|
||||||
(Session navKey ( maybeNotification, _ ) loggedUser) =
|
(Session navKey ( maybeNotification, _ ) loggedUser) =
|
||||||
@@ -150,6 +178,7 @@ setError maybeError session =
|
|||||||
Session navKey ( maybeNotification, maybeError ) loggedUser
|
Session navKey ( maybeNotification, maybeError ) loggedUser
|
||||||
|
|
||||||
|
|
||||||
|
updateNotifications : Notifications -> Session -> Session
|
||||||
updateNotifications notifications session =
|
updateNotifications notifications session =
|
||||||
let
|
let
|
||||||
(Session navKey _ loggedUser) =
|
(Session navKey _ loggedUser) =
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
module Table exposing (name, renderRowLevel, renderSelectableRow, view)
|
module Table exposing (ItemRenderer, name, renderRowLevel, renderSelectableRow, view)
|
||||||
|
|
||||||
import Html exposing (..)
|
import Html exposing (..)
|
||||||
import Html.Attributes exposing (..)
|
import Html.Attributes exposing (..)
|
||||||
|
|||||||
Reference in New Issue
Block a user