-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
178 lines (141 loc) · 5.28 KB
/
node.js
File metadata and controls
178 lines (141 loc) · 5.28 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//CONFIG
var port = 3000;
//Initialization modules
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http, {
path: '/nodejs'
});
var jwt = require('socketio-jwt');
var dotenv = require('dotenv').config({path: '.env'});
var request = require('request');
var { DateTime } = require('luxon');
var Datastore = require('nedb');
//Chat history
var db_history = new Datastore({filename : 'chat_history', autoload: true});
var online_users = [];
var banned_users = [];
//FORUM CHAT
var fchat_last = [];
var fchat_rps = [];
var lastForumPostTime = 0;
http.listen(port, function(){
console.log('listening on *:' + port);
});
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : process.env.DB_HOST,
user : process.env.DB_USERNAME,
password : process.env.DB_PASSWORD,
database : process.env.DB_DATABASE
});
function getDateTime() {
return DateTime.local().setZone('Europe/Moscow');
}
function updateBans() {
pool.query('SELECT bannable_id, expired_at, comment FROM bans WHERE expired_at > now() AND deleted_at IS NULL', [], function (error, results, fields) {
if (error) throw error;
banned_users = results;
});
}
setInterval(function (){
updateBans();
}, 5000);
setInterval(function (){
io.emit('forum.online', online_users);
}, 15000);
function checkBan(id){
return banned_users.find(ban => ban.bannable_id === id);
}
io.on('connection', function (socket) {
socket.emit('forum.online', online_users);
socket.on('forum.chat.load', function(){
if (fchat_rps[socket.conn.id]){
if ((Date.now() - fchat_rps[socket.conn.id]) < 2000){
return;
}
}
fchat_rps[socket.conn.id] = Date.now();
socket.emit('forum.chat.load', fchat_last);
});
}).on('connection', jwt.authorize({
secret: process.env.JWT_SECRET,
timeout: 15000
})).on('authenticated', function(socket) {
var user = socket.decoded_token;
user.socket = socket.conn.id;
user.time = getDateTime().toLocaleString(DateTime.TIME_24_WITH_SECONDS);
if (user.role.toLowerCase().indexOf('игрок') > 0){
user.role = '';
}
socket.on('forum.chat.moder', function(){
socket.emit('forum.chat.moder', {
moder: user.moder ? true : false
});
});
socket.on('forum.online', function(){
online_users = online_users.filter(obj => obj.uuid !== user.uuid);
user.time = getDateTime().toLocaleString(DateTime.TIME_24_WITH_SECONDS);
online_users.unshift({login: user.login, uuid: user.uuid, moder: user.moder, role: user.role});
});
socket.on('forum.posts.new', function(){
if (fchat_rps[user.id]){
if ((Date.now() - fchat_rps[user.id]) < 2000 && !user.moder){
return;
}
}
fchat_rps[user.id] = Date.now();
socket.emit('forum.posts.new');
});
socket.on('forum.chat.delete', function(text){
if (user.moder){
fchat_last = fchat_last.filter(function (msg) {
return msg.text !== text;
});
io.emit('forum.chat.delete', text);
}
});
socket.on('forum.chat.msg', function(text){
var unix = Math.round(+new Date()/1000);
if (user.reg_time > (unix - 43200)){
socket.emit('message', {type: 'error', title: 'Ошибка', msg: 'Мы можете пользоваться чатом только спустя 12 часов после регистрации!'});
return;
}
if (fchat_rps[user.id]){
if ((Date.now() - fchat_rps[user.id]) < 2000 && !user.moder){
socket.emit('message', {type: 'warn', title: 'Упс!', msg: 'Не так быстро! Попробуйте через 3 секунды!'});
return;
}
}
fchat_rps[user.id] = Date.now();
if (text.length > 500 && !user.moder){
socket.emit('message', {type: 'error', title: 'Ошибка', msg: 'Не более 500 символов в одном сообщении!'});
return;
}
text = text.replace(/<\/?[^>]+>/gi, '');
if (checkBan(user.id)){
socket.emit('message', {type: 'error', title: 'Ошибка', msg: 'Вам запрещено писать в чат!'});
return;
}
var message = {
user: {login: user.login, uuid: user.uuid, moder: user.moder, role: user.role},
text: text,
time: getDateTime().toLocaleString(DateTime.TIME_24_WITH_SECONDS)
};
if (message.text.length <= 0 || text.length <= 0){
socket.emit('message', {type: 'error', title: 'Ошибка', msg: 'Нельзя отправлять пустые сообщения!'});
return;
}
fchat_last.push(message);
if (fchat_last.length >= 20){
fchat_last.splice(0, 1);
}
db_history.insert({login: message.user.login, text: message.text});
console.log('[' + user.login + ']: ' + text);
io.emit('forum.chat.msg', message);
});
socket.on('disconnect', function() {
online_users = online_users.filter(obj => obj.uuid !== user.uuid);
});
});