Reputation: 1239
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.
Upvotes: 0
Views: 10014
Reputation: 12543
The problem is that you're comparing the value of playerToMoveID
before you set it.
You set the value of playerToMoveID
as the result of a function call caused by the setInterval
.
When the if
statement fails, it's because the if
statement is being evaluated before the setInterval
has had a chance to fire.
You need to come up with a way to ensure that playerToMoveID
is set before the if
statement. A setInterval
won'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 alert
box that displays the playerToMoveID
value, it's had a chance to run the GetWhoIsMove
function.
Upvotes: 1
Reputation: 5662
You've commented out the setting of playerToMoveID
/// playerToMoveID = $("#playerToMove").val();
Upvotes: 0
Reputation: 82933
May be there are some extra spaces. Why not try this:
if (jQuery.trim(currentPlayerID) == jQuery.trim(playerToMoveID))
Upvotes: 0