-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
279 lines (259 loc) · 8.63 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
var Player = require('./nodemmo/objects/player');
var DatabaseObject = require("./nodemmo/database");
var ConsoleLib = require("./consolelib");
var NConsole = new ConsoleLib();
var Database = new DatabaseObject();
var Server = function() {
Database.open("server.db");
this.GameObjects = {
parent: this,
Items: {},
Players: {},
Maps: {},
};
this.loop = {
parent: this,
doMovement: function(Socket) {
var player = this.parent.GameObjects.Players[Socket.id];
if(player == 'undefined') { return; }
var didHit = false;
if(this.parent.pFunc.isPlaying(player)) {
if(player.Position.x + player.movX*5 >= 500-49) {
return;
}
if(player.Position.x + player.movX*5 <= 49) {
return;
}
if(player.Position.y + player.movY*5 >= 500-49) {
return;
}
if(player.Position.y + player.movY*5 <= 49) {
return;
}
if(player.end) {
player.endAni();
} else if(!didHit) {
player.chgDir(player.movDir);player.moveX(player.movX*5);player.moveY(player.movY*5);player.nextAni();
}
}
}
};
this.pFunc = {
parent: this,
// Get Player
// Player Name Only. getPlayerID(name) -> Player ID | Can't be offline
getPlayerID: function(Name) {
for(var object in this.parent.GameObjects.Players) {
var player = this.parent.GameObjects.Players[object];
if(player.Username == Name) return object
}
return null
},
// Player ID Only. getPlayer(getPlayerID(name)) -> Player Object | Can't be offline
getPlayer: function(id) {
var player = this.parent.GameObjects.Players[id];
return player
},
// Player Only. isPlaying(getPlayer(getPlayerID(name))) -> Bool
isPlaying: function(player) {
if(player != null) {
if(player.isLoged) { return true };
}
return false
},
// Do Stuff for Player
sendPlayers: function(Socket) {
var Player = this.parent.GameObjects.Players[Socket.id];
if(this.isPlaying(Player)) {
var sfPlayers =[];
for(var gPlayerID in this.parent.GameObjects.Players) {
var gPlayer = this.parent.GameObjects.Players[gPlayerID]
if(this.isPlaying(gPlayer) && Player.Map == gPlayer.Map) {
sfPlayers.push({ID:gPlayerID, Username:gPlayer.Username, Sprite:gPlayer.Sprite, Vittles:gPlayer.Vittles, Nourishment:gPlayer.Nourishment, Position:gPlayer.Position});
}
}
Socket.emit("onPlayers", {Players:sfPlayers});
}
},
sendMapMessage: function(Socket, Message) {
var Player = this.parent.GameObjects.Players[Socket.id];
for(var gPlayerID in this.parent.GameObjects.Players) {
var gPlayer = this.parent.GameObjects.Players[gPlayerID]
if(this.isPlaying(gPlayer) && Player.Map == gPlayer.Map) {
var gSocket = gPlayer.Socket;
gSocket.emit('onMessage', {Sender:Player.getHtmlName(), Message:Message});
}
}
},
sendGlobalMessage: function(Socket, Message) {
var Player = this.parent.GameObjects.Players[Socket.id];
for(var gPlayerID in this.parent.GameObjects.Players) {
var gPlayer = this.parent.GameObjects.Players[gPlayerID]
if(this.isPlaying(gPlayer)) {
var gSocket = gPlayer.Socket;
gSocket.emit('onMessage', {Sender:Player.getHtmlName(), Message:Message});
}
}
},
// TODO, Anounce Message. Not normal message. Scrolling right to left on top???
sendNotice: function(Message) {
for(var gPlayerID in this.parent.GameObjects.Players) {
var gPlayer = this.parent.GameObjects.Players[gPlayerID]
if(this.isPlaying(gPlayer)) {
var gSocket = gPlayer.Socket;
gSocket.emit('onNotice', Message);
}
}
},
sendServerMessageTo: function(Socket, Message) {
Socket.emit("onMessage", {Sender:"<name style='color: #FF3300;font-weight:bold;'>Server</name>", Message:"<message style='color: #33CC33;font-weight:bold;'>" + Message + "</message>"})
},
sendServerMessage: function(Message) {
},
};
this.Network = {
parent: this,
kickPlayer: function(Socket, reason) {
// TODO Reason
Socket.disconnect();
},
onMessage: function(Socket, Message) {
var Player = this.parent.GameObjects.Players[Socket.id];
Message = Message.replace('<','<').replace('>', '>');
if(Message.startsWith("!")) {
var Command = Message.substring(1).split(" ");
switch(Command[0].toLowerCase()) {
case "help":
this.parent.pFunc.sendServerMessageTo(Socket, "= Help =");
this.parent.pFunc.sendServerMessageTo(Socket, "!help ~ This Menu");
if(Player.isMod()) {
this.parent.pFunc.sendServerMessageTo(Socket, "= Mod Menu =");
this.parent.pFunc.sendServerMessageTo(Socket, "!notice <msg> ~ Send Notice Message");
this.parent.pFunc.sendServerMessageTo(Socket, "!g <msg> ~ Send Global Message");
this.parent.pFunc.sendServerMessageTo(Socket, "!kick <name> <reason> ~ Kick player with reason");
}
if(Player.isAdmin()) {
this.parent.pFunc.sendServerMessageTo(Socket, "= Admin Menu =");
this.parent.pFunc.sendServerMessageTo(Socket, "!ban <name> <reason> ~ Banish player with reason");
}
this.parent.pFunc.sendServerMessageTo(Socket, "= End =");
break;
case "notice":
if(Player.isMod()) {
this.parent.pFunc.sendNotice(Message.substring(8));
}
break;
case "g":
if(Player.isMod()) {
this.parent.pFunc.sendGlobalMessage(Socket, Message.substring(3));
}
break;
case "kick":
if(Player.isMod()) {
NConsole.writeLine(Player.Username + " kicked " + Command[1]);
}
break;
case "ban":
if(Player.isAdmin()) {
NConsole.writeLine(Player.Username + " banished " + Command[1]);
}
break;
default:
this.parent.pFunc.sendServerMessageTo(Socket, "Invaild Command. (" + Message + ")");
break;
}
} else {
this.parent.pFunc.sendMapMessage(Socket, Message);
NConsole.writeLine(Player.Username + ": " + Message);
}
/*if(Message.startsWith("!")) {
var Command = Message.substring(1).split("-");
switch(Command[0].toLowerCase()) {
case "test":
this.parent.pFunc.sendMessage(Socket, "Server", "Test Done.");
break;
default:
this.parent.pFunc.sendMessage(Socket, "Server", "Invaild Command.");
break;
}
}*/
},
onConnect: function(Socket) {
var player = new Player(); player.Socket = Socket; this.parent.GameObjects.Players[Socket.id] = player; // Create Blank player, has not loged in
},
onLogin: function(Socket, username, password) {
// No password check yet, nothing is saved.
var player = "";
var self = this;
Database.isNew(username, function(userisNew) {
if(userisNew) {
player = new Player();
player.Username = username;
player.Password = password;
player.isLoged = true;
player.Socket = Socket;
NConsole.writeLine("[" + Socket.id + ":onLogin]: New Account!");
NConsole.writeLine("[" + Socket.id + ":onLogin]: Login Succesful!");
Socket.emit('onLogin', true);
self.parent.GameObjects.Players[Socket.id] = player;
} else {
Database.loginCorrect(username, password, function(iscorrect) {
if(iscorrect) {
Database.loadPlayer(username, password, function(playerdata) {
NConsole.writeLine("[" + Socket.id + ":onLogin]: Load Account!");
NConsole.writeLine("[" + Socket.id + ":onLogin]: Login Succesful!");
playerdata.Socket = Socket;
Socket.emit('onLogin', true);
self.parent.GameObjects.Players[Socket.id] = playerdata;
});
} else {
Socket.emit('onLogin', "Incorect Username or Password!");
NConsole.writeLine("[" + Socket.id + ":onLogin]: Login Failed | Incorect Creds!");
}
})
}
});
},
onLogout: function(Socket) {
this.parent.GameObjects.Players[Socket.id].isLoged = false;
Database.savePlayer(this.parent.GameObjects.Players[Socket.id]);
delete this.parent.GameObjects.Players[Socket.id];
},
onStartMovement: function(Socket, dir) {
var player = this.parent.GameObjects.Players[Socket.id];
if(player == 'undefined') { return; }
var movY = 0;
var movX = 0;
switch(dir) {
case 0:
movY = 1
break;
case 1:
movX = -1
break;
case 2:
movX = 1
break;
case 3:
movY = -1
break;
}
player.end = false;
player.movDir = dir;
player.movY = movY;
player.movX = movX;
},
onStopMovement: function(Socket, dataCheck) {
var player = this.parent.GameObjects.Players[Socket.id];
if(player == 'undefined') { return; }
if(!dataCheck) { return; }
player.end = true;
player.movDir = false;
player.movY = 0;
player.movX = 0;
},
}
};
Server.prototype = {
};
module.exports = Server;