Page 1 of 2

JS setup and nonmultiactive player

Posted: 04 November 2019, 18:28
by tchobello
Hello
I'm working on a 4 player card game and I need some help.

At the beginning, each player give an hidden card to the others and after that, they look at the cards given, accept them and they're added to their hand.
It works well when played live but something doesn't work if I need to refresh after accepting the cards.

It's multiplayer mode and when a player has accepted the cards, the div playertables displaying the 3 cards disappear.

I've added this to the setup in JS file.
It's supposed to remove the div when a player is NonMultiactive.
The problem is :
the div disappear for the next player and not the one whose index is -1 !

Can someone explain me what's wrong ? Thanks

Code: Select all

	if(gamedatas.gamestate.action=='stShowPassedCards')
	{
            for (var pl in gamedatas.playerorder)
		 {
			if (gamedatas.gamestate.multiactive.indexOf(gamedatas.playerorder[pl].toString())==-1)
			{
				dojo.style( 'playertables', 'display', 'none' ); 
			}
			else
			{
				dojo.style( 'playertables', 'display', 'inline-block' );
				for( var i in gamedatas.passedcards ) 
				{
					var card  = gamedatas.passedcards[i];
					if ( card.card_location_arg == this.player_id )
					{
						var color = card.card_type;
						var value = card.card_type_arg;
						var player_passed_from  = card.card_passed_from;
						var x = this.cardwidth * (value - 1);
						var y = this.cardheight * (color - 1);
						dojo.place( 
							this.format_block('jstpl_cardontable', {
							x: x,
							y: y,
							player_id: card.card_location_arg,
							card_id: card.card_id
							}), 'receiveplayertable_' + player_passed_from
						);
					}
				}
			}
		}
	}

Re: JS setup and nonmultiactive player

Posted: 04 November 2019, 19:47
by RicardoRix
which function is that in?

add the line 'debugger;' and hit F12. This will enable you to single step and analyse the variables.

Re: JS setup and nonmultiactive player

Posted: 05 November 2019, 08:22
by tchobello
it's in the Javascript setup.
I will try the 'debugger'...

Re: JS setup and nonmultiactive player

Posted: 05 November 2019, 10:18
by RicardoRix
The setup is a one-shot at startup is called.

I recommend you transfer that code to the onUpdateActionButtons function.

Isn't it likely that after a player has passed a card, that they all need to wait for the next game state before they get their new card?? Like when the current 'pass a card' multiplayeractive state has completed. Also this may likely happen through a notification instead. You kind of need to keep track of changes like this in 2 places, so once while playing the game real-time through notifications and also you need to make sure the user could press F5 at any time so that the setup function also needs to keep tack.

http://en.doc.boardgamearena.com/Game_i ... amename.js

you can use:
this.checkPossibleActions( "my_action_name" )

this.isCurrentPlayerActive()
Returns true if the player on whose browser the code is running is currently active (it's his turn to play).
this.getActivePlayers()
Return an array with the IDs of players who are currently active (or an empty array if there are none).

Re: JS setup and nonmultiactive player

Posted: 05 November 2019, 10:27
by Een
If this is in the setup, my suggestion is to return the list of players for which to display the cards with the getAllDatas function. As discussed in another thread (viewtopic.php?f=12&t=14059) multiactive status is guaranteed only in onUpdateActionButtons.

Re: JS setup and nonmultiactive player

Posted: 06 November 2019, 00:30
by tchobello
well, i'm gonna be more explicit since there was some misunderstanding.

it works well via normal game flow.
State A multi (giveCards)
1-you select the 3 cards to give,
2-you validate,
State B multi (showPassedCards)
3-you receive 3 cards,
4-you accept them, the player tables disappear
the next State begins once all 4 players have accepted.

the problem occurs only when you press f5 on state B after having accepted and still waiting for others to accept.
the problem was : the player tables doesn't disappear.
So I had to add some code in the Javascript Setup.
I've found some code in another source and have adapted as in the code above.
the problem now is that the player tables disappear for the next player (who still has to accept) and not the player who has accepted.

I've found a way to solve this problem by adding :
var pl_bis = (Math.round(pl) + 1) % 4 ;
And the trick is done using pl_bis instead of pl.

but I don't understand at all the line :
if (gamedatas.gamestate.multiactive.indexOf(gamedatas.playerorder[pl_bis].toString())==-1)
And of course, why I had to do this weird trick in order the fonction to work...

Code: Select all

			if(gamedatas.gamestate.action=='stShowPassedCards')
			{
                        for (var pl in gamedatas.playerorder)
				{
					var pl_bis = (Math.round(pl) + 1) % 4 ;

					if (gamedatas.gamestate.multiactive.indexOf(gamedatas.playerorder[pl_bis].toString())==-1)
					{
						dojo.style( 'playertables', 'display', 'none' ); 
					}
					else 
					{
						dojo.style( 'playertables', 'display', 'inline-block' );
						...

Re: JS setup and nonmultiactive player

Posted: 06 November 2019, 00:53
by DrKarotte
I miss some relation to the current player in this code, or is this outside the snippet?
The line with indexOf apparently checks if a player id exists in an array (playerorder). Player order sounds that there is stored information about the players order, so this may explain that the next player is affected.

If I had to do this I would consider adding a simple value to the player database ("has_accepted"). This can easily be set to 1 when a player accepts, and then handed back to the client via the gamedatas.players field.

Re: JS setup and nonmultiactive player

Posted: 06 November 2019, 01:34
by RicardoRix
I think this maybe the same issue as the forum post that Een has linked to.

The solution is to move any multiplayer active check to the onUpdateActionButtons function.

Re: JS setup and nonmultiactive player

Posted: 06 November 2019, 10:06
by tchobello
@drKarotte
pl is 0,1,2,3
gamedatas.playerorder is the list of all 4 players_id starting with the current player.
I think that the result is -1 when the player is non multiactive.

I don't need to add a field 'has_accepted' in the player DB.
I can check how many cards he has in hand : 11 before and 14 after.

Re: JS setup and nonmultiactive player

Posted: 06 November 2019, 10:45
by Een
All data used in the game setup should be returned by getAllDatas:

Code: Select all

/*
        getAllDatas: 
        
        Gather all informations about current game situation (visible by the current player).
        
        The method is called each time the game interface is displayed to a player, ie:
        _ when the game starts
        _ when a player refreshes the game page (F5)
    */
You should not rely on gamedatas.gamestate.multiactive except in onUpdateActionButtons, as explained in the thread I linked to.

If you can know which player is active by the number of cards in hand I think the best options are:
- return the list of players who have 11 cards in hand from getAllDatas and use it in your javascript setup
- or just count the number of cards for each player in your javascript setup.