Hey everyone
I've created a dice (gamble) game with JS(not client side). It's so far working smooth (with a few bugs).
Before opening site to all users, I thought it can be best to create an invest function like
JD. I'm trying to make progress and I got stuck in a problem.
The problem is, lets say:A user came and invested 20 btc %40 of bankroll
B user came and invested 30 btc %60 of bankroll
C user played and
lose 10 btc
Worked good so far, now : A user has 24 btc %40 of bankroll (+4)
B user has 36 btc %60 of bankroll (+6)
But,
then if a D user come and invest BTC, site
profit is dividing to 3 with investment rates
according to my function. D user is came last and he
mustn't get any profit.
I can achieve this problem like this : I open investments only every wednesday of week. But I don't want to do this.
How can I set up a working and flawless investment system? I need an example because I can't get it working in my brain too. Please enlighten me or direct me to a document or something.
My function:
(Since game is fully real time, I'm calling this function every a few seconds.)
function Investors_Profits() {
// find users with investment
users.find({
btc_invested ' : { $gt : 0.00000001 } } ).exec(function(err, users){
// find site bankroll
site.find().exec(function(err, site){
for (var i = 0; i < users.length; i++) {
//user profit rate * 100 = % of bankroll
var userProfit_Rate = (users[i].btc_invested.toFixed(8) / site[0].site_bankroll).toFixed(4);
// bankroll profit = users profits * -1
var Site_Profit = site[0].users_totalprofit * -1;
var userProfit_Rate_Final = Site_Profit * userProfit_Rate;
users[i].btc_invested_profit = userProfit_Rate_Final;
users[i].save();
};
})
})
}
User schema :
...
user_id
user_name
btc_balance
btc_invested
btc_invested_profit
...
Site schema :
...
site_bankroll
users_totalprofit
users_totalbets
...
...