The game uses a standard Rails controller, implemented in app/controllers/game_controller.rb. The controller consists of two actions: The index action

Search code, repositories, users, issues, pull requests...

submited by
Style Pass
2024-07-04 16:30:05

The game uses a standard Rails controller, implemented in app/controllers/game_controller.rb. The controller consists of two actions:

The index action instantiates the game view FlappyView which is then rendered by the view template app/views/game/index.html.xrb. The live action is used to accept a WebSocket connection from the client browser.

When the client connects to the server, it binds the <div class="live" data-class="FlappyView" id="..."> tag to a server side instance. User interactions generate events which are sent to the server, and the server can send HTML to the client to update the view. In addition, for things like sound effects, the server can send JavaScript to the client to execute.

The actual implementation of the game logic consists of a main game loop which updates the game physics at 30 FPS (frames per second), and then renders the update to the client browser. As the client browser may be running at something other than 30 FPS, we use CSS transforms with linear interpolation to smooth out the changes in position.

Surprisingly, this game can run on any Rack 3 compatible server, including both Puma and Falcon. Rack 3 requires support for streaming requests and responses, which is sufficient to support WebSockets. The difference then, between servers, is how they choose to expose concurrency to the application. In the case of Puma, it is one thread per request, while Falcon uses one fiber per request.

Leave a Comment