Server now fully supports game options
authorunc0rr
Wed, 08 Oct 2008 15:42:09 +0000
changeset 1317 13cf8c5a7428
parent 1316 50514e45d0b5
child 1318 18da1c5e960d
Server now fully supports game options
netserver/HWProto.hs
netserver/Miscutils.hs
--- a/netserver/HWProto.hs	Wed Oct 08 13:58:27 2008 +0000
+++ b/netserver/HWProto.hs	Wed Oct 08 15:42:09 2008 +0000
@@ -5,8 +5,10 @@
 import Data.Word
 import Miscutils
 import Maybe (fromMaybe, fromJust)
+import qualified Data.Map as Map
 
 answerBadCmd = [(clientOnly, ["ERROR", "Bad command, state or incorrect parameter"])]
+answerNotMaster = [(clientOnly, ["ERROR", "You cannot configure room parameters"])]
 answerQuit = [(clientOnly, ["off"])]
 answerAbandoned = [(sameRoom, ["BYE"])]
 answerQuitInform nick = [(othersInRoom, ["LEFT", nick])]
@@ -22,7 +24,11 @@
 answerNoRoom = [(clientOnly, ["WARNING", "There's no room with that name"])]
 answerWrongPassword = [(clientOnly, ["WARNING", "Wrong password"])]
 answerChatString nick msg = [(othersInRoom, ["CHAT_STRING", nick, msg])]
-
+answerConfigParam paramName paramStrs = [(othersInRoom, "CONFIG_PARAM" : paramName : paramStrs)]
+answerFullConfig room = map toAnswer (Map.toList $ params room)
+	where
+		toAnswer (paramName, paramStrs)=
+			(clientOnly, "CONFIG_PARAM" : paramName : paramStrs)
 
 -- Main state-independent cmd handler
 handleCmd :: CmdHandler
@@ -79,7 +85,7 @@
 	if haveSameRoom then
 		(noChangeClients, noChangeRooms, answerRoomExists)
 	else
-		(modifyClient client{room = newRoom, isMaster = True}, addRoom (RoomInfo newRoom roomPassword []), answerJoined $ nick client)
+		(modifyClient client{room = newRoom, isMaster = True}, addRoom (RoomInfo newRoom roomPassword (protocol client) [] Map.empty), answerJoined $ nick client)
 	where
 		haveSameRoom = not . null $ filter (\room -> newRoom == name room) rooms
 
@@ -89,13 +95,14 @@
 handleCmd_noRoom client clients rooms ["JOIN", roomName, roomPassword] =
 	if noSuchRoom then
 		(noChangeClients, noChangeRooms, answerNoRoom)
-	else if roomPassword /= password (roomByName roomName rooms) then
+	else if roomPassword /= password joinRoom then
 		(noChangeClients, noChangeRooms, answerWrongPassword)
 	else
-		(modifyClient client{room = roomName}, noChangeRooms, (answerJoined $ nick client) ++ answerNicks)
+		(modifyClient client{room = roomName}, noChangeRooms, (answerJoined $ nick client) ++ answerNicks ++ answerFullConfig joinRoom)
 	where
 		noSuchRoom = null $ filter (\room -> roomName == name room) rooms
 		answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ filter (\ci -> room ci == roomName) clients))]
+		joinRoom = roomByName roomName rooms
 
 handleCmd_noRoom client clients rooms ["JOIN", roomName] =
 	handleCmd_noRoom client clients rooms ["JOIN", roomName, ""]
@@ -105,7 +112,13 @@
 
 -- 'inRoom' clients state command handlers
 handleCmd_inRoom :: CmdHandler
+handleCmd_inRoom client _ _ ["CHAT_STRING", _, msg] =
+	(noChangeClients, noChangeRooms, answerChatString (nick client) msg)
 
-handleCmd_inRoom client _ _ ["CHAT_STRING", _, msg] = (noChangeClients, noChangeRooms, answerChatString (nick client) msg)
+handleCmd_inRoom client _ _ ("CONFIG_PARAM":paramName:paramStrs) =
+	if isMaster client then
+		(noChangeClients, changeRoomConfig (room client) paramName paramStrs, answerConfigParam paramName paramStrs)
+	else
+		(noChangeClients, noChangeRooms, answerNotMaster)
 
 handleCmd_inRoom _ _ _ _ = (noChangeClients, noChangeRooms, answerBadCmd)
--- a/netserver/Miscutils.hs	Wed Oct 08 13:58:27 2008 +0000
+++ b/netserver/Miscutils.hs	Wed Oct 08 15:42:09 2008 +0000
@@ -6,7 +6,7 @@
 import Data.Char
 import Data.List
 import Maybe (fromJust)
-
+import qualified Data.Map as Map
 
 data ClientInfo =
  ClientInfo
@@ -22,10 +22,14 @@
 instance Eq ClientInfo where
 	a1 == a2 = handle a1 == handle a2
 
+data HedgehogInfo =
+	HedgehogInfo String String
+
 data TeamInfo =
 	TeamInfo
 	{
-		teamname :: String
+		teamname :: String,
+		hedgehogs :: [HedgehogInfo]
 	}
 
 data RoomInfo =
@@ -33,7 +37,9 @@
 	{
 		name :: String,
 		password :: String,
-		teams :: [TeamInfo]
+		roomProto :: Word16,
+		teams :: [TeamInfo],
+		params :: Map.Map String [String]
 	}
 
 type ClientsTransform = [ClientInfo] -> [ClientInfo]
@@ -90,3 +96,11 @@
 
 removeRoom :: String -> RoomsTransform
 removeRoom roomname rooms = filter (\rm -> roomname /= name rm) rooms
+
+changeRoomConfig :: String -> String -> [String] -> RoomsTransform
+changeRoomConfig _ _ _ [] = error "changeRoomConfig: no such room"
+changeRoomConfig roomName paramName paramStrs (room:rooms) =
+	if roomName == name room then
+		room{params = Map.insert paramName paramStrs (params room)} : rooms
+	else
+		room : changeRoomConfig roomName paramName paramStrs rooms