Context
I'm making a game in ruby. It has a model named Character with attributes like energy
and money
. A Character can have a behavior, e.g. sleeping
. Finally the Character has a tick
method that calls its behavior method sleep!
. Simplified it looks like this:
class Character
attr_accessor :energy
def initialize(behavior)
@current_behavior = behavior
end
def tick
self.send "#{@current_behavior}!"
end
private
def sleep!
self.energy -= 1 if energy > 0
end
end
As in a lot of games the tick method of every Character needs to be invoked every n minutes. I want to use EventMachine for this. In a periodic timer it calls Character.tick_all
that should invoke the tick
method of every Character instance.
Question
What kind of persistence engine can I use for this? On server startup I want to load all Character instances in memory so they can be ticked. For now its ok if every instance gets persisted after its state changes because of a tick.
I've tried it with Rails and ActiveRecord. But it requires at least one write and read action for every tick which seems a bit of an overkill.
Edit I've looked into SuperModel. It seems to do exactly what I want, but it's last commit was about a year ago...