OSDN Git Service

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