[SOLVED]: Automatic turn and interface locking

Game development with Board Game Arena Studio
Post Reply
User avatar
MikeIsHere
Posts: 70
Joined: 30 April 2020, 22:52

[SOLVED]: Automatic turn and interface locking

Post by MikeIsHere » 27 June 2020, 18:06

I have a preference in my game that allows the player to have a card picked for them automatically
This feature works if the state is multiplayeraction but I seem to be coming across a race condition of some kind if the action is activePlayer, because sometimes it works and sometimes it does not

My EnterState looks like

Code: Select all

onEnteringState: function (stateName, args) {
	switch (stateName) {
		case 'cutCard':  
			if (this.player_id == this.getActivePlayerId()) {
				if ($('preference_control_130').value === "1") {
					// only auto cut if auto cut is true and if it is cut card then make sure only the active player can auto cut
					var randomCard = Math.floor(Math.random() * 52)+1;
					this.onDeckSelect(null, randomCard);
					return;
				}
			}

			this.hideElement('#cutCard')
			this.showElement('#deckContainer');
			this.spreadDeck('fullDeck', 40);
			
			break;
		case 'cutDeal':  
			if ($('preference_control_130').value === "0") {
				this.hideElement('#cutCard')
				this.showElement('#deckContainer');
				this.spreadDeck('fullDeck', 52);
			} else {
				// only auto cut if auto cut is true and if it is cut card then make sure only the active player can auto cut
				var randomCard = Math.floor(Math.random() * 40)+1;
				this.onDeckSelect(null, randomCard);
			}
			
		 
			break;
	}
},
As stated the cutDeal seems to always work
The cutCard state though when it calls onDeckSelect. onDeckSelect does a checkAction('cutCard') and fails because the this.interface_locked_by_id still has a guid. I checked it that possible action is in the array, and it is not because the player is not the active player.

The line that fails in ly_studio.js

Code: Select all

checkLock: function(_d8e) {
     if (this.isInterfaceLocked()) {
	if (typeof _d8e == "undefined") {
		this.showMessage(__("lang_mainsite", "Please wait, an action is already in progress"), "error");
	}
	return false;
}
return true;
},
checkAction: function(_d8f, _d90) {
if (!this.checkLock(_d90)) {
	if (typeof _d90 == "undefined" && this.developermode) {
		this.showMessage("(Generated by: checkAction/" + _d8f + ")", "error");
	}
	return false;
}              
 

Code: Select all

onDeckSelect: function (control_name, item_id) {
	var action = this.gamedatas.gamestate.name;
	if (item_id > 0) {
		if (this.checkAction(action)) { 
			this.ajaxcall("/" + this.game_name + "/" + this.game_name + "/" + action + ".html",
			{
				card: item_id,
				lock: true
			}, this, function (result) { }, function (is_error) {}
			);       
		} 
		this.deck.unselectAll();                    
	}
},
So overall my question is why is the interacted locked? note if the page is refreshed then the same code runs, since the state has not changed, and everything works on reload

The error message I get is
Please wait, an action is already in progress
(Generated by: checkAction/cutCard)
Last edited by MikeIsHere on 29 June 2020, 18:06, edited 1 time in total.

User avatar
Inaofr
Posts: 42
Joined: 26 March 2020, 22:26

Re: Automatic turn and interface locking

Post by Inaofr » 27 June 2020, 19:50

I'm afraid I'm not going to solve your issue, as I don't see in the given code a reason for it. But I see other issues:
- random handling should be done on server side, not on client side, as a cheater can edit the client side to choose a card instead of letting it be random; so you should send a cut action without selecting a card and the server side should compute the random card (excepted is there is no advantage to choose a card, but that would be strange).
- in the case 'cutDeal', the comment is saying 'then make sure only the active player' but you don't check the active player.

User avatar
MikeIsHere
Posts: 70
Joined: 30 April 2020, 22:52

Re: Automatic turn and interface locking

Post by MikeIsHere » 27 June 2020, 22:23

Thank you for the reply

In the game, if the user was allowed to pick there own card, it would just send a number from 1 to 40 back to the server. All this option is doing is picking that number for them. Testers have suggested that in turn based games they do not want the game to be held up picking a random card, but some other people want to have a choice in their game

I don't see this as a possible exploit since a random number is the same as the user picking N cards the left.
You can see the choice today in the cribbage game in alpha

https://boardgamearena.com/gamepanel?game=cribbage

Given that I want this user choice to follow them from game to game, and the first action in the game is to cut for deal, I still feel a game preference, which is only client side, is still the correct approach.

In short I just want to simulate the user clicking on a card so they don't have to.

User avatar
MikeIsHere
Posts: 70
Joined: 30 April 2020, 22:52

Re: Automatic turn and interface locking

Post by MikeIsHere » 27 June 2020, 22:27

Though talking things out there may be a flaw in my approach the user would still have to be at the table for the clientSide code to run

User avatar
bdrieu
Posts: 68
Joined: 01 April 2020, 21:14

Re: Automatic turn and interface locking

Post by bdrieu » 27 June 2020, 23:24

I had a similar experience when i put an ajax call into a onEnteringState callback: viewtopic.php?f=12&t=16035

I believe that such case should be handled server side for better efficiency and in order to avoid race conditions.

Benoit314
Posts: 31
Joined: 02 April 2020, 22:12

Re: Automatic turn and interface locking

Post by Benoit314 » 28 June 2020, 06:41

My best guess it still that when the user sends an action (Ajax request), it locks the interface to prevent the user from sending other actions.
I don't really know when the interface is unlocked once it receives a response, but with your problems, I guess it isn't when "OnEnteringState".

I would try to find a way to cut server-side, I suppose preferences are kept in the database?

User avatar
MikeIsHere
Posts: 70
Joined: 30 April 2020, 22:52

Re: Automatic turn and interface locking

Post by MikeIsHere » 29 June 2020, 18:06

So here is what I found and worked for me

When you make an Ajax call without changing state OR sending a notification back -- the interface stays locked
Sending back an empty notification to the player (via NotifyPlayer) that initiated the ajax call seems to unlock the interface

User avatar
Inaofr
Posts: 42
Joined: 26 March 2020, 22:26

Re: [SOLVED]: Automatic turn and interface locking

Post by Inaofr » 30 June 2020, 20:03

Thanks, good to know this.

User avatar
Brainchild
Posts: 67
Joined: 31 January 2014, 19:42

Re: [SOLVED]: Automatic turn and interface locking

Post by Brainchild » 01 July 2020, 08:55

Good info. You can add it to the Troubleshooting page if you think it's likely to block other people.

http://en.doc.boardgamearena.com/Troubleshooting

User avatar
MikeIsHere
Posts: 70
Joined: 30 April 2020, 22:52

Re: [SOLVED]: Automatic turn and interface locking

Post by MikeIsHere » 01 July 2020, 14:48

I will try... my wiki skills are not the best.

Post Reply

Return to “Developers”