OSDN Git Service

一部クラスを別ファイルに分離した
[webchat/WebChat.git] / room.js
diff --git a/room.js b/room.js
new file mode 100644 (file)
index 0000000..c295da5
--- /dev/null
+++ b/room.js
@@ -0,0 +1,221 @@
+//RoomInfomationCollecion\83N\83\89\83X
+module.exports.RoomInfomationCollection = function()
+{
+       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 collection = {};
+       this.Get = function(rno){
+               return collection[rno];
+       }
+       this.IsContains = function(rno){
+               return rno in collection;
+       };
+       this.GetMessage = function(){
+               var retval = new Array();
+               for(var rno in collection)
+               {
+                       item={};
+                       item.applyflag = !$rooms.Get(rno).IsVolatile();
+                       item.password = collection[rno].password;
+                       if(item.password == null)
+                               item.password = "";
+                       item.hiddenlog = collection[rno].hiddenlog;
+                       retval.push(item);
+               }
+               return retval;
+       };
+       this.GetKeys = function(){
+               var retval = {};
+               for(var rno in collection)
+               {
+                       retval[rno] = {};
+               }
+               return retval;
+       }
+       this.Update = function(data,callfunc){
+               Clear();
+               var async = require("async");
+               async.waterfall([
+                       function(next){
+                               pool.query("TRUNCATE TABLE rooms",null,next);
+                       },
+                       function(result,next){
+                               var util = require("util");
+                               console.log(util.inspect(data));
+                               var items = new Array();
+                               var config = data.config;
+                               for(var i = 0; i < config.length; i++)
+                               {
+                                       var rno = Number(config[i].applyflag);
+                                       if(isNaN(rno))
+                                               continue;
+                                       var password,romonly;
+                                       if(typeof(config[rno].password)=="undefined")
+                                               password = null;
+                                       else if(config[rno].password == "")
+                                               password = null;
+                                       else
+                                               password = config[rno].password;
+                                       if(typeof(config[rno].hiddenlog)=="undefined")
+                                               romonly = false;
+                                       else
+                                               romonly = config[rno].hiddenlog == "romonly";
+
+                                       Add(rno,password,romonly);
+                                       items.push(new Array(rno,password,romonly));
+                               }
+                               pool.query("INSERT INTO rooms VALUES ?",[items],callfunc);
+                       }
+               ],callfunc);
+       }
+       function GetRoomList(callback){
+               Clear();
+               var async = require("async");
+               async.waterfall([
+                       function(next){
+                               pool.query("SELECT * FROM rooms",null,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);
+                               }
+                               next(null,null);
+                       }
+               ],callback);
+       }
+       function Clear(){
+               collection = {};
+               var config = require("./configure.js");
+               for(var i = 0; i < config.max_room_number; i++)
+                       Add(i,null,null);
+       };
+       function Add(rno,pass,hiddenlogflag){
+               collection[rno] = new RoomInfomation(pass,hiddenlogflag);
+               if(pass != null)
+                       collection[rno].owner = $system_name;
+       };
+       var $gc_interval_id = setInterval(function(){
+               for(var rno in this.rom_list)
+                       collection[rno].GCRomList();
+       },$gc_time_interval);
+       GetRoomList();
+}
+
+//RoomInfomation\83N\83\89\83X
+function RoomInfomation(pass,hiddenlogflag)
+{
+       this.password = pass;
+       this.rom_list = {};
+       this.authed_list = {};
+       this.owner = null;
+       this.time = null;
+       this.hiddenlog = hiddenlogflag;
+       this.GetConfig = function(){
+               var roomconfig = {};
+               if(this.IsVolatile() == false)
+               {
+                       if(this.IsFixedPassword())
+                               roomconfig.type = 2;
+                       else if(this.IsHiddenLogFromRom())
+                               roomconfig.type = 3;
+                       else
+                               roomconfig.type = 1;
+                       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;
+       }
+       this.GetRomCount = function(){
+               var count = 0;
+               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.RemoveRom = function(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.IsFirstAuth = function(){
+               return this.owner == null;
+       };
+       this.IsAuthed = function(name){
+               return name == this.owner ||
+                       name in this.authed_list;
+       };
+       this.IsHiddenLogFromRom = function(){
+               return this.hiddenlog;
+       };
+       this.IsFixedPassword = function(){
+               return this.owner == $system_name;
+       };
+       this.IsOwner = function(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;
+       };
+       this.RemoveAuth = function(name)
+       {
+               delete this.authed_list[name];
+       };
+       this.Auth = function(name,password){
+               if(this.password != password)
+                       return false;
+               var date = new Date();
+               var time = date.getTime();
+               this.time = time;
+               this.authed_list[name] = "";
+               return true;
+       };
+       this.SetPassword = function(owner,password){
+               if(owner == this.owner &&
+                       !this.IsFixedPassword() &&
+                       !this.IsHiddenLogFromRom())
+               {
+                       var date = new Date();
+                       this.time = date.getTime();
+                       this.password = password;
+                       return true;
+               }
+               return false;
+       };
+       this.GCRomList = function(){
+               var date = new Date();
+               var current_time = date.getTime();
+               for(var ip in this.rom_list)
+               {
+                       if(current_time - this.rom_list[ip].time >= $gc_time_interval)
+                               delete this.rom_list[ip];
+               }
+       };
+}