Skip to content
Permalink
Browse files

Database Fixes

Fixed some issues with sync on the database :S Works now!
  • Loading branch information...
LexTheGreat committed Feb 25, 2015
1 parent f423837 commit 3da791351d5f8259c18e6483f95cb9b8ab0ed389
Showing with 51 additions and 39 deletions.
  1. +0 −1 app.js
  2. +14 −12 nodemmo/database.js
  3. +18 −8 public/game/game.js
  4. +9 −8 public/game/network.js
  5. +10 −10 server.js
1 app.js
@@ -96,7 +96,6 @@ Global.Server.sockets.on('connection', function(socket) {
});
});

var tickCount = 0
ServerLoop = function() {
var self = this;
for(var SocketID in ClientSocket) {
@@ -38,33 +38,34 @@ Database.prototype = {
}
},
// Players
loginCorrect: function(Username, Password) {
var userLoginCorrect = false;

loginCorrect: function(Username, Password, callback) {
if(typeof db != "string") {
db.serialize(function() {
var userLoginCorrect = false;
db.each("SELECT * FROM Players", function(err, row) {
if(row.Username == Username && row.Password == Password) {
if(row.Username == Username && row.Password == Password && !userLoginCorrect) {
userLoginCorrect = true
return
}
}, function(err, cntx) {
callback(userLoginCorrect);
});
});
}
return userLoginCorrect;
},
// Returns False! Fix something else
isNew: function(Username, callback) {
if(typeof db != "string") {
db.serialize(function() {
var foundUser = false;

var isplayernew = true;
db.each("SELECT * FROM Players", function(err, row) {
if(row.Username == Username && !foundUser) {
foundUser = true;
if(row.Username == Username && isplayernew) {
isplayernew = false;
return;
}
}, function(err, cntx) {
callback(isplayernew);
});
callback(foundUser);
});
}
},
@@ -85,9 +86,10 @@ Database.prototype = {
player.Position.y = row.y;
player.Position.dir = row.dir;
}
}, function(err, cntx) {
callback(player)
});

callback(player)

});
}
},
@@ -125,17 +125,27 @@ window.GameEngine = function() {

this.GameLoop = {
parent: this,
MovementCount: 0,
didsendReset: false,
update: function() {
var self = this;
//requestAnimationFrame(function() {self.update()});
setInterval(function() {
if(isMoving) {
Network.sendMovement(Dir);
}

setTimeout(function() {
requestAnimationFrame(function() {self.update()})


self.parent.Render.draw();
}, 1000/32);
if(isMoving) {
Network.sendMovement(Dir);
this.didsendReset = false;
} else if(!this.didsendReset) {
Network.sendReset();
this.didsendReset = true
}

self.parent.Render.draw();
}, 1000/30);
//setInterval(function() {

//}, 500);
}
};

@@ -14,10 +14,13 @@ var Network = function() {

// Data = Status of failed, or boolean true of passed
Socket.on('onLogin', function(data) {
if(typeof data == "string") {
console.log("Login Failed:", data);
} else {
// IF onLogin is called ilegaly, it would not get sent data
// Client side check is OK
if(data == true) {
console.log("[Socket:sendLogin] Passed:", data);
Game.Network.onLogin(data);
} else {
console.log("[Socket:sendLogin] Failed:", data);
}
});

@@ -26,11 +29,6 @@ var Network = function() {
window.Game.NetVar.Players = data.Players;
}
});

this.Send = {
parent: this,

};
}

Network.prototype = {
@@ -44,6 +42,9 @@ Network.prototype = {
if(dir <= 3 && dir >= -1) {
Socket.emit("onMovement", dir);
}
},
sendReset: function() {
Socket.emit("onMovement", -1);
}
}

@@ -93,7 +93,7 @@ var Server = function() {
// No password check yet, nothing is saved.
var player = "";
var self = this;
Database.isNew(username, function(userisNew) {
Database.isNew(username, function(userisNew) {
if(userisNew) {
player = new Player();
player.Username = username;
@@ -104,20 +104,20 @@ var Server = function() {
Socket.emit('onLogin', true);
self.parent.GameObjects.Players[Socket.id] = player;
} else {
Database.loadPlayer(username, password, function(playerdata) {
if(playerdata) {
NConsole.writeLine("[" + Socket.id + ":onLogin]: Load Account!");
NConsole.writeLine("[" + Socket.id + ":onLogin]: Login Succesful!");
Socket.emit('onLogin', true);
self.parent.GameObjects.Players[Socket.id] = playerdata;
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!");
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) {

0 comments on commit 3da7913

Please sign in to comment.
You can’t perform that action at this time.