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...

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化