Untitled


SUBMITTED BY: Guest

DATE: Sept. 23, 2013, 8:57 p.m.

FORMAT: Text only

SIZE: 1.3 kB

HITS: 1099

  1. // You'll have to handle balances yourself, lets assume you have them in an object like the one below which is an example.
  2. var balances = {
  3. "cainy393" : 0.7,
  4. "raoulbtc" : 2.1,
  5. "admin" : 1.2
  6. };
  7. function startANewRaffle(){
  8. var ticketsRemaining = 1000;
  9. var ticketPrice = 0.01; // In mBTC
  10. var prize = 10; // In mBTC
  11. var tickets = [];
  12. // Announce a new raffle has started and display the above variables.
  13. }
  14. function buyTickets(user, numberOfTickets){
  15. if(numberOfTickets <= ticketsRemaining && balances[user] >= ticketPrice * numberOfTickets){
  16. ticketsRemaining = ticketsRemaining - numberOfTickets;
  17. for(var i=0;i<numberOfTickets;i++){
  18. tickets.push(user);
  19. balances[user] = balances[user] - ticketPrice;
  20. }
  21. // Tell the user they have successfully bought their tickets and maybe state how may are left?
  22. // Now we need to check if the tickets are sold out.
  23. if(ticketsRemaining == 0){
  24. endRaffle();
  25. }
  26. }else{
  27. // Tell the user there aren't enough tickets left or that they haven't got enough money.
  28. }
  29. }
  30. function endRaffle(){
  31. var winner = tickets[(Math.random()*tickets.length).floor()]
  32. balances[user] = balances[user] + prize;
  33. // Announce the winner and their winnings.
  34. startANewRaffle();
  35. }

comments powered by Disqus