start imlementing client actions
This commit is contained in:
parent
fbc2c4da3e
commit
e9b0093103
4 changed files with 61 additions and 13 deletions
|
@ -11,18 +11,44 @@ import Graphics.Vty
|
||||||
-- internal imports
|
-- internal imports
|
||||||
|
|
||||||
import Client.Communication
|
import Client.Communication
|
||||||
|
import Client.Log
|
||||||
import Client.Types
|
import Client.Types
|
||||||
|
import Library.Types
|
||||||
|
|
||||||
|
-- | Function for Handling singular input events
|
||||||
handleEvent
|
handleEvent
|
||||||
:: Event
|
:: Event -- A single event
|
||||||
-> Game ()
|
-> Game ()
|
||||||
handleEvent (EvKey KEsc _) = do
|
handleEvent (EvKey KEsc _) = do
|
||||||
st <- get
|
st <- get
|
||||||
curLevel <- asks rcLogLevel
|
curLevel <- asks rcLogLevel
|
||||||
liftIO $ gracefulExit curLevel st "Quitting due to user input"
|
liftIO $ gracefulExit curLevel st "Quitting due to user input"
|
||||||
|
|
||||||
|
handleEvent (EvKey (KChar c) _) = do
|
||||||
|
curLevel <- asks rcLogLevel
|
||||||
|
cId <- asks rcClientUUID
|
||||||
|
sock <- asks rcSocket
|
||||||
|
let action = case c of
|
||||||
|
'w' -> Just StepForward
|
||||||
|
's' -> Just StepBackward
|
||||||
|
'a' -> Just TurnLeft
|
||||||
|
'd' -> Just TurnRight
|
||||||
|
'q' -> Just StrifeLeft
|
||||||
|
'e' -> Just StrifeRight
|
||||||
|
_ -> Nothing
|
||||||
|
maybe
|
||||||
|
(logPrint Verbose $ "Unmapped input:" <> [c])
|
||||||
|
(\act -> do
|
||||||
|
let msg = ClientMessage
|
||||||
|
cId
|
||||||
|
(ClientAction act)
|
||||||
|
liftIO $ sendMessage curLevel msg sock
|
||||||
|
)
|
||||||
|
action
|
||||||
handleEvent _ =
|
handleEvent _ =
|
||||||
pure ()
|
pure ()
|
||||||
|
|
||||||
|
-- | Function handling the event list from the queue
|
||||||
handleEvents
|
handleEvents
|
||||||
:: Game ()
|
:: Game ()
|
||||||
handleEvents = do
|
handleEvents = do
|
||||||
|
@ -32,8 +58,9 @@ handleEvents = do
|
||||||
handleEvent
|
handleEvent
|
||||||
evs
|
evs
|
||||||
|
|
||||||
|
-- | Function for punping all Events from Vty into a queue
|
||||||
pumpEvents
|
pumpEvents
|
||||||
:: Vty
|
:: Vty
|
||||||
-> Game ()
|
-> Game ()
|
||||||
pumpEvents vty = do
|
pumpEvents vty = do
|
||||||
eventQueue <- asks rcEventQueue
|
eventQueue <- asks rcEventQueue
|
||||||
|
|
|
@ -10,19 +10,21 @@ import Debug.Trace (traceIO)
|
||||||
|
|
||||||
import Client.Types (Game, LogLevel, ReaderContainer(rcLogLevel))
|
import Client.Types (Game, LogLevel, ReaderContainer(rcLogLevel))
|
||||||
|
|
||||||
|
-- | Print a log line
|
||||||
logPrint
|
logPrint
|
||||||
:: LogLevel
|
:: LogLevel -- ^ Desired log level of message
|
||||||
-> String
|
-> String -- ^ The message
|
||||||
-> Game ()
|
-> Game ()
|
||||||
logPrint level msg = do
|
logPrint level msg = do
|
||||||
curLevel <- asks rcLogLevel
|
curLevel <- asks rcLogLevel
|
||||||
when (level <= curLevel) $
|
when (level <= curLevel) $
|
||||||
liftIO $ traceIO $ show level <> ": " <> msg
|
liftIO $ traceIO $ show level <> ": " <> msg
|
||||||
|
|
||||||
|
-- | Print a log line
|
||||||
logPrintIO
|
logPrintIO
|
||||||
:: LogLevel
|
:: LogLevel -- ^ Current log level
|
||||||
-> LogLevel
|
-> LogLevel -- ^ Desired log level of message
|
||||||
-> String
|
-> String -- ^ The message
|
||||||
-> IO ()
|
-> IO ()
|
||||||
logPrintIO curLevel level msg = do
|
logPrintIO curLevel level msg = do
|
||||||
when (level <= curLevel) $
|
when (level <= curLevel) $
|
||||||
|
|
|
@ -36,12 +36,15 @@ instance FromJSON ServerMessage
|
||||||
instance ToJSON ServerMessage
|
instance ToJSON ServerMessage
|
||||||
|
|
||||||
data ClientMessagePayload
|
data ClientMessagePayload
|
||||||
= ClientQuit -- ^ Message notifying the server of a client about to shut down
|
= ClientQuit -- ^ Message notifying the server of a client about to shut down
|
||||||
| ClientRequestWizard -- ^ Message for requesting a new player character
|
| ClientRequestWizard -- ^ Message for requesting a new player character
|
||||||
| ClientReady -- ^ Message requesting the server to position the client's character on the
|
| ClientReady -- ^ Message requesting the server to position the client's character on the
|
||||||
-- map
|
-- map
|
||||||
| Pong -- ^ A client's reply to a heartbeat request
|
| ClientAction -- ^ Message containting a singular action of a client
|
||||||
{ pongId :: UUID -- ^ The 'UUID' of the original heartbeat request
|
{ actionPayload :: ActionPayload -- ^ The contained action
|
||||||
|
}
|
||||||
|
| Pong -- ^ A client's reply to a heartbeat request
|
||||||
|
{ pongId :: UUID -- ^ The 'UUID' of the original heartbeat request
|
||||||
}
|
}
|
||||||
deriving (Eq, Show, Generic)
|
deriving (Eq, Show, Generic)
|
||||||
|
|
||||||
|
@ -54,6 +57,15 @@ data ClientMessage
|
||||||
-- 'UUID' ad startup
|
-- 'UUID' ad startup
|
||||||
deriving (Eq, Show, Generic)
|
deriving (Eq, Show, Generic)
|
||||||
|
|
||||||
|
data ActionPayload
|
||||||
|
= StepForward -- ^ Move a step forward
|
||||||
|
| StepBackward -- ^ Move a step backwards
|
||||||
|
| StrifeLeft -- ^ Move a step left
|
||||||
|
| StrifeRight -- ^ Move a step right
|
||||||
|
| TurnLeft -- ^ Turn left
|
||||||
|
| TurnRight -- ^ Turn right
|
||||||
|
deriving (Eq, Show, Generic)
|
||||||
|
|
||||||
instance FromJSON ClientMessagePayload
|
instance FromJSON ClientMessagePayload
|
||||||
|
|
||||||
instance ToJSON ClientMessagePayload
|
instance ToJSON ClientMessagePayload
|
||||||
|
@ -61,3 +73,7 @@ instance ToJSON ClientMessagePayload
|
||||||
instance FromJSON ClientMessage
|
instance FromJSON ClientMessage
|
||||||
|
|
||||||
instance ToJSON ClientMessage
|
instance ToJSON ClientMessage
|
||||||
|
|
||||||
|
instance FromJSON ActionPayload
|
||||||
|
|
||||||
|
instance ToJSON ActionPayload
|
||||||
|
|
|
@ -132,6 +132,9 @@ handleMessage stateContainer readerContainer msg = do
|
||||||
mPlayer
|
mPlayer
|
||||||
)
|
)
|
||||||
mclient
|
mclient
|
||||||
|
-- ClientAction act -> do
|
||||||
|
-- let mclient = find (\c -> csUUID c == Just clientId) socks
|
||||||
|
x -> logPrintIO (rcLogLevel readerContainer) Verbose ("Unhandled message: " <> show x)
|
||||||
|
|
||||||
-- | handle received messages
|
-- | handle received messages
|
||||||
handleMessages :: Game ()
|
handleMessages :: Game ()
|
||||||
|
|
Loading…
Reference in a new issue