summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpb <pb@9f8f72e9-4bf4-416e-b76e-7d4203597157>2009-12-16 22:15:12 +0000
committerpb <pb@9f8f72e9-4bf4-416e-b76e-7d4203597157>2009-12-16 22:15:12 +0000
commit2a3644123341049c854ea201ffa11cab2f68ec4a (patch)
tree265547d71b0ebc36482fd17980a952a0b6e530b9
parent1f44f0bd7cec6decb4a1be2c8623bbfe79fe6d6a (diff)
git-svn-id: https://ssl.buetow.org/repos/hsbot/trunk@10 9f8f72e9-4bf4-416e-b76e-7d4203597157
-rw-r--r--HsBot.hs63
1 files changed, 40 insertions, 23 deletions
diff --git a/HsBot.hs b/HsBot.hs
index 1baa1b8..c097690 100644
--- a/HsBot.hs
+++ b/HsBot.hs
@@ -1,6 +1,7 @@
-- Karmabot By Paul C. Buetow
-module Main (main) where
+--module Main (main,matches) where
+module Main where
import IO
import System
@@ -10,14 +11,8 @@ import System
version :: String
version = "0.0"
-prompt :: String
-prompt = "$ "
-
-- End configuration
-putPrompt :: IO ()
-putPrompt = putStr prompt
-
data Karma = Karma { karmaName :: String, minPts :: Int, minPerc :: Float } deriving (Show, Read)
instance Eq Karma where x == y = (minPerc x) == (minPerc y) && (minPts x == minPts y)
@@ -116,13 +111,13 @@ userPerc user =
let rank = userRank user
userPerc'
| rank == 1 = 100 -- 1st always has 100%
- | rank == numUsers = 0 -- last always has 0%
+ | rank == numUsers = 0 -- back always has 0%
| otherwise =
let userWeight = 100 / (fromIntegral numUsers)
in 100 - userWeight * (fromIntegral rank)
in userPerc'
-userKarma :: User -> String
+userKarma :: User -> String
userKarma user = userKarma' (userPts user) (userPerc user)
where userKarma' pts perc =
let cands = sort $ filter (\x -> minPts x <= pts && minPerc x <= perc) (karmas makeConf)
@@ -132,14 +127,37 @@ sort :: (Ord a) => [a] -> [a]
sort [] = []
sort (x:xs) = sort (filter (>= x) xs) ++ [x] ++ sort (filter (< x) xs)
-matches :: String -> String -> Maybe (String, String)
-matches search string = m' "" search string
- where m' pred [] string = Just (init . init $ reverse pred, string)
- m' pred matches [] = Nothing
- m' pred (m:matches) (s:string)
- | m == s = m' (s:pred) matches string
- | otherwise = m' (s:pred) search string
-
+split :: String -> Char -> [String]
+split [] delim = [""]
+split (c:cs) delim | c == delim = "" : rest
+ | otherwise = (c : head rest) : tail rest
+ where rest = split cs delim
+
+-- Returns the string before and after the match
+occurance :: String -> String -> Maybe (String, String)
+occurance search str = o' "" search str
+ where o' pred [] str = Just (init . init $ reverse pred, str)
+ o' pred occurance [] = Nothing
+ o' pred (m:occurance) (s:str)
+ | m == s = o' (s:pred) occurance str
+ | otherwise = o' (s:pred) search str
+
+matches :: String -> String -> [String]
+matches search string = case m' $ occurance search string of
+ (list, []) -> list
+ (list, rest) -> list ++ (matches search rest)
+ where extrL str
+ | last str == ' ' = []
+ | otherwise = [last $ split str ' ']
+ extrR str
+ | str !! 0 == ' ' = []
+ | otherwise = [split str ' ' !! 0]
+ m' (Just (a, "")) = (extrR a, "")
+ m' (Just ("", b)) = (extrR b, b)
+ m' (Just (a, b)) = ((extrL a) ++ (extrR b), b)
+ m' Nothing = ([], [])
+
+
processInput :: Conf -> Conf
processInput conf = loggMessage "foo" conf
where plus = matches "++" (line conf)
@@ -147,14 +165,13 @@ processInput conf = loggMessage "foo" conf
help :: IO ()
help = do
- putStrLn "!h - Print help"
- putStrLn "!l - Print loggs"
- putStrLn "!p - Print current configuration"
- putStrLn "!q - Quit"
+ putStrLn "\t!h - Print help"
+ putStrLn "\t!l - Print loggs"
+ putStrLn "\t!p - Print current configuration"
+ putStrLn "\t!q - Quit"
loop :: Conf -> IO ()
loop conf = do
- putPrompt
line <- getLine
case line of
"!h" -> do { help; loop conf }
@@ -165,6 +182,6 @@ loop conf = do
main :: IO ()
main = do
- putStrLn $ "Welcome to " ++ version
+ putStrLn $ "Welcome to " ++ version ++ " (Enter !h for help)"
loop makeConf