From 6db9d877aa5c530ce4b3c1279b5ea30b9ce75cdf Mon Sep 17 00:00:00 2001 From: nek0 Date: Mon, 11 Dec 2023 13:47:27 +0100 Subject: [PATCH] wizard creation and transmission works --- src-client/Main.hs | 3 +- src-server/Server/Communication.hs | 49 +++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src-client/Main.hs b/src-client/Main.hs index 2d458fa..ad052db 100644 --- a/src-client/Main.hs +++ b/src-client/Main.hs @@ -29,6 +29,8 @@ main = do threadDelay $ 15 * 10 ^ 6 sendMessage (ClientMessage clientId ClientRequestWizard) sock playerWizard <- receiveMessage sock + putStrLn "received wizard" + print (initWizard playerWizard) let initRead = ReaderContainer sock clientId initState = StateContainer (initWizard playerWizard) putStrLn "sending quit message" @@ -40,7 +42,6 @@ main = do <> header "wizard-wipeout - Last Mage standing" ) - welcomeText :: String welcomeText = mconcat $ map (<> "\n") [ "Welcome to the arena, wizards." diff --git a/src-server/Server/Communication.hs b/src-server/Server/Communication.hs index 1a6508a..5f35d42 100644 --- a/src-server/Server/Communication.hs +++ b/src-server/Server/Communication.hs @@ -18,6 +18,8 @@ import qualified Data.ByteString.Lazy.Char8 as B8 import Data.List +import qualified Data.Matrix as M + import Data.Maybe import Data.UUID @@ -27,10 +29,14 @@ import qualified Data.Vector.Storable as VS import Foreign hiding (void) +import Linear + import Network.Socket as Net import System.Posix.Signals +import System.Random (randomRIO) + -- internal imports import Library.Types @@ -114,10 +120,12 @@ processMessages = do clientsVar <- gets scClientSockets clients <- liftIO $ STM.atomically $ STM.readTMVar clientsVar queue <- gets scMessageQueue + serverState <- get + readerContainer <- ask mapM_ (\(uuid, clientSocket) -> void $ liftIO $ forkIO $ do receiveMessage clientSocket queue - handleMessage queue clientsVar + handleMessage serverState readerContainer ) clients @@ -148,10 +156,12 @@ receiveMessage sock queue = do -- | function for translating 'ClientMessage's into server actions handleMessage - :: STM.TQueue ClientMessage - -> STM.TMVar [(Maybe UUID, Socket)] + :: StateContainer + -> ReaderContainer -> IO () -handleMessage queue clientList = do +handleMessage stateContainer readerContainer = do + let queue = scMessageQueue stateContainer + clientList = scClientSockets stateContainer msgs <- liftIO $ STM.atomically $ STM.flushTQueue queue clients <- liftIO $ STM.atomically $ STM.readTMVar clientList mapM_ @@ -181,6 +191,37 @@ handleMessage queue clientList = do putStrLn $ "removing client " <> show clientId let newClients = filter (\a -> fst a /= Just clientId) clients void $ liftIO $ STM.atomically $ STM.swapTMVar clientList newClients + ClientRequestWizard -> do + putStrLn "initializing new wizard" + let arena = rcMap readerContainer + initPos <- rollInitPosition arena + let clientSock = fromJust $ lookup (Just clientId) clients + sendMessage (ProvideInitialWizard (newWizard initPos)) clientSock _ -> pure () ) msgs + +newWizard + :: Position + -> Wizard +newWizard pos = + Wizard + { wizardWands = [] + , wizardRot = 0 + , wizardPos = pos + , wizardMana = 100 + , wizardHealth = 100 + , wizardEffects = [] + } + +rollInitPosition + :: ServerMap + -> IO Position +rollInitPosition arena = do + r <- randomRIO (1, M.nrows arena) + c <- randomRIO (1, M.ncols arena) + if arena M.! (r, c) == Air + then + pure $ (fromIntegral <$> V2 r c) + V2 0.5 0.5 + else + rollInitPosition arena