Add two new messages for client to load an area and to move on an area

This commit is contained in:
Tissevert 2018-12-30 23:40:51 +01:00
parent f8dc186e17
commit eec8f263ee
4 changed files with 58 additions and 15 deletions

View File

@ -8,42 +8,65 @@ import Control.Monad.State (get, put)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Data.Aeson (encode) import Data.Aeson (encode)
import Data.ByteString.Lazy.Char8 (ByteString, putStrLn, unpack) import Data.ByteString.Lazy.Char8 (ByteString, putStrLn, unpack)
import Data.Vector ((!))
import Prelude hiding (log, putStrLn) import Prelude hiding (log, putStrLn)
import qualified Area (Key(..)) import qualified Area (Key(..), dex)
import qualified Character (new) import qualified Character (new)
import Game (Game(..), Position(..), State(..)) import Game (Direction(..), Game(..), Position(..), State(..))
import Message (Connected, receive, send) import Message (Connected, receive, send)
import qualified Message.Client as Client (Message(..)) import qualified Message.Client as Client (Message(..))
import qualified Message.Server as Server (Message(..)) import qualified Message.Server as Server (Message(..))
edges :: Game -> Client.Message -> Connected Game edges :: Game -> Client.Message -> Connected ()
edges Empty Client.NewGame = return $ Game { edges Empty Client.NewGame = set $ Game {
character = Character.new character = Character.new
, state = At $ Position { , state = At $ Position {
area = Area.Key 0 area = Area.Key 0
, x = 0 , x = 0
, y = 0 , y = 0
, direction = S
} }
} }
edges Empty (Client.Resume {Client.game}) = return game edges Empty (Client.Resume {Client.game}) = set game
edges game message = do edges game@(Game {state = At position}) (Client.Move {Client.to}) =
set $ game {state = At newPosition}
where
newPosition
| direction position == to =
let (dx, dy) =
case to of
N -> (0, -1)
S -> (0, 1)
E -> (1, 0)
W -> (-1, 0)
in
position { x = x position + dx, y = y position + dy }
| otherwise = position { direction = to }
edges _ (Client.Load {Client.area}) =
case area of
Area.Key n -> send . Server.Area $ Area.dex ! n
edges _ message = do
let errorMessage = "inacceptable message " `mappend` encode message `mappend` " in this context" let errorMessage = "inacceptable message " `mappend` encode message `mappend` " in this context"
send (Server.Error $ unpack errorMessage) send (Server.Error $ unpack errorMessage)
log errorMessage log errorMessage
return game
log :: ByteString -> Connected () log :: ByteString -> Connected ()
log line = log line =
liftIO $ putStrLn line liftIO $ putStrLn line
set :: Game -> Connected ()
set game = do
log $ encode game
put game
send $ Server.Game game
run :: Connected () run :: Connected ()
run = do run = do
message <- Message.receive message <- Message.receive
game <- get game <- get
newGame <- edges game message edges game message
log $ encode newGame
put newGame
send $ Server.Game newGame
run run

View File

@ -1,6 +1,7 @@
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
module Game ( module Game (
Game(..) Direction(..)
, Game(..)
, Position(..) , Position(..)
, State(..) , State(..)
) where ) where
@ -11,10 +12,18 @@ import GHC.Generics (Generic)
import Character (Character) import Character (Character)
import qualified Area (Key) import qualified Area (Key)
data Direction = N | S | E | W deriving (Eq, Generic)
instance ToJSON Direction where
toEncoding = genericToEncoding defaultOptions
instance FromJSON Direction where
parseJSON = genericParseJSON defaultOptions
data Position = Position { data Position = Position {
area :: Area.Key area :: Area.Key
, x :: Int , x :: Int
, y :: Int , y :: Int
, direction :: Direction
} deriving (Generic) } deriving (Generic)
instance ToJSON Position where instance ToJSON Position where

View File

@ -5,13 +5,21 @@ module Message.Client (
import Data.Aeson (FromJSON(..), ToJSON(..), genericParseJSON, genericToEncoding, defaultOptions) import Data.Aeson (FromJSON(..), ToJSON(..), genericParseJSON, genericToEncoding, defaultOptions)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Game (Game) import qualified Area (Key)
import Game (Game, Direction)
data Message = data Message =
NewGame NewGame
| Resume { | Resume {
game :: Game game :: Game
} deriving (Generic) }
| Load {
area :: Area.Key
}
| Move {
to :: Direction
}
deriving (Generic)
instance ToJSON Message where instance ToJSON Message where
toEncoding = genericToEncoding defaultOptions toEncoding = genericToEncoding defaultOptions

View File

@ -4,6 +4,7 @@ module Message.Server (
) where ) where
import Data.Aeson (ToJSON(..), genericToEncoding, defaultOptions) import Data.Aeson (ToJSON(..), genericToEncoding, defaultOptions)
import Area (Area)
import Game (Game) import Game (Game)
import GHC.Generics (Generic) import GHC.Generics (Generic)
@ -11,7 +12,9 @@ data Message =
Game Game Game Game
| Error { | Error {
error :: String error :: String
} deriving (Generic) }
| Area Area
deriving (Generic)
instance ToJSON Message where instance ToJSON Message where
toEncoding = genericToEncoding defaultOptions toEncoding = genericToEncoding defaultOptions