Allow to save and delete room config in room
authorunc0rr
Thu, 13 Mar 2014 23:25:31 +0400
changeset 10194 7025bd3c3131
parent 10193 d7cd5b43588f
child 10195 d1c23bb73346
Allow to save and delete room config in room
gameServer/CoreTypes.hs
gameServer/HWProtoCore.hs
gameServer/HWProtoInRoomState.hs
gameServer/HandlerUtils.hs
--- a/gameServer/CoreTypes.hs	Wed Mar 12 22:40:49 2014 -0400
+++ b/gameServer/CoreTypes.hs	Thu Mar 13 23:25:31 2014 +0400
@@ -182,6 +182,7 @@
         Nothing
         []
 
+
 data RoomInfo =
     RoomInfo
     {
@@ -201,7 +202,8 @@
         voting :: Maybe Voting,
         roomBansList :: ![B.ByteString],
         mapParams :: Map.Map B.ByteString B.ByteString,
-        params :: Map.Map B.ByteString [B.ByteString]
+        params :: Map.Map B.ByteString [B.ByteString],
+        roomSaves :: Map.Map B.ByteString (Map.Map B.ByteString B.ByteString, Map.Map B.ByteString [B.ByteString])
     }
 
 newRoom :: RoomInfo
@@ -232,6 +234,7 @@
                 ["AMMO", "SCHEME", "SCRIPT", "THEME"]
                 [["Default"], ["Default"], ["Normal"], ["avematan"]]
         )
+        Map.empty
 
 
 data StatisticsInfo =
--- a/gameServer/HWProtoCore.hs	Wed Mar 12 22:40:49 2014 -0400
+++ b/gameServer/HWProtoCore.hs	Thu Mar 13 23:25:31 2014 +0400
@@ -36,6 +36,8 @@
 handleCmd ["CMD", parameters] = uncurry h $ extractParameters parameters
     where
         h "DELEGATE" n | not $ B.null n = handleCmd ["DELEGATE", n]
+        h "SAVE" n | not $ B.null n = handleCmd ["SAVE", n]
+        h "DELETE" n | not $ B.null n = handleCmd ["DELETE", n]
         h "STATS" _ = handleCmd ["STATS"]
         h "PART" m | not $ B.null m = handleCmd ["PART", m]
                    | otherwise = handleCmd ["PART"]
--- a/gameServer/HWProtoInRoomState.hs	Wed Mar 12 22:40:49 2014 -0400
+++ b/gameServer/HWProtoInRoomState.hs	Thu Mar 13 23:25:31 2014 +0400
@@ -225,9 +225,7 @@
         : gs
 
 
-handleCmd_inRoom ["START_GAME"] = do
-    cl <- thisClient
-    if isMaster cl then startGame else return []
+handleCmd_inRoom ["START_GAME"] = roomAdminOnly startGame
 
 handleCmd_inRoom ["EM", msg] = do
     cl <- thisClient
@@ -269,43 +267,25 @@
 handleCmd_inRoom ["ROUNDFINISHED"] =
     handleCmd_inRoom ["ROUNDFINISHED", "1"]
 
-handleCmd_inRoom ["TOGGLE_RESTRICT_JOINS"] = do
-    cl <- thisClient
-    return $
-        if not $ isMaster cl then
-            [ProtocolError $ loc "Not room master"]
-        else
-            [ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})]
+handleCmd_inRoom ["TOGGLE_RESTRICT_JOINS"] = roomAdminOnly $
+    return [ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})]
 
 
-handleCmd_inRoom ["TOGGLE_RESTRICT_TEAMS"] = do
-    cl <- thisClient
-    return $
-        if not $ isMaster cl then
-            [ProtocolError $ loc "Not room master"]
-        else
-            [ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})]
+handleCmd_inRoom ["TOGGLE_RESTRICT_TEAMS"] = roomAdminOnly $
+    return [ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})]
 
 
-handleCmd_inRoom ["TOGGLE_REGISTERED_ONLY"] = do
-    cl <- thisClient
-    return $
-        if not $ isMaster cl then
-            [ProtocolError $ loc "Not room master"]
-        else
-            [ModifyRoom (\r -> r{isRegisteredOnly = not $ isRegisteredOnly r})]
+handleCmd_inRoom ["TOGGLE_REGISTERED_ONLY"] = roomAdminOnly $
+    return [ModifyRoom (\r -> r{isRegisteredOnly = not $ isRegisteredOnly r})]
 
 
-handleCmd_inRoom ["ROOM_NAME", newName] = do
+handleCmd_inRoom ["ROOM_NAME", newName] = roomAdminOnly $ do
     cl <- thisClient
     rs <- allRoomInfos
     rm <- thisRoom
     chans <- sameProtoChans
 
     return $
