Created
July 25, 2019 23:35
-
-
Save sesameJar/37390f6ceaa46631e42e7a28d0a2f0cc to your computer and use it in GitHub Desktop.
This file contains hidden or 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.5.0; | |
| // pragma experimental ABIEncoderV2; | |
| import "github.com/oraclize/ethereum-api/provableAPI.sol"; | |
| contract CoinFlip is usingProvable { | |
| constructor() payable public { | |
| } | |
| uint public winner; | |
| mapping (uint => player) public players; | |
| uint public wager; | |
| struct player { | |
| uint id; | |
| address payable playerAddress; | |
| uint playerWager; | |
| uint timesamp; | |
| } | |
| uint coin; | |
| enum gameModes {NOT_BEGIN ,WAGER_MADE, WAGER_ACCEPTED} | |
| gameModes public mode = gameModes.NOT_BEGIN; | |
| event gameBegan(uint amount,gameModes mode); | |
| event queryMade(string result); | |
| modifier notBegan(){ | |
| require(mode == gameModes.NOT_BEGIN); | |
| _; | |
| } | |
| modifier isWagerMade(){ | |
| require(mode == gameModes.WAGER_MADE); | |
| _; | |
| } | |
| modifier isWagerAccepted(){ | |
| require(mode == gameModes.WAGER_ACCEPTED); | |
| _; | |
| } | |
| modifier isWinner(){ | |
| require(tx.origin == players[winner].playerAddress); | |
| _; | |
| } | |
| function generateRandomBit() public payable { | |
| provable_query("WolframAlpha", "random number between 1 and 2"); | |
| emit queryMade("Oraclize query was sent, standing by for the answer.."); | |
| } | |
| function __callback(bytes32 _id, string memory _result) public { | |
| require(msg.sender == provable_cbAddress()); | |
| coin = parseInt(_result); | |
| if(coin != 0) { | |
| winner = coin; | |
| players[coin].playerWager +=wager; | |
| players[coin].playerWager = 0; | |
| } | |
| } | |
| function beginGame() notBegan public payable { | |
| require(msg.value > 0); | |
| players[1] = player(1,tx.origin, msg.value,now); | |
| wager = msg.value; | |
| mode = gameModes.WAGER_MADE; | |
| emit gameBegan(wager,mode); | |
| } | |
| function acceptChallenge() public payable isWagerMade{ | |
| require(msg.value == wager && tx.origin != players[1].playerAddress,"sdsdf"); | |
| players[2]= player(2,tx.origin,msg.value,now); | |
| mode=gameModes.WAGER_ACCEPTED; | |
| generateRandomBit(); | |
| } | |
| function withdrawPrize() public isWinner{ | |
| uint amountToSend = players[winner].playerWager; | |
| players[winner].playerAddress.transfer(amountToSend); | |
| } | |
| } | |
| contract Factory { | |
| uint public currentGameInstance; | |
| mapping (uint =>CoinFlip) public games; | |
| event newInstance(CoinFlip cf); | |
| function deployCoinFlip() public payable { | |
| currentGameInstance++; | |
| CoinFlip cf = new CoinFlip(); | |
| games[currentGameInstance] = cf; | |
| emit newInstance(games[currentGameInstance]); | |
| } | |
| function getGameByID(uint _id) public view returns(CoinFlip){ | |
| return games[_id]; | |
| } | |
| } | |
| contract dashboard{ | |
| Factory gameFacoty; | |
| constructor(address _gameFactory) public { | |
| gameFacoty = Factory(_gameFactory); | |
| } | |
| function newGame() public payable{ | |
| gameFacoty.deployCoinFlip(); | |
| } | |
| function beginGameById(uint _id) public payable { | |
| CoinFlip fg = CoinFlip(gameFacoty.getGameByID(_id)); | |
| fg.beginGame.value(msg.value)(); | |
| } | |
| function acceptChallengeByGameId(uint _id) public payable{ | |
| CoinFlip flipGame = CoinFlip(gameFacoty.getGameByID(_id)); | |
| flipGame.acceptChallenge.value(msg.value)(); | |
| } | |
| function withdrawPrizeByGameId(uint _id) public { | |
| CoinFlip flipGame = CoinFlip(gameFacoty.getGameByID(_id)); | |
| flipGame.withdrawPrize(); | |
| } | |
| function getWageByGameId(uint _id) public view { | |
| CoinFlip flipGame = CoinFlip(gameFacoty.getGameByID(_id)); | |
| flipGame.wager(); | |
| } | |
| function checkModeByGameId(uint _id) public view { | |
| CoinFlip flipGame = CoinFlip(gameFacoty.getGameByID(_id)); | |
| flipGame.mode(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment