var userNam;
var whoIsMove;
var playerColor;
var playerToMoveID;
var currentPlayerID = $("#currentPlayerID").val();
$(document).ready(function ()
{
//$("#moveMessage").html("Waiting for update...");
var refreshId = setInterval("GetMoves(), GetWhoIsMove()", 1000);
var to;
var from = 's65';
var pieceName;
playerColor = $("#playerColor").val();
currentPlayerID = $("#currentPlayerID").val();
/// playerToMoveID = $("#playerToMove").val();
if (currentPlayerID == playerToMoveID)
{
if (playerColor.toString() == "white")
{
$(".whiteP").draggable({
//revert: 'invalid',
//helper: 'clone',
cursor: 'move',
containment: 'checkerBoard',
start: function (event, ui)
{
//$(".square").hover(function ()
//{
// from = $(this).attr('id')
//});
pieceName = $(this).attr('id');
//alert($from);
}
});
} else if (playerColor.toString() == "black")
{
$(".blackP").draggable({
//revert: 'invalid',
//helper: 'clone',
cursor: 'move',
containment: 'checkerBoard',
start: function (event, ui)
{
//$(".square").hover(function ()
//{
// from = $(this).attr('id')
//});
pieceName = $(this).attr('id');
//alert($from);
}
});
}
}
$("div.square").droppable({
accept: '.chessPiece',
drop: function (event, ui)
{
to = $(this).attr('id');
//alert(currentPlayerID);
$.post(
"/Game/MakeMove",
{
To: to,
From: from,
PieceName: pieceName,
UserName: $("#userName").val(),
UserID1: $("#userID1").val(),
UserID2: $("#userID2").val(),
GameID: $("#gameID").val()
},
function (data)
{
if (data == 'good')
{ }
else if (data == 'bad')
{
alert("sorry not you turn");
}
}
);
}
});
})
function GetWhoIsMove()
{
$.getJSON(
"/Game/GetWhoIsMove",
{
GameID: $("#gameID").val()
},
function (data)
{
playerToMoveID = data;
}
);
}
That line is causing problems:
if (currentPlayerID == playerToMoveID)
It always return false. I have checked values, and currentPlayerID is depeding on what player is curretnly logged in and playerToMoveID is pulled up from data base.
While I used those variables in alert(), they showed correct values for both players. So I must ask, what’s wrong with that if() ?
It should be alwyas true for exactly one player, until he make his move.
The problem is that you’re comparing the value of
playerToMoveIDbefore you set it.You set the value of
playerToMoveIDas the result of a function call caused by thesetInterval.When the
ifstatement fails, it’s because theifstatement is being evaluated before thesetIntervalhas had a chance to fire.You need to come up with a way to ensure that
playerToMoveIDis set before theifstatement. AsetIntervalwon’t cut it — there’s no guarantee that it will run in a timely manner.The reason it “works” when you check the values is probably that you’ve changed the program enough that by the time you get to the second
alertbox that displays theplayerToMoveIDvalue, it’s had a chance to run theGetWhoIsMovefunction.