OSDN Git Service

name_hashは被る可能性があるので、primaryではなくindexを指定するようにした
[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 RoomInfomationModel = require("./init").GetRoomInfomation;
7         var collection = {};
8         this.Get = function(rno){
9                 return collection[rno];
10         }
11         this.IsContains = function(rno){
12                 return rno in collection;
13         };
14         this.GetMessage = function(){
15                 var retval = new Array();
16                 for(var rno in collection)
17                 {
18                         item={};
19                         item.applyflag = !_this.Get(rno).IsVolatile();
20                         item.password = collection[rno].password;
21                         if(item.password == null)
22                                 item.password = "";
23                         item.hiddenlog = collection[rno].hiddenlog;
24                         retval.push(item);
25                 }
26                 return retval;
27         };
28         this.GetKeys = function(){
29                 var retval = {};
30                 for(var rno in collection)
31                 {
32                         retval[rno] = {};
33                 }
34                 return retval;
35         }
36         this.Update = function(data,callfunc){
37                 Clear();
38                 var async = require("async");
39                 async.waterfall([
40                         function(next){
41                                 RoomInfomationModel.drop().done(next);
42                         },
43                         function(result,next){
44                                 var util = require("util");
45                                 console.log(util.inspect(data));
46                                 var items = new Array();
47                                 var config = data.config;
48                                 for(var i = 0; i < config.length; i++)
49                                 {
50                                         var rno = Number(config[i].applyflag);
51                                         if(isNaN(rno))
52                                                 continue;
53                                         var password,romonly;
54                                         if(typeof(config[rno].password)=="undefined")
55                                                 password = null;
56                                         else if(config[rno].password == "")
57                                                 password = null;
58                                         else
59                                                 password = config[rno].password;
60                                         if(typeof(config[rno].hiddenlog)=="undefined")
61                                                 romonly = false;
62                                         else
63                                                 romonly = config[rno].hiddenlog == "romonly";
64
65                                         Add(rno,password,romonly);
66                                         items.push(new Array(rno,password,romonly));
67                                 }
68                                 newRoom = RoomInfomationModel.build(items);
69                                 newRoom.save().done(next);
70                         }
71                 ],callfunc);
72         }
73         function GetRoomList(callback){
74                 Clear();
75                 var async = require("async");
76                 async.waterfall([
77                         function(next){
78                                 RoomInfomationModel.findAll().done(next);
79                         },
80                         function(result,next){
81                                 for(var i = 0; i < result.length; i++)
82                                 {
83                                         Add(result[i].number,result[i].password,result[i].hiddenlog);
84                                 }
85                                 next(null,null);
86                         }
87                 ],callback);
88         }
89         function Clear(){
90                 collection = {};
91                 var config = require("./configure.js");
92                 for(var i = 0; i < config.max_room_number; i++)
93                         Add(i,null,null);
94         };
95         function Add(rno,pass,hiddenlogflag){
96                 collection[rno] = new RoomInfomation(pass,hiddenlogflag);
97                 if(pass != null)
98                         collection[rno].owner = $system_name;
99         };
100         var $gc_interval_id = setInterval(function(){
101                 for(var rno in _this.rom_list)
102                         collection[rno].GCRomList();
103         },$gc_time_interval);
104         GetRoomList();
105 }
106
107 //RoomInfomation\83N\83\89\83X
108 function RoomInfomation(pass,hiddenlogflag)
109 {
110         var _this = this;
111         this.password = pass;
112         this.rom_list = {};
113         this.authed_list = {};
114         this.owner = null;
115         this.time = null;
116         this.hiddenlog = hiddenlogflag;
117         this.GetConfig = function(){
118                 var roomconfig = {};
119                 if(_this.IsVolatile() == false)
120                 {
121                         if(_this.IsFixedPassword())
122                                 roomconfig.type = 2;
123                         else if(_this.IsHiddenLogFromRom())
124                                 roomconfig.type = 3;
125                         else
126                                 roomconfig.type = 1;
127                         roomconfig.IsOwned = !_this.IsFirstAuth();
128                 }else{
129                         roomconfig.type = 0;
130                 }
131                 return roomconfig;
132         }
133         this.IsVolatile = function(){
134                 return _this.owner == null &&
135                         _this.password == null &&
136                         _this.time == null &&
137                         _this.hiddenlog == null;
138         }
139         this.GetRomCount = function(){
140                 var count = 0;
141                 for(var key in _this.rom_list)
142                         count++;
143                 return count;
144         };
145         this.AddRom = function(ip){
146                 var date = new Date();
147                 _this.rom_list[ip] = {time:date.getTime()};
148         };
149         this.RemoveRom = function(ip){
150                 delete _this.rom_list[ip];
151         };
152         this.Reset = function(owner){
153                 var date = new Date();
154                 var time = date.getTime();
155                 _this.password = null;
156                 _this.authed_list = {};
157                 _this.owner = owner;
158                 _this.time = time;
159         };
160         this.IsFirstAuth = function(){
161                 return _this.owner == null;
162         };
163         this.IsAuthed = function(name){
164                 return name == _this.owner ||
165                         name in _this.authed_list;
166         };
167         this.IsHiddenLogFromRom = function(){
168                 return _this.hiddenlog;
169         };
170         this.IsFixedPassword = function(){
171                 return _this.owner == $system_name;
172         };
173         this.IsOwner = function(name){
174                 return _this.owner == name;
175         };
176         this.IsTimeout = function(){
177                 var date = new Date();
178                 var current_time = date.getTime();
179                 return !_this.IsFixedPassword() &&
180                         current_time - _this.time >= $reset_password_diff;
181         };
182         this.RemoveAuth = function(name)
183         {
184                 delete _this.authed_list[name];
185         };
186         this.Auth = function(name,password){
187                 if(_this.password != password)
188                         return false;
189                 var date = new Date();
190                 var time = date.getTime();
191                 _this.time = time;
192                 _this.authed_list[name] = "";
193                 return true;
194         };
195         this.SetPassword = function(owner,password){
196                 if(owner == _this.owner &&
197                         !_this.IsFixedPassword() &&
198                         !_this.IsHiddenLogFromRom())
199                 {
200                         var date = new Date();
201                         _this.time = date.getTime();
202                         _this.password = password;
203                         return true;
204                 }
205                 return false;
206         };
207         this.GCRomList = function(){
208                 var date = new Date();
209                 var current_time = date.getTime();
210                 for(var ip in _this.rom_list)
211                 {
212                         if(current_time - _this.rom_list[ip].time >= $gc_time_interval)
213                                 delete _this.rom_list[ip];
214                 }
215         };
216 }