Skip to content

Commit

Permalink
Feat: have the secret word picked every day (#25)
Browse files Browse the repository at this point in the history
* feat: added date and time

* feat: computing days since parle epoch

* feat: changing word daily
  • Loading branch information
akiross authored Apr 20, 2024
1 parent 2362f16 commit cbc05f3
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 29 deletions.
7 changes: 5 additions & 2 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm-explorations/test": "2.2.0",
"justinmimbs/time-extra": "1.2.0",
"mdgriffith/elm-ui": "1.1.8"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/parser": "1.1.0",
"elm/random": "1.0.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.3"
"elm/virtual-dom": "1.0.3",
"justinmimbs/date": "4.1.0"
}
},
"test-dependencies": {
Expand Down
113 changes: 98 additions & 15 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import Element.Font as Font
import Html exposing (Html)
import Html.Events
import Json.Decode as Decode
import Validate exposing (getSecretWord, isParola)
import Parole
import Task
import Time exposing (Month(..))
import Validate exposing (daysSinceStart, getSecretWord, isParola)
import Word exposing (..)


Expand All @@ -28,23 +31,26 @@ type alias Model =
{ guesses : List (List MatchedChar)
, current : List Char
, solution : List Char
, timestamp : Time.Posix
}


init : () -> ( Model, Cmd Msg )
init _ =
( { guesses = []
, current = []
, solution = String.toList getSecretWord
, solution = [ 'A', 'M', 'I', 'C', 'Y' ]
, timestamp = Time.millisToPosix 0
}
, Cmd.none
, Task.perform QueryTime Time.now
)


type Msg
= KeyPressed Char
| Backspace
| Confirm
| QueryTime Time.Posix


update : Msg -> Model -> ( Model, Cmd Msg )
Expand Down Expand Up @@ -80,6 +86,12 @@ update msg model =
Backspace ->
-- Remove last character from current, as long as it's not empty
Debug.log "Chomped" { model | current = List.take (currentGuessLen - 1) model.current }

QueryTime t ->
{ model
| timestamp = t
, solution = String.toList (getSecretWord t)
}
, Cmd.none
)

Expand Down Expand Up @@ -167,43 +179,114 @@ view model =
if isGameEnded model then
layout [ width fill, height fill ]
(column [ width (fill |> maximum 500), height fill, centerX, bgCyan ]
[ viewHeader
[ viewHeader model
, viewEndGame model
]
)

else
layout [ width fill, height fill ]
(column [ width (fill |> maximum 500), height fill, centerX, bgCyan ]
[ viewHeader
[ viewHeader model
, viewGridArea model
, viewKeyboardArea model
]
)


viewHeader =
viewHeader model =
row
[ width fill
, Border.color (rgb255 255 0 0)
, Border.widthEach { top = 0, bottom = 1, left = 0, right = 0 }
]
[ viewHeaderBurger
, viewHeaderTitle
, viewHeaderButton
[ viewHeaderBurger model
, viewHeaderDate model
, viewHeaderTime model
]


viewHeaderBurger =
el [ alignLeft, bgPink ] (text "Burger")
viewHeaderBurger model =
el [ alignLeft, bgPink ]
(text <|
String.fromInt <|
daysSinceStart model.timestamp
)


viewHeaderDate model =
let
date =
model.timestamp
in
el [ centerX, bgYell ] (text <| toUtcDate date)


viewHeaderTime model =
let
date =
model.timestamp
in
el [ alignRight, bgPink ] (text <| toUtcTime date)


toUtcTime : Time.Posix -> String
toUtcTime time =
String.fromInt (Time.toHour Time.utc time)
++ ":"
++ String.fromInt (Time.toMinute Time.utc time)
++ ":"
++ String.fromInt (Time.toSecond Time.utc time)
++ " (UTC)"


toUtcDate : Time.Posix -> String
toUtcDate time =
String.fromInt (Time.toYear Time.utc time)
++ "-"
++ toMonthNumber (Time.toMonth Time.utc time)
++ "-"
++ String.fromInt (Time.toDay Time.utc time)


toMonthNumber : Time.Month -> String
toMonthNumber month =
case month of
Jan ->
"01"

Feb ->
"02"

Mar ->
"03"

Apr ->
"04"

May ->
"05"

Jun ->
"06"

Jul ->
"07"

Aug ->
"08"

Sep ->
"09"

viewHeaderTitle =
el [ centerX, bgYell ] (text "Title")
Oct ->
"10"

Nov ->
"11"

viewHeaderButton =
el [ alignRight, bgPink ] (text "Button")
Dec ->
"12"



Expand Down
9 changes: 8 additions & 1 deletion src/Parole.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module Parole exposing (parole, segrete)
module Parole exposing (inizio, parole, segrete)

import Time


inizio =
-- 2022-01-03 00:00:00
Time.millisToPosix 1641168000000


parole =
Expand Down
31 changes: 20 additions & 11 deletions src/Validate.elm
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
module Validate exposing (getSecretWord, isParola)
module Validate exposing (daysSinceStart, getSecretWord, isParola)

import Array
import Debug
import Parole exposing (parole, segrete)
import Parole exposing (inizio, parole, segrete)
import Time
import Time.Extra exposing (Interval(..), diff)


daysSinceStart timestamp =
diff Day Time.utc inizio timestamp


secrets =
Array.fromList segrete


getSecretWord : String
getSecretWord =
secret
getSecretWord : Time.Posix -> String
getSecretWord timestamp =
let
days =
daysSinceStart timestamp

idx =
modBy (Array.length secrets) days
in
String.toUpper <|
Maybe.withDefault "OSSAP" <|
Array.get idx secrets


isParola : String -> Bool
Expand All @@ -21,9 +36,3 @@ isParola w =


-- Secret word


secret =
String.toUpper <|
Maybe.withDefault "OSSAP" <|
Array.get 100 secrets

0 comments on commit cbc05f3

Please sign in to comment.