2022-07-17 20:48:20 -03:00
|
|
|
pragma solidity ^0.4.19;
|
|
|
|
|
|
|
|
contract ZombieFactory {
|
|
|
|
|
|
|
|
event NewZombie(uint zombieId, string name, uint dna);
|
|
|
|
|
|
|
|
uint dnaDigits = 16;
|
|
|
|
uint dnaModulus = 10 ** dnaDigits;
|
|
|
|
|
|
|
|
struct Zombie {
|
|
|
|
string name;
|
|
|
|
uint dna;
|
|
|
|
}
|
|
|
|
|
|
|
|
Zombie[] public zombies;
|
|
|
|
|
2022-07-18 08:15:49 -03:00
|
|
|
mapping (uint => address) public zombieToOwner;
|
|
|
|
mapping (address => uint) ownerZombieCount;
|
2022-07-18 12:19:56 -03:00
|
|
|
|
2022-07-17 20:48:20 -03:00
|
|
|
function _createZombie(string _name, uint _dna) private {
|
2022-07-18 12:19:56 -03:00
|
|
|
uint id = zombies.push(Zombie(_name, _dna)) - 1;
|
|
|
|
zombieToOwner[id] = msg.sender;
|
|
|
|
ownerZombieCount[msg.sender]++;
|
2022-07-17 20:48:20 -03:00
|
|
|
NewZombie(id, _name, _dna);
|
2022-07-18 12:19:56 -03:00
|
|
|
}
|
2022-07-17 20:48:20 -03:00
|
|
|
|
|
|
|
function _generateRandomDna(string _str) private view returns (uint) {
|
|
|
|
uint rand = uint(keccak256(_str));
|
|
|
|
return rand % dnaModulus;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRandomZombie(string _name) public {
|
2022-07-18 12:19:56 -03:00
|
|
|
require(ownerZombieCount[msg.sender] == 0);
|
2022-07-17 20:48:20 -03:00
|
|
|
uint randDna = _generateRandomDna(_name);
|
|
|
|
_createZombie(_name, randDna);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-07-18 12:19:56 -03:00
|
|
|
|
|
|
|
contract ZombieFeeding is ZombieFactory {}
|