Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6152329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:54:07+00:00 2026-05-23T19:54:07+00:00

I encountered a code design problem while working on a small application. In terms

  • 0

I encountered a code design problem while working on a small application.

In terms of features, there are a list of tables, each having 2 seats. If two players sit down at the same table, then a game starts.

For this part, I have a tables controller, a table model, and a game state model (creating a game state means a game has started).

When a user sits down, the code fires an Ajax request which is handled by the tables controller, which calls the appropriate method in table model for sitting. If the table model finds out both seats are filled, the game starts, which is the tricky part.

I didn’t want to have the table model call game state model because it feels messy and tracking who is calling the game state model may become difficult later on. So I made the table model return a :success=>true hash to table controller, which determines whether to call game state model.

But then I realize I’m putting logic in the controller, which according to Rails 3 Way, is a no-no.

Can someone tell me what I can do better?

I’m also having problem with "forfeit the game if user disconnects part". Currently the user pulls the table controller in order to let my app know hes still connected. And having that part handle game forfeit seems awkward and coupling.

Additionally, I’m making the JavaScript code do one setInterval pull for each type of resource, in an attempt to keep things modular. But as a result, I’m making 6-7 different AJAX requests every interval. And that seems inefficient.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:54:08+00:00Added an answer on May 23, 2026 at 7:54 pm

    First we need to decide which models know about which other models. In our case, we can probably say something like

    GameState -> Table -> User

    Which means that the model knows everything to the right of it, and knows nothing about the model to the left. This way, we can more easily determine where a lot of logic naturally belongs to, because a User model doesn’t know anything about the Table model itself, it just knows that it belongs to a Table.

    Now, let’s think about the different "states" we have in a game.

    1. Pre-game state where a table being waiting to be filled
    2. Game state, and of course this will a lot of sub states
    3. After game, scores are counted, variables are updated

    With regards to #1, our first guess that this belongs to the Table. It should only know about itself and the state that itself is in. But it’s only role is that it has two seats, and it can be filled. It shouldn’t know about when a game can be started or anything. What does this mean? We actually need to delegate the job to the GameState, because the pre-game is also a "state" of the game. The GameState would be the "gatekeeper" if you will, and the Table is just a pawn. With that being said, it’ll be a good idea to have your GameState model call your Table model to SEE it can go ahead and start the game. When a user clicks to join a table, it will go to the GameState controller (make sure your logic belongs in the model, so that your controller is just calling methods in your model). The GameState controller will attempt to add this User to the Table and see if it can start a game (the table merely returns if all seats have been filled). If so, it send the right information back to the client and says, "Okay! Start!".

    Once the game has started, then it’s up to the GameState to manipulate itself and the data that belongs to it (the Table and Users if need be). Once the game is finished, GameState cleans itself up (along with it’s members) and archives itself into the db. So all-in-all it would seem like GameState overlooks the whole process, and Tables/Users are just data that GameState manipulates.

    With regards to user disconnecting, it can be hard to say what’s the right thing to do without much context. But if I were to make something like this, it doesn’t seem like you need polling. What I can think of is a user either navigates away from the page (either closing the browser, typing new url, or clicking on a link), you can use unload() to send a request to the server telling you that the user left. Another way would be if a user clicks "disconnect", which is also another request sent to the server.

    In terms of having to send 6-7 AJAX requests every interval seems a little excessive. If you really want to, you should package up all your resources into one object, send one object every interval, let the server manipulate it, then handle it when the object comes back. But it would also seem like you need not all these polling. The only thing you need to do is validation, and encryption if you want, to make sure that GameStates are transitioning in a legal way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just landed in a new code base. While working to make the application
I encountered this problem while solving a practice test Consider the following code written
I encountered a problem when running some old code that was handed down to
I recently encountered a situation in some code I am working on that doesn't
I have encountered a weird situation while updating/upgrading some legacy code. I have a
I'm currently coding my design portfolio and have encountered a problem with the layout.
I just encountered this (made up code to demonstrate the problem): public ICollection<string> CreateCollection(int
When doing form validation, I encountered this code: //check each input to ensure it
I encountered the following code in a project that I'm working with. i don't
I encountered some code reading typedef enum eEnum { c1, c2 } tagEnum; typedef

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.