SolidityLabs/zombie-project/zombiefeeding.sol

59 lines
1.5 KiB
Solidity
Raw Permalink Normal View History

2022-07-18 17:05:51 -03:00
pragma solidity ^0.4.19;
2022-07-18 17:39:12 -03:00
import "./zombiefactory.sol";
2022-07-18 17:05:51 -03:00
2022-07-18 19:52:33 -03:00
contract KittyInterface {
function getKitty(uint256 _id) external view returns (
bool isGestating,
bool isReady,
uint256 cooldownIndex,
uint256 nextActionAt,
uint256 siringWithId,
uint256 birthTime,
uint256 matronId,
uint256 sireId,
uint256 generation,
uint256 genes
);
}
2022-07-18 17:05:51 -03:00
contract ZombieFeeding is ZombieFactory {
2022-07-22 10:48:50 -03:00
2022-07-21 14:53:22 -03:00
KittyInterface kittyContract;
2022-07-18 17:05:51 -03:00
2022-08-05 16:58:35 -03:00
modifier onlyOwnerOf(uint _zombieId) {
2022-08-05 15:29:45 -03:00
require(msg.sender == zombieToOwner[_zombieId]);
_;
}
2022-07-26 17:43:25 -03:00
function setKittyContractAddress(address _address) external onlyOwner {
2022-07-21 14:53:22 -03:00
kittyContract = KittyInterface(_address);
}
2022-07-18 19:52:33 -03:00
2022-08-05 06:15:37 -03:00
function _triggerCooldown(Zombie storage _zombie) internal {
_zombie.readyTime = uint32(now + cooldownTime);
2022-08-05 06:35:00 -03:00
}
2022-08-05 06:15:37 -03:00
function _isReady(Zombie storage _zombie) internal view returns (bool) {
2022-08-05 06:35:00 -03:00
return (_zombie.readyTime <= now);
2022-08-05 06:15:37 -03:00
}
2022-08-05 16:58:35 -03:00
function feedAndMultiply(uint _zombieId, uint _targetDna, string _species) internal onlyOwnerOf(_zombieId) {
2022-07-18 17:39:12 -03:00
Zombie storage myZombie = zombies[_zombieId];
2022-08-05 06:35:00 -03:00
require(_isReady(myZombie));
2022-07-18 17:47:08 -03:00
_targetDna = _targetDna % dnaModulus;
uint newDna = (myZombie.dna + _targetDna) / 2;
2022-07-18 19:52:33 -03:00
if (keccak256(_species) == keccak256("kitty")) {
2022-07-21 14:53:22 -03:00
newDna = newDna - newDna % 100 + 99;
2022-07-18 19:52:33 -03:00
}
2022-07-18 17:47:08 -03:00
_createZombie("NoName", newDna);
2022-08-05 06:35:00 -03:00
_triggerCooldown(myZombie);
2022-07-18 17:39:12 -03:00
}
2022-07-18 19:52:33 -03:00
function feedOnKitty(uint _zombieId, uint _kittyId) public {
uint kittyDna;
(,,,,,,,,,kittyDna) = kittyContract.getKitty(_kittyId);
feedAndMultiply(_zombieId, kittyDna, "kitty");
}
2022-07-18 17:05:51 -03:00
}