wizard creation and transmission works
This commit is contained in:
parent
ad52b9c037
commit
6db9d877aa
2 changed files with 47 additions and 5 deletions
|
@ -29,6 +29,8 @@ main = do
|
||||||
threadDelay $ 15 * 10 ^ 6
|
threadDelay $ 15 * 10 ^ 6
|
||||||
sendMessage (ClientMessage clientId ClientRequestWizard) sock
|
sendMessage (ClientMessage clientId ClientRequestWizard) sock
|
||||||
playerWizard <- receiveMessage sock
|
playerWizard <- receiveMessage sock
|
||||||
|
putStrLn "received wizard"
|
||||||
|
print (initWizard playerWizard)
|
||||||
let initRead = ReaderContainer sock clientId
|
let initRead = ReaderContainer sock clientId
|
||||||
initState = StateContainer (initWizard playerWizard)
|
initState = StateContainer (initWizard playerWizard)
|
||||||
putStrLn "sending quit message"
|
putStrLn "sending quit message"
|
||||||
|
@ -40,7 +42,6 @@ main = do
|
||||||
<> header "wizard-wipeout - Last Mage standing"
|
<> header "wizard-wipeout - Last Mage standing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
welcomeText :: String
|
welcomeText :: String
|
||||||
welcomeText = mconcat $ map (<> "\n")
|
welcomeText = mconcat $ map (<> "\n")
|
||||||
[ "Welcome to the arena, wizards."
|
[ "Welcome to the arena, wizards."
|
||||||
|
|
|
@ -18,6 +18,8 @@ import qualified Data.ByteString.Lazy.Char8 as B8
|
||||||
|
|
||||||
import Data.List
|
import Data.List
|
||||||
|
|
||||||
|
import qualified Data.Matrix as M
|
||||||
|
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
|
||||||
import Data.UUID
|
import Data.UUID
|
||||||
|
@ -27,10 +29,14 @@ import qualified Data.Vector.Storable as VS
|
||||||
|
|
||||||
import Foreign hiding (void)
|
import Foreign hiding (void)
|
||||||
|
|
||||||
|
import Linear
|
||||||
|
|
||||||
import Network.Socket as Net
|
import Network.Socket as Net
|
||||||
|
|
||||||
import System.Posix.Signals
|
import System.Posix.Signals
|
||||||
|
|
||||||
|
import System.Random (randomRIO)
|
||||||
|
|
||||||
-- internal imports
|
-- internal imports
|
||||||
|
|
||||||
import Library.Types
|
import Library.Types
|
||||||
|
@ -114,10 +120,12 @@ processMessages = do
|
||||||
clientsVar <- gets scClientSockets
|
clientsVar <- gets scClientSockets
|
||||||
clients <- liftIO $ STM.atomically $ STM.readTMVar clientsVar
|
clients <- liftIO $ STM.atomically $ STM.readTMVar clientsVar
|
||||||
queue <- gets scMessageQueue
|
queue <- gets scMessageQueue
|
||||||
|
serverState <- get
|
||||||
|
readerContainer <- ask
|
||||||
mapM_
|
mapM_
|
||||||
(\(uuid, clientSocket) -> void $ liftIO $ forkIO $ do
|
(\(uuid, clientSocket) -> void $ liftIO $ forkIO $ do
|
||||||
receiveMessage clientSocket queue
|
receiveMessage clientSocket queue
|
||||||
handleMessage queue clientsVar
|
handleMessage serverState readerContainer
|
||||||
)
|
)
|
||||||
clients
|
clients
|
||||||
|
|
||||||
|
@ -148,10 +156,12 @@ receiveMessage sock queue = do
|
||||||
|
|
||||||
-- | function for translating 'ClientMessage's into server actions
|
-- | function for translating 'ClientMessage's into server actions
|
||||||
handleMessage
|
handleMessage
|
||||||
:: STM.TQueue ClientMessage
|
:: StateContainer
|
||||||
-> STM.TMVar [(Maybe UUID, Socket)]
|
-> ReaderContainer
|
||||||
-> IO ()
|
-> IO ()
|
||||||
handleMessage queue clientList = do
|
handleMessage stateContainer readerContainer = do
|
||||||
|
let queue = scMessageQueue stateContainer
|
||||||
|
clientList = scClientSockets stateContainer
|
||||||
msgs <- liftIO $ STM.atomically $ STM.flushTQueue queue
|
msgs <- liftIO $ STM.atomically $ STM.flushTQueue queue
|
||||||
clients <- liftIO $ STM.atomically $ STM.readTMVar clientList
|
clients <- liftIO $ STM.atomically $ STM.readTMVar clientList
|
||||||
mapM_
|
mapM_
|
||||||
|
@ -181,6 +191,37 @@ handleMessage queue clientList = do
|
||||||
putStrLn $ "removing client " <> show clientId
|
putStrLn $ "removing client " <> show clientId
|
||||||
let newClients = filter (\a -> fst a /= Just clientId) clients
|
let newClients = filter (\a -> fst a /= Just clientId) clients
|
||||||
void $ liftIO $ STM.atomically $ STM.swapTMVar clientList newClients
|
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 ()
|
_ -> pure ()
|
||||||
)
|
)
|
||||||
msgs
|
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
|
||||||
|
|
Loading…
Reference in a new issue