OSDN Git Service

a5ff81c98f3501117d8f4712c776283c246e7770
[webchat/WebChat.git] / room.js
1 //RoomInfomationCollecion\83N\83\89\83X
2 module.exports = function()
3 {
4         var _this = this;
5         var config = require("./configure.js");
6         var MySQLPool = new require("./mysql_pool.js");
7         var pool = new MySQLPool({
8                                 host     : config.db_host,
9                                 user     : config.db_user,
10                                 password : config.db_password,
11                                 port     : config.db_port,
12                                 database : config.db_name,
13                         });
14         var collection = {};
15         this.Get = function(rno){
16                 return collection[rno];
17         }
18         this.IsContains = function(rno){
19                 return rno in collection;
20         };
21         this.GetMessage = function(){
22                 var retval = new Array();
23                 for(var rno in collection)
24                 {
25                         item={};
26                         item.applyflag = !_this.Get(rno).IsVolatile();
27                         item.password = collection[rno].password;
28                         if(item.password == null)
29                                 item.password = "";
30                         item.hiddenlog = collection[rno].hiddenlog;
31                         retval.push(item);
32                 }
33                 return retval;
34         };
35         this.GetKeys = function(){
36                 var retval = {};
37                 for(var rno in collection)
38                 {
39                         retval[rno] = {};
40                 }
41                 return retval;
42         }
43         this.Update = function(data,callfunc){
44                 Clear();
45                 var async = require("async");
46                 async.waterfall([
47                         function(next){
48                                 pool.query("TRUNCATE TABLE rooms",null,next);
49                         },
50                         function(result,next){
51                                 var util = require("util");
52                                 console.log(util.inspect(data));
53                                 var items = new Array();
54                                 var config = data.config;
55                                 for(var i = 0; i < config.length; i++)
56                                 {
57                                         var rno = Number(config[i].applyflag);
58                                         if(isNaN(rno))
59                                                 continue;
60                                         var password,romonly;
61                                         if(typeof(config[rno].password)=="undefined")
62                                                 password = null;
63                                         else if(config[rno].password == "")
64                                                 password = null;
65                                         else
66                                                 password = config[rno].password;
67                                         if(typeof(config[rno].hiddenlog)=="undefined")
68                                                 romonly = false;
69                                         else
70                                                 romonly = config[rno].hiddenlog == "romonly";
71
72                                         Add(rno,password,romonly);
73                                         items.push(new Array(rno,password,romonly));
74                                 }
75                                 pool.query("INSERT INTO rooms VALUES ?",[items],callfunc);
76                         }
77                 ],callfunc);
78         }
79         function GetRoomList(callback){
80                 Clear();
81                 var async = require("async");
82                 async.waterfall([
83                         function(next){
84                                 pool.query("SELECT * FROM rooms",null,next);
85                         },
86                         function(result,next){
87                                 for(var i = 0; i < result.length; i++)
88                                 {
89                                         //MySQL\82Å\82ÍTINYINT\82ª\8eg\82í\82ê\82Ä\82¢\82é
90                                         Add(result[i].number,result[i].password,result[i].hiddenlog != 0);
91                                 }
92                                 next(null,null);
93                         }
94                 ],callback);
95         }
96         function Clear(){
97                 collection = {};
98                 var config = require("./configure.js");
99                 for(var i = 0; i < config.max_room_number; i++)
100                         Add(i,null,null);
101         };
102         function Add(rno,pass,hiddenlogflag){
103                 collection[rno] = new RoomInfomation(pass,hiddenlogflag);
104                 if(pass != null)
105                         collection[rno].owner = $system_name;
106         };
107         var $gc_interval_id = setInterval(function(){
108                 for(var rno in _this.rom_list)
109                         collection[rno].GCRomList();
110         },$gc_time_interval);
111         GetRoomList();
112 }
113
114 //RoomInfomation\83N\83\89\83X
115 function RoomInfomation(pass,hiddenlogflag)
116 {
117         var _this = this;
118         this.password = pass;
119         this.rom_list = {};
120         this.authed_list = {};
121         this.owner = null;
122         this.time = null;
123         this.hiddenlog = hiddenlogflag;
124         this.GetConfig = function(){
125                 var roomconfig = {};
126                 if(_this.IsVolatile() == false)
127                 {
128                         if(_this.IsFixedPassword())
129                                 roomconfig.type = 2;
130                         else if(_this.IsHiddenLogFromRom())
131                                 roomconfig.type = 3;
132                         else
133                                 roomconfig.type = 1;
134                         roomconfig.IsOwned = !_this.IsFirstAuth();
135                 }else{
136                         roomconfig.type = 0;
137                 }
138                 return roomconfig;
139         }
140         this.IsVolatile = function(){
141                 return _this.owner == null &&
142                         _this.password == null &&
143                         _this.time == null &&
144                         _this.hiddenlog == null;
145         }
146         this.GetRomCount = function(){
147                 var count = 0;
148                 for(var key in _this.rom_list)
149                         count++;
150                 return count;
151         };
152         this.AddRom = function(ip){
153                 var date = new Date();
154                 _this.rom_list[ip] = {time:date.getTime()};
155         };
156         this.RemoveRom = function(ip){
157                 delete _this.rom_list[ip];
158         };
159         this.Reset = function(owner){
160                 var date = new Date();
161                 var time = date.getTime();
162                 _this.password = null;
163                 _this.authed_list = {};
164                 _this.owner = owner;
165                 _this.time = time;
166         };
167         this.IsFirstAuth = function(){
168                 return _this.owner == null;
169         };
170         this.IsAuthed = function(name){
171                 return name == _this.owner ||
172                         name in _this.authed_list;
173         };
174         this.IsHiddenLogFromRom = function(){
175                 return _this.hiddenlog;
176         };
177         this.IsFixedPassword = function(){
178                 return _this.owner == $system_name;
179         };
180         this.IsOwner = function(name){
181                 return _this.owner == name;
182         };
183         this.IsTimeout = function(){
184                 var date = new Date();
185                 var current_time = date.getTime();
186                 return !_this.IsFixedPassword() &&
187                         current_time - _this.time >= $reset_password_diff;
188         };
189         this.RemoveAuth = function(name)
190         {
191                 delete _this.authed_list[name];
192         };
193         this.Auth = function(name,password){
194                 if(_this.password != password)
195                         return false;
196                 var date = new Date();
197                 var time = date.getTime();
198                 _this.time = time;
199                 _this.authed_list[name] = "";
200                 return true;
201         };
202         this.SetPassword = function(owner,password){
203                 if(owner == _this.owner &&
204                         !_this.IsFixedPassword() &&
205                         !_this.IsHiddenLogFromRom())
206                 {
207                         var date = new Date();
208                         _this.time = date.getTime();
209                         _this.password = password;
210                         return true;
211                 }
212                 return false;
213         };
214         this.GCRomList = function(){
215                 var date = new Date();
216                 var current_time = date.getTime();
217                 for(var ip in _this.rom_list)
218                 {
219                         if(current_time - _this.rom_list[ip].time >= $gc_time_interval)
220                                 delete _this.rom_list[ip];
221                 }
222         };
223 }