fix data garbling
This commit is contained in:
parent
b2d75ca3fd
commit
3e72e87e56
7 changed files with 80 additions and 55 deletions
|
@ -93,12 +93,17 @@ receiveMessage sock queue = do
|
||||||
let maxBufferLength = 4096
|
let maxBufferLength = 4096
|
||||||
ptr <- mallocArray maxBufferLength
|
ptr <- mallocArray maxBufferLength
|
||||||
bufferLength <- recvBuf sock ptr maxBufferLength
|
bufferLength <- recvBuf sock ptr maxBufferLength
|
||||||
msg <- B.pack <$> peekArray bufferLength ptr
|
rawMsg <- B.pack <$> peekArray bufferLength ptr
|
||||||
let mJsonMsg = A.decode' $ B8.fromStrict msg
|
let msgs = map B8.tail $ init $ B8.split '>' $ B8.fromStrict rawMsg
|
||||||
|
mapM_
|
||||||
|
(\msg -> do
|
||||||
|
let mJsonMsg = A.decode' msg
|
||||||
maybe
|
maybe
|
||||||
(putStrLn $ "received garbled data: " <> B8.unpack (B8.fromStrict msg))
|
(putStrLn $ "received garbled data: " <> B8.unpack msg)
|
||||||
(STM.atomically . STM.writeTQueue queue)
|
(STM.atomically . STM.writeTQueue queue)
|
||||||
mJsonMsg
|
mJsonMsg
|
||||||
|
)
|
||||||
|
msgs
|
||||||
|
|
||||||
-- | Function that installs a handler on SIGINT to terminate game
|
-- | Function that installs a handler on SIGINT to terminate game
|
||||||
terminateGameOnSigint
|
terminateGameOnSigint
|
||||||
|
|
|
@ -21,7 +21,9 @@ runGame :: Game ()
|
||||||
runGame = do
|
runGame = do
|
||||||
sock <- asks rcSocket
|
sock <- asks rcSocket
|
||||||
queue <- asks rcQueue
|
queue <- asks rcQueue
|
||||||
|
clientId <- asks rcClientUUID
|
||||||
recvThread <- liftIO $ forkIO $ forever $ receiveMessage sock queue
|
recvThread <- liftIO $ forkIO $ forever $ receiveMessage sock queue
|
||||||
|
liftIO $ sendMessage (ClientMessage clientId ClientReady) sock
|
||||||
whileM_
|
whileM_
|
||||||
(not . clientStop <$> (liftIO . STM.atomically . STM.readTMVar =<< gets scClientState))
|
(not . clientStop <$> (liftIO . STM.atomically . STM.readTMVar =<< gets scClientState))
|
||||||
(do
|
(do
|
||||||
|
|
|
@ -33,6 +33,7 @@ instance ToJSON ServerMessage
|
||||||
data ClientMessagePayload
|
data ClientMessagePayload
|
||||||
= ClientQuit
|
= ClientQuit
|
||||||
| ClientRequestWizard
|
| ClientRequestWizard
|
||||||
|
| ClientReady
|
||||||
deriving (Eq, Show, Generic)
|
deriving (Eq, Show, Generic)
|
||||||
|
|
||||||
data ClientMessage
|
data ClientMessage
|
||||||
|
|
|
@ -23,7 +23,7 @@ import qualified Data.Matrix as M
|
||||||
|
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
|
||||||
import Data.UUID
|
import Data.UUID hiding (null)
|
||||||
import Data.UUID.V4
|
import Data.UUID.V4
|
||||||
|
|
||||||
import qualified Data.Vector.Storable as VS
|
import qualified Data.Vector.Storable as VS
|
||||||
|
@ -108,7 +108,7 @@ sendMessage
|
||||||
-> IO ()
|
-> IO ()
|
||||||
sendMessage msg sock = do
|
sendMessage msg sock = do
|
||||||
let msgJson = A.encode msg
|
let msgJson = A.encode msg
|
||||||
msgVector = VS.fromList $ B.unpack $ B.toStrict msgJson
|
msgVector = VS.fromList $ B.unpack $ B.toStrict $ ('<' `B8.cons` (msgJson `B8.snoc` '>'))
|
||||||
-- putStrLn $ "sending: " <> B8.unpack msgJson
|
-- putStrLn $ "sending: " <> B8.unpack msgJson
|
||||||
VS.unsafeWith
|
VS.unsafeWith
|
||||||
msgVector
|
msgVector
|
||||||
|
@ -206,12 +206,22 @@ handleMessage stateContainer readerContainer msg = do
|
||||||
putStrLn "initializing new wizard"
|
putStrLn "initializing new wizard"
|
||||||
let arena = rcMap readerContainer
|
let arena = rcMap readerContainer
|
||||||
initPos <- rollInitPosition arena
|
initPos <- rollInitPosition arena
|
||||||
let freshWIzard = newWizard initPos
|
let freshWizard = newWizard initPos
|
||||||
STM.atomically $ do
|
STM.atomically $ do
|
||||||
currentPlayers <- STM.readTMVar (scPlayers stateContainer)
|
currentPlayers <- STM.readTMVar (scPlayers stateContainer)
|
||||||
void $ STM.swapTMVar (scPlayers stateContainer) $ Player clientId freshWIzard : currentPlayers
|
void $ STM.swapTMVar (scPlayers stateContainer) $
|
||||||
|
Player clientId freshWizard False : currentPlayers
|
||||||
let clientSock = fromJust $ lookup (Just clientId) clients
|
let clientSock = fromJust $ lookup (Just clientId) clients
|
||||||
sendMessage (ProvideInitialWizard (newWizard initPos)) clientSock
|
sendMessage (ProvideInitialWizard freshWizard) clientSock
|
||||||
|
ClientReady -> do
|
||||||
|
putStrLn $ "client " <> show clientId <> " is ready!"
|
||||||
|
STM.atomically $ do
|
||||||
|
currentPlayers <- STM.readTMVar (scPlayers stateContainer)
|
||||||
|
let (thisPlayers, otherPlayers) =
|
||||||
|
partition (\p -> playerId p == clientId) currentPlayers
|
||||||
|
unless (null thisPlayers) $
|
||||||
|
void $ STM.swapTMVar (scPlayers stateContainer) $
|
||||||
|
(head thisPlayers) {playerReady = True} : otherPlayers
|
||||||
_ -> pure ()
|
_ -> pure ()
|
||||||
|
|
||||||
sendUpdates :: Game ()
|
sendUpdates :: Game ()
|
||||||
|
@ -221,7 +231,8 @@ sendUpdates = do
|
||||||
players <- liftIO $ STM.atomically $ STM.readTMVar (scPlayers stateContainer)
|
players <- liftIO $ STM.atomically $ STM.readTMVar (scPlayers stateContainer)
|
||||||
sockets <- liftIO $ STM.atomically $ STM.readTMVar (scClientSockets stateContainer)
|
sockets <- liftIO $ STM.atomically $ STM.readTMVar (scClientSockets stateContainer)
|
||||||
mapM_
|
mapM_
|
||||||
(\(player@(Player playerId wizard@(Wizard {..}))) -> do
|
(\(Player playerId wizard@(Wizard {..}) readiness) -> do
|
||||||
|
when readiness $ do
|
||||||
let V2 wr wc = wizardPos
|
let V2 wr wc = wizardPos
|
||||||
subCoords = (,) <$> [floor wr - 4 .. floor wr + 4] <*> [floor wc - 4 .. floor wc + 4]
|
subCoords = (,) <$> [floor wr - 4 .. floor wr + 4] <*> [floor wc - 4 .. floor wc + 4]
|
||||||
leftBound = wizardRot + (pi / 4)
|
leftBound = wizardRot + (pi / 4)
|
||||||
|
|
|
@ -48,7 +48,7 @@ runGame = do
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
fps <- asks rcFPS
|
fps <- asks rcFPS
|
||||||
-- sendUpdates
|
sendUpdates
|
||||||
let remainingTime = recip (fromIntegral fps) - delta
|
let remainingTime = recip (fromIntegral fps) - delta
|
||||||
when (remainingTime > 0) $
|
when (remainingTime > 0) $
|
||||||
liftIO $ threadDelay $ floor $ remainingTime * 10 ^ 6
|
liftIO $ threadDelay $ floor $ remainingTime * 10 ^ 6
|
||||||
|
@ -81,7 +81,7 @@ updateWizards dt = do
|
||||||
playersVar <- gets scPlayers
|
playersVar <- gets scPlayers
|
||||||
players <- liftIO $ STM.atomically $ STM.readTMVar playersVar
|
players <- liftIO $ STM.atomically $ STM.readTMVar playersVar
|
||||||
let newPlayers = map
|
let newPlayers = map
|
||||||
(\player@(Player pId wizard@(Wizard _ _ health _ _ effects)) ->
|
(\player@(Player pId wizard@(Wizard _ _ health _ _ effects) readiness) ->
|
||||||
let newEffects = foldl
|
let newEffects = foldl
|
||||||
(\acc effect@(Effect _ ttl) ->
|
(\acc effect@(Effect _ ttl) ->
|
||||||
if ttl - dt < 0
|
if ttl - dt < 0
|
||||||
|
@ -100,12 +100,17 @@ updateWizards dt = do
|
||||||
)
|
)
|
||||||
0
|
0
|
||||||
effects
|
effects
|
||||||
in player {
|
in
|
||||||
playerWizard = wizard
|
if readiness
|
||||||
|
then
|
||||||
|
player
|
||||||
|
{ playerWizard = wizard
|
||||||
{ wizardEffects = newEffects
|
{ wizardEffects = newEffects
|
||||||
, wizardHealth = health - effectDamage
|
, wizardHealth = health - effectDamage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
player
|
||||||
)
|
)
|
||||||
players
|
players
|
||||||
void $ liftIO $ STM.atomically $ STM.swapTMVar playersVar newPlayers
|
void $ liftIO $ STM.atomically $ STM.swapTMVar playersVar newPlayers
|
||||||
|
|
|
@ -64,6 +64,7 @@ data StateContainer = StateContainer
|
||||||
data Player = Player
|
data Player = Player
|
||||||
{ playerId :: UUID
|
{ playerId :: UUID
|
||||||
, playerWizard :: Wizard
|
, playerWizard :: Wizard
|
||||||
|
, playerReady :: Bool
|
||||||
}
|
}
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ newWizard
|
||||||
newWizard pos =
|
newWizard pos =
|
||||||
Wizard
|
Wizard
|
||||||
{ wizardWands = []
|
{ wizardWands = []
|
||||||
, wizardRot = 3
|
, wizardRot = 0
|
||||||
, wizardPos = pos
|
, wizardPos = pos
|
||||||
, wizardMana = 100
|
, wizardMana = 100
|
||||||
, wizardHealth = 100
|
, wizardHealth = 100
|
||||||
|
|
Loading…
Reference in a new issue