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

106
Main.elm
View file

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