Handle the case when the room is already created by someone else
authorunc0rr
Sat, 19 Apr 2008 19:34:19 +0000
changeset 852 f756a1d3324c
parent 851 8ffa4ad0d8ea
child 853 0b4a23795530
Handle the case when the room is already created by someone else
netserver/Miscutils.hs
netserver/hwserv.hs
--- a/netserver/Miscutils.hs	Sat Apr 19 19:29:58 2008 +0000
+++ b/netserver/Miscutils.hs	Sat Apr 19 19:34:19 2008 +0000
@@ -42,12 +42,13 @@
 			ls <- readTVar state
 			writeTVar state $ op ls
 
-manipState2 :: TVar[ClientInfo] -> TVar[RoomInfo] -> ([ClientInfo] -> [RoomInfo] -> ([ClientInfo], [RoomInfo])) -> IO()
+manipState2 :: TVar[ClientInfo] -> TVar[RoomInfo] -> ([ClientInfo] -> [RoomInfo] -> ([ClientInfo], [RoomInfo], Bool)) -> IO Bool
 manipState2 state1 state2 op =
 	atomically $ do
 			ls1 <- readTVar state1
 			ls2 <- readTVar state2
-			let (ol1, ol2) = op ls1 ls2
+			let (ol1, ol2, res) = op ls1 ls2
 			writeTVar state1 ol1
 			writeTVar state2 ol2
+			return res
 	
--- a/netserver/hwserv.hs	Sat Apr 19 19:29:58 2008 +0000
+++ b/netserver/hwserv.hs	Sat Apr 19 19:34:19 2008 +0000
@@ -16,8 +16,8 @@
 		return ()
 
 handleCmd clientHandle clientsList roomsList ("CREATE", [roomname]) = do
-		manipState2 clientsList roomsList (hcCreate)
-		sendMsg clientHandle ("JOINED " ++ roomname)
+		res <- manipState2 clientsList roomsList (hcCreate)
+		if res then sendMsg clientHandle ("JOINED " ++ roomname) else sendMsg clientHandle "Already exists"
 		where
 			hcCreate ci ri = if (null $ filter (\ xr -> roomname == name xr) ri) then
 				(map
@@ -27,9 +27,9 @@
 							else
 								xc)
 					ci,
-					(RoomInfo roomname "") : ri)
+					(RoomInfo roomname "") : ri, True)
 				else
-					(ci, ri)
+					(ci, ri, False)
 
 handleCmd clientHandle clientsList roomsList ("LIST", []) = do
 		rl <- atomically $ readTVar roomsList