应用程序控制器中的 Rails 游戏循环?

发布于 2024-10-19 09:31:26 字数 116 浏览 8 评论 0原文

由于我正在 RoR 中编写游戏,因此我需要一个游戏循环,负责在每次页面刷新时检查不同的内容。我的问题是,最好的实施方式是什么?

我目前将 game_loop 包含在我的应用程序控制器中。这是最佳实践吗?

Since i'm writing a game in RoR, i need to have a game loop that is responsible for checking different things every time a page refresh happens. My question is, what is the best way to implement ?

I currently include the game_loop in my application controller. Is this the best practice ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哎呦我呸! 2024-10-26 09:31:26

执行游戏看起来像 before_filter 在您的 ApplicationController 中听起来很合理,尽管您可能不希望将逻辑放入此类中:

class ApplicationController < ActionController::Base
  before_filter :do_game_loop

  private

    def do_game_loop
      Game.do_game_loop # the implementation of Game is in another file, maybe in lib
    end
end

请注意,这将在应用程序中的每个操作之前执行游戏循环涉及到从 ApplicationController 扩展而来的控制器,包括用户登录、注销等。最好只在肯定需要处理游戏循环的控制器中添加 before_filter

Executing the game look as a before_filter in your ApplicationController sounds reasonable, although you may not wish to put your logic in this class:

class ApplicationController < ActionController::Base
  before_filter :do_game_loop

  private

    def do_game_loop
      Game.do_game_loop # the implementation of Game is in another file, maybe in lib
    end
end

Do note that this will execute the game loop before every action in your application that involves a controller that extends from ApplicationController, including user login, logout, etc. It may be better to add the before_filter only in the controllers that definitely need to process the game loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文