-
-
Save Abstrct/dcb3fa2e7fb974f9040714580d351256 to your computer and use it in GitHub Desktop.
The ethereum tamagotchi-like smart 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
pragma solidity ^0.4.17; | |
contract doroidotchiBasuketto { | |
int constant hungerPerBlock = 1; | |
int constant boredomPerBlock = 2; | |
int constant energyPerBlock = 2; | |
int constant hungerPerFeed = 4000; | |
int constant boredomPerEntertainment = 2000; | |
int constant energyPerSleep = 8000; | |
struct DoroidotchiStruct { | |
string doroidotchiName; | |
int fed; | |
uint fedBlock; | |
int entertained; | |
uint entertainedBlock; | |
int rested; | |
uint restedBlock; | |
uint blockBorn; | |
bool initiated; | |
} | |
mapping (address => DoroidotchiStruct) public doroidotchi; | |
uint doroidotchiCount = 1; | |
mapping (uint => address) public doroidotchiMap; | |
function createDoroidotchi(string doroidotchiName) external { | |
address parent = msg.sender; | |
require (!doroidotchi[parent].initiated); | |
require (bytes(doroidotchiName).length > 0); | |
doroidotchi[parent].initiated = true; | |
doroidotchi[parent].doroidotchiName = doroidotchiName; | |
doroidotchi[parent].fed = 5000; | |
doroidotchi[parent].fedBlock = block.number; | |
doroidotchi[parent].entertained = 7000; | |
doroidotchi[parent].entertainedBlock = block.number; | |
doroidotchi[parent].rested = 90000; | |
doroidotchi[parent].restedBlock = block.number; | |
doroidotchi[parent].blockBorn = block.number; | |
doroidotchiMap[doroidotchiCount++] = msg.sender; | |
} | |
function destroyDoroidotchi() public { | |
require (doroidotchi[msg.sender].initiated); | |
delete doroidotchi[msg.sender]; | |
} | |
function feed() external { | |
require (doroidotchi[msg.sender].initiated); | |
require(!hasDoroidotchiDied(msg.sender)); | |
doroidotchi[msg.sender].fed = hungerPerFeed + doroidotchi[msg.sender].fed - calcHungerSince(msg.sender); | |
doroidotchi[msg.sender].fedBlock = block.number; | |
} | |
function play() external { | |
require (doroidotchi[msg.sender].initiated); | |
require(!hasDoroidotchiDied(msg.sender)); | |
doroidotchi[msg.sender].entertained = boredomPerEntertainment + doroidotchi[msg.sender].entertained - calcBoredomSince(msg.sender); | |
doroidotchi[msg.sender].entertainedBlock = block.number; | |
} | |
function sleep() external { | |
require (doroidotchi[msg.sender].initiated); | |
require(!hasDoroidotchiDied(msg.sender)); | |
doroidotchi[msg.sender].rested = energyPerSleep + doroidotchi[msg.sender].rested - calcEnergySince(msg.sender); | |
doroidotchi[msg.sender].restedBlock = block.number; | |
} | |
function getDoroidotchi(address parent) public view | |
returns ( | |
string doroidotchiName, | |
int fed, | |
int entertained, | |
int rested, | |
uint age, | |
bool isDead | |
) { | |
require (doroidotchi[parent].initiated); | |
return ( | |
doroidotchi[parent].doroidotchiName, | |
doroidotchi[parent].fed - calcHungerSince(parent), | |
doroidotchi[parent].entertained - calcBoredomSince(parent), | |
doroidotchi[parent].rested - calcEnergySince(parent), | |
block.number - doroidotchi[parent].blockBorn, | |
hasDoroidotchiDied(parent) | |
); | |
} | |
function calcHungerSince(address parent) public view returns (int amount){ | |
return int(block.number - doroidotchi[parent].fedBlock) * hungerPerBlock; | |
} | |
function calcBoredomSince(address parent) public view returns (int amount){ | |
return int(block.number - doroidotchi[parent].entertainedBlock) * boredomPerBlock; | |
} | |
function calcEnergySince(address parent) public view returns (int amount){ | |
return int(block.number - doroidotchi[parent].restedBlock) * energyPerBlock; | |
} | |
function hasDoroidotchiDied(address parent) public view returns (bool isDead) { | |
require (doroidotchi[parent].initiated); | |
int fed = doroidotchi[parent].fed - calcHungerSince(parent); | |
int entertained = doroidotchi[parent].entertained - calcBoredomSince(parent); | |
int rested = doroidotchi[parent].rested - calcEnergySince(parent); | |
if ((fed <= 0) || (entertained <= 0) || (rested <= 0) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function transferDoroidotchi(address newParent) public returns (bool isTransfered) { | |
require (doroidotchi[msg.sender].initiated); | |
require (!doroidotchi[newParent].initiated); | |
doroidotchi[newParent] = doroidotchi[msg.sender]; | |
delete doroidotchi[msg.sender]; | |
return true; | |
} | |
function getDoroidotchiListElement(uint listPosition) public view returns (address doroidotchiAddress){ | |
return doroidotchiMap[listPosition]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment