OSDN Git Service

RoomInfomationクラスに一部メソッドを移動した
authorkonekoneko <jbh03215@gmail.com>
Mon, 1 Oct 2012 12:19:24 +0000 (21:19 +0900)
committerkonekoneko <jbh03215@gmail.com>
Mon, 1 Oct 2012 12:19:24 +0000 (21:19 +0900)
chatServer.js

index 6cf9da8..95bdb36 100644 (file)
@@ -198,15 +198,18 @@ for(var i = 0; i < $max_room_number; i++)
 \r
                var rno = GetRoomNumberFromName(socket.namespace.name);\r
                var roomconfig = {};\r
-               if($rooms.IsFixedPassword(rno))\r
-                       roomconfig.type = 2;\r
-               else if($rooms.IsHiddenLogFromRom(rno))\r
-                       roomconfig.type = 3;\r
-               else if($rooms.IsContains(rno))\r
-                       roomconfig.type = 1;\r
-               else\r
+               if($rooms.IsContains(rno))\r
+               {\r
+                       if($rooms.Get(rno).IsFixedPassword())\r
+                               roomconfig.type = 2;\r
+                       else if($rooms.Get(rno).IsHiddenLogFromRom())\r
+                               roomconfig.type = 3;\r
+                       else\r
+                               roomconfig.type = 1;\r
+                       roomconfig.IsOwned = !$rooms.Get(rno).IsFirstAuth();\r
+               }else{\r
                        roomconfig.type = 0;\r
-               roomconfig.IsOwned = !$rooms.IsFirstAuth(rno);\r
+               }\r
                socket.json.emit("send roominfo",roomconfig);\r
 \r
                socket.on("get pastLogList", function (msg) {\r
@@ -248,8 +251,10 @@ function ParseAuthorization(handshakeData, callback)
                var sessionID = parseCookie(cookie)["connect.sid"];\r
                sessionStore.get(sessionID, function (err, session) {\r
                        var result = null;\r
-                       if (err || ipbanlist.IsBaned(handshakeData.address.address))\r
+                       if (ipbanlist.IsBaned(handshakeData.address.address))\r
                                result = "failed get from session store";\r
+                       else if(err)\r
+                               result = err;\r
                        else if(handshakeData.query.token != session.items.token)\r
                                result = "invaild token";\r
                        sessionStore.destroy(sessionID);\r
@@ -272,7 +277,7 @@ function ParseSetPassword(socket,msg)
                name:$system_name,\r
                message:null,\r
        };\r
-       if($rooms.SetPassword(rno,msg.owner,msg.password))\r
+       if($rooms.IsContains(rno) && $rooms.Get(rno).SetPassword(msg.owner,msg.password))\r
                newMeg.message = $password_setted_message;\r
        else\r
                newMeg.message = $failed_set_password_message;\r
@@ -292,13 +297,13 @@ function ParseJoin(socket,msg)
        var rno = GetRoomNumberFromName(socket.namespace.name);\r
        if($rooms.IsContains(rno))\r
        {\r
-               if($rooms.IsTimeout(rno) ||\r
-                       $rooms.IsFirstAuth(rno))\r
+               if($rooms.Get(rno).IsTimeout() ||\r
+                       $rooms.Get(rno).IsFirstAuth())\r
                {\r
-                       $rooms.Reset(rno,msg.name);\r
+                       $rooms.Get(rno).Reset(msg.name);\r
                        ParseGetPastLog(socket,util.format($log_file_name,rno));\r
                }\r
-               else if($rooms.Auth(rno,msg.name,msg.password))\r
+               else if($rooms.Get(rno).Auth(msg.name,msg.password))\r
                {\r
                        ParseGetPastLog(socket,util.format($log_file_name,rno));\r
                }\r
@@ -334,16 +339,16 @@ function ParseQuit(socket,msg)
        };\r
        if($rooms.IsContains(rno))\r
        {\r
-               if($rooms.IsOwner(rno,msg.name))\r
+               if($rooms.Get(rno).IsOwner(msg.name))\r
                {\r
-                       $rooms.Reset(rno,null);\r
+                       $rooms.Get(rno).Reset(null);\r
                        ParseSendMsg(socket,newMeg);\r
                }\r
-               if(!$rooms.IsFirstAuth(rno) &&\r
-                       !$rooms.IsAuthed(rno,msg.name))\r
+               if(!$rooms.Get(rno).IsFirstAuth() &&\r
+                       !$rooms.Get(rno).IsAuthed(msg.name))\r
                        return;\r
                else\r
-                       $rooms.RemoveAuth(rno,msg.name);\r
+                       $rooms.Get(rno).RemoveAuth(msg.name);\r
        }\r
 \r
        newMeg.message = util.format("/quitedby %s",msg.name);\r
@@ -366,8 +371,8 @@ function ParseSendMsg(socket,msg)
 \r
        if(msg.name != $system_name && \r
                $rooms.IsContains(rno) &&\r
-               !$rooms.IsAuthed(rno,msg.name) &&\r
-               !$rooms.IsOwner(rno,msg.name))\r
+               !$rooms.Get(rno).IsAuthed(msg.name) &&\r
+               !$rooms.Get(rno).IsOwner(rno,msg.name))\r
        {\r
                return;\r
        }\r
@@ -530,81 +535,12 @@ function CreateMessageFromText(text)
 function RoomInfomationCollection()\r
 {\r
        var collection = {};\r
-       this.Reset = function(rno,owner){\r
-               var date = new Date();\r
-               var time = date.getTime();\r
-               collection[rno].password = null;\r
-               collection[rno].authed_list = {};\r
-               collection[rno].owner = owner;\r
-               collection[rno].time = time;\r
-               console.log(util.format("password is reseted in %s",rno));\r
-       };\r
+       this.Get = function(rno){\r
+               return collection[rno];\r
+       }\r
        this.IsContains = function(rno){\r
                return rno in collection;\r
        };\r
-       this.IsFirstAuth = function(rno){\r
-               if(!this.IsContains(rno) || typeof(collection[rno].owner) == "undefined")\r
-                       return false;\r
-               return collection[rno].owner == null;\r
-       }\r
-       this.IsAuthed = function(rno,name){\r
-               if(!this.IsContains(rno))\r
-                       return false;\r
-               return name == collection[rno].owner ||\r
-                       name in collection[rno].authed_list;\r
-       };\r
-       this.IsHiddenLogFromRom = function(rno){\r
-               if(!this.IsContains(rno))\r
-                       return false;\r
-               return collection[rno].hiddenlog;\r
-       };\r
-       this.IsFixedPassword = function(rno){\r
-               if(!this.IsContains(rno))\r
-                       return false;\r
-               return collection[rno].owner == $system_name;\r
-       };\r
-       this.IsOwner = function(rno,name){\r
-               return this.IsContains(rno) &&\r
-                       typeof(collection[rno].owner) != "undefined" &&\r
-                       collection[rno].owner == name;\r
-       }\r
-       this.IsTimeout = function(rno){\r
-               var date = new Date();\r
-               var time = date.getTime();\r
-               return this.IsContains(rno) &&\r
-                       !this.IsFixedPassword(rno) &&\r
-                       (typeof(collection[rno].time) != "undefined" &&\r
-                       time - collection[rno].time >= $reset_password_diff);\r
-       };\r
-       this.RemoveAuth = function(rno,name)\r
-       {\r
-               delete collection[rno].authed_list[name];\r
-       }\r
-       this.Auth = function(rno,name,password){\r
-               if(typeof(collection[rno].password) != "undefined" &&\r
-                       collection[rno].password != password)\r
-                       return false;\r
-               var date = new Date();\r
-               var time = date.getTime();\r
-               collection[rno].time = time;\r
-               collection[rno].authed_list[name] = "";\r
-               return true;\r
-       }\r
-       this.SetPassword = function(rno,owner,password){\r
-               if(this.IsContains(rno) && \r
-                       owner == collection[rno].owner &&\r
-                       !this.IsFixedPassword(rno) &&\r
-                       !this.IsHiddenLogFromRom(rno))\r
-               {\r
-                       var date = new Date();\r
-                       collection[rno].time = date.getTime();\r
-                       collection[rno].password = password;\r
-                       \r
-                       console.log(util.format("password is seted to %s in %s",password,rno));\r
-                       return true;\r
-               }\r
-               return false;\r
-       };\r
        this.GetString = function(){\r
                var retval = "";\r
                for(var rno in collection)\r
@@ -691,11 +627,7 @@ function RoomInfomationCollection()
                collection = {};\r
        };\r
        function Add(rno,pass,hiddenlogflag){\r
-               collection[rno] = {time : null,\r
-                       password : pass,\r
-                       owner : null,\r
-                       hiddenlog : hiddenlogflag,\r
-                       authed_list : {}};\r
+               collection[rno] = new RoomInfomation(pass,hiddenlogflag);\r
                if(pass != null)\r
                        collection[rno].owner = $system_name;\r
        };\r
@@ -703,6 +635,71 @@ function RoomInfomationCollection()
        GetRoomList();\r
 }\r
 \r
+//RoomInfomationクラス\r
+function RoomInfomation(pass,hiddenlogflag)\r
+{\r
+       this.password = pass;\r
+       this.authed_list = {};\r
+       this.owner = null;\r
+       this.time = null;\r
+       this.hiddenlog = hiddenlogflag;\r
+       this.Reset = function(owner){\r
+               var date = new Date();\r
+               var time = date.getTime();\r
+               this.password = null;\r
+               this.authed_list = {};\r
+               this.owner = owner;\r
+               this.time = time;\r
+       };\r
+       this.IsFirstAuth = function(){\r
+               return this.owner == null;\r
+       };\r
+       this.IsAuthed = function(name){\r
+               return name == this.owner ||\r
+                       name in this.authed_list;\r
+       };\r
+       this.IsHiddenLogFromRom = function(){\r
+               return this.hiddenlog;\r
+       };\r
+       this.IsFixedPassword = function(){\r
+               return this.owner == $system_name;\r
+       };\r
+       this.IsOwner = function(name){\r
+               return this.owner == name;\r
+       };\r
+       this.IsTimeout = function(){\r
+               var date = new Date();\r
+               var current_time = date.getTime();\r
+               return !this.IsFixedPassword() &&\r
+                       current_time - this.time >= $reset_password_diff;\r
+       };\r
+       this.RemoveAuth = function(name)\r
+       {\r
+               delete this.authed_list[name];\r
+       };\r
+       this.Auth = function(name,password){\r
+               if(this.password != password)\r
+                       return false;\r
+               var date = new Date();\r
+               var time = date.getTime();\r
+               this.time = time;\r
+               this.authed_list[name] = "";\r
+               return true;\r
+       };\r
+       this.SetPassword = function(owner,password){\r
+               if(owner == this.owner &&\r
+                       !this.IsFixedPassword() &&\r
+                       !this.IsHiddenLogFromRom())\r
+               {\r
+                       var date = new Date();\r
+                       this.time = date.getTime();\r
+                       this.password = password;\r
+                       return true;\r
+               }\r
+               return false;\r
+       };\r
+}\r
+\r
 //IPBANクラス\r
 function IpBanCollecion()\r
 {\r