From 30e6b96c224db8bc5e5b33174ff8672a4506c037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Mon, 25 Jul 2022 22:58:43 -0500 Subject: [PATCH 1/5] Challenge #1 Challenge #1 - Add an event to emit the new pokemon --- PokemonFactory.sol | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index a3267da1..aa7c170a 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -4,20 +4,23 @@ pragma solidity >=0.7.0 <0.9.0; contract PokemonFactory { - struct Pokemon { - uint id; - string name; - } + struct Pokemon { + uint id; + string name; + } + + event eventNewPokemon(Pokemon pokemon); Pokemon[] private pokemons; mapping (uint => address) public pokemonToOwner; mapping (address => uint) ownerPokemonCount; - function createPokemon (string memory _name, uint _id) public { + function createPokemon (string memory _name, uint _id) public { pokemons.push(Pokemon(_id, _name)); pokemonToOwner[_id] = msg.sender; ownerPokemonCount[msg.sender]++; + emit eventNewPokemon(Pokemon(_id, _name)); } function getAllPokemons() public view returns (Pokemon[] memory) { From 64872c3c040c3b27ec602ac1a86ba35fbef16fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Mon, 25 Jul 2022 23:32:20 -0500 Subject: [PATCH 2/5] Challenge #2 Challenge #2 - require id > 0 and name length >=2 --- PokemonFactory.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index aa7c170a..a966121a 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -17,6 +17,8 @@ contract PokemonFactory { mapping (address => uint) ownerPokemonCount; function createPokemon (string memory _name, uint _id) public { + require(_id > 0, "Id should be more than 0."); + require(bytes(_name).length >= 2, "name needs at least two letters"); pokemons.push(Pokemon(_id, _name)); pokemonToOwner[_id] = msg.sender; ownerPokemonCount[msg.sender]++; From 8bef98c7554084063119df612c34d93cafcec5dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Wed, 27 Jul 2022 12:28:09 -0500 Subject: [PATCH 3/5] Challenge #3 Challenge #3 Pokemon skills --- PokemonFactory.sol | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index a966121a..94eb838d 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -2,6 +2,59 @@ pragma solidity >=0.7.0 <0.9.0; +contract PokemonFactory { + + struct Skill { + string name; + string description; + } + + struct Pokemon { + uint id; + string name; + } + + + event eventNewPokemon(Pokemon pokemon); + + Pokemon[] private pokemons; + + mapping (uint => Skill[]) public pokemonToSkills; + mapping (uint => address) public pokemonToOwner; + mapping (address => uint) ownerPokemonCount; + + function createPokemon (string memory _name, uint _id, Skill[] memory _skills) public { + require(_id > 0, "Id should be more than 0."); + require(bytes(_name).length >= 2, "Name needs at least two letters"); + uint256 length = _skills.length; + for (uint256 i = 0; i < length; i+=1) { + pokemonToSkills[_id].push(Skill(_skills[i].name,_skills[i].description)); + } + pokemons.push(Pokemon(_id, _name)); + pokemonToOwner[_id] = msg.sender; + ownerPokemonCount[msg.sender]++; + emit eventNewPokemon(Pokemon(_id, _name)); + } + + function getAllPokemons() public view returns (Pokemon[] memory) { + return pokemons; + } + + + function getPokemonSkills(uint256 _id) public view returns (Skill[] memory) { + return pokemonToSkills[_id]; + } + + function getResult() public pure returns(uint product, uint sum, uint d){ + uint a = 1; + uint b = 2; + product = a * b; + sum = a + b; + d = bytes("pa").length; + } + +} + contract PokemonFactory { struct Pokemon { From 7c65b3932af36afc3ad83826b3e32ef4d78694e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Wed, 27 Jul 2022 12:28:56 -0500 Subject: [PATCH 4/5] Challenge #3 Challenge #3 Pokemon skills --- PokemonFactory.sol | 37 ------------------------------------- 1 file changed, 37 deletions(-) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index 94eb838d..22b707cb 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -54,40 +54,3 @@ contract PokemonFactory { } } - -contract PokemonFactory { - - struct Pokemon { - uint id; - string name; - } - - event eventNewPokemon(Pokemon pokemon); - - Pokemon[] private pokemons; - - mapping (uint => address) public pokemonToOwner; - mapping (address => uint) ownerPokemonCount; - - function createPokemon (string memory _name, uint _id) public { - require(_id > 0, "Id should be more than 0."); - require(bytes(_name).length >= 2, "name needs at least two letters"); - pokemons.push(Pokemon(_id, _name)); - pokemonToOwner[_id] = msg.sender; - ownerPokemonCount[msg.sender]++; - emit eventNewPokemon(Pokemon(_id, _name)); - } - - function getAllPokemons() public view returns (Pokemon[] memory) { - return pokemons; - } - - - function getResult() public pure returns(uint product, uint sum){ - uint a = 1; - uint b = 2; - product = a * b; - sum = a + b; - } - -} From 4a567bda945a86dab4cfdf45a9cf5f9c66869a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Wed, 27 Jul 2022 13:17:57 -0500 Subject: [PATCH 5/5] Challenge #4 Challenge #4 - Pokemon Weaknesses and types --- PokemonFactory.sol | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/PokemonFactory.sol b/PokemonFactory.sol index 22b707cb..3449db74 100644 --- a/PokemonFactory.sol +++ b/PokemonFactory.sol @@ -14,6 +14,7 @@ contract PokemonFactory { string name; } + enum Type { NORMAL, WATER, GRASS, FIRE, ELECTRIC, ICE, FIGHTING, PISON, GROUND, FLYING, PSYCHIC, BUG, ROCK, GHOST, DARK, DRAGON, STEEL, FAIRY } event eventNewPokemon(Pokemon pokemon); @@ -22,15 +23,30 @@ contract PokemonFactory { mapping (uint => Skill[]) public pokemonToSkills; mapping (uint => address) public pokemonToOwner; mapping (address => uint) ownerPokemonCount; + mapping (uint => Type[]) pokemonToTypes; + mapping (uint => Type[]) pokemonToWeaknesses; - function createPokemon (string memory _name, uint _id, Skill[] memory _skills) public { + + function createPokemon (string memory _name, uint _id, Skill[] memory _skills, Type[] memory _types, Type[] memory _weaknesses) public { require(_id > 0, "Id should be more than 0."); require(bytes(_name).length >= 2, "Name needs at least two letters"); - uint256 length = _skills.length; - for (uint256 i = 0; i < length; i+=1) { + pokemons.push(Pokemon(_id, _name)); + // loop skills to push in storage variable + uint256 skillsLength = _skills.length; + for (uint i = 0; i < skillsLength; i+=1) { pokemonToSkills[_id].push(Skill(_skills[i].name,_skills[i].description)); } - pokemons.push(Pokemon(_id, _name)); + //loop types to push in storage variable + uint256 typesLength = _types.length; + for (uint j = 0; j < typesLength; j+=1) { + pokemonToTypes[_id].push(_types[j]); + } + //loop types to push in storage variable + uint256 weaknessesLength = _weaknesses.length; + for (uint k = 0; k < weaknessesLength; k+=1) { + pokemonToWeaknesses[_id].push(_weaknesses[k]); + } + pokemonToOwner[_id] = msg.sender; ownerPokemonCount[msg.sender]++; emit eventNewPokemon(Pokemon(_id, _name)); @@ -45,6 +61,14 @@ contract PokemonFactory { return pokemonToSkills[_id]; } + function getPokemonTypes(uint256 _id) public view returns (Type[] memory) { + return pokemonToTypes[_id]; + } + + function getPokemonWeaknesses(uint256 _id) public view returns (Type[] memory) { + return pokemonToWeaknesses[_id]; + } + function getResult() public pure returns(uint product, uint sum, uint d){ uint a = 1; uint b = 2;