-- | This module is for throwing dice for e.g. RPGs. (\@dice 3d6+2)

-- Original version copyright Einar Karttunen <ekarttun@cs.helsinki.fi> 2005-04-06.
-- Massive rewrite circa 2008-10-20 copyright James Cook <mokus@deepbondi.net>
module Lambdabot.Plugin.Novelty.Dice (dicePlugin) where

import Lambdabot.Plugin
import Lambdabot.Util

import Data.List
import Data.Random.Dice (rollEm)

type Dice = ModuleT () LB

dicePlugin :: Module ()
dicePlugin :: Module ()
dicePlugin = forall st. Module st
newModule
    { moduleCmds :: ModuleT () LB [Command Dice]
moduleCmds = forall (m :: * -> *) a. Monad m => a -> m a
return
        [ (String -> Command Identity
command String
"dice")
            { aliases :: [String]
aliases = [String
"roll"]
            , help :: Cmd Dice ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"@dice <expr>. Throw random dice. <expr> is of the form 3d6+2."
            , process :: String -> Cmd Dice ()
process = Bool -> String -> Cmd Dice ()
doDice Bool
True
            }
        ]
    , contextual :: String -> Cmd Dice ()
contextual = Bool -> String -> Cmd Dice ()
doDice Bool
False
    }

doDice :: Bool -> String -> Cmd Dice ()
doDice :: Bool -> String -> Cmd Dice ()
doDice Bool
printErrs String
text = do
    String
user <- forall (m :: * -> *). Monad m => Nick -> Cmd m String
showNick forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *). Monad m => Cmd m Nick
getSender
    Either ParseError String
result <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (String -> IO (Either ParseError String)
rollEm String
text)
    case Either ParseError String
result of
        Left ParseError
err    -> if Bool
printErrs
            then forall (m :: * -> *). Monad m => String -> Cmd m ()
say (ParseError -> String
trimError ParseError
err)
            else forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Right String
str   ->
            forall (m :: * -> *). Monad m => String -> Cmd m ()
say (Int -> String -> String
limitStr Int
75 (String
user forall a. [a] -> [a] -> [a]
++ String
": " forall a. [a] -> [a] -> [a]
++ String
str))

    where
        trimError :: ParseError -> String
trimError = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a] -> [a]
intersperse String
": " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
tail forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show