doutuo3935 2014-09-10 18:09
浏览 62
已采纳

我如何通过REST API处理游戏操作?

I have a PHP-based game that currently works in your typical request/response fashion, generating and outputting pages. I wish to completely separate the presentation and game logic, so I am porting to be API-based (using Phalcon). This will also allow me to make a mobile app that can interact with the same code base.

I'm new to the whole REST API thing so I am trying to follow the guidelines set forth in RESTful Web Services. It is my understanding that the API should work as follows:

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

However, the client that is consuming my API (via AJAX most likely) should only issue higher-level commands. For example, I don't want a savvy user making an API call that modifies their experience. Instead, data modification would be the side effect of a game action that takes place based on player input.

For example, the client may say "hey, attack this monster", at which point the server will verify that the player has enough energy, carry out the attack, add the gold and experience to the player, and return the result to the client. These actions are very transactional, with multiple conditions needing to be true and multiple results needing to occur in order for the action to be considered successful.

I picture this functioning somewhere along the lines of:

/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, this seems like it would fall under an RPC type of architecture, as defined in the book, which I understand as not the same.

  • How do I handle this scenario?
  • Am I correct in thinking a REST API is appropriate for this? It would seem the nature of me needing to call remote procedures is fundamentally against what I'm reading REST is.
  • Assuming I am approaching this with the wrong mindset - is there some other architecture better suited or should I simply continue with the way I mentioned seeing it work?

Any advice on whether I'm headed in the right direction and what I can do better would be very beneficial to me.

Thank you.

  • 写回答

1条回答 默认 最新

  • douyi0902 2014-09-10 19:18
    关注

    I would send a POST /attack and describe the parameters in the body. You could create links for every opponent. So this might work with simple games, but the server won't be able to push data to your browser, unless you use polling.

    For realtime games an event based approach with websockets is much better...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥20 win10商店接入问题
    • ¥15 java 这种树形框吗
    • ¥40 找同学帮敲Python代码
    • ¥15 MYSQL 订单的商品明细重复计算问题
    • ¥15 微信实时共享位置修改
    • ¥100 TG的session协议号转成直登号号后客户端登录几分钟后自动退出设备
    • ¥50 共模反馈回路的小信号增益
    • ¥15 arduino ssd1306函数与tone函数放歌代码不兼容问题
    • ¥70 0.96版本hbase的row_key里含有双引号,无法deleteall
    • ¥15 诊断性META分析合并效能的检验