Stock's getSelectedItems returns invalid types

Game development with Board Game Arena Studio
Post Reply
User avatar
JCase16
Posts: 116
Joined: 26 June 2015, 19:07

Stock's getSelectedItems returns invalid types

Post by JCase16 »

I'm in my games JS file. I have built a function that will react when someone clicks a card. When clicked I am printing to the console my hand and the results of getSelectedItems:

console.log(this.gamedatas.hand);
console.log(this.playerHand.getSelectedItems());

The problem is that they don't match. The card IDs do but not the types. I selected the 8 of Diamonds. So I get this in my hand printout:
{id: "3", type: "4", type_arg: "8", location: "hand", location_arg: "2300762"}

and I get this from the selected items:
{id: "3", type: 45}

So the IDs match with the 3 but why would I get a type of 45 from the getSelectedItems function ? I'm confused as to what this is and the wiki isn't super helpful as it simply says that this function "Return the list of selected items, as an array with the following format" and shows ids and types. This seems super odd.

I need to know what card is being selected by suit and number. Is there a way in the JS file to do this? I can see how I would do it with the deck library used in the game.php file with getCard but how can this be done from the JS file?

Sorry again if this is a very basic thing and I'm simply not catching what should happen.
User avatar
Victoria_La
Posts: 619
Joined: 28 December 2015, 20:55

Re: Stock's getSelectedItems returns invalid types

Post by Victoria_La »

These are two different things this.gamedatas.hand is data structure of your own creation send from server, which reflects
database rows of deck table.
this.playerHand is probably is stock object, which has its own structure id's and so on
Stock wiki does explain what type is (search for type)
http://en.doc.boardgamearena.com/Stock

Stock does not match directly with Deck
In case its from hearts turials type is probably something like
var card_type_id = this.getCardUniqueId( color, value );
Which is color * 4 + value - 1 (or similar)

So if you need the card type you can either decode it back from stock type, or lookup by id from hand data
User avatar
JCase16
Posts: 116
Joined: 26 June 2015, 19:07

Re: Stock's getSelectedItems returns invalid types

Post by JCase16 »

Thanks but I'm still confused. How do I do this:
"So if you need the card type you can either decode it back from stock type, or lookup by id from hand data"

I see no function I can call to get the suit and number of the card when what I have access to is this:
{id: "3", type: 45}

How do I take the id of 3 and get back that this is the 8 of diamonds?
User avatar
fa81
Posts: 32
Joined: 25 November 2016, 16:35

Re: Stock's getSelectedItems returns invalid types

Post by fa81 »

JCase16 wrote:I see no function I can call to get the suit and number of the card when what I have access to is this:
{id: "3", type: 45}

How do I take the id of 3 and get back that this is the 8 of diamonds?
You cannot. Not from the id (3) at least. The id of a Stock component's item is "transparent" to you. An internal id for the Stock component to work. It could be 812987291, you don't care.

The type is what you set. I also found the Hearts tutorial a bit confusing in the beginning. You have to come up with a (clever) way to recognize one of your cards from a "type id" (or type_arg, how it's called in the back-ends Deck helper).

Either you assign these type ids manually, e.g. I have some cards in material.inc.php

Code: Select all

$this->animal_cards = array(
  1 => array('name' => clienttranslate('Cat'),...
  2 => array('name' => clienttranslate('Dog'),...
  3 => array('name' => clienttranslate('Mouse'),...
  ...
And then, when you create the Stock in the frontend:

Code: Select all

            this.animals = new ebg.stock();
            ...
            for (var i = 1; i <= 42; i++) {
                var pos_in_img = i - 1;  // it's zero-based
                this.animals.addItemType(i, 0, g_gamethemeurl + 'img/animals.jpg', pos_in_img);
            }
Then later, when you get your {id: "dontcarewhatever", type: 2}, you look into your gamedatas.animal_cards (which you passed from PHP to JS), and know that type=2 means "Dog" card.

Or, and this is how the Hearts tutorial does it, you come up with a clever system/calculation how to "interpret" type ids to card values, e.g.

Blue animals: 1 (Cat), 2 (Dog), 3 (Mouse),...
Red animals: 11 cat, 12 dog, 13 mouse,...
Yellow animals: 21 cat, 22 dog, 23 mouse,...

Then you know, when the stock components tells you {id: 98765, type:12} was selected, that it is the Red Dog.

Hope this helps.

Sorry for bad formatting. I'm lazy ;)
User avatar
JCase16
Posts: 116
Joined: 26 June 2015, 19:07

Re: Stock's getSelectedItems returns invalid types

Post by JCase16 »

Thank you! So what you are telling me is that I must create a hack from the php that will be carried into the JS so that I can know what's going on unless I want to design a logical way to figure it out on my own.

understood. thank you. I will go with option A because it sounds like once I build the logic for it that I will be able to utilize that solution in any other game I build more easily than option 2.

You guys are the best!
User avatar
Victoria_La
Posts: 619
Joined: 28 December 2015, 20:55

Re: Stock's getSelectedItems returns invalid types

Post by Victoria_La »

Personally I find Stock component pretty confusing and not flexible, so I just create
divs directly and place them in container with display: inline-block
which works amazing especially with css animations. Check Lewis & Clark code for example.
Post Reply

Return to “Developers”