0

I am new to web development. I am building a simple text based web game. I am using heroku and postgresql. I have sql table for users and their coin amount(their earnings).

I can receive/transmit data from this database by using requests made from players. However what I want to achieve is to automate the coin addition to each users account.

So let's say at the beginning of each hour, I want to add 15 coins to each user. How can I possible achieve this kind of automation with heroku and postgresql ?

I tried searching for almost an hour, but I wasn't even able to find the name of this process :(

2 Answers 2

2

While you could schedule this (as sonnyhe notes), it's probably better not to.

Instead, just update the value when you're updating their balance for some other reason, by adding the difference between the time you last added coins and the current time, extracting the hours, and multiplying by 15.

If the user asks to just view the balance, all you need to do is display its last stored value plus the hours since then * 15.

That way you're not doing lots of unnecessary updates and causing unnecessary load.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a gem you can look into.

Just include the gem rufus-scheduler in your gemfile.

The you can set something up in you config/initializer/scheduler.rb

scheduler.every '15m' do
   # Update all coins with 15 more.
end

1 Comment

Thanks for your answer, Craigs answer worked fine for now. If I have to deal with a more complex situation, I will definitely use this gem. Thanks a lot !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.