-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTTTClient.hs
60 lines (47 loc) · 1.56 KB
/
TTTClient.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import Network.Socket
import Network.BSD
import Control.Monad
import System.IO
import TTTServer
import TicTacToe
import Board
playGame :: HostName -> String -> IO ()
playGame hostname port = do
p <- getPlayerFromConnection hostname port
playersTurn' p
return ()
playersTurn' :: Player -> IO ()
playersTurn' p = do
m <- hGetLine $ handle p
putStrLn m
maybeInp <- processMsg m
case maybeInp of
Nothing -> playersTurn' p
Just inp -> do
hPutStrLn (handle p) inp
playersTurn' p
processMsg :: String -> IO (Maybe String)
processMsg s = let msg = stringToMsg s
in case msgType msg of
REQ_INPUT -> do
inp <- getLine
return $ Just inp
_ -> return Nothing
getPlayerFromConnection :: HostName -> String -> IO Player
getPlayerFromConnection hostname port = do
-- | create the server's address using the input parameters
addrinfos <- getAddrInfo Nothing (Just hostname) (Just port)
let serveraddr = head addrinfos
putStrLn $ "did create server address: " ++ show serveraddr
-- | create a TCP socket for the incoming data
sock <- socket (addrFamily serveraddr) Stream defaultProtocol
-- | configure socket
setSocketOption sock KeepAlive 1
-- | connect to server
connect sock (addrAddress serveraddr)
-- | turn socket into handle
h <- socketToHandle sock ReadWriteMode
hSetBuffering h LineBuffering
--info <- hGetLine h
--putStrLn $ "initial info: " ++ info
return $ Player h Cross