Page 1 of 1

[SOLVED] Advise needed on AjaxCalls

Posted: 25 April 2021, 15:20
by Badguizmo
Hello.

In my developping a card game (Riftforce, you guessed right ;) ).
During a certain player action, this player :
  1. Choose a card on his board
  2. Then choose a new destination (on his board)
At this time it calls a JS method that have to
  1. inflict damage to the first card of the leaving space
  2. move the card on the screen
  3. inflict damage to the first card of the destination space (i.e. : destination he just choosed)
Actually my method uses 3 differents Ajax calls one after the other
  1. this.ajaxcall("/riftforce/riftforce/dealDamage.html", ...
  2. this.ajaxcall("/riftforce/riftforce/moveCard.html", ...
  3. this.ajaxcall("/riftforce/riftforce/dealDamage.html", ...
It is working (with error "trying to lock but interface already locked") but I have 3 questions :
  1. is it "better" to chain the AjaxCalls with notifications that chain other AjaxCalls (i.e. : ajaxcall_DealDamage -> notification_Damage -> ajaxcall_move -> notification_Move ->...) or to have all 3 AjaxCalls in the same method ?
  2. do the error "trying to lock but interface already locked" have to be avoided ?
  3. appart from solution 1, is there a way to "wait" for the end of the Ajax call ? Apparently the studio doc does not recommend to make another ajaxcall in a callback in order not to create race conditions (i.e. : by using the return on the ajax call)
Bonus question : I don"t get what is a "race conditions" mentionned in the doc ?

Thank you all.

Re: Advise needed on AjaxCalls

Posted: 25 April 2021, 16:03
by Benoit314
What action is the player actually doing?
In you description, it seems to be moveCard, so you should only have 1 Ajax call, which is moveCard.
Then in the backend you execute all the effects that comes with the action. You can send a lot of notifications in a single Ajax call: you could send a dealDamage notification, then moveCard, then dealDamage again.

What happened in your case is that your first call locks the interface, so when you do more, your interface is still locked. You should really only have 1 call if it is one action.

Re: Advise needed on AjaxCalls

Posted: 25 April 2021, 17:57
by tchobello
I agree with Benoît314.

you should only get one ajaxcall moveCard with card_id and destination_Id...

that's the way to go from Front end to Back end.

once in Back end, you update DB, gameStateValue if needed, get back to Front end with one or several Notify in order to add some animations or update some values displayed on board and then go to another gamestate (end turn or next action...)

Re: Advise needed on AjaxCalls

Posted: 25 April 2021, 23:24
by Badguizmo
Thanks ;)

I will rewrite (a little) my Ajax Calls to stick to that ^^

Re: [SOLVED] Advise needed on AjaxCalls

Posted: 26 April 2021, 03:44
by tsaunat
Check this page for what it says about client only states for these kind of multistep actions
https://boardgamearena.com/doc/BGA_Studio_Cookbook (under Multi Step Interactions: Select Worker/Place Worker - Using Client States)


By only sending the selection server side after all parts are done you avoid getting stuck in a wierd incomplete (or worse incompletable) move.

Re: [SOLVED] Advise needed on AjaxCalls

Posted: 26 April 2021, 08:31
by Tisaac
Badguizmo wrote: 25 April 2021, 15:20 Bonus question : I don"t get what is a "race conditions" mentionned in the doc ?
Race conditions are linked with async nature of ajax call.
If you run an ajax call THEN another one, you have no guarantee the server will receive these two in the same orders.
That's why BGA recommend using the lock to avoid sending multiple calls, and to unlock interface, you just need to send a notification/change state in backend.


I fully agree with what was said above, you just need to send one ajax call for your action, it should be the backend that contains all the logic and send notifications to update front end. Just consider the frontend does not know anything about the game he is displaying, that will help you figure out how to interface front and back.