-        if not $ isMaster cl then
-            [ProtocolError $ loc "Not room master"]
-        else
         if illegalName newName then
             [Warning $ loc "Illegal room name"]
         else
@@ -321,10 +301,9 @@
         roomUpdate r = r{name = newName}
 
 
-handleCmd_inRoom ["KICK", kickNick] = do
+handleCmd_inRoom ["KICK", kickNick] = roomAdminOnly $ do
     (thisClientId, rnc) <- ask
     maybeClientId <- clientByNick kickNick
-    master <- liftM isMaster thisClient
     rm <- thisRoom
     let kickId = fromJust maybeClientId
     let kickCl = rnc `client` kickId
@@ -332,8 +311,7 @@
     let notOnly2Players = (length . group . sort . map teamowner . teams $ rm) > 2
     return
         [KickRoomClient kickId |
-            master
-            && isJust maybeClientId
+            isJust maybeClientId
             && (kickId /= thisClientId)
             && sameRoom
             && ((isNothing $ gameInfo rm) || notOnly2Players || teamsInGame kickCl == 0)
@@ -383,13 +361,11 @@
     s <- roomClientsChans
     return [AnswerClients s ["CHAT", n, B.unwords $ "/rnd" : rs], Random s rs]
 
-handleCmd_inRoom ["FIX"] = do
-    cl <- thisClient
-    return [ModifyRoom (\r -> r{isSpecial = True}) | isAdministrator cl]
+handleCmd_inRoom ["FIX"] = serverAdminOnly $
+    return [ModifyRoom (\r -> r{isSpecial = True})]
 
-handleCmd_inRoom ["UNFIX"] = do
-    cl <- thisClient
-    return [ModifyRoom (\r -> r{isSpecial = False}) | isAdministrator cl]
+handleCmd_inRoom ["UNFIX"] = serverAdminOnly $
+    return [ModifyRoom (\r -> r{isSpecial = False})]
 
 handleCmd_inRoom ["GREETING", msg] = do
     cl <- thisClient
@@ -429,6 +405,14 @@
         else
         return [AnswerClients [sendChan cl] ["CHAT", "[server]", "vote: 'yes' or 'no'"]]
 
+
+handleCmd_inRoom ["SAVE", stateName] = serverAdminOnly $ do
+    return [ModifyRoom $ \r -> r{roomSaves = Map.insert stateName (mapParams r, params r) (roomSaves r)}]
+
+handleCmd_inRoom ["DELETE", stateName] = serverAdminOnly $ do
+    return [ModifyRoom $ \r -> r{roomSaves = Map.delete stateName (roomSaves r)}]
+
+
 handleCmd_inRoom ["LIST"] = return [] -- for old clients (<= 0.9.17)
 
 handleCmd_inRoom (s:_) = return [ProtocolError $ "Incorrect command '" `B.append` s `B.append` "' (state: in room)"]
--- a/gameServer/HandlerUtils.hs	Wed Mar 12 22:40:49 2014 -0400
+++ b/gameServer/HandlerUtils.hs	Thu Mar 13 23:25:31 2014 +0400
@@ -66,3 +66,10 @@
     let allClientIDs = allClients rnc
     return $ find (\clId -> let cl = client rnc clId in n == nick cl && not (isChecker cl)) allClientIDs
 
+
+roomAdminOnly :: Reader (ClientIndex, IRnC) [Action] -> Reader (ClientIndex, IRnC) [Action]
+roomAdminOnly h = thisClient >>= \cl -> if isMaster cl then h else return []
+
+
+serverAdminOnly :: Reader (ClientIndex, IRnC) [Action] -> Reader (ClientIndex, IRnC) [Action]
+serverAdminOnly h = thisClient >>= \cl -> if isAdministrator cl then h else return []