How can I handle in-game actions via REST API?

I have a PHP based game that is currently running in your typical request / response mode, generating and rendering pages. I want to completely separate presentation and game logic, so I am porting to the API (using Phalcon ). It will also allow me to create a mobile app that can interact with the same codebase.

I'm new to the whole REST API utility, so I try to follow the guidelines outlined in RESTful Web Services . As I understand it, the API should work like this:

POST to /v1/users adds a new user.
GET to /v1/users returns a list of existing users... and so on.

      

However, the client consuming my API (most likely using AJAX) should only issue higher level commands. For example, I don't want a power user to make an API call that changes their experience. Instead, modifying data will be a side effect of the game action that occurs based on player input.

For example, the client might say "hey, attack this monster", at which point the server will check that the player has enough energy, execute the attack, add gold and experience to the player, and return the result to the client. These actions are highly transactional, where multiple conditions need to be true and multiple results must be executed for the action to be considered successful.

I imagine this works somewhere near:

/v1/attack/player - with the id of the player being a post variable.
/v1/work - with the amount of time being worked as a post variable.

      

However, it looks like it would fall under the RPC type architecture as defined in the book, which I understand as not the same.

  • How should I handle this scenario?
  • Am I correct in my belief that REST API is the right fit for this? It looks like I need me to call the remote procedures, basically against what I read REST.
  • Assuming I am approaching this with the wrong mindset - is there some other architecture more suitable or should I just continue as I mentioned that it works?

Any advice on whether I am heading in the right direction and what I can do better would be very helpful to me.

Thank.

+3


source to share


1 answer


I would send POST /attack

and describe the parameters in the body. You can create links for each opponent. So this can work with simple games, but the server won't be able to send data to your browser if you don't use poll.



For real-time gaming, the event-based approach with websockets is much better ...

0


source







All Articles