, send
, say
+
+ , statsMain
)
where
import qualified Data.Map as M
import Data.Map (Map)
import Data.List
+import Data.Maybe
import Control.Monad.State
import Control.Monad.Reader
import Control.Monad.Writer
int3 = read (param_s !! 2) :: Int
parsePlayer str = read (words str !! 2)
- parseScore str = (read (words str !! 2), read (words str !! 2))
- parseName str = (read (words str !! 2), read (words str !! 2))
+ parseScore str = (read (words str !! 2), read (words str !! 3))
+ parseName str = (read (words str !! 2), read (words str !! 3))
lineEater :: Int -> ([String] -> IncomingMessage) -> IncomingMessage
lineEater n f = ComplexInput $ \h ps -> do lines <- sequence (replicate n (hGetLine h))
say s = do
name <- asks psLoginName
liftIO $ putStrLn $ "[" ++ name ++ "] " ++ s
+
+
+statsMain :: FilePath -> IO ()
+statsMain file = do
+ [host,port_s,username,password] <- getArgs
+
+ h <- connectTo host (PortNumber (fromIntegral (read port_s)))
+ hSetBuffering h LineBuffering
+ putStrLn $ "[" ++ username ++ "] Connected to " ++ host
+ InternalMessage Welcome <- parseIncomingMessage `liftM` hGetLine h
+ hPutStrLn h $ unwords ["login", username, password]
+ InternalMessage (PlayerJoined myName) <- parseIncomingMessage `liftM` hGetLine h
+
+ lastStats <- readFile file
+ when (lastStats == lastStats) $ return ()
+
+ let dummyState = ParttyState
+ { psMyName = "0"
+ , psLoginName = username
+ , psDay = 0
+ , psPlayerInfo = error "psPlayerInfo not implemented"
+ , psHistory = []
+ , psHintHistory = []
+ , psPlace = 1
+ , psPlayers = []
+ , psPlayersHere = []
+ , psActionsLeft = 0
+ , psPartyPlace = Nothing
+ , psFriends = []
+ }
+ fix (\loop -> do (msg,_) <- getNextMessage h dummyState
+ case msg of
+ (InternalMessage (GameStarting)) -> return ()
+ otherwise -> loop
+ )
+
+ fix (\loop score -> do
+ this <- fix (\loop players points -> do
+ (msg,_) <- getNextMessage h dummyState
+ case msg of
+ (InternalMessage (NameList l)) -> loop l points
+ (InternalMessage (ScoreList l))-> loop players l
+ (InternalMessage (GameStarting))-> return $ merge players points
+ otherwise -> loop players points
+ ) undefined undefined
+ let next = this : score
+ putStrLn $ "Score: " ++ show this
+ putStrLn "Writing score..."
+ writeFile file (show next)
+ loop next
+ ) (read lastStats)
+
+ where merge player scores = map (
+ \(p1,n) -> (n, fromJust (lookup p1 scores))
+ ) player
+