start imlementing client actions

This commit is contained in:
Amedeo Molnár 2024-12-17 10:03:05 +01:00
parent fbc2c4da3e
commit e9b0093103
4 changed files with 61 additions and 13 deletions

View file

@ -11,18 +11,44 @@ import Graphics.Vty
-- internal imports
import Client.Communication
import Client.Log
import Client.Types
import Library.Types
-- | Function for Handling singular input events
handleEvent
:: Event
:: Event -- A single event
-> Game ()
handleEvent (EvKey KEsc _) = do
st <- get
curLevel <- asks rcLogLevel
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 _ =
pure ()
-- | Function handling the event list from the queue
handleEvents
:: Game ()
handleEvents = do
@ -32,6 +58,7 @@ handleEvents = do
handleEvent
evs
-- | Function for punping all Events from Vty into a queue
pumpEvents
:: Vty
-> Game ()

View file

@ -10,19 +10,21 @@ import Debug.Trace (traceIO)
import Client.Types (Game, LogLevel, ReaderContainer(rcLogLevel))
-- | Print a log line
logPrint
:: LogLevel
-> String
:: LogLevel -- ^ Desired log level of message
-> String -- ^ The message
-> Game ()
logPrint level msg = do
curLevel <- asks rcLogLevel
when (level <= curLevel) $
liftIO $ traceIO $ show level <> ": " <> msg
-- | Print a log line
logPrintIO
:: LogLevel
-> LogLevel
-> String
:: LogLevel -- ^ Current log level
-> LogLevel -- ^ Desired log level of message
-> String -- ^ The message
-> IO ()
logPrintIO curLevel level msg = do
when (level <= curLevel) $

View file

@ -40,6 +40,9 @@ data ClientMessagePayload
| ClientRequestWizard -- ^ Message for requesting a new player character
| ClientReady -- ^ Message requesting the server to position the client's character on the
-- map
| ClientAction -- ^ Message containting a singular action of a client
{ actionPayload :: ActionPayload -- ^ The contained action
}
| Pong -- ^ A client's reply to a heartbeat request
{ pongId :: UUID -- ^ The 'UUID' of the original heartbeat request
}
@ -54,6 +57,15 @@ data ClientMessage
-- 'UUID' ad startup
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 ToJSON ClientMessagePayload
@ -61,3 +73,7 @@ instance ToJSON ClientMessagePayload
instance FromJSON ClientMessage
instance ToJSON ClientMessage
instance FromJSON ActionPayload
instance ToJSON ActionPayload

View file

@ -132,6 +132,9 @@ handleMessage stateContainer readerContainer msg = do
mPlayer
)
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
handleMessages :: Game ()