Stock - Switch items within stock ?

Game development with Board Game Arena Studio
Post Reply
User avatar
klone_rom
Posts: 5
Joined: 04 April 2020, 22:51

Stock - Switch items within stock ?

Post by klone_rom »

Hello,
Please, I look for some help with the stock.
I have 6 items (cards) on my stock displayed in line and I would like to switch 2 items inside this stock.
In php side, to switch 2 cards I use moveCard method and switch their positions (location_arg) in database. It works fine.

Code: Select all

$this->mystock->moveCard($card_id_1, 'onboard', $card_2_location_arg);
$this->mystock->moveCard($card_id_2, 'onboard', $card_1_location_arg);
In js side, there is an issue.
After moveCard in php, I notify js and use slideToObject methode to slide my 2 cards. The animation is fine, cards switch fine.

Code: Select all

this.slideToObject( 'board_card_item_'+notif.args.card_1.id, 'board_card_item_'+notif.args.card_2.id ).play();
this.slideToObject( 'board_card_item_'+notif.args.card_2.id, 'board_card_item_'+notif.args.card_1.id ).play();
But after some tests, I realized than the order of my items is not updated after switch. this.mystock.getAllItems() shows items in exactly same order than before switch.
So the HTML blocks of items are not in the right order than displayed (HTML is like getAllItems, always in before switch order). So my stock is no more the mirror of the display and the database. And for the next actions it is a problem.
My HTML before and after switch:

Code: Select all

<div id="board_card">
    <div id="board_card_item_1" class="stockitem "></div>
    <div id="board_card_item_2" class="stockitem "></div>
</div>
And after screen refresh, the items order is ok:

Code: Select all

<div id="board_card">
    <div id="board_card_item_2" class="stockitem "></div>
    <div id="board_card_item_1" class="stockitem "></div>
</div>
If someone have a clue or solution or alternative solution to switch some items inside the same stock ?

Thank you for your help.
User avatar
Benoit314
Posts: 82
Joined: 02 April 2020, 22:12

Re: Stock - Switch items within stock ?

Post by Benoit314 »

Inside a stock, the order of items is defined by the weight of the items defined when you do "addItemType".

The displayed order will depend on the weight.
The actual HTML item order is the order the items were added to to stock.

You can change the weight of an item with this:

Code: Select all

changeItemsWeight( newWeights )
If the displayed order should be the same as the order items were created, you can use this:

Code: Select all

this.yourstock.order_items = false;
But the fact that you are trying to move your card somewhere else in the hand seems to me that you could just use weight, it will perform the animation for you.
Last edited by Benoit314 on 07 May 2020, 15:42, edited 1 time in total.
User avatar
DrKarotte
Posts: 279
Joined: 22 September 2015, 23:42

Re: Stock - Switch items within stock ?

Post by DrKarotte »

I think you need item weight for stock. This is set when creating the stock and can be changed later using changeItemsWeight(newWeights). The items re-sort automatically then, no need for slides ;)

I now notice that the exact answer has been already written...
User avatar
klone_rom
Posts: 5
Joined: 04 April 2020, 22:51

Re: Stock - Switch items within stock ?

Post by klone_rom »

Thank you very much for your responses.

I had not thought about weight ... and not actually used it until now.

I am going to try this way.
User avatar
klone_rom
Posts: 5
Joined: 04 April 2020, 22:51

Re: Stock - Switch items within stock ?

Post by klone_rom »

Hello,

So after some tries and code refactoring, I realized I am not really understand how to use the weight.

Please, could you have some project examples that use this changeItemsWeight method because I not use it well I think ?

Thank you,
User avatar
Benoit314
Posts: 82
Joined: 02 April 2020, 22:12

Re: Stock - Switch items within stock ?

Post by Benoit314 »

The Hearts tutorial uses weight. Lower cards go to the left.

Left say you pick a card with a weight of 5, it will slide to the right of cards with a weight lower than 5 and to the left of cards with a weight higher than 5.

Maybe you could explain what should be the order of cards in the context of your game? ;)
User avatar
klone_rom
Posts: 5
Joined: 04 April 2020, 22:51

Re: Stock - Switch items within stock ?

Post by klone_rom »

I had probably read too quickly and not fully understood that step, when I have followed the hearts tutorial.
Thank Benoit314 for your clarification.

After some others tests. It works ! :D

To help people who would have the same questions and answer to benoit314, here is my context and what I did :
To schematize my situation:
Imagine 4 players. Each player has a color that corresponds to a card. At the beginning of the game, the player has 5 cards of that color (player 1 has 5 blue cards, player 2 has 5 red cards ...).
All cards are equally important, so it is there positions in table that is important.
The game begin with 6 cards put on table and each player will be successively able to switch these cards.

My issue was, animations worked fine but stock was not updated after switching cards. So when I refreshed my page all the changes were lost. My stock was not in phasis with my database.

Solution was effectively using weight.

So what I did ?
First, I order my stock with the positions of my cards as weight.
And after each switch I update the weight of 2 switching cards with changeItemsWeight method.
After each switch I send to js the new positions (location_arg) of the 2 switching cards. And I call changeItemsWeight method with location_arg field from Deck as weight.
Thus, I am sure that my stock is updated and in phasis with database card informations.

Hoping that this will help,
Post Reply

Return to “Developers”