// You'll have to handle balances yourself, lets assume you have them in an object like the one below which is an example.
var balances = {
"cainy393" : 0.7,
"raoulbtc" : 2.1,
"admin" : 1.2
};
function startANewRaffle(){
var ticketsRemaining = 1000;
var ticketPrice = 0.01; // In mBTC
var prize = 10; // In mBTC
var tickets = [];
// Announce a new raffle has started and display the above variables.
}
function buyTickets(user, numberOfTickets){
if(numberOfTickets <= ticketsRemaining && balances[user] >= ticketPrice * numberOfTickets){
ticketsRemaining = ticketsRemaining - numberOfTickets;
for(var i=0;i<numberOfTickets;i++){
tickets.push(user);
balances[user] = balances[user] - ticketPrice;
}
// Tell the user they have successfully bought their tickets and maybe state how may are left?
// Now we need to check if the tickets are sold out.
if(ticketsRemaining == 0){
endRaffle();
}
}else{
// Tell the user there aren't enough tickets left or that they haven't got enough money.
}
}
function endRaffle(){
var winner = tickets[(Math.random()*tickets.length).floor()]
balances[user] = balances[user] + prize;
// Announce the winner and their winnings.
startANewRaffle();
}