--- a/netserver/Miscutils.hs Sat Apr 19 13:12:08 2008 +0000
+++ b/netserver/Miscutils.hs Sat Apr 19 19:29:58 2008 +0000
@@ -6,6 +6,23 @@
import Control.Concurrent.STM
import Control.Exception (finally)
+data ClientInfo =
+ ClientInfo
+ {
+ handle :: Handle,
+ nick :: String,
+ room :: String,
+ isMaster :: Bool
+ }
+
+data RoomInfo =
+ RoomInfo
+ {
+ name :: String,
+ password :: String
+ }
+
+
sendMsg :: Handle -> String -> IO()
sendMsg clientHandle str = finally (return ()) (hPutStrLn clientHandle str >> hFlush clientHandle) -- catch exception when client tries to send to other
@@ -25,3 +42,12 @@
ls <- readTVar state
writeTVar state $ op ls
+manipState2 :: TVar[ClientInfo] -> TVar[RoomInfo] -> ([ClientInfo] -> [RoomInfo] -> ([ClientInfo], [RoomInfo])) -> IO()
+manipState2 state1 state2 op =
+ atomically $ do
+ ls1 <- readTVar state1
+ ls2 <- readTVar state2
+ let (ol1, ol2) = op ls1 ls2
+ writeTVar state1 ol1
+ writeTVar state2 ol2
+
--- a/netserver/hwserv.hs Sat Apr 19 13:12:08 2008 +0000
+++ b/netserver/hwserv.hs Sat Apr 19 19:29:58 2008 +0000
@@ -8,22 +8,6 @@
import Control.Exception (finally)
import Miscutils
-data ClientInfo =
- ClientInfo
- {
- handle :: Handle,
- nick :: String,
- game :: String,
- isMaster :: Bool
- }
-
-data RoomInfo =
- RoomInfo
- {
- name :: String,
- password :: String
- }
-
handleCmd :: Handle -> TVar[ClientInfo] -> TVar[RoomInfo] -> (String, [String]) -> IO()
handleCmd clientHandle clientsList roomsList ("SAY", param) = do
@@ -32,9 +16,20 @@
return ()
handleCmd clientHandle clientsList roomsList ("CREATE", [roomname]) = do
- manipState roomsList (\x -> (RoomInfo roomname ""):x)
- manipState clientsList (\x -> map (\xc -> if (clientHandle == handle xc) then xc{isMaster = True, game = roomname} else xc) x)
+ manipState2 clientsList roomsList (hcCreate)
sendMsg clientHandle ("JOINED " ++ roomname)
+ where
+ hcCreate ci ri = if (null $ filter (\ xr -> roomname == name xr) ri) then
+ (map
+ (\ xc
+ -> if (clientHandle == handle xc) then
+ xc {isMaster = True, room = roomname}
+ else
+ xc)
+ ci,
+ (RoomInfo roomname "") : ri)
+ else
+ (ci, ri)
handleCmd clientHandle clientsList roomsList ("LIST", []) = do
rl <- atomically $ readTVar roomsList
@@ -57,7 +52,7 @@
clientsList <- atomically $ newTVar[]
roomsList <- atomically $ newTVar[]
bracket
- (listenOn $ PortNumber 46631)
+ (listenOn $ Service "hedgewars")
(sClose)
(loop clientsList roomsList)
where