mtlstats/src/Mtlstats/Menu.hs

199 lines
5.3 KiB
Haskell
Raw Normal View History

2019-08-21 00:51:08 -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/>.
-}
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-08-31 12:27:34 -04:00
gameMonthMenu,
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.IO.Class (liftIO)
import Control.Monad.Trans.State (gets, modify)
import Data.Aeson (encodeFile)
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-09-19 07:33:57 -04:00
import Lens.Micro.Extras (view)
import System.EasyFile
( createDirectoryIfMissing
, getAppUserDataDirectory
, (</>)
)
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
2019-09-19 07:33:57 -04:00
import Mtlstats.Config
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
C.drawString $ show m
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 "*** MAIN MENU ***" True
[ MenuItem '1' "New Season" $
modify startNewSeason >> return True
, MenuItem '2' "New Game" $
modify startNewGame >> return True
, MenuItem '3' "Create Player" $
modify createPlayer >> return True
2019-10-25 00:37:14 -04:00
, MenuItem '4' "Create Goalie" $
modify createGoalie >> return True
2019-12-17 12:04:11 -05:00
, MenuItem '5' "Edit" $
modify edit >> return True
2019-11-12 23:44:39 -05:00
, MenuItem 'X' "Exit" $ do
2019-09-19 07:33:57 -04:00
db <- gets $ view database
liftIO $ do
dir <- getAppUserDataDirectory appName
let dbFile = dir </> dbFname
createDirectoryIfMissing True dir
encodeFile dbFile db
2019-08-21 01:08:12 -04:00
return False
]
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
. resetStandings
. startNewGame
, MenuItem 'P' "Playoffs" $ modify
2019-11-14 11:21:52 -05:00
$ resetStandings
. startNewGame
2019-08-21 01:15:26 -04:00
]
2019-08-31 12:27:34 -04:00
-- | Requests the month in which the game took place
gameMonthMenu :: Menu ()
gameMonthMenu = Menu "Month:" () $ map
(\(ch, name, val) ->
MenuItem ch name $
modify $ progMode.gameStateL.gameMonth ?~ val)
[ ( 'A', "January", 1 )
, ( 'B', "February", 2 )
, ( 'C', "March", 3 )
, ( 'D', "April", 4 )
, ( 'E', "May", 5 )
, ( 'F', "June", 6 )
, ( 'G', "July", 7 )
, ( 'H', "August", 8 )
, ( 'I', "September", 9 )
, ( 'J', "October", 10 )
, ( 'K', "November", 11 )
, ( 'L', "December", 12 )
]
-- | The game type menu (home/away)
gameTypeMenu :: Menu ()
gameTypeMenu = Menu "Game type:" ()
[ MenuItem '1' "Home Game" $
2019-08-31 11:27:02 -04:00
modify $ progMode.gameStateL.gameType ?~ HomeGame
, MenuItem '2' "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
in Menu title () $ map
(\(ch, (gid, goalie)) -> MenuItem ch (goalieSummary goalie) $
modify $ GI.setGameGoalie gid) $
zip ['1'..] goalies
2019-12-17 11:38:35 -05:00
-- | The edit menu
editMenu :: Menu ()
2019-12-17 11:48:01 -05:00
editMenu = Menu "*** EDIT ***" ()
[ MenuItem '1' "Edit Player" $
modify editPlayer
, MenuItem '2' "Edit Goalie" $
modify editGoalie
, MenuItem 'R' "Return to Main Menu" $
modify backHome
]