Page 1 of 1

Storing and updating list of possible player's actions

Posted: 21 February 2021, 22:59
by Hornir91
Hello.

I came into a problem with managing player actions. Generally speaking how should I store possible actions and check which actions player have made already?

I was thinking about declaring in game.js (in setup) an array to store them and removing each one after player have done it. But there is also a question: will they stay after refreshing the page? Should I made a table in DB and provide it as args for the specific state?

Please let me know your tricks.

Thanks,
Hornir

Re: Storing and updating list of possible player's actions

Posted: 21 February 2021, 23:10
by Tisaac
Hornir91 wrote: 21 February 2021, 22:59 Hello.

I came into a problem with managing player actions. Generally speaking how should I store possible actions and check which actions player have made already?

I was thinking about declaring in game.js (in setup) an array to store them and removing each one after player have done it. But there is also a question: will they stay after refreshing the page? Should I made a table in DB and provide it as args for the specific state?

Please let me know your tricks.

Thanks,
Hornir
Not sure to understand what you mean ? Can you give a more precise example ?

Re: Storing and updating list of possible player's actions

Posted: 21 February 2021, 23:22
by Hornir91
For example: a player can do two actions: Pay gold to get something (one action) and spy on a card (another action). These are possible in one gamestate: PlayerTurn (defined in possibleactions in states.inc.php).

So, when the player pay gold to get something, this action should be considered as done and should be blocked. Now he would be able to do only Spy on a card action.

How should I manage that? Something like declaring an array on a client side (something like this.possibleActions = [] (in setup), and filling it with actions in OnEnteringState()) and after getting notification from doing 'Buy_food', delete this action from the list and update client for a player to block this action and enable only Spy_on_a_card? And after doing these two actions update DB, change the player, etc.

Re: Storing and updating list of possible player's actions

Posted: 22 February 2021, 00:12
by Tisaac
Hornir91 wrote: 21 February 2021, 23:22 For example: a player can do two actions: Pay gold to get something (one action) and spy on a card (another action). These are possible in one gamestate: PlayerTurn (defined in possibleactions in states.inc.php).

So, when the player pay gold to get something, this action should be considered as done and should be blocked. Now he would be able to do only Spy on a card action.

How should I manage that? Something like declaring an array on a client side (something like this.possibleActions = [] (in setup), and filling it with actions in OnEnteringState()) and after getting notification from doing 'Buy_food', delete this action from the list and update client for a player to block this action and enable only Spy_on_a_card? And after doing these two actions update DB, change the player, etc.
You will need to store the actions done in DB and use that to check if the action is possible or not on an ajax call.

Re: Storing and updating list of possible player's actions

Posted: 22 February 2021, 02:27
by BrianLovesMarvel
I'm just getting started, but here's how I'd do it.

The general flow would be like this.
At the beginning of the state, on the server side, you will need to figure out what actions are possible for the player.
In the server side arguments function for the state, send the list of what is possible now in args.
In onUpdateActionButtons on the client, you can use args values to set the action buttons correctly.
When the player clicks on an action button, your onAction event function can send an ajax call for that action.
In the server side action php, you map the onAction ajax call to an action function onAction in game.php.
In the server side game action function you take the action, then adjust the database to show the action is already taken, then restart the action state.

I'd start with the state table.
(game) state 20 start player turn, reset all actions to "not taken", enter state 21
(activeplayer) state 21 player turn, check what actions are not taken
if all actions are taken, you can end turn by going to state 22
otherwise send actions list to client, show the right buttons
As player takes each action, set the action as "taken", then enter state 21 again
state 22 end player turn

I'd define some utility functions:

Code: Select all

setAllActionsAvailable() {...}
setActionTaken( action ) {...}
anyActionsAvailable() {...}
getActionsAvailable() {...}
There are lots of database table designs that could handle this. Here's one idea. You could have a table like this:

Code: Select all

...
player_id int(10)
name varchar(20)
taken tinyint(1)
...
When you set up the game, you'd create the table with all the possible actions for each player.

Once you have the table layout, writing the above functions would not be difficult, and using them makes your code readable. For example:

Code: Select all

function setActionTaken( $action ) { 
    $active_player_id = self::getActivePlayerId();
    $sql = "update actions_table set taken = 1 where player_id = '$active_player_id' and name = '$action'";
    self::DbQuery($sql);
}
Best wishes, Brian

Re: Storing and updating list of possible player's actions

Posted: 23 February 2021, 00:26
by Hornir91
So in general I will need to have additional table in DB to store the actions - which one has been used and which not, with the mark as boolean and load the information to specific gamestate through args. Also in 'game' states to check if go to endplayerturn when they are all used up.

I was thinking making it somehow without using additional table, but now I see it's the must.

Really thanks for both answers, Brian and Tisaac. It was really helpful :D.

Re: Storing and updating list of possible player's actions

Posted: 05 March 2021, 00:00
by tsaunat
I know I'm late for the conversation, but if it's just boolean gamestate settings use the gamestate variables
initGameStateLabels
setGameStateInitialValue
getGameState
setGameState

Outlined here in the documentation https://en.doc.boardgamearena.com/Main_ ... e.game.php