-
Notifications
You must be signed in to change notification settings - Fork 2
/
classes.js
118 lines (96 loc) · 2.88 KB
/
classes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
let getToken = () => {
let chars = 'ABCDEFGHIJKLMNOPQRSTUVWQYZ123456789'.split('');
let token = '';
for (let i = 0; i < 32; i++){
if(Math.round(Math.random())) token += chars[Math.round( Math.random() * (chars.length - 1) )].toLowerCase();
else token += chars[Math.round( Math.random() * (chars.length) - 1 )];
}
return token;
};
class User{
constructor(nickname, perms, props, socket){
this.id = Date.now() * (Math.round( Math.random() * 20 ) + 1);
this.name = nickname;
this.verified = false;
this.avatar = '/resources/graphics/avatars/azuma-3.svg';
this.color = '#666';
this.status = 'online';
this.banned = 'none';
this.perms = new UserPermissions(perms || '');
this.token = getToken();
this.lastSocket = (socket ? socket.id : 'none');
this.signUpAddress = (socket ? socket.handshake.address : 'none');
this.createdAt = Date.now();
if(props && props != 'none'){
props.map(k => {
this[k[0]] = k[1];
});
}
}
ban(reason, executor){
this.banned = new Ban(reason, executor);
}
prop(property, value){
this[property] = value;
}
safeUser(){
let us = Object.assign({}, this);
delete us.token;
delete us.lastSocket;
delete us.signUpAddress;
return us;
}
}
class Channel{
constructor(name, type){
this.id = Date.now() * (Math.round( Math.random() * 20 ) + 1);
this.name = name;
this.type = type || 'text';
this.messages = []; // Preparation for persistent messages, going to introduce a db later.
this.createdAt = Date.now();
}
}
class Message{
constructor(content, author, channel){
this.id = Date.now() * (Math.round( Math.random() * 20 ) + 1);
this.content = content;
this.author = author;
this.channel = channel;
// Preparation for making banners
// if(author == 'system') this.type == 'system';
// else this.type = 'message';
this.createdAt = Date.now();
}
}
class Ban{
constructor(reason, author){
this.status = true;
this.reason = reason;
this.executor = author;
this.expiring = 'never';
this.createdAt = Date.now();
}
}
class UserPermissions{
constructor(perms){
this.ban = false;
this.channels = false;
this.announce = false;
this.admin = false;
switch(perms){
case('admin'):
this.admin = true;
break;
case('moderator'):
this.ban = true;
this.channels = true;
break;
case('announcer'):
this.announce = true;
break;
}
}
}
module.exports = {
Message, User, Channel
};