-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.hs
39 lines (33 loc) · 997 Bytes
/
Server.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
{-# LANGUAGE OverloadedStrings #-}
module Server where
import Board
import Game
-- import Control.Monad (forever)
import Control.Monad.State
import Data.Maybe (fromMaybe)
import Data.Text as T
import qualified Network.WebSockets as WS
step :: String -> StateT [GameState] IO ()
step command = do
gss <- get
put $ fromMaybe gss (update command gss)
go :: WS.Connection -> [GameState] -> IO ()
go conn states =
do
command <- WS.receiveData conn :: IO T.Text
print . T.unpack $ T.append "command was " command
newStates@(h:_) <- execStateT (step $ T.unpack command) states
print h
WS.sendTextData conn (T.pack $ show (posMap . board $ h))
go conn newStates
gameLoop :: WS.Connection -> IO()
gameLoop conn =
forever $ go conn [initState]
-- runStateT (playGame conn) [initState]
app :: WS.ServerApp -- WS.PendingConnection -> IO ()
app pending =
do
conn <- WS.acceptRequest pending
gameLoop conn
main :: IO ()
main = WS.runServer "127.0.0.1" 5000 app