Page 1 of 1

Validating specific game actions (with Arguments)

Posted: 09 November 2024, 21:41
by zetaLeonis
TL;DR: Is there a framework method to validate specific action arguments like 'playToken(X, Y)'' and check for valid X and or Y values?

Long version:
Noob question again, bear with me. I was wondering if there's a granular way to check if a move is valid or not.

I'm aware that on the frontend side we have a method to check if an action is possible given the current state of the state machine and all the possible actions of a given state (ie see the code below)

Code: Select all

if (!this.checkAction('myActionName')) {
  return;
}
But I was wondering if there's anything more specific. Let's suppose that in the game I'm coding there's an action called "playToken" that receives two arguments, X and Y, and there's a number of valid X and Y values given the current game state.

It's there any method provided by the framework to cover these?

I'm also aware that In the Reversi tutorial, there's an elaborated method (https://gist.github.com/leocaseiro/a8bc ... 5035937d15) that returns all posible moves and then on the frontend side some board squares are marked as "possibleMove" by adding a class attribute. Is this the recommended approach?

Thanks in advance

Re: Validating specific game actions (with Arguments)

Posted: 10 November 2024, 06:54
by imralav
I don't think there is a mechanism you have in mind. You need to have your own code do the validation, considering the game's rules.

As for the general approach to give frontend a tip on which moves are available now, which cards can be played and so on, I am too young here to be 100% certain, but it makes a lot of sense to have it done like this. It's the server that holds all the game rules, it must ensure everything is correct and uncorrupted. So it is also the best candidate to decide what can be done in given state, as it will validate it later anyway. It helps keep the frontend focused on presentation and make it "dumber" when it comes to game rules.

Re: Validating specific game actions (with Arguments)

Posted: 11 November 2024, 00:23
by Victoria_La
* No - there is no function in framework, its game specific (and simple)
* Yes get possible moves is recommended way
* BUT you have to validate args on the server anyways, because you cannot trust client

Usually it looks like this - where checkValid you own function, in this case it just case that $card in array of possible_moves

Code: Select all

function action_playCard($card) {
       $this->checkAction('playCard');
       $this->checkValid($card, $this->arg_playCard());
       ...
}
function arg_playCard() {
   return ['possible_moves'=>$this->getPossibleMovesForPlayCard($this->getActivePlayerId())];
}

Re: Validating specific game actions (with Arguments)

Posted: 11 November 2024, 11:08
by thoun
One thing I often do is calling the arg function in the action

Code: Select all

$args = $this->argPlayCard()
So my $args contains all the informations I send to the front, listing valid moves.
Then something like this will check if the argument is valid :

Code: Select all

if (in_array($move, $args['possibleMoves'])

Re: Validating specific game actions (with Arguments)

Posted: 12 November 2024, 04:28
by zetaLeonis
Thanks the three of you (imralav, Victoria_La and thoun) for the responses.

This is what I'm going to do: first code my own possibleMoves function based on my current game logic, then sent that data to the frontend side (and use that to make some stuff selectively interactive).

In case of any client side action calling the backend, double check the validity of the arguments manually using the same possibleMoves function (probably checking if the move is in an array of possible moves as thoun suggested) (and also have into account who's the active player).

Thanks.

(Edit: typos)