mtlstats/src/Mtlstats/Menu.hs

173 lines
4.6 KiB
Haskell
Raw Normal View History

2019-08-21 00:51:08 -04:00
{- |
mtlstats
Copyright (C) 1984, 1985, 2019, 2020, 2021 Rhéal Lamothe
2019-08-21 00:51:08 -04:00
<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.Menu (
2019-08-21 01:08:12 -04:00
-- * Menu Functions
2019-11-04 04:12:20 -05:00
menuController,
menuControllerWith,
2019-11-23 01:13:17 -05:00
menuStateController,
2019-08-21 00:51:08 -04:00
drawMenu,
menuHandler,
2019-08-21 01:08:12 -04:00
-- * Menus
2019-08-21 01:15:26 -04:00
mainMenu,
newSeasonMenu,
2019-11-01 05:24:18 -04:00
gameTypeMenu,
2019-12-17 11:38:35 -05:00
gameGoalieMenu,
editMenu
2019-08-21 00:51:08 -04:00
) where
2019-09-19 07:33:57 -04:00
import Control.Monad.Trans.State (gets, modify)
2019-08-31 12:26:17 -04:00
import Data.Char (toUpper)
2019-11-04 04:12:20 -05:00
import qualified Data.Map as M
import Data.Maybe (mapMaybe)
import Lens.Micro ((^.), (?~))
2019-08-21 00:51:08 -04:00
import qualified UI.NCurses as C
2019-08-21 01:08:12 -04:00
import Mtlstats.Actions
import qualified Mtlstats.Actions.NewGame.GoalieInput as GI
import Mtlstats.Actions.EditStandings
2020-01-22 21:23:32 -05:00
import Mtlstats.Format
2019-08-21 00:51:08 -04:00
import Mtlstats.Types
import Mtlstats.Types.Menu
2019-11-04 04:12:20 -05:00
import Mtlstats.Util
-- | Generates a simple 'Controller' for a Menu
menuController :: Menu () -> Controller
2019-11-11 20:52:56 -05:00
menuController = menuControllerWith $ const $ return ()
2019-08-21 00:51:08 -04:00
-- | Generate a simple 'Controller' for a 'Menu' with a header
menuControllerWith
:: (ProgState -> C.Update ())
-- ^ Generates the header
-> Menu ()
-- ^ The menu
-> Controller
-- ^ The resulting controller
2019-11-11 20:52:56 -05:00
menuControllerWith header menu = Controller
{ drawController = \s -> do
header s
drawMenu menu
, handleController = \e -> do
menuHandler menu e
return True
}
2019-11-23 01:13:17 -05:00
-- | Generate and create a controller for a menu based on the current
-- 'ProgState'
menuStateController
:: (ProgState -> Menu ())
-- ^ The function to generate the menu
-> Controller
-- ^ The resulting controller
menuStateController menuFunc = Controller
{ drawController = drawMenu . menuFunc
, handleController = \e -> do
menu <- gets menuFunc
menuHandler menu e
return True
}
2019-08-21 00:51:08 -04:00
-- | The draw function for a 'Menu'
drawMenu :: Menu a -> C.Update C.CursorMode
drawMenu m = do
2020-01-22 21:23:32 -05:00
(_, cols) <- C.windowSize
let
width = fromIntegral $ pred cols
menuText = map (centre width) $ lines $ show m
C.drawString $ unlines menuText
return C.CursorInvisible
2019-08-21 00:51:08 -04:00
-- | The event handler for a 'Menu'
2019-08-23 09:22:32 -04:00
menuHandler :: Menu a -> C.Event -> Action a
2019-08-21 00:51:08 -04:00
menuHandler m (C.EventCharacter c) =
2019-08-31 12:26:17 -04:00
case filter (\i -> i^.miKey == toUpper c) $ m^.menuItems of
i:_ -> i^.miAction
[] -> return $ m^.menuDefault
menuHandler m _ = return $ m^.menuDefault
2019-08-21 01:08:12 -04:00
2019-08-22 01:16:26 -04:00
-- | The main menu
2019-08-21 01:08:12 -04:00
mainMenu :: Menu Bool
mainMenu = Menu "MASTER MENU" True
[ MenuItem 'A' "NEW SEASON" $
2019-08-21 01:08:12 -04:00
modify startNewSeason >> return True
, MenuItem 'B' "NEW GAME" $
2019-08-21 01:08:12 -04:00
modify startNewGame >> return True
, MenuItem 'C' "EDIT MENU" $
2019-12-17 12:04:11 -05:00
modify edit >> return True
, MenuItem 'E' "EXIT" $
2020-03-12 02:44:41 -04:00
saveDatabase >> return False
2019-08-21 01:08:12 -04:00
]
2019-08-21 01:15:26 -04:00
2019-08-22 01:16:26 -04:00
-- | The new season menu
2019-08-21 01:15:26 -04:00
newSeasonMenu :: Menu ()
newSeasonMenu = Menu "SEASON TYPE" ()
[ MenuItem 'R' "REGULAR SEASON" $ modify
2019-11-14 11:21:52 -05:00
$ resetYtd
2020-01-11 02:29:17 -05:00
. clearRookies
2019-11-14 11:21:52 -05:00
. resetStandings
. backHome
, MenuItem 'P' "PLAYOFFS" $ modify
2019-11-14 11:21:52 -05:00
$ resetStandings
. backHome
2019-08-21 01:15:26 -04:00
]
-- | The game type menu (home/away)
gameTypeMenu :: Menu ()
gameTypeMenu = Menu "GAME TYPE:" ()
[ MenuItem 'H' "HOME GAME" $
2019-08-31 11:27:02 -04:00
modify $ progMode.gameStateL.gameType ?~ HomeGame
, MenuItem 'A' "AWAY GAME" $
2019-08-31 11:27:02 -04:00
modify $ progMode.gameStateL.gameType ?~ AwayGame
]
2019-11-01 05:24:18 -04:00
2019-11-04 04:12:20 -05:00
-- | Game goalie selection menu
gameGoalieMenu :: ProgState -> Menu ()
gameGoalieMenu s = let
title = "Which goalie should get credit for the game?"
gids = map fst $ M.toList $ s^.progMode.gameStateL.gameGoalieStats
goalies = mapMaybe
(\n -> do
goalie <- nth n $ s^.database.dbGoalies
Just (n, goalie))
gids
2020-03-05 05:15:58 -05:00
in Menu title () $ zipWith
(\ch (gid, goalie) -> MenuItem ch (goalieSummary goalie) $
modify $ GI.setGameGoalie gid)
['1'..]
goalies
2019-12-17 11:38:35 -05:00
-- | The edit menu
editMenu :: Menu ()
editMenu = Menu "EDIT MENU" ()
[ MenuItem 'A' "CREATE PLAYER" $
modify createPlayer
, MenuItem 'B' "CREATE GOALIE" $
modify createGoalie
, MenuItem 'C' "EDIT PLAYER" $
2019-12-17 11:48:01 -05:00
modify editPlayer
, MenuItem 'D' "EDIT GOALIE" $
2019-12-17 11:48:01 -05:00
modify editGoalie
, MenuItem 'E' "EDIT STANDINGS" $
2020-01-15 00:13:50 -05:00
modify editStandings
, MenuItem 'R' "RETURN TO MAIN MENU" $
2019-12-17 11:48:01 -05:00
modify backHome
]