packages feed

werewolf-0.4.3.0: app/Werewolf/Commands/End.hs

{-|
Module      : Werewolf.Commands.End
Description : Handler for the end subcommand.

Copyright   : (c) Henry J. Wylde, 2015
License     : BSD3
Maintainer  : public@hjwylde.com

Handler for the end subcommand.
-}

{-# LANGUAGE OverloadedStrings #-}

module Werewolf.Commands.End (
    -- * Handle
    handle,
) where

import Control.Lens
import Control.Monad.Extra
import Control.Monad.IO.Class

import           Data.Maybe
import           Data.Text  (Text)
import qualified Data.Text  as T

import Game.Werewolf.Engine
import Game.Werewolf.Game
import Game.Werewolf.Player
import Game.Werewolf.Response

-- | Handle.
handle :: MonadIO m => Text -> m ()
handle callerName = do
    unlessM doesGameExist $ exitWith failure
        { messages = [noGameRunningMessage callerName]
        }

    game <- readGame

    when (isNothing $ findByName callerName (game ^. players)) $ exitWith failure
        { messages = [playerCannotDoThatMessage callerName]
        }

    deleteGame

    exitWith success { messages = [gameEndedMessage] }
    where
        gameEndedMessage = publicMessage $ T.concat ["Game ended by ", callerName, "."]