-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
124 lines (102 loc) · 3.05 KB
/
server.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
119
120
121
122
123
124
const WebSocketServer = require("ws").Server;
const ws = new WebSocketServer({
port: 7666
});
const loadMaps = require("./loadMaps");
const loadObjs = require("./loadObjs");
const loadSpells = require("./loadSpells");
(async () => {
const startInitialize = new Date();
const LoadMaps = new loadMaps();
const LoadObjs = new loadObjs();
const LoadSpells = new loadSpells();
await Promise.all([
LoadMaps.initialize(),
LoadObjs.initialize(),
LoadSpells.initialize()
]);
vars.serverReady = true;
const endInitialize = new Date() - startInitialize;
const textInitializeServer = `[Servidor] Iniciado en ${endInitialize}ms.`;
funct.sendTelegramMessage(textInitializeServer);
console.log(textInitializeServer);
})();
const funct = require("./functions");
const protocol = require("./protocol");
const game = require("./game");
const socket = require("./socket");
const vars = require("./vars");
const pkg = require("./package");
const npcs = require("./npcs");
const handleProtocol = require("./handleProtocol");
ws.on("connection", function(ws) {
ws.on("message", function(res) {
try {
if (ws.readyState != ws.OPEN || !vars.serverReady) {
return;
}
pkg.setData(res);
protocol.handleData(ws, pkg.getPackageID());
} catch (err) {
funct.dumpError(err);
}
});
ws.on("close", function(message) {
try {
socket.closePj(ws);
} catch (err) {
funct.dumpError(err);
}
});
});
setInterval(function() {
for (var idNpc in vars.npcs) {
var npc = vars.npcs[idNpc];
if (
npc.movement == 3 &&
vars.areaNpc[idNpc].length > 0 &&
!npc.rute.length
) {
npcs.npcAttackUser(idNpc);
}
}
}, 650);
setInterval(function() {
for (var idUser in vars.personajes) {
var user = vars.personajes[idUser];
if (user.meditar && user.mana < user.maxMana) {
game.meditar(idUser);
}
if (
user.cooldownFuerza > 0 &&
+Date.now() - user.cooldownFuerza > vars.cooldownFA
) {
user.attrFuerza = user.bkAttrFuerza;
user.cooldownFuerza = 0;
handleProtocol.updateFuerza(user.attrFuerza, vars.clients[idUser]);
}
if (
user.cooldownAgilidad > 0 &&
+Date.now() - user.cooldownAgilidad > vars.cooldownFA
) {
user.attrAgilidad = user.bkAttrAgilidad;
user.cooldownAgilidad = 0;
handleProtocol.updateAgilidad(
user.attrAgilidad,
vars.clients[idUser]
);
}
}
}, 500);
//Limpio los personajes cerrados
setInterval(function() {
for (var idUser in vars.personajes) {
if (vars.personajes[idUser].cerrado) {
delete vars.personajes[idUser];
}
}
}, 60000);
//TaskManager 60 ticks por segundo
setInterval(function() {
game.worldSave(() => {});
}, 300000);