wizard-wipeout/src-client/Main.hs

122 lines
3 KiB
Haskell
Raw Permalink Normal View History

2023-12-04 16:20:06 +00:00
module Main where
import Control.Concurrent (threadDelay)
import qualified Control.Concurrent.STM as STM
2023-12-13 12:36:06 +00:00
import Control.Monad
import Control.Monad.RWS
2024-04-17 17:44:47 +00:00
import Data.IORef
2023-12-20 07:12:22 +00:00
import qualified Data.Matrix as M
2023-12-13 12:36:06 +00:00
import Graphics.Vty
2024-05-01 21:53:12 +00:00
import Graphics.Vty.CrossPlatform
2023-12-13 12:36:06 +00:00
import Options.Applicative
-- internal imports
import Client.Communication
2023-12-20 07:12:22 +00:00
import Client.Game
import Client.Types
2023-12-10 16:00:50 +00:00
import Library.Types
2023-12-04 16:20:06 +00:00
main :: IO ()
main = do
putStrLn "Hello, Arena!"
(Options socketLocation) <- execParser opts
putStrLn $ "connecting to Socket " <> socketLocation
2023-12-10 19:12:53 +00:00
sock <- connectSocket socketLocation
putStrLn "connected"
2023-12-12 01:53:05 +00:00
2023-12-12 08:01:21 +00:00
queue <- STM.newTQueueIO
2023-12-13 12:36:06 +00:00
-- threadDelay $ 1 * 10 ^ 6
2024-04-19 18:18:49 +00:00
mockClientState <- STM.newTMVarIO (ClientState undefined False)
let mockState = StateContainer undefined mockClientState undefined undefined
sendMessage IdRequest sock
2024-03-29 22:01:41 +00:00
receiveMessage sock queue mockState
2023-12-12 08:01:21 +00:00
awaitResponse queue 1
clientIdMsg <- head <$> STM.atomically (STM.flushTQueue queue)
let clientId = acClientUUID clientIdMsg
putStrLn $ "received client UUID: " <> show clientId
2023-12-12 01:53:05 +00:00
putStrLn welcomeText
2023-12-12 08:01:21 +00:00
threadDelay $ 5 * 10 ^ 6
2023-12-12 01:53:05 +00:00
2024-04-19 18:18:49 +00:00
sendMessage (ClientMessage clientId ClientRequestWizard) sock
2023-12-13 12:36:06 +00:00
-- threadDelay $ 1 * 10 ^ 6
2024-03-29 22:01:41 +00:00
receiveMessage sock queue mockState
2023-12-12 08:01:21 +00:00
awaitResponse queue 1
playerWizard <- head <$> STM.atomically (STM.flushTQueue queue)
2023-12-12 01:53:05 +00:00
putStrLn $ "received wizard: " <> show (initWizard playerWizard)
2024-05-01 21:53:12 +00:00
vty <- mkVty defaultConfig
hideCursor (outputIface vty)
2023-12-13 12:36:06 +00:00
-- shut down graphical interface for now
2024-03-29 03:14:21 +00:00
-- shutdown vty
2023-12-12 01:53:05 +00:00
2024-04-19 18:18:49 +00:00
stopper <- newIORef False
clientState <- STM.newTMVarIO (ClientState undefined False)
2023-12-20 07:12:22 +00:00
let initSlice = MapSlice (M.matrix 9 9 (const Nothing)) []
let initRead = ReaderContainer sock clientId queue
2024-04-19 18:18:49 +00:00
initState = StateContainer (initWizard playerWizard) clientState initSlice stopper
2023-12-13 12:36:06 +00:00
-- putStrLn "sending quit message"
-- sendMessage (ClientMessage clientId ClientQuit) sock
void $ execRWST
(do
2024-04-17 17:44:47 +00:00
terminateGameOnSigint stopper
2024-04-19 18:18:49 +00:00
runGame
2023-12-13 12:36:06 +00:00
)
initRead
initState
2024-05-01 21:53:12 +00:00
showCursor (outputIface vty)
shutdown vty
2024-04-17 17:44:47 +00:00
putStrLn "Shutting down client…"
-- threadDelay 100
-- close sock
2024-04-07 11:35:48 +00:00
putStrLn "bye bye"
where
opts = info (options <**> helper)
( fullDesc
2023-12-20 19:17:18 +00:00
<> progDesc "Run the \"wizard-wipeout\" Client."
<> header "wizard-wipeout - Last Mage standing"
)
welcomeText :: String
welcomeText = mconcat $ map (<> "\n")
[ "Welcome to the arena, wizards."
, ""
, "Let the toughest and bravest of you survive"
, ""
, "Some last hints before you enter the fighting grounds:"
, ""
, "press [SPACE] to fire your wand"
, "press [1] through [9] to change wands, if you have collected more wands"
, "press [ESC] to leave"
, ""
, "Good Luck!"
]
2023-12-12 08:01:21 +00:00
awaitResponse
:: STM.TQueue ServerMessage
-> Int
-> IO ()
awaitResponse _ 10 = error "Tries to communicate with server exceeded"
awaitResponse queue numTries = do
responsePresent <- not <$> STM.atomically (STM.isEmptyTQueue queue)
if responsePresent
then pure ()
else do
threadDelay $ 10 ^ 6
awaitResponse queue (succ numTries)