From 066b27151074536d5598c8aedde5dc952c77ad68 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 5 Jun 2011 01:12:46 +0200 Subject: Simplified and greatly improved scoring for the duck game. --- Hsbot/Plugin/Duck.hs | 61 ++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) (limited to 'Hsbot/Plugin/Duck.hs') diff --git a/Hsbot/Plugin/Duck.hs b/Hsbot/Plugin/Duck.hs index 187c62d..4b05982 100644 --- a/Hsbot/Plugin/Duck.hs +++ b/Hsbot/Plugin/Duck.hs @@ -22,46 +22,29 @@ import Hsbot.Message import Hsbot.Types import Hsbot.Utils --- | A user statistic -data Stat = Stat - { statRounds :: Int - , statShot :: Int - , statKilled :: Int - } deriving (Show, Typeable) - --- | Default values for new stat -emptyStat :: Stat -emptyStat = Stat { statRounds = 0 - , statShot = 0 - , statKilled = 0 } - -- The statistics database data StatDB = StatDB - { nickStats :: M.Map String Stat + { nickStats :: M.Map String Int } deriving (Show, Typeable) -- | Statistics database initial state emptyStatDB :: StatDB emptyStatDB = StatDB { nickStats = M.empty } -$(deriveSafeCopy 0 'base ''Stat) $(deriveSafeCopy 0 'base ''StatDB) -- | Statistics database transactions -scoreAction :: String -> Int -> Int -> Int -> Update StatDB () -scoreAction sender rounds shots kills = do +updateScore :: String -> Int -> Update StatDB () +updateScore sender score = do statDB <- get let stats = nickStats statDB - stat = fromMaybe emptyStat $ M.lookup sender stats - stat' = stat { statRounds = rounds + statRounds stat - , statShot = shots + statShot stat - , statKilled = kills + statKilled stat } - put statDB { nickStats = M.insert sender stat' stats } + stat = fromMaybe 0 $ M.lookup sender stats + put statDB { nickStats = M.insert sender (stat + score) stats } getDuckStats :: Query StatDB StatDB getDuckStats = ask -$(makeAcidic ''StatDB ['getDuckStats, 'scoreAction]) +$(makeAcidic ''StatDB ['getDuckStats, 'updateScore]) -- | The duck plugin identity duck :: PluginId @@ -85,21 +68,19 @@ theDuck channel seconds = 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 when (getDestination msg == channel) $ do + -- Then we check if someone shot some duck let shots = howManyBulletFiredInThere . concat $ IRC.msg_params msg when (shots > 0) $ do - _ <- update' statDB (ScoreAction (getSender msg) 0 shots 0) - noDucksToShoot <- liftIO $ isEmptyMVar ducksMVar - unless noDucksToShoot $ do - ducksWaitingForDeath <- liftIO $ modifyMVar ducksMVar (\x -> return (x - shots, x)) - _ <- update' statDB (ScoreAction (getSender msg) 0 0 (min ducksWaitingForDeath shots)) - when (shots >= ducksWaitingForDeath) $ do - _ <- liftIO $ takeMVar ducksMVar - time <- liftIO $ readMVar timeMVar - duckSpawner channel time ducksMVar - _ <- update' statDB (ScoreAction (getSender msg) 1 0 0) - return () + empty <- liftIO $ isEmptyMVar ducksMVar + ducksWaitingForDeath <- if empty then return 0 + else liftIO $ modifyMVar ducksMVar (\x -> return (x - shots, x)) + _ <- update' statDB . UpdateScore (getSender msg) $ computeScore ducksWaitingForDeath shots + when (and [ ducksWaitingForDeath > 0, shots >= ducksWaitingForDeath ]) $ do + _ <- liftIO $ takeMVar ducksMVar + time <- liftIO $ readMVar timeMVar + duckSpawner channel time ducksMVar + return () -- Finally we check if we received some command cmdArgs <- lift $ getCommand msg case cmdArgs of @@ -113,6 +94,11 @@ theDuck channel seconds = do _ -> return () | otherwise = return () eval _ _ _ _ = return () + computeScore :: Int -> Int -> Int + computeScore ducksWaitingForDeath shots + | shots < ducksWaitingForDeath = shots - 1 + | shots == ducksWaitingForDeath = shots + 1 + | otherwise = negate shots -- | Spawns ducks on a channel, just waiting to be shot duckSpawner :: String -> Int -> MVar Int -> Plugin (Env IO) () @@ -162,9 +148,8 @@ printDuckStats channel statDB = do sendLine "Duck slaughter simulator - Hall of Fame" mapM_ (sendLine . buildLine) $ M.toList (nickStats statDB) where - buildLine :: (String, Stat) -> String - buildLine (nick, stat) = concat [ nick, ": ", show $ statRounds stat, " rounds won, ", show $ statShot stat - , " shots fired, ", show $ statKilled stat, " ducks killed" ] + buildLine :: (String, Int) -> String + buildLine (nick, score) = concat [ nick, ": ", show score ] sendLine :: String -> Plugin (Env IO) () sendLine msg = writeMsg . OutgoingMsg $ IRC.Message Nothing "PRIVMSG" [channel, msg] -- cgit v1.2.3 From 8fae43453245bdc683e64957b9499423b8825e7b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 17 Jun 2011 13:08:33 +0200 Subject: Improved the duck plugin arguments handling --- Hsbot/Plugin/Duck.hs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'Hsbot/Plugin/Duck.hs') diff --git a/Hsbot/Plugin/Duck.hs b/Hsbot/Plugin/Duck.hs index 4b05982..7ba2146 100644 --- a/Hsbot/Plugin/Duck.hs +++ b/Hsbot/Plugin/Duck.hs @@ -1,7 +1,8 @@ {-# LANGUAGE TypeFamilies, DeriveDataTypeable, TemplateHaskell #-} -- | This module is an IRC plugin that generates and kills ducks module Hsbot.Plugin.Duck - ( duck + ( DuckArgs (..) + , duck , theDuck ) where @@ -50,11 +51,15 @@ $(makeAcidic ''StatDB ['getDuckStats, 'updateScore]) duck :: PluginId duck = PluginId { pluginName = "duck" - , pluginEp = theDuck "" 10 } + , pluginEp = theDuck $ DuckArgs { duckChannel = "", duckFreq = 10 } } + +data DuckArgs = DuckArgs + { duckChannel :: String + , duckFreq :: Int } -- | An IRC plugin that generates and kills ducks -theDuck :: String -> Int -> Plugin (Env IO) () -theDuck channel seconds = do +theDuck :: DuckArgs -> Plugin (Env IO) () +theDuck (DuckArgs channel seconds) = do baseDir <- liftIO $ System.Environment.XDG.BaseDir.getUserDataDir "hsbot" statDB <- liftIO $ openAcidStateFrom (baseDir ++ "/duckDB/") emptyStatDB ducksMVar <- liftIO newEmptyMVar -- cgit v1.2.3 From 62a4220ce0c9b1b6c263a23f8871ab54dfe523a9 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 17 Jun 2011 13:09:09 +0200 Subject: Added some duck funny faces --- Hsbot/Plugin/Duck.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Hsbot/Plugin/Duck.hs') diff --git a/Hsbot/Plugin/Duck.hs b/Hsbot/Plugin/Duck.hs index 7ba2146..946cb0c 100644 --- a/Hsbot/Plugin/Duck.hs +++ b/Hsbot/Plugin/Duck.hs @@ -141,7 +141,7 @@ ducks = [ x : y : z | x <- ">=", y <- face, z <- ["__/", "_/"] ] ++ [ L.reverse $ x : y : z | x <- "<=", y <- face, z <- ["__\\", "_\\"] ] where face :: String - face = "oO°@©®ð*òôóø⊕ΩꙫꙩꙨ◔" + face = "oO°@©®ð*òôóø⊕ΩꙫꙩꙨ◔ȯõ⁰" -- | Weapons can have different noises bangs :: [String] -- cgit v1.2.3 From 4f50db840967c2409c9bf96c2d6525f99e39cdca Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 8 Aug 2011 13:03:31 +0200 Subject: Fixed bad comments and some cosmetics --- Hsbot/Plugin/Duck.hs | 2 +- Hsbot/Plugin/Ping.hs | 3 ++- Hsbot/Utils.hs | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'Hsbot/Plugin/Duck.hs') diff --git a/Hsbot/Plugin/Duck.hs b/Hsbot/Plugin/Duck.hs index 946cb0c..b152685 100644 --- a/Hsbot/Plugin/Duck.hs +++ b/Hsbot/Plugin/Duck.hs @@ -51,7 +51,7 @@ $(makeAcidic ''StatDB ['getDuckStats, 'updateScore]) duck :: PluginId duck = PluginId { pluginName = "duck" - , pluginEp = theDuck $ DuckArgs { duckChannel = "", duckFreq = 10 } } + , pluginEp = theDuck DuckArgs { duckChannel = "", duckFreq = 10 } } data DuckArgs = DuckArgs { duckChannel :: String diff --git a/Hsbot/Plugin/Ping.hs b/Hsbot/Plugin/Ping.hs index 6d28ef9..9105630 100644 --- a/Hsbot/Plugin/Ping.hs +++ b/Hsbot/Plugin/Ping.hs @@ -11,12 +11,13 @@ import Prelude hiding (catch) import Hsbot.Message import Hsbot.Types +-- | The ping plugin identity ping :: PluginId ping = PluginId { pluginName = "ping" , pluginEp = thePing } --- | The IrcPlugin monad main function +-- | An IRC plugin that answer PING requests thePing :: Plugin (Env IO) () thePing = forever $ readMsg >>= eval where diff --git a/Hsbot/Utils.hs b/Hsbot/Utils.hs index 912e746..2a8f58c 100644 --- a/Hsbot/Utils.hs +++ b/Hsbot/Utils.hs @@ -35,15 +35,15 @@ setGlobalQuitMVar status = do liftIO $ putMVar quitMv status -- Access rights -hasAccess :: Maybe IRC.Prefix -> AccessRight -> Env IO (Bool) +hasAccess :: Maybe IRC.Prefix -> AccessRight -> Env IO Bool hasAccess Nothing _ = return False hasAccess (Just mask) right = do botMVar <- asks envBotState - liftIO (readMVar botMVar) >>= evalStateT (gets botAccess >>= return . or . map accessMatch) + liftIO (readMVar botMVar) >>= evalStateT (fmap (any accessMatch) (gets botAccess)) where accessMatch :: AccessList -> Bool accessMatch (AccessList amask arights) - | mask == amask = or [L.elem Admin arights, L.elem right arights] + | mask == amask = or [Admin `L.elem` arights, right `L.elem` arights] | otherwise = False -- Helpers -- cgit v1.2.3 From f1ce0cfee09c5ba316abe437befa8ad75b3a5aa6 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 2 Sep 2011 16:38:27 +0200 Subject: Added score sorting for the duck module --- Hsbot/Plugin/Duck.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'Hsbot/Plugin/Duck.hs') diff --git a/Hsbot/Plugin/Duck.hs b/Hsbot/Plugin/Duck.hs index b152685..1044591 100644 --- a/Hsbot/Plugin/Duck.hs +++ b/Hsbot/Plugin/Duck.hs @@ -13,6 +13,7 @@ import Data.Acid import qualified Data.List as L import qualified Data.Map as M import Data.Maybe +import Data.Ord () import Data.SafeCopy import Data.Typeable import qualified Network.IRC as IRC @@ -151,8 +152,13 @@ bangs = [ "PAN", "PAN!" ] printDuckStats :: String -> StatDB -> Plugin (Env IO) () printDuckStats channel statDB = do sendLine "Duck slaughter simulator - Hall of Fame" - mapM_ (sendLine . buildLine) $ M.toList (nickStats statDB) + mapM_ (sendLine . buildLine) . reverse . L.sortBy scoreSort $ M.toList (nickStats statDB) where + scoreSort :: (String, Int) -> (String, Int) -> Ordering + scoreSort (_, s1) (_,s2) + | s1 < s2 = LT + | s1 > s2 = GT + | otherwise = EQ buildLine :: (String, Int) -> String buildLine (nick, score) = concat [ nick, ": ", show score ] sendLine :: String -> Plugin (Env IO) () -- cgit v1.2.3