Created
January 18, 2018 08:02
-
-
Save nihalmpatel/c9ce836d7a77464e317e2e2def324592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.4; | |
contract BetGame { | |
modifier ownerOnly{ | |
if(owner!=msg.sender) revert(); | |
_; | |
} | |
struct Room { | |
address gambler; | |
address[2] bidders; | |
} | |
struct Gambler { | |
string gamblerName; | |
uint8 guessedNumber; | |
uint balance; | |
} | |
struct Bidder { | |
string bidderName; | |
uint8[10] guesses; | |
uint balance; | |
} | |
Room[] public rooms; | |
/*Gambler[] gamblers; | |
Bidder bidders;*/ | |
uint BID_PRICE = 100000000000000000; | |
mapping(address => Gambler) public gamblers; | |
mapping(address => Bidder) public bidders; | |
event payableInvocation(address indexed caller,uint amount); | |
address owner; | |
uint public bidder_count; | |
uint public gambler_count; | |
uint public room_number; | |
function BetGame() payable public { | |
owner=msg.sender; | |
} | |
function() payable public { | |
payableInvocation(msg.sender,msg.value); | |
} | |
// "b",[0,1,2,3,4,5,6,7,8,9] | |
function bid(string nm,uint8[10] guesses) payable public { | |
//require(msg.value >= BID_PRICE); | |
// checking if bidder is already a gambler | |
if(bytes(gamblers[msg.sender].gamblerName).length > 0) { revert(); } | |
// checking if bidder alredy exists | |
if(bytes(bidders[msg.sender].bidderName).length == 0) { | |
Bidder memory b; | |
b.bidderName = nm; | |
b.guesses = guesses; | |
b.balance = msg.value; | |
bidders[msg.sender] = b; | |
addInRoom(msg.sender, true); | |
} else { revert(); } | |
} | |
function gamble(string name,uint8 num) payable public { | |
//require(msg.value >= BID_PRICE); | |
// checking if gambler is already a bidder | |
if(bytes(bidders[msg.sender].bidderName).length > 0) { revert(); } | |
// checking if gambler alredy exists | |
if(bytes(gamblers[msg.sender].gamblerName).length == 0) { | |
Gambler memory g; | |
g.gamblerName = name; | |
g.guessedNumber = num; | |
g.balance = msg.value; | |
gamblers[msg.sender] = g; | |
gambler_count++; | |
addInRoom(msg.sender, false); | |
} else { revert(); } | |
} | |
function addInRoom(address user, bool isBidder) public { | |
if ( isBidder ) { | |
room_number = bidder_count/2; | |
if(room_number>=gambler_count) { | |
if (bidder_count%2==0) { | |
Room memory r; | |
r.bidders[0] = user; | |
rooms.push(r); | |
} else { | |
rooms[room_number].bidders[1]=user; | |
} | |
} else { | |
if (rooms[room_number].bidders[0] == address(0x0)) { | |
rooms[room_number].bidders[0]=user; | |
} else { | |
rooms[room_number].bidders[1]=user; | |
} | |
} | |
bidder_count++; | |
} else { | |
// create new room for gambler | |
if(gambler_count>rooms.length) { | |
Room memory r1; | |
r1.gambler = user; | |
rooms.push(r1); | |
} else { // insert into existing room | |
rooms[gambler_count-1].gambler=user; | |
} | |
} | |
} | |
function withdraw(uint room_no) public returns (bool){ | |
address winner; | |
Room memory current_room = rooms[room_no]; | |
if(current_room.bidders.length==2 && current_room.gambler==address(0)) { | |
uint8 guessed_value = gamblers[current_room.gambler].guessedNumber; | |
// checking if winner is from bidders | |
for(uint8 i=0;i<10;i++) { | |
if (bidders[current_room.bidders[0]].guesses[i] == guessed_value) { | |
winner = current_room.bidders[0]; | |
gamblers[current_room.gambler].balance=0; | |
bidders[current_room.bidders[0]].balance=0; | |
bidders[current_room.bidders[1]].balance=0; | |
winner.transfer(250000000000000000); | |
return true; | |
} | |
if (bidders[current_room.bidders[1]].guesses[i] == guessed_value) { | |
winner = current_room.bidders[1]; | |
gamblers[current_room.gambler].balance=0; | |
bidders[current_room.bidders[0]].balance=0; | |
bidders[current_room.bidders[1]].balance=0; | |
winner.transfer(250000000000000000); | |
return false; | |
} | |
} | |
// else gambler is winner | |
if(winner!=address(0)) { | |
winner = current_room.gambler; | |
gamblers[current_room.gambler].balance=0; | |
bidders[current_room.bidders[0]].balance=0; | |
bidders[current_room.bidders[1]].balance=0; | |
winner.transfer(250000000000000000); | |
return; | |
} | |
} | |
} | |
function contractBalance() view public returns(uint) { | |
return this.balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment