Skip to content

Commit

Permalink
gelopfalcon#4 added pokemon abilities using a struct array
Browse files Browse the repository at this point in the history
  • Loading branch information
albbeltran committed Jul 20, 2022
1 parent e91a6e8 commit e87d92e
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions contracts/PokemonFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,40 @@ contract PokemonFactory {
struct Pokemon {
uint256 id;
string name;
Ability[] abilities;
}

Pokemon[] private pokemons;
struct Ability {
string name;
string description;
}

Pokemon[] public pokemons;

mapping(uint256 => address) public pokemonToOwner;
mapping(uint => address) public pokemonToOwner;
mapping(address => uint256) public ownerPokemonCount;
mapping(address => mapping(uint256 => Pokemon)) ownedPokemons;
mapping(address => mapping(uint => Pokemon)) ownedPokemons;

function createPokemon(string memory _name) public {
function createPokemon(string memory _name, string[] memory _abilityName, string[] memory _abilityDscription) public {
require(bytes(_name).length > 2, "The name must have at least 2 characters.");
uint256 id = pokemons.length;
pokemons.push(Pokemon(id, _name));
require((_abilityName).length == (_abilityDscription).length, "You have to provide the same number of abilitie and description.");

uint id = pokemons.length;
require(bytes((_abilityName)[id]).length > 3, "The name must have at least 3 characters.");
require(bytes((_abilityDscription)[id]).length > 5, "The description must have at least 5 characters.");

pokemons.push();
pokemons[id].id = id;
pokemons[id].name = _name;

for(uint i=0; i<_abilityName.length; i++) {
pokemons[id].abilities.push(
Ability(_abilityName[i], _abilityDscription[i])
);
}

pokemonToOwner[id] = msg.sender;
ownerPokemonCount[msg.sender]++;
ownedPokemons[msg.sender][ownerPokemonCount[msg.sender]] = pokemons[id];
emit eventNewPokemon(id, _name);
}

Expand Down

0 comments on commit e87d92e

Please sign in to comment.