Skip to content

Commit

Permalink
add weakness - challenge gelopfalcon#4
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfcito committed Jul 27, 2022
1 parent 5dcc469 commit 9409e40
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions PokemonFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ contract PokemonFactory {
uint256 id;
string name;
Skill[] skills;
Aweakness[] aweakness;
}

struct Skill {
string name;
string description;
}

struct Aweakness {
WeaknessStatus weaknesName;
}

enum WeaknessStatus {
Fire,
Psychic,
Flying,
Ice
}

Pokemon[] private pokemons;

mapping(uint256 => address) public pokemonToOwner;
Expand All @@ -30,13 +42,17 @@ contract PokemonFactory {
string memory _name,
uint256 _id,
string memory _skillName,
string memory _skillDescription
string memory _skillDescription,
WeaknessStatus _weaknessStatus
) public NameMinimunTwoCharacters(_name) IsGreaterThanZero(_id) {
pokemons.push();
uint256 lastIndex = pokemons.length - 1;
pokemons[lastIndex].id = _id;
pokemons[lastIndex].name = _name;
pokemons[lastIndex].skills.push(Skill(_skillName, _skillDescription));
addSkill(_id, _skillName, _skillDescription);
// pokemons[lastIndex].skills.push(Skill(_skillName, _skillDescription));
addAweakness(_id, _weaknessStatus);
// pokemons[lastIndex].aweakness.push(Aweakness(_weaknessStatus));
pokemonIndexed[_id] = lastIndex;

pokemonToOwner[_id] = msg.sender;
Expand Down Expand Up @@ -86,11 +102,13 @@ contract PokemonFactory {
// }
}

modifier onlyExistingPokemon(uint256 _pokemonId) {
require(
bytes(pokemons[pokemonIndexed[_pokemonId]].name).length > 0,
"Please create a Pokemon to add skills."
function addAweakness(uint256 _pokemonId, WeaknessStatus _weaknessStatus)
public
IsGreaterThanZero(_pokemonId)
{
// Solución al buscar directamente en un mapping
pokemons[pokemonIndexed[_pokemonId]].aweakness.push(
Aweakness(_weaknessStatus)
);
_;
}
}

0 comments on commit 9409e40

Please sign in to comment.