问题描述:

I'm coding a poker variant game, and need either ruby or rails to run one big loop to keep the game automated. Ie:

  • check every 10 seconds or so that there are enough players in the game;
  • when there are, deal the cards, then count down ~45 seconds, upon which showdown will happen;
  • in my poker variant there are 3 consecutive showdowns - therefore each showdown needs to happen slowly so the players know what's going on: ie 5 seconds to show down the first portion of the hand and declare the winner of that showdown, 5 seconds for the second and 5 seconds the third
  • pause a few seconds after the hand
  • start loop from the top again

I've had a quick look at delayed_job and resque, but they look quite complicated for what I'm trying to do. I've also considered using a javascript/jquery loop to push the server along the loop, but that seems inelegant, seeing as each of the potentially numerous players on a table will be seeking to push the loop along, rather than the server doing it centrally.

Am I missing a fairly simple function, or is it time to look deeper into delayed_job?

网友答案:

You pretty much need some kind daemon process, but you don't necessarily need any library.

You can write your code as a method on some module, and invoke it using rails runner. The method can execute a loop containing sleep <seconds> to execute code and wait. The easy way to stop the process is have it watch for a file to exist in /tmp or something, but you can also do things like write a pid file, and then kill the process later using its pid.

相关阅读:
Top