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 vb6.0截取当前窗体保存为jpg文件
  • ¥20 苹果手机不使用大疆sdk怎么获取遥控器控制信息或如何接入大疆sdk并且成功上架sdk
  • ¥15 改动头文件造成的编译错误如何解决
  • ¥20 woocommerce 注册按键重定向
  • ¥100 求书法图像文字切割代码
  • ¥15 同一个波形探测距离不同的目标,为什么fft之后得到的频谱图会发生移动,不应该时移不改变幅度谱吗(标签-matlab)(相关搜索:matlab仿真)
  • ¥15 Proteus仿真程序只能执行一次
  • ¥15 语音识别websocket报错
  • ¥15 激光器,引脚问题,无法处理
  • ¥20 求写一份!只提交Mapper映射文件 如:UsersMapper.xml