Last active
February 2, 2018 20:05
-
-
Save joeb000/4269f209988391371fe1eb61295c3db2 to your computer and use it in GitHub Desktop.
Solidity contract for superbowl squares
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.15; | |
contract SuperbowlSquares { | |
address public owner; | |
bool public isLocked; | |
bool public isRefunded; | |
uint public squareCost = 0.05 ether; | |
mapping (address => uint8) public squareCount; | |
address[10][10] public squares; | |
uint pot; | |
address public winner; | |
uint public r_offset; | |
uint public c_offset; | |
uint8 public homeScore; | |
uint8 public awayScore; | |
modifier onlyOwner { | |
if (owner==msg.sender) { | |
_; | |
} | |
} | |
modifier lockable { | |
if (!isLocked){ | |
_; | |
} | |
} | |
function SuperbowlSquares(){ | |
owner = msg.sender; | |
} | |
function buySquare(uint8 _c, uint8 _r) lockable payable { | |
require (squareCount[msg.sender]<10 && squares[_c][_r]==address(0x0) && msg.value==squareCost); | |
squareCount[msg.sender]++; | |
squares[_c][_r] = msg.sender; | |
pot+=msg.value; | |
} | |
function setOffsetValues() onlyOwner { | |
c_offset = random(block.number); //random enough | |
r_offset = random(block.number-1); //random enough | |
isLocked = true; | |
} | |
function setScoreValues(uint8 _home, uint8 _away) onlyOwner { | |
winner = addressAtSquare(_home,_away); | |
} | |
function claimWinnings() { | |
if (msg.sender==winner) { | |
msg.sender.send(pot); | |
} | |
} | |
function refundAll() lockable onlyOwner { | |
isRefunded = true; | |
isLocked = true; | |
} | |
function claimRefund() { | |
if (isRefunded) { | |
msg.sender.send(squareCount[msg.sender]*squareCost); | |
} | |
} | |
function random(uint nonce) public constant returns (uint){ | |
return uint(sha3(nonce))%(9); | |
} | |
function addressAtSquare(uint8 _col, uint8 _row) public constant returns(address){ | |
uint colNumber = _col + c_offset; | |
uint rowNumber = _row + r_offset; | |
if (colNumber>=10) { | |
colNumber-=10; | |
} | |
if (rowNumber>=10) { | |
rowNumber-=10; | |
} | |
return squares[colNumber][rowNumber]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment