2019-08-24 16:23:56 -04:00
|
|
|
{- |
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2019-09-19 04:35:01 -04:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
|
2019-08-24 16:23:56 -04:00
|
|
|
module Mtlstats.Prompt (
|
|
|
|
-- * Prompt Functions
|
|
|
|
drawPrompt,
|
|
|
|
promptHandler,
|
2019-11-04 00:51:50 -05:00
|
|
|
promptControllerWith,
|
|
|
|
promptController,
|
2019-08-24 21:15:50 -04:00
|
|
|
strPrompt,
|
2019-11-30 11:52:06 -05:00
|
|
|
ucStrPrompt,
|
2019-08-24 19:02:29 -04:00
|
|
|
numPrompt,
|
2019-10-30 16:57:08 -04:00
|
|
|
selectPrompt,
|
2019-08-24 19:02:29 -04:00
|
|
|
-- * Individual prompts
|
2019-09-09 22:50:44 -04:00
|
|
|
playerNumPrompt,
|
|
|
|
playerNamePrompt,
|
2019-09-19 04:35:01 -04:00
|
|
|
playerPosPrompt,
|
2019-11-04 02:38:48 -05:00
|
|
|
goalieNumPrompt,
|
|
|
|
goalieNamePrompt,
|
2019-09-19 04:35:01 -04:00
|
|
|
selectPlayerPrompt,
|
2019-10-30 00:29:33 -04:00
|
|
|
selectGoaliePrompt,
|
2019-11-01 03:05:40 -04:00
|
|
|
playerToEditPrompt
|
2019-08-24 16:23:56 -04:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad (when)
|
2019-10-30 16:57:08 -04:00
|
|
|
import Control.Monad.Extra (whenJust)
|
2019-08-24 16:23:56 -04:00
|
|
|
import Control.Monad.Trans.State (gets, modify)
|
|
|
|
import Data.Char (isDigit, toUpper)
|
|
|
|
import Data.Foldable (forM_)
|
2019-11-30 11:52:06 -05:00
|
|
|
import Lens.Micro ((^.), (&), (.~), (?~), (%~))
|
2019-08-24 16:23:56 -04:00
|
|
|
import Lens.Micro.Extras (view)
|
|
|
|
import Text.Read (readMaybe)
|
|
|
|
import qualified UI.NCurses as C
|
|
|
|
|
|
|
|
import Mtlstats.Actions
|
2019-09-19 06:34:03 -04:00
|
|
|
import Mtlstats.Config
|
2019-08-24 16:23:56 -04:00
|
|
|
import Mtlstats.Types
|
2019-09-19 04:35:01 -04:00
|
|
|
import Mtlstats.Util
|
2019-08-24 16:23:56 -04:00
|
|
|
|
|
|
|
-- | 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
|
2019-11-30 11:52:06 -05:00
|
|
|
promptHandler p (C.EventCharacter c) =
|
|
|
|
modify $ inputBuffer %~ promptProcessChar p c
|
2019-08-24 16:23:56 -04:00
|
|
|
promptHandler _ (C.EventSpecialKey C.KeyBackspace) =
|
|
|
|
modify removeChar
|
2019-09-19 02:45:50 -04:00
|
|
|
promptHandler p (C.EventSpecialKey k) =
|
|
|
|
promptSpecialKey p k
|
2019-08-24 16:23:56 -04:00
|
|
|
promptHandler _ _ = return ()
|
|
|
|
|
2019-11-04 00:51:50 -05:00
|
|
|
-- | Builds a controller out of a prompt with a header
|
|
|
|
promptControllerWith
|
|
|
|
:: (ProgState -> C.Update ())
|
|
|
|
-- ^ The header
|
|
|
|
-> Prompt
|
|
|
|
-- ^ The prompt to use
|
|
|
|
-> Controller
|
|
|
|
-- ^ The resulting controller
|
|
|
|
promptControllerWith header prompt = Controller
|
|
|
|
{ drawController = \s -> do
|
|
|
|
header s
|
|
|
|
drawPrompt prompt s
|
|
|
|
, handleController = \e -> do
|
|
|
|
promptHandler prompt e
|
|
|
|
return True
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Builds a controller out of a prompt
|
|
|
|
promptController
|
|
|
|
:: Prompt
|
|
|
|
-- ^ The prompt to use
|
|
|
|
-> Controller
|
|
|
|
-- ^ The resulting controller
|
|
|
|
promptController = promptControllerWith (const $ return ())
|
|
|
|
|
2019-08-24 21:15:50 -04:00
|
|
|
-- | Builds a string prompt
|
|
|
|
strPrompt
|
|
|
|
:: String
|
|
|
|
-- ^ The prompt string
|
|
|
|
-> (String -> Action ())
|
|
|
|
-- ^ The callback function for the result
|
|
|
|
-> Prompt
|
|
|
|
strPrompt pStr act = Prompt
|
2019-11-30 11:52:06 -05:00
|
|
|
{ promptDrawer = drawSimplePrompt pStr
|
|
|
|
, promptProcessChar = \ch -> (++ [ch])
|
|
|
|
, promptAction = act
|
|
|
|
, promptSpecialKey = const $ return ()
|
2019-08-24 21:15:50 -04:00
|
|
|
}
|
|
|
|
|
2019-11-30 11:52:06 -05:00
|
|
|
-- | Creates an upper case string prompt
|
|
|
|
ucStrPrompt
|
|
|
|
:: String
|
|
|
|
-- ^ The prompt string
|
|
|
|
-> (String -> Action ())
|
|
|
|
-- ^ The callback function for the result
|
|
|
|
-> Prompt
|
|
|
|
ucStrPrompt pStr act = (ucStrPrompt pStr act)
|
|
|
|
{ promptProcessChar = \ch -> (++ [toUpper ch]) }
|
|
|
|
|
2019-08-24 16:23:56 -04:00
|
|
|
-- | Builds a numeric prompt
|
|
|
|
numPrompt
|
|
|
|
:: String
|
|
|
|
-- ^ The prompt string
|
|
|
|
-> (Int -> Action ())
|
|
|
|
-- ^ The callback function for the result
|
|
|
|
-> Prompt
|
|
|
|
numPrompt pStr act = Prompt
|
2019-11-30 11:52:06 -05:00
|
|
|
{ promptDrawer = drawSimplePrompt pStr
|
|
|
|
, promptProcessChar = \ch str -> if isDigit ch
|
|
|
|
then str ++ [ch]
|
|
|
|
else str
|
|
|
|
, promptAction = \inStr -> forM_ (readMaybe inStr) act
|
|
|
|
, promptSpecialKey = const $ return ()
|
2019-08-24 16:23:56 -04:00
|
|
|
}
|
2019-08-24 19:02:29 -04:00
|
|
|
|
2019-10-30 16:57:08 -04:00
|
|
|
-- | Builds a selection prompt
|
|
|
|
selectPrompt :: SelectParams a -> Prompt
|
|
|
|
selectPrompt params = Prompt
|
|
|
|
{ promptDrawer = \s -> do
|
|
|
|
let sStr = s^.inputBuffer
|
|
|
|
C.drawString $ spPrompt params ++ sStr
|
|
|
|
(row, col) <- C.cursorPosition
|
|
|
|
C.drawString $ "\n\n" ++ spSearchHeader params ++ "\n"
|
|
|
|
let results = zip [1..maxFunKeys] $ spSearch params sStr (s^.database)
|
|
|
|
C.drawString $ unlines $ map
|
|
|
|
(\(n, (_, x)) -> let
|
|
|
|
desc = spElemDesc params x
|
|
|
|
in "F" ++ show n ++ ") " ++ desc)
|
|
|
|
results
|
|
|
|
C.moveCursor row col
|
2019-11-30 11:52:06 -05:00
|
|
|
, promptProcessChar = \ch -> (++[ch])
|
2019-10-30 16:57:08 -04:00
|
|
|
, promptAction = \sStr -> if null sStr
|
|
|
|
then spCallback params Nothing
|
|
|
|
else do
|
|
|
|
db <- gets (^.database)
|
|
|
|
case spSearchExact params sStr db of
|
|
|
|
Nothing -> spNotFound params sStr
|
|
|
|
Just n -> spCallback params $ Just n
|
|
|
|
, promptSpecialKey = \case
|
|
|
|
C.KeyFunction rawK -> do
|
|
|
|
sStr <- gets (^.inputBuffer)
|
|
|
|
db <- gets (^.database)
|
|
|
|
let
|
|
|
|
n = pred $ fromInteger rawK
|
|
|
|
results = spSearch params sStr db
|
|
|
|
when (n < maxFunKeys) $
|
2019-11-12 17:01:08 -05:00
|
|
|
whenJust (nth n results) $ \(sel, _) -> do
|
2019-10-30 16:57:08 -04:00
|
|
|
modify $ inputBuffer .~ ""
|
2019-11-12 17:01:08 -05:00
|
|
|
spCallback params $ Just sel
|
2019-10-30 16:57:08 -04:00
|
|
|
_ -> return ()
|
|
|
|
}
|
|
|
|
|
2019-09-09 13:04:39 -04:00
|
|
|
-- | Prompts for a new player's number
|
|
|
|
playerNumPrompt :: Prompt
|
|
|
|
playerNumPrompt = numPrompt "Player number: " $
|
|
|
|
modify . (progMode.createPlayerStateL.cpsNumber ?~)
|
|
|
|
|
2019-09-09 22:50:44 -04:00
|
|
|
-- | Prompts for a new player's name
|
|
|
|
playerNamePrompt :: Prompt
|
|
|
|
playerNamePrompt = strPrompt "Player name: " $
|
|
|
|
modify . (progMode.createPlayerStateL.cpsName .~)
|
|
|
|
|
2019-09-09 22:57:36 -04:00
|
|
|
-- | Prompts for a new player's position
|
|
|
|
playerPosPrompt :: Prompt
|
|
|
|
playerPosPrompt = strPrompt "Player position: " $
|
|
|
|
modify . (progMode.createPlayerStateL.cpsPosition .~)
|
|
|
|
|
2019-11-04 02:38:48 -05:00
|
|
|
-- | Prompts tor the goalie's number
|
|
|
|
goalieNumPrompt :: Prompt
|
|
|
|
goalieNumPrompt = numPrompt "Goalie number: " $
|
|
|
|
modify . (progMode.createGoalieStateL.cgsNumber ?~)
|
|
|
|
|
|
|
|
-- | Prompts for the goalie's name
|
|
|
|
goalieNamePrompt :: Prompt
|
|
|
|
goalieNamePrompt = strPrompt "Goalie name: " $
|
|
|
|
modify . (progMode.createGoalieStateL.cgsName .~)
|
|
|
|
|
2019-09-19 04:35:01 -04:00
|
|
|
-- | Selects a player (creating one if necessary)
|
|
|
|
selectPlayerPrompt
|
|
|
|
:: String
|
|
|
|
-- ^ The prompt string
|
|
|
|
-> (Maybe Int -> Action ())
|
|
|
|
-- ^ The callback to run (takes the index number of the payer as
|
|
|
|
-- input)
|
|
|
|
-> Prompt
|
2019-10-30 17:29:47 -04:00
|
|
|
selectPlayerPrompt pStr callback = selectPrompt SelectParams
|
|
|
|
{ spPrompt = pStr
|
|
|
|
, spSearchHeader = "Player select:"
|
|
|
|
, spSearch = \sStr db -> playerSearch sStr (db^.dbPlayers)
|
|
|
|
, spSearchExact = \sStr db -> fst <$> playerSearchExact sStr (db^.dbPlayers)
|
|
|
|
, spElemDesc = playerSummary
|
|
|
|
, spCallback = callback
|
|
|
|
, spNotFound = \sStr -> do
|
|
|
|
mode <- gets (^.progMode)
|
|
|
|
let
|
|
|
|
cps = newCreatePlayerState
|
|
|
|
& cpsName .~ sStr
|
|
|
|
& cpsSuccessCallback .~ do
|
|
|
|
modify $ progMode .~ mode
|
|
|
|
index <- pred . length <$> gets (^.database.dbPlayers)
|
|
|
|
callback $ Just index
|
|
|
|
& cpsFailureCallback .~ modify (progMode .~ mode)
|
|
|
|
modify $ progMode .~ CreatePlayer cps
|
2019-09-19 04:35:01 -04:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:29:33 -04:00
|
|
|
-- | Selects a goalie (creating one if necessary)
|
|
|
|
selectGoaliePrompt
|
|
|
|
:: String
|
|
|
|
-- ^ The prompt string
|
|
|
|
-> (Maybe Int -> Action ())
|
|
|
|
-- ^ The callback to run (takes the index number of the goalie as
|
|
|
|
-- input)
|
|
|
|
-> Prompt
|
2019-10-30 21:57:31 -04:00
|
|
|
selectGoaliePrompt pStr callback = selectPrompt SelectParams
|
|
|
|
{ spPrompt = pStr
|
|
|
|
, spSearchHeader = "Goalie select:"
|
|
|
|
, spSearch = \sStr db -> goalieSearch sStr (db^.dbGoalies)
|
|
|
|
, spSearchExact = \sStr db -> fst <$> goalieSearchExact sStr (db^.dbGoalies)
|
|
|
|
, spElemDesc = goalieSummary
|
|
|
|
, spCallback = callback
|
|
|
|
, spNotFound = \sStr -> do
|
|
|
|
mode <- gets (^.progMode)
|
|
|
|
let
|
|
|
|
cgs = newCreateGoalieState
|
|
|
|
& cgsName .~ sStr
|
|
|
|
& cgsSuccessCallback .~ do
|
|
|
|
modify $ progMode .~ mode
|
|
|
|
index <- pred . length <$> gets (^.database.dbGoalies)
|
|
|
|
callback $ Just index
|
|
|
|
& cgsFailureCallback .~ modify (progMode .~ mode)
|
|
|
|
modify $ progMode .~ CreateGoalie cgs
|
|
|
|
}
|
2019-10-30 00:29:33 -04:00
|
|
|
|
2019-11-01 03:05:40 -04:00
|
|
|
playerToEditPrompt :: Prompt
|
2019-11-01 03:44:03 -04:00
|
|
|
playerToEditPrompt = selectPlayerPrompt "Player to edit: " $
|
|
|
|
modify . (progMode.editPlayerStateL.epsSelectedPlayer .~)
|
2019-11-01 03:05:40 -04:00
|
|
|
|
2019-08-24 21:15:50 -04:00
|
|
|
drawSimplePrompt :: String -> ProgState -> C.Update ()
|
2019-08-31 11:27:02 -04:00
|
|
|
drawSimplePrompt pStr s = C.drawString $ pStr ++ s^.inputBuffer
|