Last active
October 15, 2019 23:15
-
-
Save ann-kilzer/ad77c3002cf8e811bb5de6898f9f56a8 to your computer and use it in GitHub Desktop.
Copy and paste into Remix for Stamp Rally Build-Along
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.5.0; | |
contract StampRally { | |
uint8 public numStamps; // number of Stamps in the game | |
string public name; // name of the Stamp Rally | |
address public owner; // Ownership for the game manager of the contract | |
// A StampKey keeps track of every stamp in the game | |
struct StampKey { | |
// The hashedPassphrase keeps the keccak256 encoded passphrase | |
bytes32 hashedPassphrase; | |
string url; | |
string prompt; | |
} | |
StampKey[] internal stampKeys; // All the hashed passphrases are stored here | |
// TODO: 1. Add Constructor | |
// TODO: 2. Add Function Modifiers | |
// TODO: 3. Code to store player's cards | |
// TODO: 4. Hash Functions and stamp collections | |
} // End of Stamp Rally Contract |
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
constructor(uint8 _numStamps, | |
string memory _name) public { | |
numStamps = _numStamps; | |
stampKeys.length = numStamps; | |
name = _name; | |
owner = msg.sender; | |
} |
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
// ensures that only the contract owner can modify the game | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; // Placeholder to run function code | |
} | |
// check _position greater than or equal to 0 and | |
// less than numStamps | |
modifier validPosition(uint8 _position) { | |
// TODO: write me | |
// hint: Look at onlyOwner() as a reference | |
} |
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
// Represents one users Stamp Rally "Card" | |
struct RallyCard { | |
bool[] stamps; | |
} | |
// all cards in the game | |
RallyCard[] internal cards; | |
// links to a Rally Card | |
struct PlayerRallyCard { | |
uint64 id; | |
bool valid; | |
} | |
// lookup table between player addresses and card id | |
mapping (address => PlayerRallyCard) public playerToRallyCard; | |
// Memory Example I | |
function userHasStamp(uint8 _position, address _user) public view validPosition(_position) returns (bool stamp) { | |
PlayerRallyCard memory prc = playerToRallyCard[_user]; | |
if(!prc.valid) { | |
return false; | |
} | |
RallyCard memory rc = cards[prc.id]; | |
return rc.stamps[_position]; | |
} | |
// Memory example II | |
function getStampImage(uint8 _position) public view returns (string memory) { | |
if (userHasStamp(_position, msg.sender)) { | |
StampKey memory sk = stampKeys[_position]; | |
return sk.url; | |
} | |
return ""; | |
} | |
/// @notice Allows the game manager to set up a stamp | |
/// @param _position The index of the stamp | |
/// @param _hashedPassphrase The hashed passphrase from a call to generateHash | |
/// @param _url The URL of the stamp image | |
/// @param _prompt A text hint for the passphrase | |
function setStamp(uint8 _position, | |
bytes32 _hashedPassphrase, | |
string memory _url, | |
string memory _prompt) public validPosition(_position) onlyOwner { | |
// TODO: write me | |
// hint: StampKey ??? s = stampKeys[_position]; | |
} | |
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
function generateHash(string memory _passphrase) public pure returns (bytes32 hashed) { | |
return keccak256(abi.encode(_passphrase)); | |
} | |
function collectStamp(uint8 _position, string memory _passphrase) public validPosition(_position) { | |
PlayerRallyCard memory prc = playerToRallyCard[msg.sender]; | |
if (!prc.valid) { | |
uint64 max = 2**64 - 1; | |
require(cards.length < max, "Reached maximum number of players"); | |
// make a new card | |
uint64 id = uint64(cards.length); | |
bool[] memory s = new bool[](numStamps); | |
cards.push(RallyCard(s)); | |
playerToRallyCard[msg.sender] = PlayerRallyCard(id, true); | |
} | |
// TODO: calculate the hash | |
StampKey memory sk = stampKeys[_position]; | |
if ( /*TODO*/) { // check if hash matches the stored passphrase | |
RallyCard storage rc = cards[prc.id]; // Get the player's card | |
rc.stamps[_position] = true; // record that they have the stamp! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment