I'm working in a rest API for a turned based game from scratch, and I'm having some troubles figuring out the best architecture to do it.

I need to explain how the game works and what is my architecture so you can help me out.

The game is a turned based board game with up to 4 characters that can interact with eachother in various ways in the game. Everything is decided in the backend and returned to the game via the API.

In the backend I have the following entities:

  • Board, that holds all sorts of map details like terrain type, items, etc.
  • User, that holds the actual data or the user (nickname, email, password, etc).
  • Player, that holds things like health, skills, etc.
  • Grenade, a special power that players can use one each round.

I have just a few more entities, but lets keep it simple for the purpose of this question.

So I have a endpoint called start-game that returns all the initial stats of the game, and here is where my problem begins. Since I need to send everything to the game, I came up with a structure like this:

    gameData: {
        board: {
            players: [], // all players data
            terrain: {}, // all terrain data
    userData: {} // user data, nickname, etc

I don't really know where to put the Grenade, because we throw the grenade at the board, so I feel like it should be a node inside board, but also it is a item used by the Player, so in my classes diagram, I have Grenade inside Player.

Also, I don't have the wrappers "gameData" neither "userData" in my classes, it is a wrapper created specially to make the API return more readable.

My question is: How should I map my API responses to? I heard that I should always map my responses to a class in my code, but if I do that, the API response look weird and also will "leak" a lot of unused data to the game.

It's not only the grenade problem, but also the custom wrappers that I have in place in the returns, are those considered bad practice? If yes, what is the best way to handle my case?

I'm also not using database yet, so I'm wondering if I should have a database diagram before diving into API responses.

Since this is my first time writing a API, I want to do it in the most scalable and proper way.

EDIT: Why the downvotes?

  • 1
    It depends. If the grenade "special power" has to be earned and a player may have one ready or not then you need to indicate that info in the player info. Once they throw it and it is on the board and has yet not activated then it is no longer in the players possession. When it is in the board's possession then you need to indicate that info in the board info. Maybe you need to give more information about how the grenade "special power" works Feb 19 '20 at 21:49
  • Thanks @JerryJeremiah, the player can throw 2 grenades every turn, always. The grenade itself will damage other players, change the terrain status/sprites an destroy items when thrown. What do you think?
    – Pablo
    Feb 19 '20 at 21:55
  • 1
    So, a player can have 2 or 1 or 0 of them depending on how many they have used. Doesn't the player info need to know how many are left? It sounds like there is never a grenade on the board so never in the board's possession. If the grenade, when thrown, immediately damages things and is never in the board's possession then the board doesn't need to have info about the grenade - just the damage that was done. Feb 19 '20 at 22:01
  • 1
    It's not my game, but I would make it land unactivated and then explode at the start of the next round so that other players could do things (dive out of the way, dive into the way to protect stuff, or even pick it up and throw it back). That only applies to people that act in the same round after the player that threw it - it doesn't apply to people that already acted this round... Feb 19 '20 at 22:02
  • 1
    I wouldn't. If it were me I would only provide data that matters to the client. Otherwise your whole idea of "make the API return more readable" is compromised because all that extra stuff means the client can't tell what is important and what is not. Feb 19 '20 at 23:24

In my mind, a game system would have a world state and actions. If the client performs an action, the resulting world state is returned.

As such, I would not model a grenade on a player if there are no interesting interactions with it. In fact if the grenade explodes immediately, I would just model it as an action and the grenade would not appear in the world state, just the scorched squares or whatever it has caused. Naturally if ticking grenades hang out in the world and can be kicked around or disarmed, then they would need to be an object in the world state.

It might be worth while to study a free game engine like Unity to see how they handle game state.

  • 2
    From the questions I have been asking in the comments it looks like the number of grenades the player has is important but there is nothing else because when it leaves the player it explodes immediately (like you said) Feb 19 '20 at 23:27
  • Even tho users can always throw 2 grenades every turn, we want to send the grenade quantity and some attributes from the backend, in case we want to chance it in the future. But my main problem now is also the custom wrappers that I have in place in the returns, are those considered bad practice? If yes, what is the best way to handle my case?
    – Pablo
    Feb 20 '20 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.