Permalink
Browse files

Access Update

Notice messages
Chat color names
and more
  • Loading branch information...
LexTheGreat committed Mar 2, 2015
1 parent 3a61d30 commit 4db5c62c7d6f6bf7e8fb0baf67fe3dd39cd4b227
Showing with 122 additions and 26 deletions.
  1. +14 −4 app.js
  2. +4 −3 nodemmo/database.js
  3. +13 −0 nodemmo/objects/player.js
  4. +2 −2 public/game/game.js
  5. +11 −2 public/game/network.js
  6. +15 −5 public/index.html
  7. +63 −10 server.js
View
18 app.js
@@ -39,6 +39,12 @@ if (typeof String.prototype.startsWith != 'function') {
};
}
if (typeof String.prototype.hasWhiteSpace != 'function') {
String.prototype.hasWhiteSpace = function () {
return this.indexOf(' ') >= 0;
}
}
var ClientSocket = {};
Global.Server.sockets.on('connection', function(socket) {
NConsole.writeLine("[" + socket.id + ":connection] Socket Connected!");
@@ -50,14 +56,15 @@ Global.Server.sockets.on('connection', function(socket) {
if(typeof data.Username != 'string' || typeof data.Password != "string") {
//NConsole.writeLine("Login Test: False | Not String");
socket.emit('onLogin', "Incorect Data. Kicked! (Stop Trying to Cheat!)");
NConsole.writeLine("[" + socket.id + ":AntiPacket]: Socket sent incorect Data! (" + data + ")");
NConsole.writeLine("[" + socket.id + ":onLogin]: Kicked for Incorect login data.");
GameServer.Network.kickPlayer(socket);
return;
}
if((data.Username.length <= 0 || data.Username.length > 12) || (data.Password.length < 6 || data.Password.length > 12)) {
if((data.Username.length < 6 || data.Username.length > 12) || (data.Password.length < 6 || data.Password.length > 12 || data.Username.hasWhiteSpace() || data.Password.hasWhiteSpace())) {
//NConsole.writeLine("Login Test: False | Username or Password To long");
NConsole.writeLine("[" + socket.id + ":onLogin]: Username or Password length incorect.");
socket.emit('onLogin', "Username or Password length incorect.");
socket.emit('onLogin', "Username or Password length incorect. | > 6 | < 12 | No Spaces!");
return;
}
@@ -68,24 +75,27 @@ Global.Server.sockets.on('connection', function(socket) {
socket.on('onMovement', function(data) {
if(typeof data != 'number') {
socket.emit('popup', "Incorect Data. Kicked! (Stop Trying to Cheat!)");
NConsole.writeLine("[" + socket.id + ":AntiPacket]: Socket sent incorect Data! (" + data + ")");
GameServer.Network.kickPlayer(socket);
return;
}
if(data > 3 && data < -1) {
socket.emit('popup', "Incorect Data. Kicked! (Stop Trying to Cheat!)");
NConsole.writeLine("[" + socket.id + ":AntiPacket]: Socket sent incorect Data! (" + data + ")");
GameServer.Network.kickPlayer(socket);
return;
}
GameServer.Network.onMovement(socket, data);
});
socket.on('onMessage', function(data) {
if(typeof data != 'string') {
NConsole.writeLine("[" + socket.id + ":AntiPacket]: Socket sent incorect Data! (" + data + ")");
socket.emit('popup', "Incorect Data. Kicked! (Stop Trying to Cheat!)");
GameServer.Network.kickPlayer(socket);
return;
}
if(data.length > 100 && data.length == 0) {
socket.emit('popup', "Message to length incorect!");
socket.emit('popup', "Message to length incorect! | < 100 | > 0 |");
return;
}
GameServer.Network.onMessage(socket, data);
@@ -111,7 +121,7 @@ ServerLoop = function() {
}
}
setImmediate(ServerLoop)
}, 1000/30);
}, 1000/60);
}
setImmediate(ServerLoop)
View
@@ -26,7 +26,7 @@ Database.prototype = {
db.serialize(function() {
if(!exists) {
NConsole.writeLine("Creating table...");
db.run("CREATE TABLE Players (Username TEXT, Password TEXT, Sprite TEXT, Map TEXT, Health INT, Mana INT, x INT, y INT, dir INT)");
db.run("CREATE TABLE Players (Username TEXT, Password TEXT, Access INT, Sprite TEXT, Map TEXT, Health INT, Mana INT, x INT, y INT, dir INT)");
}
});
},
@@ -78,6 +78,7 @@ Database.prototype = {
player.isLoged = true;
player.Username = row.Username;
player.Password = row.Password;
player.Access = row.Access;
player.Sprite = row.Sprite;
player.Map = row.Map;
player.Vittles.health = row.Health;
@@ -100,14 +101,14 @@ Database.prototype = {
self.newPlayer(player);
} else {
db.serialize(function() {
db.run("UPDATE Players SET Sprite = '" + player.Sprite + "', Map = '" + player.Map + "', Health = " + player.Vittles.health + ", Mana = " + player.Vittles.mana + ", x = " + player.Position.x + ", y = " + player.Position.y + ", dir = " + player.Position.dir + " WHERE Username = '" + player.Username + "'");
db.run("UPDATE Players SET Access = " + player.Access + ", Sprite = '" + player.Sprite + "', Map = '" + player.Map + "', Health = " + player.Vittles.health + ", Mana = " + player.Vittles.mana + ", x = " + player.Position.x + ", y = " + player.Position.y + ", dir = " + player.Position.dir + " WHERE Username = '" + player.Username + "'");
});
}
});
},
newPlayer: function(player) {
db.serialize(function() {
db.run("INSERT INTO Players (Username, Password, Sprite, Map, Health, Mana, x, y, dir) VALUES ('" + player.Username + "', '" + player.Password + "', '" + player.Sprite + "', '" + player.Map + "', " + player.Vittles.health + ", " + player.Vittles.mana + ", " + player.Position.x + ", " + player.Position.y + ", " + player.Position.dir + ")");
db.run("INSERT INTO Players (Username, Password, Access, Sprite, Map, Health, Mana, x, y, dir) VALUES ('" + player.Username + "', '" + player.Password + "', " + player.Access + ", '" + player.Sprite + "', '" + player.Map + "', " + player.Vittles.health + ", " + player.Vittles.mana + ", " + player.Position.x + ", " + player.Position.y + ", " + player.Position.dir + ")");
});
}
}
View
@@ -2,6 +2,7 @@ var Player = function() {
this.Username = ''; // Username for login, sent as ign
this.Password = '';
this.Sprite = "dragon";
this.Access = 0;
this.Vittles = { health: 100, mana: 100}; // Health | Energy
this.Map = "Spawn";
this.Position = { x: 100, y: 100, dir: 3, ani: 0 };
@@ -15,6 +16,18 @@ var Player = function() {
};
Player.prototype = {
isMod: function() {
if(this.Access >= 1) return true;
},
isAdmin: function() {
if(this.Access >= 2) return true;
},
getHtmlName: function() {
var Sty = "color: #000000;";
if(this.isMod()) { Sty = "color: #33CC33;font-weight:bold;"; }
if(this.isAdmin()) { Sty = "color: #FF3300;font-weight:bold;"; }
return '<name style="' + Sty + '">' + this.Username + '</name>'
},
nextAni: function() {
if(this.AniT != this.nextAniT) {
this.AniT += 1;
View
@@ -119,7 +119,7 @@ window.GameEngine = function() {
drawPlayers: function(ctx) {
for(var obj in this.parent.NetVar.Players) {
var player = this.parent.NetVar.Players[obj];
this.drawNameText(ctx, player.Username, "#FFFFFF", player.Position.x+(96/2), player.Position.y-100);
this.drawNameText(ctx, player.Username, "#000", player.Position.x+(96/2), player.Position.y-100);
this.drawSprite(ctx, player.Sprite, player.Position.x, player.Position.y, player.Position.dir, player.Position.ani)
}
}
@@ -144,7 +144,7 @@ window.GameEngine = function() {
}
self.parent.Render.draw();
}, 1000/30);
}, 1000/60);
//setInterval(function() {
//}, 500);
View
@@ -31,23 +31,32 @@ var Network = function() {
console.log("[Socket:sendLogin] Passed:", data);
Game.Network.onLogin(data);
} else {
alert(data);
console.log("[Socket:sendLogin] Failed:", data);
}
});
Socket.on('onPlayers', function(data) {
if(typeof data == 'object') {
window.Game.NetVar.Players = data.Players;
}
}
});
Socket.on('onMessage', function(data) {
$( "#chatLog" ).append( "<p><xmp>" + data.Sender + ": " + data.Message + "</xmp></p>" );
$( "#chatLog" ).append( "<p>" + data.Sender + ": " + data.Message + "</p>" );
});
Socket.on('popup', function(data) {
alert("Server: " + data);
});
Socket.on('onNotice', function(data) {
$('#Notice').text(data)
setTimeout(function(){
$('#Notice').text("")
}, 30000);
});
}
Network.prototype = {
View
@@ -18,6 +18,14 @@
display: block;
width: 100%;
}
#NoticeBox
{
width: 100%;
height: 10px;
left: 45%;
top: 1%;
position: absolute;
}
#loginGUI
{
bottom: 50%;
@@ -44,23 +52,25 @@
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="http://phastforms.com/reveal-it.js/js/reveal-it.js"></script>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="./game/Cache.js"></script>
<script type="text/javascript" src="./game/game.js"></script>
<script type="text/javascript" src="./game/network.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<div id="container">
<canvas id='gameCanvas' tabindex='1'></canvas>
<menu id="loginGUI">
<form>
Username: <input id="LoginUsername" type="text" value="123456"><br>
Password: <input id="LoginPassword" type="Password" value="123456"><br>
Username: <input id="LoginUsername" type="text" value="Username"><br>
Password: <input id="LoginPassword" type="Password" value="Password"><br>
<input id="LoginButton" type="button" value="Submit" onclick="Network.sendLogin();">
</form>
</menu>
<menu id="NoticeBox">
<p id="Notice" style="color: #FF3300;font-weight:bold;"></p>
</menu>
<menu id="gameGUI" style="display: none;">
<div id="chatLog">
</div>
View
@@ -53,6 +53,42 @@ var Server = function() {
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:"Server", Message:Message})
},
sendServerMessage: function(Message) {
},
};
this.Network = {
@@ -62,18 +98,35 @@ var Server = function() {
Socket.disconnect();
},
onMessage: function(Socket, Message) {
if(Message.startsWith("!")) {
var Player = this.parent.GameObjects.Players[Socket.id];
} else {
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.parent.pFunc.isPlaying(gPlayer) && Player.Map == gPlayer.Map) {
var gSocket = gPlayer.Socket;
gSocket.emit('onMessage', {Sender:Player.Username, Message:Message});
}
Message = Message.replace('<','&lt;').replace('>', '&gt;');
if(Message.startsWith("!")) {
var Command = Message.substring(1).split(" ");
switch(Command[0].toLowerCase()) {
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]);
}
}
NConsole.writeLine("M[" + Player.Username + ": " + Message + "]");
} else {
this.parent.pFunc.sendMapMessage(Socket, Message);
NConsole.writeLine(Player.Username + ": " + Message);
}
/*if(Message.startsWith("!")) {

0 comments on commit 4db5c62

Please sign in to comment.