2

I'm working on writing a poker room game in ruby. I'd like to set a time limit on how long each player has to decide what their play is, at the end of which period the game will decide what happens next. (did this particular player time out? did everyone fold? is this the end of the game? etc)

I'd like to poll what's happening on the server from the user's side with JS, but how do I make the server run a background task that advances the state of the game every N minutes? (a value that could be different per each poker room)

dsp_099
  • 5,801
  • 17
  • 72
  • 128
  • Isn't this a JS question rather than Ruby? – sawa Feb 28 '14 at 22:07
  • I should have clarified: client-side Js I can deal with. I just don't know how to make a rails server check on something periodically. – dsp_099 Feb 28 '14 at 22:11
  • 2
    Unless you are using something like websocket, you cannot poll from the server side. It is done by the client side. And if you are using something like websocket, then there is no point in doing polling. – sawa Feb 28 '14 at 22:16

2 Answers2

1

This is quite a hard problem to solve. Here might be one of the easiest ways to solve it. Using a background scheduling. For example with sidekiq.

You can schedule a job to update the game state like:

class GameTimoutTrigger
  include Sidekiq::Worker

  def perform(game_id)
    game = Game.find(game_id)
    game.timeout!
  end
end

And when a game round starts you schedule the timeout trigger

round_timeout = game.round_interval
GameTimoutTrigger.perform_in(round_timeout, game.id)

Be aware that sidekiq will poll jobs in the schedule on intervals (15 secs by default). So there will be always some delay on the timeout.

If the round time must be always exactly 60 seconds for example, you could save the round start and stop timestamps, and user's action timestamp. And only accept user action if it's between the round range. But this might be not necessary. It's just a heads up.

Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75
0

You got the right idea with setInterval().

window.setInterval("javascript function",milliseconds);

The "javascript function" can update the game on the client side and make a request to the server to update. Make sure to clearInterval() if you want to prevent the function from running.

If you're really looking for a way to make the server run a background task, you can maybe try the delayed-jobs gem, but websockets or setInterval is probably a better way to go.

leesungchul
  • 111
  • 1
  • Apparently I suck at wording my questions. I'm asking more about, how to advance the game on the server - clients asking a controller what the new state of the game is - that's a little different. Shouldn't have written anything about JS in the question, my bad. – dsp_099 Feb 28 '14 at 22:31