Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retos completados. #89

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 53 additions & 16 deletions PokemonFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,69 @@ pragma solidity >=0.7.0 <0.9.0;

contract PokemonFactory {

struct Abilities {
string name;
string description;
}

struct Pokemon {
uint id;
uint8 id;
string name;
Abilities[] abilities;
string[] types;
string[] weaknesses;
}

Pokemon[] private pokemons;
Pokemon[] private pokemons;

mapping (uint => address) public pokemonToOwner;
mapping (address => uint8) ownerPokemonCount;

event eventNewPokemon(string _pokemonName);

function createPokemon (string memory _name, uint8 _id, Abilities[] memory _abilities, string[] memory _types, string[] memory _weaknesses) public {
require (_id > 0, "el id debe ser mayor a 0" );
require(bytes(_name).length > 2, "el nombre debe contener mas de 2 caracteres");
pokemons.push(Pokemon(_id, _name, _abilities, _types, _weaknesses));
pokemonToOwner[_id] = msg.sender;
ownerPokemonCount[msg.sender]++;

emit eventNewPokemon(_name);
}

mapping (uint => address) public pokemonToOwner;
mapping (address => uint) ownerPokemonCount;
function getAllPokemons() public view returns (Pokemon[] memory) {
return pokemons;
}

function createPokemon (string memory _name, uint _id) public {
pokemons.push(Pokemon(_id, _name));
pokemonToOwner[_id] = msg.sender;
ownerPokemonCount[msg.sender]++;
function addAbilityById(uint8 _id, string memory _ability, string memory _description) public {
for(uint8 i = 0; i < pokemons.length; i++){
if(pokemons[i].id == _id){
pokemons[i].abilities.push(Abilities(_ability, _description));
}
}
}

function getAllPokemons() public view returns (Pokemon[] memory) {
return pokemons;
function addTypeById(uint8 _id, string memory _type) public {
for(uint8 i = 0; i < pokemons.length; i++){
if(pokemons[i].id == _id){
pokemons[i].types.push(_type);
}
}
}

function addWeaknessById(uint8 _id, string memory _weakness) public {
for(uint8 i = 0; i < pokemons.length; i++){
if(pokemons[i].id == _id){
pokemons[i].weaknesses.push(_weakness);
}
}
}

function getResult() public pure returns(uint product, uint sum){
uint a = 1;
uint b = 2;
product = a * b;
sum = a + b;
}
function getResult() public pure returns(uint8 product, uint8 sum){
uint8 a = 1;
uint8 b = 2;
product = a * b;
sum = a + b;
}

}
9 changes: 8 additions & 1 deletion Retos.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<h2>Reto #1</h2>
Investigar que son los Events en Solidity. Luego, debes implementar un evento que se llame eventNewPokemon, el cual se disparará cada vez que un nuevo Pokemon es creado. Lo que emitirá el evento será el Pokemon que se creó.

<h3>Eventos</h3>
Permite conectar lo que pasa dentro de la Blockchain con el exterior porque a tráves de un protocolo otras aplicaciones se pueden suscribir a ellos y escuchar todo lo que está pasando en el Smart Contract.

<h4>Se usan para:</h4>
- Registrar cambios que se hicieron
- Feedback (Retroalimentación)

<h2>Reto #2</h2>

- Investigar sobre “”require.
- Investigar sobre "require".
- Entonces, antes de agregar un nuevo Pokemon, se debe validar que el id sea mayor a 0. De lo contrario, se debe desplegar un mensaje que corrija al usuario.
- Entonces, antes de agregar un nuevo Pokemon, se debe validar que el name no sea vació y mayor a 2 caracteres. De lo contrario, se debe desplegar un mensaje que corrija al usuario.

Expand Down