diff --git a/contracts/PokemonFactory.sol b/contracts/PokemonFactory.sol index 2926c786..f00ff946 100644 --- a/contracts/PokemonFactory.sol +++ b/contracts/PokemonFactory.sol @@ -3,6 +3,8 @@ pragma solidity >=0.7.0 <0.9.0; contract PokemonFactory { + uint _id; + event eventNewPokemon(Pokemon pokemon); struct Pokemon { @@ -10,6 +12,7 @@ contract PokemonFactory { string name; Ability[] abilities; string[] pokemonType; + string[] weaknesses; } struct Ability { @@ -23,33 +26,48 @@ contract PokemonFactory { mapping(address => uint256) public ownerPokemonCount; mapping(address => mapping(uint => Pokemon)) ownedPokemons; - function createPokemon(string memory _name, string[] memory _abilityName, string[] memory _abilityDscription, string[] memory _type) public { - require(bytes(_name).length > 2, "The name must have at least 2 characters."); - require((_abilityName).length == (_abilityDscription).length, "You have to provide the same number of abilitie and description."); + modifier checkData + (uint _id, string memory _name, string[] memory _abilityName, string[] memory _abilityDescription, string[] memory _type, string[] memory _weaknesses) { + require(bytes(_name).length > 2, "The name must have at least 2 characters."); + require((_abilityName).length == (_abilityDescription).length, "You must provide the name of each ability for each ability 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<_weaknesses.length; i++) { + require(bytes((_weaknesses)[i]).length > 3, "The weakness name must have at least 3 characters."); + } - for(uint i=0; i<_type.length; i++) { - require(bytes((_type)[i]).length > 3, "The type must have at least 3 characters."); - } + for(uint i=0; i<_type.length; i++) { + require(bytes((_type)[i]).length > 3, "The type must have at least 3 characters."); + } - pokemons[_id].pokemonType = _type; + for(uint i=0; i<_abilityName.length; i++) { + require(bytes((_abilityName)[_id]).length > 3, "The ability name must have at least 3 characters."); + require(bytes((_abilityDescription)[_id]).length > 5, "The ability description must have at least 5 characters."); + } - for(uint i=0; i<_abilityName.length; i++) { - pokemons[_id].abilities.push( - Ability(_abilityName[i], _abilityDscription[i]) - ); - } + _; + } + + function createPokemon + (string memory _name, string[] memory _abilityName, string[] memory _abilityDescription, string[] memory _type, string[] memory _weaknesses) + public + checkData(_id, _name, _abilityName, _abilityDescription, _type, _weaknesses) { + _id = pokemons.length; + + pokemons.push(); + pokemons[_id].id = _id; + pokemons[_id].name = _name; + pokemons[_id].pokemonType = _type; + pokemons[_id].weaknesses = _weaknesses; + + for(uint i=0; i<_abilityName.length; i++) { + pokemons[_id].abilities.push( + Ability(_abilityName[i], _abilityDescription[i]) + ); + } - pokemonToOwner[_id] = msg.sender; - ownerPokemonCount[msg.sender]++; - emit eventNewPokemon(pokemons[_id]); + pokemonToOwner[_id] = msg.sender; + ownerPokemonCount[msg.sender]++; + emit eventNewPokemon(pokemons[_id]); } function getAllPokemons() public view returns (Pokemon[] memory) {