From e87d92e700277863653237201900d5488e06927d Mon Sep 17 00:00:00 2001 From: Alberto Beltran Date: Wed, 20 Jul 2022 10:33:12 -0500 Subject: [PATCH] #4 added pokemon abilities using a struct array --- contracts/PokemonFactory.sol | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/contracts/PokemonFactory.sol b/contracts/PokemonFactory.sol index 576ac5d1..c1abd237 100644 --- a/contracts/PokemonFactory.sol +++ b/contracts/PokemonFactory.sol @@ -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); }