0
     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.

1
  • 1
    Don't pass a string to setInterval. Commented Jan 5, 2011 at 21:35

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have check if(playerToMoveID != null), and it works fine, so I dont think this is an case, but I might be wrong.
0

May be there are some extra spaces. Why not try this:

if (jQuery.trim(currentPlayerID) == jQuery.trim(playerToMoveID))

1 Comment

== is evil. === is recommended. :)
0

You've commented out the setting of playerToMoveID

/// playerToMoveID = $("#playerToMove").val();

6 Comments

I don't it is commented out in OP's actual code since he was able to test the values of both. But maybe I'm wrong...
I would hope not. That really is a facepalm moment.
No playerToMoveID is set in function. I had it hardcoded and set from database but it proved to not working as intended ;p.
But you haven't called that function in the $(document).ready(). That variable will not get set at present.
I thought var refreshId = setInterval("GetMoves(), GetWhoIsMove()", 5000); this line will do. GetWhoIsMove() is function that set this variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.