commit
3626dde6fc
|
@ -19,10 +19,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
-}
|
-}
|
||||||
|
|
||||||
|
{-# LANGUAGE LambdaCase #-}
|
||||||
|
|
||||||
module Mtlstats.Actions
|
module Mtlstats.Actions
|
||||||
( startNewSeason
|
( startNewSeason
|
||||||
, resetYtd
|
, resetYtd
|
||||||
, startNewGame
|
, startNewGame
|
||||||
|
, addChar
|
||||||
|
, removeChar
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Lens.Micro (over, (&), (.~), (?~), (%~))
|
import Lens.Micro (over, (&), (.~), (?~), (%~))
|
||||||
|
@ -44,3 +48,13 @@ startNewGame :: ProgState -> ProgState
|
||||||
startNewGame
|
startNewGame
|
||||||
= (progMode .~ NewGame newGameState)
|
= (progMode .~ NewGame newGameState)
|
||||||
. (database . dbGames %~ succ)
|
. (database . dbGames %~ succ)
|
||||||
|
|
||||||
|
-- | Adds a character to the input buffer
|
||||||
|
addChar :: Char -> ProgState -> ProgState
|
||||||
|
addChar c = inputBuffer %~ (++[c])
|
||||||
|
|
||||||
|
-- | Removes a character from the input buffer (if possible)
|
||||||
|
removeChar :: ProgState -> ProgState
|
||||||
|
removeChar = inputBuffer %~ \case
|
||||||
|
"" -> ""
|
||||||
|
str -> init str
|
||||||
|
|
|
@ -30,6 +30,7 @@ import qualified UI.NCurses as C
|
||||||
|
|
||||||
import Mtlstats.Actions
|
import Mtlstats.Actions
|
||||||
import Mtlstats.Menu
|
import Mtlstats.Menu
|
||||||
|
import Mtlstats.Prompt
|
||||||
import Mtlstats.Types
|
import Mtlstats.Types
|
||||||
|
|
||||||
-- | Event handler
|
-- | Event handler
|
||||||
|
@ -41,6 +42,10 @@ handleEvent e = gets (view progMode) >>= \case
|
||||||
MainMenu -> menuHandler mainMenu e
|
MainMenu -> menuHandler mainMenu e
|
||||||
NewSeason -> menuHandler newSeasonMenu e >> return True
|
NewSeason -> menuHandler newSeasonMenu e >> return True
|
||||||
NewGame gs
|
NewGame gs
|
||||||
| null $ gs ^. gameType ->
|
| null $ gs ^. gameType -> do
|
||||||
menuHandler gameTypeMenu e >> return True
|
menuHandler gameTypeMenu e
|
||||||
|
return True
|
||||||
|
| null $ gs ^. homeScore -> do
|
||||||
|
promptHandler homeScorePrompt e
|
||||||
|
return True
|
||||||
| otherwise -> undefined
|
| otherwise -> undefined
|
||||||
|
|
|
@ -38,8 +38,10 @@ import Mtlstats.Types
|
||||||
import Mtlstats.Types.Menu
|
import Mtlstats.Types.Menu
|
||||||
|
|
||||||
-- | The draw function for a 'Menu'
|
-- | The draw function for a 'Menu'
|
||||||
drawMenu :: Menu a -> C.Update ()
|
drawMenu :: Menu a -> C.Update C.CursorMode
|
||||||
drawMenu = C.drawString . show
|
drawMenu m = do
|
||||||
|
C.drawString $ show m
|
||||||
|
return C.CursorInvisible
|
||||||
|
|
||||||
-- | The event handler for a 'Menu'
|
-- | The event handler for a 'Menu'
|
||||||
menuHandler :: Menu a -> C.Event -> Action a
|
menuHandler :: Menu a -> C.Event -> Action a
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
{- |
|
||||||
|
|
||||||
|
mtlstats
|
||||||
|
Copyright (C) 2019 Rhéal Lamothe
|
||||||
|
<rheal.lamothe@gmail.com>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but
|
||||||
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
-}
|
||||||
|
|
||||||
|
module Mtlstats.Prompt (
|
||||||
|
-- * Prompt Functions
|
||||||
|
drawPrompt,
|
||||||
|
promptHandler,
|
||||||
|
numPrompt,
|
||||||
|
-- * Individual prompts
|
||||||
|
homeScorePrompt
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Control.Monad (when)
|
||||||
|
import Control.Monad.Trans.State (gets, modify)
|
||||||
|
import Data.Char (isDigit, toUpper)
|
||||||
|
import Data.Foldable (forM_)
|
||||||
|
import Lens.Micro ((^.), (.~), (?~))
|
||||||
|
import Lens.Micro.Extras (view)
|
||||||
|
import Text.Read (readMaybe)
|
||||||
|
import qualified UI.NCurses as C
|
||||||
|
|
||||||
|
import Mtlstats.Actions
|
||||||
|
import Mtlstats.Types
|
||||||
|
|
||||||
|
-- | Draws the prompt to the screen
|
||||||
|
drawPrompt :: Prompt -> ProgState -> C.Update C.CursorMode
|
||||||
|
drawPrompt p s = do
|
||||||
|
promptDrawer p s
|
||||||
|
return C.CursorVisible
|
||||||
|
|
||||||
|
-- | Event handler for a prompt
|
||||||
|
promptHandler :: Prompt -> C.Event -> Action ()
|
||||||
|
promptHandler p (C.EventCharacter '\n') = do
|
||||||
|
val <- gets $ view inputBuffer
|
||||||
|
modify $ inputBuffer .~ ""
|
||||||
|
promptAction p val
|
||||||
|
promptHandler p (C.EventCharacter c) = let
|
||||||
|
c' = toUpper c
|
||||||
|
in when (promptCharCheck p c') $
|
||||||
|
modify $ addChar c'
|
||||||
|
promptHandler _ (C.EventSpecialKey C.KeyBackspace) =
|
||||||
|
modify removeChar
|
||||||
|
promptHandler p (C.EventSpecialKey (C.KeyFunction k)) =
|
||||||
|
promptFunctionKey p k
|
||||||
|
promptHandler _ _ = return ()
|
||||||
|
|
||||||
|
-- | Builds a numeric prompt
|
||||||
|
numPrompt
|
||||||
|
:: String
|
||||||
|
-- ^ The prompt string
|
||||||
|
-> (Int -> Action ())
|
||||||
|
-- ^ The callback function for the result
|
||||||
|
-> Prompt
|
||||||
|
numPrompt pStr act = Prompt
|
||||||
|
{ promptDrawer = \s -> C.drawString $ pStr ++ s ^. inputBuffer
|
||||||
|
, promptCharCheck = isDigit
|
||||||
|
, promptAction = \inStr -> forM_ (readMaybe inStr) act
|
||||||
|
, promptFunctionKey = const $ return ()
|
||||||
|
}
|
||||||
|
|
||||||
|
homeScorePrompt :: Prompt
|
||||||
|
homeScorePrompt = numPrompt "Home score: " $
|
||||||
|
modify . (progMode . homeScoreL ?~)
|
|
@ -33,10 +33,12 @@ module Mtlstats.Types (
|
||||||
PlayerStats (..),
|
PlayerStats (..),
|
||||||
Goalie (..),
|
Goalie (..),
|
||||||
GoalieStats (..),
|
GoalieStats (..),
|
||||||
|
Prompt (..),
|
||||||
-- * Lenses
|
-- * Lenses
|
||||||
-- ** ProgState Lenses
|
-- ** ProgState Lenses
|
||||||
database,
|
database,
|
||||||
progMode,
|
progMode,
|
||||||
|
inputBuffer,
|
||||||
-- ** GameState Lenses
|
-- ** GameState Lenses
|
||||||
gameType,
|
gameType,
|
||||||
homeScore,
|
homeScore,
|
||||||
|
@ -102,7 +104,7 @@ import Data.Aeson
|
||||||
)
|
)
|
||||||
import Lens.Micro (Lens', lens, (&), (^.), (.~))
|
import Lens.Micro (Lens', lens, (&), (^.), (.~))
|
||||||
import Lens.Micro.TH (makeLenses)
|
import Lens.Micro.TH (makeLenses)
|
||||||
import UI.NCurses (Curses)
|
import UI.NCurses (Curses, Update)
|
||||||
|
|
||||||
-- | Action which maintains program state
|
-- | Action which maintains program state
|
||||||
type Action a = StateT ProgState Curses a
|
type Action a = StateT ProgState Curses a
|
||||||
|
@ -113,6 +115,8 @@ data ProgState = ProgState
|
||||||
-- ^ The data to be saved
|
-- ^ The data to be saved
|
||||||
, _progMode :: ProgMode
|
, _progMode :: ProgMode
|
||||||
-- ^ The program's mode
|
-- ^ The program's mode
|
||||||
|
, _inputBuffer :: String
|
||||||
|
-- ^ Buffer for user input
|
||||||
} deriving (Eq, Show)
|
} deriving (Eq, Show)
|
||||||
|
|
||||||
-- | The game state
|
-- | The game state
|
||||||
|
@ -308,6 +312,18 @@ instance ToJSON GoalieStats where
|
||||||
"losses" .= l <>
|
"losses" .= l <>
|
||||||
"ties" .= t
|
"ties" .= t
|
||||||
|
|
||||||
|
-- | Defines a user prompt
|
||||||
|
data Prompt = Prompt
|
||||||
|
{ promptDrawer :: ProgState -> Update ()
|
||||||
|
-- ^ Draws the prompt to thr screen
|
||||||
|
, promptCharCheck :: Char -> Bool
|
||||||
|
-- ^ Determines whether or not the character is valid
|
||||||
|
, promptAction :: String -> Action ()
|
||||||
|
-- ^ Action to perform when the value is entered
|
||||||
|
, promptFunctionKey :: Integer -> Action ()
|
||||||
|
-- ^ Action to perform when a function key is pressed
|
||||||
|
}
|
||||||
|
|
||||||
makeLenses ''ProgState
|
makeLenses ''ProgState
|
||||||
makeLenses ''GameState
|
makeLenses ''GameState
|
||||||
makeLenses ''Database
|
makeLenses ''Database
|
||||||
|
@ -348,6 +364,7 @@ newProgState :: ProgState
|
||||||
newProgState = ProgState
|
newProgState = ProgState
|
||||||
{ _database = newDatabase
|
{ _database = newDatabase
|
||||||
, _progMode = MainMenu
|
, _progMode = MainMenu
|
||||||
|
, _inputBuffer = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
-- | Constructor for a 'GameState'
|
-- | Constructor for a 'GameState'
|
||||||
|
|
|
@ -21,22 +21,27 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
module Mtlstats.UI (draw) where
|
module Mtlstats.UI (draw) where
|
||||||
|
|
||||||
|
import Control.Monad (void)
|
||||||
import Lens.Micro ((^.))
|
import Lens.Micro ((^.))
|
||||||
import qualified UI.NCurses as C
|
import qualified UI.NCurses as C
|
||||||
|
|
||||||
import Mtlstats.Menu
|
import Mtlstats.Menu
|
||||||
|
import Mtlstats.Prompt
|
||||||
import Mtlstats.Types
|
import Mtlstats.Types
|
||||||
|
|
||||||
-- | Drawing function
|
-- | Drawing function
|
||||||
draw :: ProgState -> C.Curses ()
|
draw :: ProgState -> C.Curses ()
|
||||||
draw s = do
|
draw s = do
|
||||||
|
void $ C.setCursorMode C.CursorInvisible
|
||||||
w <- C.defaultWindow
|
w <- C.defaultWindow
|
||||||
C.updateWindow w $ do
|
cm <- C.updateWindow w $ do
|
||||||
C.clear
|
C.clear
|
||||||
case s ^. progMode of
|
case s ^. progMode of
|
||||||
MainMenu -> drawMenu mainMenu
|
MainMenu -> drawMenu mainMenu
|
||||||
NewSeason -> drawMenu newSeasonMenu
|
NewSeason -> drawMenu newSeasonMenu
|
||||||
NewGame gs
|
NewGame gs
|
||||||
| null $ gs ^. gameType -> drawMenu gameTypeMenu
|
| null $ gs ^. gameType -> drawMenu gameTypeMenu
|
||||||
|
| null $ gs ^. homeScore -> drawPrompt homeScorePrompt s
|
||||||
| otherwise -> undefined
|
| otherwise -> undefined
|
||||||
C.render
|
C.render
|
||||||
|
void $ C.setCursorMode cm
|
||||||
|
|
|
@ -34,6 +34,8 @@ spec = describe "Mtlstats.Actions" $ do
|
||||||
startNewSeasonSpec
|
startNewSeasonSpec
|
||||||
startNewGameSpec
|
startNewGameSpec
|
||||||
resetYtdSpec
|
resetYtdSpec
|
||||||
|
addCharSpec
|
||||||
|
removeCharSpec
|
||||||
|
|
||||||
startNewSeasonSpec :: Spec
|
startNewSeasonSpec :: Spec
|
||||||
startNewSeasonSpec = describe "startNewSeason" $ do
|
startNewSeasonSpec = describe "startNewSeason" $ do
|
||||||
|
@ -101,6 +103,29 @@ resetYtdSpec = describe "resetYtd" $
|
||||||
lt ^. gsTies `shouldNotBe` 0) $
|
lt ^. gsTies `shouldNotBe` 0) $
|
||||||
s ^. database . dbGoalies
|
s ^. database . dbGoalies
|
||||||
|
|
||||||
|
addCharSpec :: Spec
|
||||||
|
addCharSpec = describe "addChar" $
|
||||||
|
it "should add the character to the input buffer" $ let
|
||||||
|
s = newProgState
|
||||||
|
& inputBuffer .~ "foo"
|
||||||
|
& addChar 'd'
|
||||||
|
in s ^. inputBuffer `shouldBe` "food"
|
||||||
|
|
||||||
|
removeCharSpec :: Spec
|
||||||
|
removeCharSpec = describe "removeChar" $ do
|
||||||
|
|
||||||
|
context "empty" $
|
||||||
|
it "should remove the character from the input buffer" $ let
|
||||||
|
s = removeChar newProgState
|
||||||
|
in s ^. inputBuffer `shouldBe` ""
|
||||||
|
|
||||||
|
context "not empty" $
|
||||||
|
it "should remove the character from the input buffer" $ let
|
||||||
|
s = newProgState
|
||||||
|
& inputBuffer .~ "foo"
|
||||||
|
& removeChar
|
||||||
|
in s ^. inputBuffer `shouldBe` "fo"
|
||||||
|
|
||||||
makePlayer :: IO Player
|
makePlayer :: IO Player
|
||||||
makePlayer = Player
|
makePlayer = Player
|
||||||
<$> makeNum
|
<$> makeNum
|
||||||
|
|
Loading…
Reference in New Issue
Block a user