Channel About Photos Files Calendar Cards Articles Wikis Directory Help Hexit Language Public Stream QRator Random Channel Report Bug Search
   
[h2]
-- Show the current time in your time zone.[/h2]

-- Read how it works:

[h2]-- https://guide.elm-lang.org/effects/time.html[/h2]

-- For an analog clock, check out this SVG example:

[h2]-- https://elm-lang.org/examples/clock[/h2]

import Browser
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Task
import Time

-- MAIN

main =
Browser.element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}

-- MODEL

type alias Model =
{ zone : Time.Zone
, time : Time.Posix
, current : Bool
}

init : () -> (Model, Cmd Msg)
init _ =
( Model Time.utc (Time.millisToPosix 0) True
, Task.perform AdjustTimeZone Time.here
)

-- UPDATE

type Msg
= Tick Time.Posix
| AdjustTimeZone Time.Zone
| ToggleTime

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Tick newTime ->
( { model | time = newTime }
, Cmd.none
)

AdjustTimeZone newZone -> ( { model | zone = newZone } , Cmd.none )ToggleTime -> ( { model | current = not model.current } , Cmd.none )

-- SUBSCRIPTIONS

subscriptions : Model -> Sub Msg
subscriptions model =
case model.current of
True ->
Time.every 10 Tick
False ->
Sub.none

-- VIEW

fill : String -> String
fill value =
case value of
"0" ->
"00"
"1" ->
"01"
"2" ->
"02"

"3" ->
"03"
"4" ->
"04"
"5" ->
"05"
"6" ->
"06"
"7" ->
"07"
"8" ->
"08"
"9" ->
"09"
_ ->
value

view : Model -> Html Msg
view model =
let
hour = fill ( String.fromInt (Time.toHour model.zone model.time))
minute = fill ( String.fromInt (Time.toMinute model.zone model.time))
second = fill ( String.fromInt (Time.toSecond model.zone model.time))
in
div [ style "margin" "1em"
, style "color" "blue"
, style "font-size" "5em"] [
h1 [] [ text (hour ++ ":" ++ minute ++ ":" ++ second) ]
, button [ onClick ToggleTime, style "font-size" "1em" ] [ text "Halt" ]
]
  last edited: Mon, 20 Jul 2020 10:01:40 +0200   
-- Press a button to generate a random number between 1 and 6.

-- Read how it works:

-- https://guide.elm-lang.org/effects/random.html

import Browser
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Random
import Http

-- MAIN

main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}

-- MODEL

type alias Model =
{ dieFace1 : Int
, dieFace2: Int
}

init : () -> (Model, Cmd Msg)
init _ =
( Model 1 1
, Cmd.none
)

-- UPDATE

getDiceFace: Int -> String
getDiceFace int =
case int of
1 ->
"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fstatic.independent.co.uk%2Fs3fs-public%2Fthumbnails%2Fimage%2F2018%2F11%2F05%2F09%2Fzonkey-1.jpg&f=1&nofb=1"
2 ->
"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fpinkbananamilk.files.wordpress.com%2F2013%2F10%2Fdonkeys.jpg&f=1&nofb=1"
3 ->
"https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Fcdn.images.express.co.uk%2Fimg%2Fdynamic%2F109%2F590x%2Fdonkey-397805.jpg&f=1&nofb=1"
4 ->
"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.donkeybreedsociety.co.uk%2Fwp-content%2Fuploads%2F2017%2F11%2Fcropped-donkeys-1.jpg&f=1&nofb=1"
5 ->
"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.irishcentral.com%2Fuploads%2Farticle%2F119447%2Fcropped_donegal-donkey-sanctuary-group-facebook.jpg&f=1&nofb=1"
6 ->
"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fthumbs.dreamstime.com%2Fb%2Fsix-curious-funny-donkeys-carpathian-mountains-looking-looking-straight-ahead-73282529.jpg&f=1&nofb=1"
_ ->
""

type Msg
= Roll
| NewFace (Int, Int)

-- helper for two randoms
tworand: Random.Generator (Int, Int)
tworand =
Random.pair
(Random.weighted (10,1)
[(20,2)
,(5,3)
,(100,4)
,(25,5)
,(1,6)
])
(Random.weighted (50,1)
[(30,2)
,(20,3)
,(10,4)
,(1,5)
,(20,6)
])

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
( model
, Random.generate NewFace tworand
)

NewFace (nf1, nf2) -> ( Model nf1 nf2 , Cmd.none )

-- SUBSCRIPTIONS

subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none

-- VIEW

view : Model -> Html Msg
view model =
div []
[ img [ src (getDiceFace model.dieFace1), height 100] [] ,
img [ src (getDiceFace model.dieFace2), height 100] [] ,
br [][],
button [ onClick Roll ] [ text "Roll" ]
]