-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jim Posen
committed
Oct 27, 2017
1 parent
71b8771
commit 052ee48
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
pragma solidity ^0.4.0; | ||
|
||
contract TheButton { | ||
uint public timeLimit; | ||
uint public lastPress; | ||
mapping(address => uint) public pressTimes; | ||
|
||
event ButtonPress(address pressedBy, uint time); | ||
|
||
function TheButton(uint timeLimit_) public { | ||
timeLimit = timeLimit_; | ||
lastPress = now; | ||
} | ||
|
||
function press(uint expectLastPress) public returns (bool) { | ||
// If the time limit expires, the game is over. | ||
if (lastPress + timeLimit < now) { | ||
return false; | ||
} | ||
|
||
// Don't allow users to press multiple times. | ||
if (pressTimes[msg.sender] != 0) { | ||
return false; | ||
} | ||
|
||
// Check the lastPress is the expected one so that if another user | ||
// presses first, this one is rejected. The user should be notified | ||
// that they should try again. | ||
if (lastPress != expectLastPress) { | ||
return false; | ||
} | ||
|
||
pressTimes[msg.sender] = now - lastPress; | ||
lastPress = now; | ||
ButtonPress(msg.sender, now); | ||
return true; | ||
} | ||
} |