I have a page which two users play each other on that and I set some global variables at the top of my javascript code:
myScore = 0;
OpponentScore = 0;
winner = loser = loser_point = winner_point = dual = null;
and check user answers in another part of code while server broadcasts user's answer :
I'm using laravel echo and pusher
.listen('UserAnswered', (e) => {
$(document).find('.myAvatar').find('i.fa').removeClass('animated');
$(document).find('.myAvatar').find('i.fa').removeClass('heartBeat');
$(document).find('.myAvatar').find('i.fa').removeClass('swing');
$(document).find('.opponentAvatar').find('i.fa').removeClass('animated');
$(document).find('.opponentAvatar').find('i.fa').removeClass('heartBeat');
$(document).find('.opponentAvatar').find('i.fa').removeClass('swing');
if( e.user_id === parseInt("{{ Auth::id() }}", 10) )
{
//this user answered
if( e.result == 1 )
{
myScore++;
$('.myResult').find('.target').text(myScore);
$('.myAvatar').find('i.fa').addClass('heartBeat animated');
}
else
{
$('.myAvatar').find('i.fa').addClass('swing animated');
}
}
else
{
//opponent answered
if( e.result == 1 )
{
OpponentScore++;
$('.opponentResult').find('.target').text(OpponentScore);
$('.opponentAvatar').find('i.fa').addClass('heartBeat animated');
}
else
{
$('.opponentAvatar').find('i.fa').addClass('swing animated');
}
}
});
and I have a method named setScoreboard
which set winner
, loser
, winner_point
and loser_point
, when the game is finished .
Set Scorebaord function :
/**
* Set Scoreboard Function
*/
function setScoreboard() {
window.Echo.disconnect();
if( myScore > OpponentScore )
{
winner = "{{ json_encode(Auth::id()) }}";
loser = OpponentId;
winner_point = myScore;
loser_point = OpponentScore;
dual = false;
$(document).find('input[name="game_id"]').val(String(gameId));
$(document).find('input[name="winner"]').val(winner);
$(document).find('input[name="loser"]').val(loser);
$(document).find('input[name="winner_point"]').val(String(myScore));
$(document).find('input[name="loser_point"]').val(String(OpponentScore));
$(document).find('input[name="dual"]').val('false');
//submit hidden form
$(document).find('form[name="setResultForm"]').submit();
}
else if (OpponentScore > myScore)
{
winner = OpponentId;
loser = "{{ json_encode(Auth::id()) }}";
winner_point = OpponentScore;
loser_point = myScore;
dual = false;
$(document).find('input[name="game_id"]').val(String(gameId));
$(document).find('input[name="winner"]').val(winner);
$(document).find('input[name="loser"]').val(loser);
$(document).find('input[name="winner_point"]').val(String(OpponentScore));
$(document).find('input[name="loser_point"]').val(String(myScore));
$(document).find('input[name="dual"]').val('false');
//submit hidden form
$(document).find('form[name="setResultForm"]').submit();
}
else if (OpponentScore === myScore)
{
winner = "{{ json_encode(Auth::id()) }}";
loser = OpponentId;
winner_point = loser_point = myScore;
dual = true;
$(document).find('input[name="game_id"]').val(String(gameId));
{{--$(document).find('input[name="winner"]').val(String(parseInt("{{ json_encode(Auth::id()) }}", 10)));--}}
$(document).find('input[name="winner"]').val(winner);
// $(document).find('input[name="loser"]').val(String(OpponentId));
$(document).find('input[name="loser"]').val(loser);
$(document).find('input[name="winner_point"]').val(String(OpponentScore));
$(document).find('input[name="loser_point"]').val(String(OpponentScore));
$(document).find('input[name="dual"]').val('true');
//submit hidden form
$(document).find('form[name="setResultForm"]').submit();
}
Everything looks good and the code works fine, but randomly in one side, winner
or loser
would be null!
PLAYER ONE:
PLAYER TWO:
As the code is the same for both users side, Does anyone know why is that?