Skip to content

Instantly share code, notes, and snippets.

@sesameJar
Last active July 25, 2019 23:50
Show Gist options
  • Save sesameJar/24539e06923039a682259a126064ee10 to your computer and use it in GitHub Desktop.
Save sesameJar/24539e06923039a682259a126064ee10 to your computer and use it in GitHub Desktop.
pragma solidity ^0.5.0;
// pragma experimental ABIEncoderV2;
contract CoinFlip {
uint public winner;
mapping (uint => player) public players;
uint public wager;
struct player {
uint id;
address payable playerAddress;
uint playerWager;
uint timesamp;
}
enum gameModes {NOT_BEGIN ,WAGER_MADE, WAGER_ACCEPTED}
gameModes public mode = gameModes.NOT_BEGIN;
event gameBegan(uint amount,gameModes mode);
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() private view returns(bool) {
// MODIFY ME to randomly return either true or false
if (now %2 ==0)return true;
else return false;
}
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);
players[2]= player(2,tx.origin,msg.value,now);
mode=gameModes.WAGER_ACCEPTED;
if(generateRandomBit()) {
winner = 1;
players[1].playerWager +=wager;
players[2].playerWager = 0;
}
else {
winner = 2;
players[2].playerWager +=wager;
players[1].playerWager = 0;
}
}
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 {
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 {
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