I'm working through how best to utilize firebase as a backend for my turn based rpg style game. The difficulty is that rather than turns taking place consecutively (p1 does his turn, it completes, then p2 does his turn) the players actions take place concurrently (both players confirm the moves they want to make on the next turn, and once both players have confirmed, the backend resolves them at the same time)
Think of it like a pokemon battle. Both players have to decide which move to take, and once both have decided, the moves resolve at the same time.
I have an idea for how to solve this but it seems heavy handed.
I'm going to have 2 firebase functions.
selectSkills - the players send a list of what skills they want to use on this turn (players can use multiple skills in one turn unlike pokemon). This will update a flag saying that they have confirmed their skills. I will have two of these flags under one node in firebase called skillsConfirmed, which will contain entries for p1SkillsConfirmed and p2SkillsConfirmed.
resolveSkills - An onChange function which watches the skillsConfirmed node. When both p1SkillsConfirmed and p2SkillsConfirmed are set to true, it runs the game logic to resolve the skills (calculate damage etc), and then writes a list of the resolvedMoves to the database. It then increments a roundNumber variable in the database (tracks which round the game is on)
The front end will have a listener on this roundNumber, so when it is updated, it will know that moves have been resolved and it needs to display the animations of the skills that were used.
- is there a better way to accomplish this sort of turn based combat? Something more efficient/simpler. Are there any projects I can look at which implement something similar?
- is it possible to return the resolvedMoves to both of the players directly from the resolveSkills function as a JS object like normal, rather than having to write them first to the database, then have the clients read that data. This would avoid having to implement some sort of ordering scheme on the resolvedMoves since firebase does not maintain any sort of order on lists.