I working on a code to displaying positions data (for a piece on my javascript game ) positions data are on mysql db to display it on a web page for a piece i can get the data without any problems and when I open my web page after login i found the piece in the right position from the database and i can move the piece and I can insert the new position data to mysql db but the problem when i refresh the game page i lost the position from the webpage but i can see the position on my database but the piece can't get to it some time i need to refresh the page many time to get the position again on the page my ajax code should checking the position from my database and display the piece on it I don't know from where this problem from my node.js or javascript part or what ?
my javascript part
function loadLMove(){
$.ajax({
type:'POST',
url:'/dispalymove',
data:{MoveString:MoveString},
dataType:'json',
cache: false,
//timeout: 20000,
success:function(data){
if (data.msg != ""){
if (MoveString!=data.msg){
var now = new Date().getTime();
//alert(data.msg);
ParseFen(data.msg);
++timesRun;
console.log('Move displayed: ' + data.msg + 'Action ' + timesRun + ' started ' + (now - startTime) + 'ms after script start');
//PrintBoard();
SetInitialBoardPieces();
GameController.PlayerSide = brd_side;
CheckAndSet();
EvalPosition();
//PerftTest(5);
//newGameAjax();
MoveString=data.msg;
}
} else{
if (MoveString!=data.msg){
ParseFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
//PrintBoard();
SetInitialBoardPieces();
GameController.PlayerSide = brd_side;
CheckAndSet();
EvalPosition();
//PerftTest(5);
//newGameAjax();
}
}
}
});
}
//---------------------------------------------------------
loadLMove();
setInterval(loadLMove,2000);
my node.js code
app.post('/dispalymove', function (req, res, next) {
var lMove="";
if(req.body.MoveString !== null){
Move.setMoveUserId(req.user.id);
Move.setMoveString(req.body.MoveString);
a.getLastMove(req.user.GameId,function(move){
console.log("Return from display req.body:",req.body.MoveString);
console.log("Return from display themove:",move);
res.json({"msg": move, "loggedin": "true"});
});
} else {
var output = {"msg": lMove, "loggedin": "true"}; // <=== here always equals ""
res.json(output);
}
});
getlastmove function code
getLastMove(id,callback){
var MoveRequest = "SELECT * FROM users ORDER BY id";
var query = connection.query(MoveRequest, function(err,rows, result) {
if (rows.length == 0) {
return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
}
if (rows.length > 0) {
for (var i in rows) {
var move = rows[i].MoveString;
if (rows[i].GameId == id){
callback(move);
return;
}
}
}
});
}
I tried to use my write this code with php and i get display the positions even after refreshing many of time are the wrong with node ?
via dark night
No comments:
Post a Comment