Skip to content

Instantly share code, notes, and snippets.

@IntrepidShape
Last active August 23, 2021 10:23
Show Gist options
  • Save IntrepidShape/f84320d44dde5d628baa722edd0d80e2 to your computer and use it in GitHub Desktop.
Save IntrepidShape/f84320d44dde5d628baa722edd0d80e2 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.4+commit.c7e474f2.js&optimize=false&runs=200&gist=
pragma solidity >=0.7.0 <0.9.0;
/*The whole point of this particular contract is to demonstrate time on the ethereum blockchain,
written in remix IDE by Jake Colson */
contract myGame {
uint public playerCount = 0;
mapping (address => Player) public players;
enum Level {Novice, Intermediate, Advance}
struct Player {
address playerAddress;
Level playerLevel;
string firstName;
string lastName;
uint createdTime;
}
function addPlayer(string memory firstName, string memory lastName) public {
//timestamp the time when this line is run
players[msg.sender] = Player(msg.sender, Level.Novice, firstName, lastName, block.timestamp);
playerCount += 1;
}
function getPlayerLevel(address playerAddress) public view returns(Level){
Player storage player = players[playerAddress];
return player.playerLevel;
}
/* When this function is called, if 20 seconds has elapsed
from the player creation (player instantiation). The players level
is changed from Novice to Intermediate (it will only work
once in this current set up and will not do anything
after it has be succesfully run) */
function changePlayerLevel(address playerAddress) public {
Player storage player = players[playerAddress];
//If current time is greater than or equal to createdTime plus 20 seconds
if (block.timestamp >= player.createdTime + 20) {
player.playerLevel = Level.Intermediate;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment