multiple resources view + model

This commit is contained in:
Yann Esposito (Yogsototh) 2016-08-01 23:47:06 +02:00
parent 9d4ed240df
commit 017f7e7c20
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646

162
Main.elm
View file

@ -3,6 +3,10 @@ import Html.Attributes exposing (style)
import Html.App as App import Html.App as App
import Time exposing (Time,millisecond) import Time exposing (Time,millisecond)
import Html.Events exposing (onClick) import Html.Events exposing (onClick)
-- import List exposing (map, foldl, head, tail)
import Set as Set
import Dict as Dict
-- import Maybe exposing (withDefault)
main : Program Never main : Program Never
main = App.program { init = init main = App.program { init = init
@ -13,59 +17,81 @@ main = App.program { init = init
-- MODEL -- MODEL
-- type alias NatResource = { name : String type alias NeededResource =
-- , growthBySec : Float { resourceName : String
-- , minBound : Float , neededQuantity : Float
-- , maxBound : Float , period : Time
-- , value : Float -- variance of resource access by in need resource unity
-- } , variance : Float
-- -- death probability for each needed resource
-- humanity : NatResource -- that don't reach its consumption goal
-- humanity = { name = "sane people" , deathProbability : Float
-- , minBound = 0 }
-- , maxBound = 20000000000
-- , value = 7000000000 type alias NatResource =
-- } { name : String
-- flu = { resourceName = "sane people" , quantity: Float
-- , growthBySec = -0.00001 , stdGrowBySecByUnit : (Float,Float)
-- } , neededResources : Set.Set NeededResource
-- }
type alias Model = { money : Float
, mbs : Float year : Float
year = 1000 * 60 * 60 * 24 * 365
humanity : NatResource
humanity = { name = "humanity"
, quantity = 7000000000
, stdGrowBySecByUnit = (3,30 * year)
, neededResources = Set.empty
}
type alias Model = { resources : Dict.Dict String NatResource
, t : Time , t : Time
, timeSpeed : Int , timeSpeed : Int
, relTime : Time , relTime : Time
} }
init : (Model,Cmd Msg) init : (Model,Cmd Msg)
init = ({ money = 0 init = (initModel, Cmd.none)
, mbs = 1
, relTime = 0 initModel : Model
, t = 0 initModel =
, timeSpeed = 1 { resources = initResources
}, Cmd.none) , relTime = 0
, t = 0
, timeSpeed = 1
}
initResources : Dict.Dict String NatResource
initResources = Dict.fromList (List.map (\h -> (h.name,h)) [humanity])
-- UPDATE -- UPDATE
type Msg = Gain Int | Tick Time | Reset | ChangeTimeSpeed Int type Msg = Tick Time | Reset | ChangeTimeSpeed Int
update : Msg -> Model -> (Model, Cmd Msg) update : Msg -> Model -> (Model, Cmd Msg)
update msg model = update msg model =
let newmodel = case msg of let newmodel = case msg of
Gain i -> { model | mbs = max 0 (model.mbs + (toFloat i)) }
ChangeTimeSpeed i -> { model | timeSpeed = max 0 i } ChangeTimeSpeed i -> { model | timeSpeed = max 0 i }
Tick newTime -> Tick newTime ->
{ model | money = model.money + let elapsedTimeInMs = (if model.t > 0
(if model.t > 0 then (toFloat model.timeSpeed) * (newTime - model.t)
then model.mbs * (toFloat model.timeSpeed) * (newTime - model.t) / 1000 else 0)
else 0) in { model
, t = newTime | resources = Dict.map (updateResource model.resources elapsedTimeInMs) model.resources
, relTime = (if model.t > 0 , t = newTime
then model.relTime + (toFloat model.timeSpeed) * (newTime - model.t) , relTime = (if model.t > 0
else 0)} then model.relTime + (toFloat model.timeSpeed) * (newTime - model.t)
Reset -> { model | money = 0 } else 0)}
Reset -> fst init
in (newmodel,Cmd.none) in (newmodel,Cmd.none)
updateResource : Dict.Dict String NatResource -> Float -> String -> NatResource -> NatResource
updateResource allResources elapsedTimeInMs resourceName natResource =
let growRatio = (fst natResource.stdGrowBySecByUnit) / (snd natResource.stdGrowBySecByUnit)
in { natResource | quantity = natResource.quantity + natResource.quantity * growRatio
}
-- SUBSCRIPTION -- SUBSCRIPTION
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
@ -108,31 +134,39 @@ displayTime t =
view : Model -> Html Msg view : Model -> Html Msg
view model = div [ wdgStyle ] view model = div [ wdgStyle ]
[ div [ style [("color","#888") ([ div [ style [("color","#888")
, ("font-size","12px")]] , ("font-size","12px")]]
[text ("dbs: " ++ (toString model.mbs))] [text ("dbs: " ++ (toString model.resources))]
, div [ style [("color","#888") , div [ style [("color","#888")
, ("font-size","12px")]] , ("font-size","12px")]]
[text ("time: " ++ (displayTime <| truncate <| model.relTime))] [text ("time: " ++ (displayTime <| truncate <| model.relTime))]
, div [ style [("color","#888") , div [ style [("color","#888")
, ("font-size","12px") , ("font-size","12px")
, ("display","inline")]] , ("display","inline")]]
[text "time speed: "] [text "time speed: "]
, button [ onClick (ChangeTimeSpeed 1) ] [text "x1"] , button [ onClick (ChangeTimeSpeed 1) ] [text "x1"]
, button [ onClick (ChangeTimeSpeed 60) ] [text "1min/s"] , button [ onClick (ChangeTimeSpeed 60) ] [text "1min/s"]
, button [ onClick (ChangeTimeSpeed 3600) ] [text "1h/s"] , button [ onClick (ChangeTimeSpeed 3600) ] [text "1h/s"]
, button [ onClick (ChangeTimeSpeed (24*3600)) ] [text "1d/s"] , button [ onClick (ChangeTimeSpeed (24*3600)) ] [text "1d/s"]
, node "hr" [] [] , node "hr" [] []
, button [ onClick (Gain -1) ] [text "-1/s"] , button [ onClick Reset ] [text "reset"]]
, button [ onClick (Gain 1) ] [text "+1/s"] ++
, button [ onClick Reset ] [text "reset"] (Dict.toList model.resources |>
, div [ style [ ("font-size","21px") List.map snd |>
, ("font-family","Menlo, monaco, monospace") List.map viewResource))
, ("background","#EEE")
, ("padding","1em") viewResource : NatResource -> Html Msg
, ("margin","1em") viewResource resource =
, ("border","solid 1px #CCC") div [ style [ ("font-size","21px")
]] , ("font-family","Menlo, monaco, monospace")
[text (toString (truncate model.money))] , ("background","#EEE")
] , ("padding","1em")
, ("margin","1em")
, ("border","solid 1px #CCC")
]]
[ Html.span [style [ ("font-weight","bold")
, ("font-size",".8em")
, ("margin-bottom","2ex")]]
[text <| (resource.name) ++ ": "]
, resource.quantity |> round |> toString |> text ]