module Mastermind.View where
open import FRP.JS.Behaviour using ( [_] ; accumHoldBy ; map ; join )
open import FRP.JS.Product using ( _,_ )
open import FRP.JS.Event using ( tag )
open import FRP.JS.DOM using ( element ; attr ; attr+ ; text
; text+ ; listen+ ; click ; _++_
; element+ ; _+++_ ) renaming ([] to []D)
open import FRP.JS.Delay using ( _ms )
open import FRP.JS.Time using ( Time ; epoch )
open import FRP.JS.String using ( String ) renaming (_++_ to _++s_)
open import FRP.JS.Maybe using ( Maybe ; just ; nothing )
open import FRP.JS.Nat using ( ℕ ; zero ; suc )
open import Mastermind.View.Base
open import Mastermind.Model
open import Mastermind.Auxiliary
step : State → Button → State
step state restart = newGame state
step state button with State.gamestate state
... | ended _ = state
... | active with button
... | color c = modifyColor c state
... | ok = checkGuess state
... | clear = clearGuess state
... | move direction = moveGuessPos direction state
... | _ = state
model : Model
model {epoch (time ms)} =
accumHoldBy step (init time)
button : ButtonHandler
button button =
listen+ click (λ _ → button) (
element+ "button" (
text+ [ button$ button ] +++
attr+ "class" [ buttonClass button ]))
keypad : InteractiveButtons
keypad =
element+ "div" (
button (color 1) +++
button (color 2) +++
button (color 3) +++
button (color 4) +++
button (color 5) +++
button (color 6)) +++
element+ "div" (
button (move ⟵) +++
button (move ⟶)) +++
element+ "div" (
button ok +++
button clear +++
button restart)
addID : (State → String) → UIElement
addID fun σ = attr "id" (map fun σ)
displayColors : (ℕ → UIElement) → UIElement
displayColors display-fun σ =
element "table" (
attr "id" [ "guesstable" ] ++
element "tr" (
element "td" []D ++
display-fun 0 σ ++
display-fun 1 σ ++
display-fun 2 σ ++
display-fun 3 σ ++
element "td" []D))
colorClass-i : Colors → ℕ → String
colorClass-i colors i with lookupv colors i
... | nothing = "color_none"
... | just c = colorClass c
displayGuessPart : ℕ → UIElement
displayGuessPart i σ =
element "td" (
text (map (guessElemText i) σ) ++
attr "class" (map (λ state → "color " ++s class state) σ) ++
attr "id" (map (guessElemID i) σ))
where
class : State → String
class state =
colorClass-i (State.guess state) i
displayGuess : UIElement
displayGuess σ =
displayColors displayGuessPart σ
displayHistoryGuessElem : ℕ → ℕ → UIElement
displayHistoryGuessElem line i σ =
element "td" (
attr "class" (map (λ state → "color " ++s class state) σ))
where
class : State → String
class state with getHistoryGuess-i line state
... | nothing = "color_none"
... | just colors = colorClass-i colors i
displayHistoryGuess : (n : ℕ) → UIElement
displayHistoryGuess n σ =
displayColors (displayHistoryGuessElem n) σ
matchField : ℕ → ℕ → UIElement
matchField line i σ =
element "td" (
addID (histelemToStr line (histmatchtoStr i)) σ ++
text [ " " ])
displayHistoryMatches : (n : ℕ) → UIElement
displayHistoryMatches n σ =
element "table" (
element "tr" (
matchField n 0 σ ++
matchField n 1 σ) ++
element "tr" (
matchField n 2 σ ++
matchField n 3 σ) ++
attr "id" [ "matchtable" ])
displayHistoryLine : (n : ℕ) → UIElement
displayHistoryLine n σ =
element "tr" (
element "td" []D ++
element "td" (
displayHistoryGuess n σ ++
attr "id" [ "histval" ] ) ++
element "td" (
displayHistoryMatches n σ ++
attr "id" [ "hist_matches" ] ) ++
element "td" []D)
displayHistory : ℕ → UIElement
displayHistory 0 σ = []D
displayHistory (suc n) σ =
displayHistory n σ ++ displayHistoryLine n σ
display : UIElement
display σ =
element "table" (
displayHistory maxGuesses σ ++
attr "id" [ "historytable" ] ) ++
element "div" (
displayGuess σ) ++
element "div" (
text (map state$ σ))
view : View
view with keypad
... | (dom , evt) = display (model evt) ++ dom