Compare commits

...

2 Commits

Author SHA1 Message Date
c50cb37900 made it work again 2019-11-27 22:02:47 +01:00
c52fea683d just triggered an compiler error, marks it 2019-11-27 21:54:54 +01:00
7 changed files with 82 additions and 62 deletions

View File

@@ -12,6 +12,7 @@ module Api exposing
, confirmAction
, fetchClaimsOf
, fetchLoot
, fetchSession
, replaceShopItems
)
@@ -421,6 +422,26 @@ replaceShopItems toMsg loot =
-- This is where the error happened
fetchSession toMsg =
let
gotResponse r =
case Debug.log "got session:" r of
Ok player ->
toMsg (Just player)
Err _ ->
toMsg Nothing
in
Http.get
{ url = "http://localhost:8088/session"
, expect = Http.expectJson gotResponse Api.Player.playerDecoder
}
-- UTILS

View File

@@ -1,4 +1,4 @@
module Api.Player exposing (Player, Wealth, blankPlayer, get, list, wealthDecoder)
module Api.Player exposing (Player, Wealth, blankPlayer, get, list, playerDecoder, wealthDecoder)
import Http
import Json.Decode as D exposing (Decoder, int, string)

View File

@@ -5,6 +5,7 @@ import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Page.Admin as Admin
import Page.Chest as Chest
import Page.Chest.Wealth as Wealth
import Page.Shop as Shop
import Session exposing (Session)
import Utils exposing (renderIcon)
@@ -83,8 +84,8 @@ view page =
case maybeSession of
Just session ->
case Session.user session of
Session.Player id ->
String.fromInt id
Session.Player player _ ->
player.name
Session.Admin ->
"Administration"
@@ -96,13 +97,13 @@ view page =
case maybeSession of
Just session ->
case Session.user session of
Session.Player id ->
Session.Player player _ ->
let
linkWithGem =
navLink "fas fa-gem"
in
[ navLink "fas fa-store-alt" "Marchand" "/marchand"
, if id == 0 then
, if player.id == 0 then
linkWithGem "Nouveau loot" "/nouveau-tresor"
else
@@ -117,43 +118,52 @@ view page =
in
( title
, { title = navbarTitle, links = navbarLinks }
, viewSessionBar maybeSession controls
:: content
, [ div [ class "container" ] <|
viewSessionBar maybeSession [ controls ]
:: content
]
)
viewSessionBar maybeSession controls =
controls
let
user =
case Maybe.map Session.user maybeSession of
Nothing ->
[ text "" ]
Just (Session.Player player wealth) ->
Wealth.view player.wealth wealth
++ (if player.debt > 0 then
[ div [ class "level-item" ]
[ p [ class "heading is-size-4 has-text-danger" ]
[ text ("Dette : " ++ String.fromInt player.debt ++ "po") ]
]
]
else
[]
)
|> List.map (Html.map Wealth)
Just Session.Admin ->
[ text "Admin" ]
in
section [ class "hero is-dark is-bold" ]
[ div [ class "hero-body" ]
[ renderLevel user controls ]
]
renderLevel left right =
div [ class "level container is-mobile" ]
[ div [ class "level-left" ] left
, div [ class "level-right" ] right
]
-- PLAYER BAR
{-
viewPlayerBar : Player -> List (Html Msg) -> Wealth.Model -> Html Msg
viewPlayerBar player actionControls wealthModel =
section [ class "hero is-dark is-bold" ]
[ div [ class "hero-body" ]
[ div [ class "level container is-mobile" ]
[ div [ class "level-left" ]
(Wealth.view player.wealth wealthModel
++ (if player.debt > 0 then
[ div [ class "level-item" ]
[ p [ class "heading is-size-4 has-text-danger" ]
[ text ("Dette : " ++ String.fromInt player.debt ++ "po") ]
]
]
else
[]
)
)
|> Html.map WealthMsg
, div [ class "level-right" ] actionControls
]
]
]
-}
navLink icon linkText url =
@@ -172,6 +182,7 @@ type PageMsg
= GotChestMsg Chest.Msg
| GotAdminMsg Admin.Msg
| GotShopMsg Shop.Msg
| Wealth Wealth.Msg
update msg page =
@@ -204,7 +215,7 @@ updatePage toPage toMsg ( subModel, subMsg ) =
gotoHome session =
case Session.user session of
Session.Player _ ->
Session.Player _ _ ->
Chest.init session
|> updatePage Chest GotChestMsg

View File

@@ -121,8 +121,8 @@ init session =
playerId =
case Session.user session of
Session.Player id ->
id
Session.Player player _ ->
player.id
Session.Admin ->
0
@@ -148,9 +148,7 @@ init session =
Route.PlayerLoot
Nothing
""
(Wealth.init
blankPlayer.wealth
)
Wealth.init
[]
, Cmd.batch
[ Api.Player.get GotPlayer playerId

View File

@@ -11,7 +11,7 @@ type Model
| Edit String
init wealth =
init =
View

View File

@@ -47,7 +47,7 @@ view model =
Session.Admin ->
button [ class "button", onClick IntoRefresh ] [ text "Remplacer" ]
Session.Player _ ->
Session.Player _ _ ->
button [ class "button" ] [ text "Acheter" ]
, [ Table.view Table.name loot ]
)

View File

@@ -1,12 +1,15 @@
module Session exposing (Session, User(..), getSession, init, key, user)
import Api
import Api.Player as Player exposing (Player)
import Browser.Navigation as Nav
import Http
import Json.Decode as D
import Page.Chest.Wealth as Wealth
type User
= Player Int
= Player Player Wealth.Model
| Admin
@@ -17,29 +20,16 @@ type Session
init : (Maybe Session -> msg) -> Nav.Key -> Cmd msg
init toMsg navKey =
let
toSession : Result Http.Error String -> msg
toSession : Maybe Player -> msg
toSession response =
case Debug.log "got session:" response of
Ok value ->
if value == "admin" then
toMsg <| Just (Session navKey Admin)
case response of
Just player ->
toMsg <| Just (Session navKey (Player player Wealth.init))
else
case String.toInt value of
Just id ->
toMsg <| Just (Session navKey (Player id))
Nothing ->
toMsg
Nothing
Err _ ->
Nothing ->
toMsg Nothing
in
Http.get
{ url = "http://localhost:8088/session"
, expect = Http.expectJson toSession D.string
}
Api.fetchSession toSession
getSession : { r | session : Session } -> Session