OSDN Git Service

ルームの設定をmysqlに移行した
authorkonekoneko <jbh03215@hotmail.co.jp>
Fri, 16 Nov 2012 16:11:19 +0000 (01:11 +0900)
committerkonekoneko <jbh03215@hotmail.co.jp>
Fri, 16 Nov 2012 16:11:19 +0000 (01:11 +0900)
chat.js
init.sql

diff --git a/chat.js b/chat.js
index 30e57da..0f736d5 100644 (file)
--- a/chat.js
+++ b/chat.js
@@ -144,6 +144,13 @@ function removeLog(files,callback)
 //RoomInfomationCollecionクラス\r
 function RoomInfomationCollection()\r
 {\r
+       var MySQLPool = new require("./mysql_pool.js");\r
+       var pool = new MySQLPool({\r
+                               host     : config.db_host,\r
+                               user     : config.db_user,\r
+                               password : config.db_password,\r
+                               database : "configure",\r
+                       });\r
        var collection = {};\r
        this.Get = function(rno){\r
                return collection[rno];\r
@@ -174,40 +181,23 @@ function RoomInfomationCollection()
                return retval;\r
        }\r
        this.Update = function(text,callfunc){\r
+               Clear();\r
                async.waterfall([\r
-                       function(callback){\r
-                               fs.open($room_configure_file_name,"w",callback);\r
-                       },\r
-                       function(fd,callback){\r
-                               var buf = new Buffer(text);\r
-                               fs.write(fd,buf,0,Buffer.byteLength(text),null,function(){\r
-                                       callback(null,fd);\r
-                               });\r
+                       function(next){\r
+                               pool.query("TRUNCATE TABLE rooms",null,next);\r
                        },\r
-                       function(fd,callback){\r
-                               fs.close(fd,function(){\r
-                                       GetRoomList(callfunc);\r
-                               });\r
-                       }\r
-               ]);\r
-       }\r
-       function GetRoomList(callback){\r
-               Clear();\r
-               fs.exists($room_configure_file_name,function(exists){\r
-                       if(exists == false)\r
-                       {\r
-                               if(typeof(callback) == "function")\r
-                                       callback();\r
-                               return;\r
-                       }\r
-                       var stream = fs.createReadStream($room_configure_file_name);\r
-                       new lazy(stream)\r
-                               .lines\r
-                               .forEach(function(line){\r
-                                       var token = line.toString().replace(/(\r|\n|\r\n)/gm, "").split(":");\r
+                       function(result,next){\r
+                               lines = text.split("\r\n");\r
+                               var items = new Array();\r
+                               for(var i = 0; i < lines.length; i++)\r
+                               {\r
+                                       if(lines[i] == "")\r
+                                               continue;\r
+                                       var token = lines[i].split(":");\r
                                        if(token.length == 1)\r
                                        {\r
                                                Add(token[0],null,false);\r
+                                               items.push(new Array(token[0],null,false));\r
                                        }\r
                                        else if(token.length == 2)\r
                                        {\r
@@ -216,6 +206,7 @@ function RoomInfomationCollection()
                                                if(pass == "")\r
                                                        pass = null;\r
                                                Add(rno, pass,false);\r
+                                               items.push(new Array(token[0],pass,false));\r
                                        }\r
                                        else if(token.length == 3)\r
                                        {\r
@@ -227,13 +218,28 @@ function RoomInfomationCollection()
                                                if(token[2] == "true")\r
                                                        hiddenlog = true;\r
                                                Add(rno, pass,hiddenlog);\r
+                                               items.push(new Array(token[0],pass,hiddenlog));\r
                                        }\r
-                               })\r
-                               .join(function(){\r
-                                       if(typeof(callback) == "function")\r
-                                               callback();\r
-                               });\r
-               });\r
+                               }\r
+                               pool.query("INSERT INTO rooms VALUES ?",[items],callfunc);\r
+                       }\r
+               ],callfunc);\r
+       }\r
+       function GetRoomList(callback){\r
+               Clear();\r
+               async.waterfall([\r
+                       function(next){\r
+                               pool.query("SELECT * FROM rooms",null,next);\r
+                       },\r
+                       function(result,next){\r
+                               for(var i = 0; i < result.length; i++)\r
+                               {\r
+                                       //MySQLではTINYINTが使われている\r
+                                       Add(result[i].number,result[i].password,result[i].hiddenlog != 0);\r
+                               }\r
+                               next(null,null);\r
+                       }\r
+               ],callback);\r
        }\r
        function Clear(){\r
                collection = {};\r
@@ -570,7 +576,7 @@ function ParseJoin(socket,msg)
                }\r
                else\r
                {\r
-                       socket.emit("error",$not_match_password);\r
+                       socket.emit("error",resource.unmatch_password);\r
                        return;\r
                }\r
        }\r
index 42e084e..f0d7b2b 100644 (file)
--- a/init.sql
+++ b/init.sql
@@ -21,3 +21,7 @@ use configure;
 DROP TABLE IF EXISTS ipbanlist;
 CREATE TABLE ipbanlist(ip VARCHAR(64) NOT NULL,
        type CHAR(1));
+DROP TABLE IF EXISTS rooms;
+CREATE TABLE rooms(number SMALLINT UNSIGNED NOT NULL,
+       password VARCHAR(16),
+       hiddenlog BOOL);