From: konekoneko Date: Fri, 16 Nov 2012 16:11:19 +0000 (+0900) Subject: ルームの設定をmysqlに移行した X-Git-Url: http://git.osdn.jp/view?a=commitdiff_plain;h=c1cd4db993f0b59e5355ac3a9d120ea784ff8c64;p=webchat%2FWebChat.git ルームの設定をmysqlに移行した --- diff --git a/chat.js b/chat.js index 30e57da..0f736d5 100644 --- a/chat.js +++ b/chat.js @@ -144,6 +144,13 @@ function removeLog(files,callback) //RoomInfomationCollecionクラス function RoomInfomationCollection() { + var MySQLPool = new require("./mysql_pool.js"); + var pool = new MySQLPool({ + host : config.db_host, + user : config.db_user, + password : config.db_password, + database : "configure", + }); var collection = {}; this.Get = function(rno){ return collection[rno]; @@ -174,40 +181,23 @@ function RoomInfomationCollection() return retval; } this.Update = function(text,callfunc){ + Clear(); async.waterfall([ - function(callback){ - fs.open($room_configure_file_name,"w",callback); - }, - function(fd,callback){ - var buf = new Buffer(text); - fs.write(fd,buf,0,Buffer.byteLength(text),null,function(){ - callback(null,fd); - }); + function(next){ + pool.query("TRUNCATE TABLE rooms",null,next); }, - function(fd,callback){ - fs.close(fd,function(){ - GetRoomList(callfunc); - }); - } - ]); - } - function GetRoomList(callback){ - Clear(); - fs.exists($room_configure_file_name,function(exists){ - if(exists == false) - { - if(typeof(callback) == "function") - callback(); - return; - } - var stream = fs.createReadStream($room_configure_file_name); - new lazy(stream) - .lines - .forEach(function(line){ - var token = line.toString().replace(/(\r|\n|\r\n)/gm, "").split(":"); + function(result,next){ + lines = text.split("\r\n"); + var items = new Array(); + for(var i = 0; i < lines.length; i++) + { + if(lines[i] == "") + continue; + var token = lines[i].split(":"); if(token.length == 1) { Add(token[0],null,false); + items.push(new Array(token[0],null,false)); } else if(token.length == 2) { @@ -216,6 +206,7 @@ function RoomInfomationCollection() if(pass == "") pass = null; Add(rno, pass,false); + items.push(new Array(token[0],pass,false)); } else if(token.length == 3) { @@ -227,13 +218,28 @@ function RoomInfomationCollection() if(token[2] == "true") hiddenlog = true; Add(rno, pass,hiddenlog); + items.push(new Array(token[0],pass,hiddenlog)); } - }) - .join(function(){ - if(typeof(callback) == "function") - callback(); - }); - }); + } + pool.query("INSERT INTO rooms VALUES ?",[items],callfunc); + } + ],callfunc); + } + function GetRoomList(callback){ + Clear(); + async.waterfall([ + function(next){ + pool.query("SELECT * FROM rooms",null,next); + }, + function(result,next){ + for(var i = 0; i < result.length; i++) + { + //MySQLではTINYINTが使われている + Add(result[i].number,result[i].password,result[i].hiddenlog != 0); + } + next(null,null); + } + ],callback); } function Clear(){ collection = {}; @@ -570,7 +576,7 @@ function ParseJoin(socket,msg) } else { - socket.emit("error",$not_match_password); + socket.emit("error",resource.unmatch_password); return; } } diff --git a/init.sql b/init.sql index 42e084e..f0d7b2b 100644 --- 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);