From c4c9896a175758f4637b0678a27dbb59a16ba0a9 Mon Sep 17 00:00:00 2001 From: Alejandro Medina <5almero5@gmail.com> Date: Sun, 24 Jul 2022 12:51:44 -0500 Subject: [PATCH] Add reto #3 Los datos de la tubla deben enviarce asi : [ [ "Nombre de abiilidad", "descriptcion" ] ] --- PokemonFactory.sol | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index 00eba0f5..f214f765 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -7,17 +7,20 @@ contract PokemonFactory { struct Pokemon { uint id; string name; + Ability[] abilities; } - + struct Ability { + string name; + string description; + } Pokemon[] private pokemons; event eventNewPokemon( uint id, string name ); - mapping (uint => address) public pokemonToOwner; mapping (address => uint) ownerPokemonCount; - +// VALIDATIONS modifier validPokemon(string memory _name ,uint _id){ require(_id > 0, "El Id debe ser mayor de 0"); bytes memory strinCheck = bytes(_name); @@ -25,15 +28,23 @@ contract PokemonFactory { require(strinCheck.length > 2,"La longitud del nombre debe ser mayor de 2"); _; } - - function createPokemon (string memory _name, uint _id) validPokemon(_name, _id) public { - pokemons.push(Pokemon(_id, _name)); +////////////// +// + function createPokemon (string memory _name, uint _id, Ability[] memory _abilities) validPokemon(_name, _id) public { + + Pokemon storage pokemon = pokemons.push(); + pokemon.id = _id; + pokemon.name = _name; + for (uint i = 0; i <_abilities.length; i++) { + pokemon.abilities.push(Ability(_abilities[i].name, _abilities[i].description)); + } + pokemonToOwner[_id] = msg.sender; ownerPokemonCount[msg.sender]++; emit eventNewPokemon(_id, _name); } - +// VIEWS function getAllPokemons() public view returns (Pokemon[] memory) { return pokemons; }