Re-engineered and finished the duck spawning capability.
This commit is contained in:
parent
27df2808fc
commit
a6ac073a6c
2 changed files with 58 additions and 28 deletions
|
@ -19,44 +19,70 @@ import Hsbot.Utils
|
||||||
duck :: PluginId
|
duck :: PluginId
|
||||||
duck = PluginId
|
duck = PluginId
|
||||||
{ pluginName = "duck"
|
{ pluginName = "duck"
|
||||||
, pluginEp = theDuck "" 11 }
|
, pluginEp = theDuck "" 10 }
|
||||||
|
|
||||||
-- | An IRC plugin that generates and kills ducks
|
-- | An IRC plugin that generates and kills ducks
|
||||||
theDuck :: String -> Int -> Plugin (Env IO) ()
|
theDuck :: String -> Int -> Plugin (Env IO) ()
|
||||||
theDuck channel seconds = do
|
theDuck channel seconds = do
|
||||||
env <- lift ask
|
ducksMVar <- liftIO newEmptyMVar
|
||||||
pEnv <- ask
|
duckSpawner channel seconds ducksMVar
|
||||||
secondsMVar <- liftIO $ newMVar seconds
|
forever $ readMsg >>= eval ducksMVar
|
||||||
killMVar <- liftIO newEmptyMVar
|
|
||||||
(liftIO . forkIO $ runReaderT (runReaderT (duckSpawner channel secondsMVar killMVar) pEnv) env) >>= lift . addThreadIdToQuitMVar
|
|
||||||
forever $ readMsg >>= eval
|
|
||||||
where
|
where
|
||||||
eval :: Message -> Plugin (Env IO) ()
|
eval :: MVar Int -> Message -> Plugin (Env IO) ()
|
||||||
eval (IncomingMsg msg)
|
eval ducksMVar (IncomingMsg msg)
|
||||||
| IRC.msg_command msg == "PRIVMSG" = answerMsg msg . isThereADuckToKillInThere . concat $ IRC.msg_params msg
|
| IRC.msg_command msg == "PRIVMSG" = do
|
||||||
|
-- First we kill the ducks that we find in the message
|
||||||
|
let kills = howManyDucksInThere . concat $ IRC.msg_params msg
|
||||||
|
when (kills /= "") $ answerMsg msg kills
|
||||||
|
-- Then we check if someone shot some duck
|
||||||
|
let shots = howManyBulletFiredInThere . concat $ IRC.msg_params msg
|
||||||
|
noDucksToShot <- liftIO $ isEmptyMVar ducksMVar
|
||||||
|
when (and [getDestination msg == channel, not noDucksToShot, shots > 0]) $ do
|
||||||
|
-- TODO: score dead ducks (with a min (ducksWaitingForDeath, shots))
|
||||||
|
ducksWaitingForDeath <- liftIO $ modifyMVar ducksMVar (\x -> return (x - shots, x))
|
||||||
|
when (shots >= ducksWaitingForDeath) $ do
|
||||||
|
_ <- liftIO $ takeMVar ducksMVar
|
||||||
|
duckSpawner channel seconds ducksMVar
|
||||||
|
-- TODO : score lost bullets and round
|
||||||
|
return ()
|
||||||
|
-- Finally we check if we received some command
|
||||||
|
cmdArgs <- lift $ getCommand msg
|
||||||
|
case cmdArgs of
|
||||||
|
--"ducks":"stats":args -> TODO
|
||||||
|
"ducks":_ -> answerMsg msg "Invalid duck command."
|
||||||
|
_ -> return ()
|
||||||
| otherwise = return ()
|
| otherwise = return ()
|
||||||
eval _ = return ()
|
eval _ _ = return ()
|
||||||
|
|
||||||
-- | Regularly spawns ducks on a channel, just waiting to be shot
|
-- | Regularly spawns ducks on a channel, just waiting to be shot
|
||||||
duckSpawner :: String -> MVar Int -> MVar Int -> Plugin (Env IO) ()
|
duckSpawner :: String -> Int -> MVar Int -> Plugin (Env IO) ()
|
||||||
duckSpawner channel secondsMVar killMVar = forever $ do
|
duckSpawner channel secs ducksMVar = do
|
||||||
nbDucks <- liftIO . getStdRandom $ randomR (1,4)
|
pEnv <- ask
|
||||||
liftIO $ putMVar killMVar nbDucks
|
lift ask >>= liftIO . forkIO . runReaderT (runReaderT trueSpawner pEnv) >>= lift . addThreadIdToQuitMVar
|
||||||
thoseDucks <- liftIO $ someRandomDucks nbDucks ""
|
where
|
||||||
writeMsg . OutgoingMsg $ IRC.Message Nothing "PRIVMSG" [channel, thoseDucks]
|
trueSpawner :: Plugin (Env IO) ()
|
||||||
secs <- liftIO $ readMVar secondsMVar
|
trueSpawner = do
|
||||||
liftIO $ threadDelay (3000000 * secs)
|
rsecs <- liftIO . getStdRandom $ randomR (1,secs)
|
||||||
|
liftIO $ threadDelay (1000000 * rsecs)
|
||||||
|
nbDucks <- liftIO . getStdRandom $ randomR (1,4)
|
||||||
|
thoseDucks <- liftIO $ replicateM nbDucks someRandomDuck
|
||||||
|
liftIO $ putMVar ducksMVar nbDucks
|
||||||
|
writeMsg . OutgoingMsg $ IRC.Message Nothing "PRIVMSG" [channel, concat thoseDucks]
|
||||||
|
liftIO myThreadId >>= lift . delThreadIdFromQuitMVar
|
||||||
|
|
||||||
-- | Shoot as many times are there are ducks in the initial string
|
-- | Shoot as many times are there are ducks in the initial string
|
||||||
isThereADuckToKillInThere :: String -> String
|
howManyBulletFiredInThere :: String -> Int
|
||||||
isThereADuckToKillInThere = concat . concatMap (\y -> map (\x -> if x `L.isInfixOf` y then "PAN! " else "") ducks) . words
|
howManyBulletFiredInThere = sum . concatMap (\y -> map (\x -> if x `L.isInfixOf` y then 1 else 0) bangs) . words
|
||||||
|
|
||||||
someRandomDucks :: Int -> String -> IO String
|
-- | Shoot as many times are there are ducks in the initial string
|
||||||
someRandomDucks 0 theDucks = return theDucks
|
howManyDucksInThere :: String -> String
|
||||||
someRandomDucks nbDucks theDucks = do
|
howManyDucksInThere = concat . concatMap (\y -> map (\x -> if x `L.isInfixOf` y then "PAN! " else "") ducks) . words
|
||||||
whatDuck <- getStdRandom $ randomR (1,length ducks)
|
|
||||||
let thisDuck = ducks !! whatDuck
|
-- | Output a string made of the specified number of random ducks
|
||||||
someRandomDucks (nbDucks -1 ) $ concat [thisDuck, " ", theDucks]
|
someRandomDuck :: IO String
|
||||||
|
someRandomDuck = do
|
||||||
|
whatDuck <- getStdRandom $ randomR (0,length ducks - 1)
|
||||||
|
return $ ducks !! whatDuck ++ " "
|
||||||
|
|
||||||
-- | There are many ways to hide as a duck, this function tries to cover most of them
|
-- | There are many ways to hide as a duck, this function tries to cover most of them
|
||||||
ducks :: [String]
|
ducks :: [String]
|
||||||
|
@ -68,3 +94,7 @@ ducks = [ x : y : z | x <- nose, y <- face, z <- ["__/", "_/", "/"] ]
|
||||||
face :: String
|
face :: String
|
||||||
face = "oO°@©®ð*òôóø"
|
face = "oO°@©®ð*òôóø"
|
||||||
|
|
||||||
|
-- | Weapons can have different noises
|
||||||
|
bangs :: [String]
|
||||||
|
bangs = [ "PAN", "PAN!" ]
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
Name: hsbot
|
Name: hsbot
|
||||||
Version: 0.4.13
|
Version: 0.4.14
|
||||||
Cabal-version: >=1.2
|
Cabal-version: >=1.2
|
||||||
Synopsis: A multipurposes IRC bot
|
Synopsis: A multipurposes IRC bot
|
||||||
Description:
|
Description:
|
||||||
|
|
Reference in a new issue