2023-12-04 17:20:06 +01:00
|
|
|
module Main where
|
|
|
|
|
2023-12-10 12:44:23 +01:00
|
|
|
import Control.Concurrent (threadDelay)
|
|
|
|
|
2023-12-11 17:21:24 +01:00
|
|
|
import qualified Control.Concurrent.STM as STM
|
|
|
|
|
2023-12-13 13:36:06 +01:00
|
|
|
import Control.Monad
|
|
|
|
|
|
|
|
import Control.Monad.RWS
|
|
|
|
|
2024-04-17 19:44:47 +02:00
|
|
|
import Data.IORef
|
|
|
|
|
2023-12-20 08:12:22 +01:00
|
|
|
import qualified Data.Matrix as M
|
|
|
|
|
2023-12-13 13:36:06 +01:00
|
|
|
import Graphics.Vty
|
2024-05-01 23:53:12 +02:00
|
|
|
import Graphics.Vty.CrossPlatform
|
2023-12-13 13:36:06 +01:00
|
|
|
|
2024-11-03 04:40:46 +01:00
|
|
|
import Network.Socket (Socket, close)
|
2024-11-02 23:37:15 +01:00
|
|
|
|
2023-12-10 12:44:23 +01:00
|
|
|
import Options.Applicative
|
|
|
|
|
|
|
|
-- internal imports
|
|
|
|
|
|
|
|
import Client.Communication
|
2023-12-20 08:12:22 +01:00
|
|
|
import Client.Game
|
2023-12-10 12:44:23 +01:00
|
|
|
import Client.Types
|
|
|
|
|
2023-12-10 17:00:50 +01:00
|
|
|
import Library.Types
|
|
|
|
|
2023-12-04 17:20:06 +01:00
|
|
|
main :: IO ()
|
2023-12-10 12:44:23 +01:00
|
|
|
main = do
|
|
|
|
putStrLn "Hello, Arena!"
|
|
|
|
(Options socketLocation) <- execParser opts
|
|
|
|
putStrLn $ "connecting to Socket " <> socketLocation
|
2023-12-10 20:12:53 +01:00
|
|
|
sock <- connectSocket socketLocation
|
|
|
|
putStrLn "connected"
|
2023-12-12 02:53:05 +01:00
|
|
|
|
|
|
|
|
2024-10-28 12:20:04 +01:00
|
|
|
messageQueue <- STM.newTQueueIO
|
|
|
|
eventQueue <- STM.newTQueueIO
|
2023-12-12 09:01:21 +01:00
|
|
|
|
|
|
|
|
2023-12-13 13:36:06 +01:00
|
|
|
-- threadDelay $ 1 * 10 ^ 6
|
2024-04-19 20:18:49 +02:00
|
|
|
mockClientState <- STM.newTMVarIO (ClientState undefined False)
|
|
|
|
let mockState = StateContainer undefined mockClientState undefined undefined
|
|
|
|
sendMessage IdRequest sock
|
2024-11-03 04:40:46 +01:00
|
|
|
awaitResponse sock messageQueue mockState 0
|
2024-10-28 12:20:04 +01:00
|
|
|
clientIdMsg <- head <$> STM.atomically (STM.flushTQueue messageQueue)
|
2023-12-11 11:54:17 +01:00
|
|
|
let clientId = acClientUUID clientIdMsg
|
|
|
|
putStrLn $ "received client UUID: " <> show clientId
|
2023-12-12 02:53:05 +01:00
|
|
|
|
|
|
|
|
2023-12-11 17:21:24 +01:00
|
|
|
putStrLn welcomeText
|
2023-12-12 09:01:21 +01:00
|
|
|
threadDelay $ 5 * 10 ^ 6
|
2023-12-12 02:53:05 +01:00
|
|
|
|
|
|
|
|
2024-04-19 20:18:49 +02:00
|
|
|
sendMessage (ClientMessage clientId ClientRequestWizard) sock
|
2023-12-13 13:36:06 +01:00
|
|
|
-- threadDelay $ 1 * 10 ^ 6
|
2024-11-03 04:40:46 +01:00
|
|
|
awaitResponse sock messageQueue mockState 1
|
2024-10-28 12:20:04 +01:00
|
|
|
playerWizard <- head <$> STM.atomically (STM.flushTQueue messageQueue)
|
2023-12-12 02:53:05 +01:00
|
|
|
putStrLn $ "received wizard: " <> show (initWizard playerWizard)
|
|
|
|
|
2024-05-01 23:53:12 +02:00
|
|
|
vty <- mkVty defaultConfig
|
|
|
|
hideCursor (outputIface vty)
|
2023-12-13 13:36:06 +01:00
|
|
|
|
|
|
|
-- shut down graphical interface for now
|
2024-03-29 04:14:21 +01:00
|
|
|
-- shutdown vty
|
2023-12-12 02:53:05 +01:00
|
|
|
|
2024-04-19 20:18:49 +02:00
|
|
|
stopper <- newIORef False
|
2024-06-09 07:32:03 +02:00
|
|
|
clientState <- STM.newTMVarIO (ClientState vty False)
|
2023-12-20 08:12:22 +01:00
|
|
|
let initSlice = MapSlice (M.matrix 9 9 (const Nothing)) []
|
2024-10-28 12:20:04 +01:00
|
|
|
let initRead = ReaderContainer sock clientId messageQueue eventQueue
|
2024-04-19 20:18:49 +02:00
|
|
|
initState = StateContainer (initWizard playerWizard) clientState initSlice stopper
|
2023-12-13 13:36:06 +01:00
|
|
|
-- putStrLn "sending quit message"
|
|
|
|
-- sendMessage (ClientMessage clientId ClientQuit) sock
|
2024-11-03 11:19:19 +01:00
|
|
|
putStrLn "entering Game Monad"
|
2023-12-13 13:36:06 +01:00
|
|
|
void $ execRWST
|
|
|
|
(do
|
2024-04-17 19:44:47 +02:00
|
|
|
terminateGameOnSigint stopper
|
2024-11-03 11:19:19 +01:00
|
|
|
liftIO $ putStrLn "entering main game function"
|
2024-04-19 20:18:49 +02:00
|
|
|
runGame
|
2023-12-13 13:36:06 +01:00
|
|
|
)
|
|
|
|
initRead
|
|
|
|
initState
|
2024-11-02 23:37:15 +01:00
|
|
|
putStrLn "Shutting down client…"
|
2024-05-01 23:53:12 +02:00
|
|
|
showCursor (outputIface vty)
|
|
|
|
shutdown vty
|
2024-11-03 04:40:46 +01:00
|
|
|
threadDelay (10 ^ 3)
|
2024-11-02 23:37:15 +01:00
|
|
|
putStrLn "Closing connection to server…"
|
|
|
|
close sock
|
2024-04-07 13:35:48 +02:00
|
|
|
putStrLn "bye bye"
|
2023-12-10 12:44:23 +01:00
|
|
|
where
|
|
|
|
opts = info (options <**> helper)
|
|
|
|
( fullDesc
|
2023-12-20 20:17:18 +01:00
|
|
|
<> progDesc "Run the \"wizard-wipeout\" Client."
|
2023-12-10 12:44:23 +01:00
|
|
|
<> header "wizard-wipeout - Last Mage standing"
|
|
|
|
)
|
2023-12-11 11:54:17 +01:00
|
|
|
|
|
|
|
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:"
|
|
|
|
, ""
|
2023-12-11 17:21:24 +01:00
|
|
|
, "press [SPACE] to fire your wand"
|
2023-12-11 11:54:17 +01:00
|
|
|
, "press [1] through [9] to change wands, if you have collected more wands"
|
|
|
|
, "press [ESC] to leave"
|
|
|
|
, ""
|
|
|
|
, "Good Luck!"
|
|
|
|
]
|
2023-12-12 09:01:21 +01:00
|
|
|
|
|
|
|
awaitResponse
|
2024-11-03 04:40:46 +01:00
|
|
|
:: Socket
|
|
|
|
-> STM.TQueue ServerMessage
|
|
|
|
-> StateContainer
|
2023-12-12 09:01:21 +01:00
|
|
|
-> Int
|
|
|
|
-> IO ()
|
2024-11-03 04:40:46 +01:00
|
|
|
awaitResponsea _ _ _ 10 = error "Tries to communicate with server exceeded"
|
|
|
|
awaitResponse sock messageQueue mockState numTries = do
|
|
|
|
receiveMessage sock messageQueue mockState
|
2024-10-28 12:20:04 +01:00
|
|
|
responsePresent <- not <$> STM.atomically (STM.isEmptyTQueue messageQueue)
|
2023-12-12 09:01:21 +01:00
|
|
|
if responsePresent
|
|
|
|
then pure ()
|
|
|
|
else do
|
|
|
|
threadDelay $ 10 ^ 6
|
2024-11-03 04:40:46 +01:00
|
|
|
awaitResponse sock messageQueue mockState (succ numTries)
|