Bitcoin Forum
May 07, 2024, 07:29:02 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: I need help with "invest" function of game.  (Read 1018 times)
KaozTiposta (OP)
Full Member
***
Offline Offline

Activity: 244
Merit: 101


View Profile
September 10, 2014, 08:49:14 PM
Last edit: September 10, 2014, 09:02:49 PM by KaozTiposta
 #1

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.)

Code:
 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 :
Code:
...
user_id
user_name
btc_balance
btc_invested
btc_invested_profit
...

Site schema :
Code:
...
site_bankroll
users_totalprofit
users_totalbets
...
...
1715066942
Hero Member
*
Offline Offline

Posts: 1715066942

View Profile Personal Message (Offline)

Ignore
1715066942
Reply with quote  #2

1715066942
Report to moderator
1715066942
Hero Member
*
Offline Offline

Posts: 1715066942

View Profile Personal Message (Offline)

Ignore
1715066942
Reply with quote  #2

1715066942
Report to moderator
Remember that Bitcoin is still beta software. Don't put all of your money into BTC!
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715066942
Hero Member
*
Offline Offline

Posts: 1715066942

View Profile Personal Message (Offline)

Ignore
1715066942
Reply with quote  #2

1715066942
Report to moderator
1715066942
Hero Member
*
Offline Offline

Posts: 1715066942

View Profile Personal Message (Offline)

Ignore
1715066942
Reply with quote  #2

1715066942
Report to moderator
Cortex7
Full Member
***
Offline Offline

Activity: 238
Merit: 106


View Profile
September 10, 2014, 09:08:26 PM
 #2

I guess you could timestamp all new investments.

Add new user vars:
btc_invested_new
btc_invested_new_time


Then when you calc payouts filter against time.

After payouts are calculated you can zero those user vars and add btc_invested_new to the solid btc_invested.

This will mean you can call Investors_Profits() less frequently without issues.

NOTE: // find site bankroll
will not be a single call but will be part of the function loop.
bankroll will grow as your function processes new investments.
KaozTiposta (OP)
Full Member
***
Offline Offline

Activity: 244
Merit: 101


View Profile
September 10, 2014, 09:18:32 PM
 #3

Thank you so much Cortex7, you saved my days. Your solution looks solid and suitable and I'll (try to) implement it.

Thanks again.
Cortex7
Full Member
***
Offline Offline

Activity: 238
Merit: 106


View Profile
September 11, 2014, 01:20:22 AM
Last edit: September 11, 2014, 01:51:28 AM by Cortex7
 #4

You're welcome. OK, thinking a little more, just to correct what i said about "filter against time"... that is wrong!

You need to loop through new_investments (ordered by earliest time) as you process them and add to the bankroll.

And I guess all bets will need to be timestamped:

pseudocode:

Code:
BANKROLL = bankroll now
T1 = last time this func called

loop through NEW_INVESTS[] by time
{
   A_NOW = NEW_INVEST[NOW]->AMOUNT
   if A_NOW > 0
   {
      BANKROLL += A_NOW
      T_NOW = NEW_INVEST[NOW]->TIME

      NEW_INVEST[NOW]->AMOUNT = 0
      NEW_INVEST[NOW]->TIME = 0

      PROFIT = 0
      loop through BETS[] from T1 to T_NOW
      {
         PROFIT = PROFIT + BETS[NOW]->PROFIT
      }
     calculate shares for all investors based on BANKROLL and PROFIT
     T1 = T_NOW
   }
}


So with the above method you're server only needs to work hard at playing and storing all timestamped bets as they occur. You could run profit share every day or hour.

Now you also need to cover the issue that an investor might invest twice (at different times) between calls to your function. I suppose a simple workaround would be to disallow new investment for a user until any outstanding investments are processed. Otherwise you will have to implement a queue to hold timestamped new investment entries in user schema.

EDIT: Actually a better structure might be to make a new table just for timestamped new investments, have an entry that contains user ID, then just process that table and empty it when done, this would elegantly solve single user making several investments quickly. The table would grow down over time as new investments were made so you wouldn't even need to order it before processing.

  
KaozTiposta (OP)
Full Member
***
Offline Offline

Activity: 244
Merit: 101


View Profile
September 11, 2014, 05:53:21 PM
 #5

I'll make some modifications and will notice you.

You saved my brain, I want to hug you. :-)
Miitch
Full Member
***
Offline Offline

Activity: 126
Merit: 100


View Profile
September 11, 2014, 08:04:32 PM
 #6

EDIT: Actually a better structure might be to make a new table just for timestamped new investments, have an entry that contains user ID, then just process that table and empty it when done, this would elegantly solve single user making several investments quickly. The table would grow down over time as new investments were made so you wouldn't even need to order it before processing.
+1 to this. You will ideally need a one to many relationship (one investor has many investments (or transactions)). Think of the use case where someone puts in X BTC and then takes out a smaller subset of that investment later. All deposits/withdrawals must be timestamped so you can accurately check what proceeds are fairly payable to each investor at any point in time.

▇ ▇ ▇ ▇ ▇ ▇ ▇ ▇ ▇   COIN-SWEEPER
▇ ▇ ▇ ▇ ▇ ▇ ▇ ▇    Avoid Bombs Win BIG
▇ ▇ ▇ ▇ ▇ ▇ ▇ ▇   1% House Edge   ▇ ▇ ▇ ▇ ▇ ▇ ▇
KaozTiposta (OP)
Full Member
***
Offline Offline

Activity: 244
Merit: 101


View Profile
September 12, 2014, 10:48:30 AM
Last edit: September 15, 2014, 08:46:51 AM by KaozTiposta
 #7

Edit: Thanks to @CoinRollin I made it.
CoinRollin
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
September 12, 2014, 09:42:02 PM
Last edit: September 12, 2014, 10:16:45 PM by CoinRollin
 #8

I suggest you:

1-Make a snapshot of bankroll when an invest made
2-Make a snapshot of bankroll profit when an invest made
3- Then you can dynamically calculate in for loop of your investments table. This should give you profit of investors.

Code:
(((investment[i].amount) / Bankroll -Bankroll_Snapshot[i]) * (Bankroll_profit - Bankroll_profit_Snapshot[i])).toFixed(8)

Your investments schema:

Code:
user_id  //who
amount  //amount
site_profit_SNAPSHOT  //snapshot of the site profit when it's made
site_bankroll_SNAPSHOT  //snapshot of the bankroll profit when it's made
inv_profit  // in begin, it's 0, I'll update this continuous

I will make similar thing for our site but I didn't test it because I'm on tablet. It's something like this.
Let me know if you didn't get it's logic. I'll edit my answer later.

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!