summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpb <pb@9f8f72e9-4bf4-416e-b76e-7d4203597157>2009-11-29 00:24:58 +0000
committerpb <pb@9f8f72e9-4bf4-416e-b76e-7d4203597157>2009-11-29 00:24:58 +0000
commit0bcb04ba9b6efd1b3208852d4c213f9194fa7328 (patch)
tree0ae9efe78c50f6238f719253635dbea4e8d6267b
parent6ca6b3ae67c2e345be8d672ccc542b4edff99e3d (diff)
git-svn-id: https://ssl.buetow.org/repos/hsbot/trunk@5 9f8f72e9-4bf4-416e-b76e-7d4203597157
-rw-r--r--Foo.hs5
-rw-r--r--HsBot.hs77
-rw-r--r--Makefile4
3 files changed, 63 insertions, 23 deletions
diff --git a/Foo.hs b/Foo.hs
index 08ec797..ae5930d 100644
--- a/Foo.hs
+++ b/Foo.hs
@@ -2,8 +2,7 @@
module Main (main) where
-import Control.Monad.State
-import IO
+import Control.Monad
-main = return $ "hello"
+main = return $ "hello "
diff --git a/HsBot.hs b/HsBot.hs
index dd0e875..ee88a78 100644
--- a/HsBot.hs
+++ b/HsBot.hs
@@ -2,16 +2,22 @@
module Main (main) where
-import Control.Monad.State
import IO
-import Monad
-import Random
import System
+-- Start configuration
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)
@@ -24,7 +30,7 @@ instance Ord Karma where
x >= y = not (x < y)
x <= y = not (x > y)
-data Conf = Conf { users :: [User], karmas :: [Karma] } deriving (Show, Read)
+data Conf = Conf { line :: String, loggs :: [String], maxLoggs :: Int, users :: [User], karmas :: [Karma] } deriving (Show, Read)
data User = User { userName :: String, userPts :: Int } deriving (Show, Read)
@@ -45,8 +51,11 @@ instance Render User where
" (rank " ++ (show $ userRank user) ++ "/" ++ (show numUsers) ++ "; " ++
(show $ userPts user) ++ "pts; " ++ (show $ userPerc user) ++ "%)"
-makeDefaultConf :: Conf
-makeDefaultConf = Conf {
+makeTestConf :: Conf
+makeTestConf = Conf {
+ line = "",
+ loggs = [],
+ maxLoggs = 10,
users = [
User "thunder" 100,
User "otto" 1,
@@ -74,8 +83,19 @@ makeDefaultConf = Conf {
] }
makeConf :: Conf
-makeConf = makeDefaultConf
-
+makeConf = makeTestConf
+
+loggMessage :: String -> Conf -> Conf
+loggMessage message conf = let l = message : (loggs conf)
+ l' | length l > (maxLoggs conf) = l
+ | otherwise = init l
+ in conf { loggs = l }
+
+printLoggs :: Conf -> IO ()
+printLoggs conf = printLoggs' $ reverse (loggs conf)
+ where printLoggs' [] = return ()
+ printLoggs' (l:logg) = do { putStrLn l; printLoggs' logg }
+
numUsers :: Int
numUsers = length $ users makeConf
@@ -112,14 +132,39 @@ sort :: (Ord a) => [a] -> [a]
sort [] = []
sort (x:xs) = sort (filter (>= x) xs) ++ [x] ++ sort (filter (< x) xs)
-readline :: IO ()
-readline = do
+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
+
+processInput :: Conf -> Conf
+processInput conf = loggMessage "foo" conf
+ where plus = matches "++" (line conf)
+ minus = matches "--" (line conf)
+
+help :: IO ()
+help = do
+ putStrLn "!h - Print help"
+ putStrLn "!l - Print loggs"
+ putStrLn "!p - Print current configuration"
+ putStrLn "!q - Quit"
+
+loop :: Conf -> IO ()
+loop conf = do
+ putPrompt
line <- getLine
case line of
- "q" -> do { putStrLn "Good bye"; exitWith ExitSuccess }
- _ -> putStrLn ""
-
-
-main :: IO ()
-main = putStrLn "Good bye"
+ "!h" -> do { help; loop conf }
+ "!l" -> do { printLoggs conf; loop conf }
+ "!p" -> do { putStrLn $ show conf; loop conf }
+ "!q" -> do { putStrLn "Good bye"; exitWith ExitSuccess }
+ _ -> do { putStrLn line; loop $ processInput $ conf { line = line } }
+
+main :: IO ()
+main = do
+ putStrLn $ "Welcome to " ++ version
+ loop makeConf
diff --git a/Makefile b/Makefile
index e5a5b48..877f109 100644
--- a/Makefile
+++ b/Makefile
@@ -2,8 +2,4 @@ all:
ghc HsBot.hs -o hsbot
test: all
./hsbot
-foo:
- ghc Foo.hs -o foo
-test: foo
- ./foo