wizard creation and transmission works

This commit is contained in:
nek0 2023-12-11 13:47:27 +01:00
parent ad52b9c037
commit 6db9d877aa
2 changed files with 47 additions and 5 deletions

View file

@ -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."

View file

@ -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