OSDN Git Service

name_hashは被る可能性があるので、primaryではなくindexを指定するようにした
[webchat/WebChat.git] / room.js
diff --git a/room.js b/room.js
index b64a059..950fa36 100644 (file)
--- a/room.js
+++ b/room.js
@@ -1,15 +1,9 @@
 //RoomInfomationCollecion\83N\83\89\83X
 module.exports = function()
 {
+       var _this = this;
        var config = require("./configure.js");
-       var MySQLPool = new require("./mysql_pool.js");
-       var pool = new MySQLPool({
-                               host     : config.db_host,
-                               user     : config.db_user,
-                               password : config.db_password,
-                               port     : config.db_port,
-                               database : config.db_name,
-                       });
+       var RoomInfomationModel = require("./init").GetRoomInfomation;
        var collection = {};
        this.Get = function(rno){
                return collection[rno];
@@ -22,7 +16,7 @@ module.exports = function()
                for(var rno in collection)
                {
                        item={};
-                       item.applyflag = !$rooms.Get(rno).IsVolatile();
+                       item.applyflag = !_this.Get(rno).IsVolatile();
                        item.password = collection[rno].password;
                        if(item.password == null)
                                item.password = "";
@@ -44,7 +38,7 @@ module.exports = function()
                var async = require("async");
                async.waterfall([
                        function(next){
-                               pool.query("TRUNCATE TABLE rooms",null,next);
+                               RoomInfomationModel.drop().done(next);
                        },
                        function(result,next){
                                var util = require("util");
@@ -71,7 +65,8 @@ module.exports = function()
                                        Add(rno,password,romonly);
                                        items.push(new Array(rno,password,romonly));
                                }
-                               pool.query("INSERT INTO rooms VALUES ?",[items],callfunc);
+                               newRoom = RoomInfomationModel.build(items);
+                               newRoom.save().done(next);
                        }
                ],callfunc);
        }
@@ -80,13 +75,12 @@ module.exports = function()
                var async = require("async");
                async.waterfall([
                        function(next){
-                               pool.query("SELECT * FROM rooms",null,next);
+                               RoomInfomationModel.findAll().done(next);
                        },
                        function(result,next){
                                for(var i = 0; i < result.length; i++)
                                {
-                                       //MySQL\82Å\82ÍTINYINT\82ª\8eg\82í\82ê\82Ä\82¢\82é
-                                       Add(result[i].number,result[i].password,result[i].hiddenlog != 0);
+                                       Add(result[i].number,result[i].password,result[i].hiddenlog);
                                }
                                next(null,null);
                        }
@@ -104,7 +98,7 @@ module.exports = function()
                        collection[rno].owner = $system_name;
        };
        var $gc_interval_id = setInterval(function(){
-               for(var rno in this.rom_list)
+               for(var rno in _this.rom_list)
                        collection[rno].GCRomList();
        },$gc_time_interval);
        GetRoomList();
@@ -113,6 +107,7 @@ module.exports = function()
 //RoomInfomation\83N\83\89\83X
 function RoomInfomation(pass,hiddenlogflag)
 {
+       var _this = this;
        this.password = pass;
        this.rom_list = {};
        this.authed_list = {};
@@ -121,90 +116,90 @@ function RoomInfomation(pass,hiddenlogflag)
        this.hiddenlog = hiddenlogflag;
        this.GetConfig = function(){
                var roomconfig = {};
-               if(this.IsVolatile() == false)
+               if(_this.IsVolatile() == false)
                {
-                       if(this.IsFixedPassword())
+                       if(_this.IsFixedPassword())
                                roomconfig.type = 2;
-                       else if(this.IsHiddenLogFromRom())
+                       else if(_this.IsHiddenLogFromRom())
                                roomconfig.type = 3;
                        else
                                roomconfig.type = 1;
-                       roomconfig.IsOwned = !this.IsFirstAuth();
+                       roomconfig.IsOwned = !_this.IsFirstAuth();
                }else{
                        roomconfig.type = 0;
                }
                return roomconfig;
        }
        this.IsVolatile = function(){
-               return this.owner == null &&
-                       this.password == null &&
-                       this.time == null &&
-                       this.hiddenlog == null;
+               return _this.owner == null &&
+                       _this.password == null &&
+                       _this.time == null &&
+                       _this.hiddenlog == null;
        }
        this.GetRomCount = function(){
                var count = 0;
-               for(var key in this.rom_list)
+               for(var key in _this.rom_list)
                        count++;
                return count;
        };
        this.AddRom = function(ip){
                var date = new Date();
-               this.rom_list[ip] = {time:date.getTime()};
+               _this.rom_list[ip] = {time:date.getTime()};
        };
        this.RemoveRom = function(ip){
-               delete this.rom_list[ip];
+               delete _this.rom_list[ip];
        };
        this.Reset = function(owner){
                var date = new Date();
                var time = date.getTime();
-               this.password = null;
-               this.authed_list = {};
-               this.owner = owner;
-               this.time = time;
+               _this.password = null;
+               _this.authed_list = {};
+               _this.owner = owner;
+               _this.time = time;
        };
        this.IsFirstAuth = function(){
-               return this.owner == null;
+               return _this.owner == null;
        };
        this.IsAuthed = function(name){
-               return name == this.owner ||
-                       name in this.authed_list;
+               return name == _this.owner ||
+                       name in _this.authed_list;
        };
        this.IsHiddenLogFromRom = function(){
-               return this.hiddenlog;
+               return _this.hiddenlog;
        };
        this.IsFixedPassword = function(){
-               return this.owner == $system_name;
+               return _this.owner == $system_name;
        };
        this.IsOwner = function(name){
-               return this.owner == name;
+               return _this.owner == name;
        };
        this.IsTimeout = function(){
                var date = new Date();
                var current_time = date.getTime();
-               return !this.IsFixedPassword() &&
-                       current_time - this.time >= $reset_password_diff;
+               return !_this.IsFixedPassword() &&
+                       current_time - _this.time >= $reset_password_diff;
        };
        this.RemoveAuth = function(name)
        {
-               delete this.authed_list[name];
+               delete _this.authed_list[name];
        };
        this.Auth = function(name,password){
-               if(this.password != password)
+               if(_this.password != password)
                        return false;
                var date = new Date();
                var time = date.getTime();
-               this.time = time;
-               this.authed_list[name] = "";
+               _this.time = time;
+               _this.authed_list[name] = "";
                return true;
        };
        this.SetPassword = function(owner,password){
-               if(owner == this.owner &&
-                       !this.IsFixedPassword() &&
-                       !this.IsHiddenLogFromRom())
+               if(owner == _this.owner &&
+                       !_this.IsFixedPassword() &&
+                       !_this.IsHiddenLogFromRom())
                {
                        var date = new Date();
-                       this.time = date.getTime();
-                       this.password = password;
+                       _this.time = date.getTime();
+                       _this.password = password;
                        return true;
                }
                return false;
@@ -212,10 +207,10 @@ function RoomInfomation(pass,hiddenlogflag)
        this.GCRomList = function(){
                var date = new Date();
                var current_time = date.getTime();
-               for(var ip in this.rom_list)
+               for(var ip in _this.rom_list)
                {
-                       if(current_time - this.rom_list[ip].time >= $gc_time_interval)
-                               delete this.rom_list[ip];
+                       if(current_time - _this.rom_list[ip].time >= $gc_time_interval)
+                               delete _this.rom_list[ip];
                }
        };
 }