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

Solución al Desafío #99

Closed
wants to merge 4 commits into from
Closed
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
59 changes: 43 additions & 16 deletions PokemonFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,59 @@ pragma solidity >=0.7.0 <0.9.0;

contract PokemonFactory {

struct Ability {
string name;
string description;
}

struct Pokemon {
uint id;
string name;
Ability[] abilities;
string[] types;
string[] debilities;
}

Pokemon[] private pokemons;
string[] public Types = ['Normal', 'Fire', 'Water', 'Grass', 'Flying', 'Fighting', 'Poison', 'Electric', 'Ground', 'Rock', 'Psychic', 'Ice', 'Bug', 'Ghost', 'Steel', 'Dragon', 'Dark', 'Fairy'];

mapping (uint => address) public pokemonToOwner;
mapping (address => uint) ownerPokemonCount;
Pokemon[] private pokemons;
Ability[] private abilities;

function createPokemon (string memory _name, uint _id) public {
pokemons.push(Pokemon(_id, _name));
pokemonToOwner[_id] = msg.sender;
ownerPokemonCount[msg.sender]++;
}
mapping (uint => address) public pokemonToOwner;
mapping (address => uint) ownerPokemonCount;

event eventNewPokemon(
uint id,
string name
);

function getAllPokemons() public view returns (Pokemon[] memory) {
return pokemons;
function createPokemon (string memory _name, uint _id, uint8[] memory _abilities, uint8[] memory _types, uint8[] memory _debilities) public {
require(_id > 0, "El Id debe ser mayor a 0");
require(bytes(_name).length != 0, "El nombre no puede estar vacio");
require(bytes(_name).length > 2, "El nombre debe ser mayor a 2 caracteres");
Pokemon storage p = pokemons.push();
for(uint i = 0; i < _abilities.length; i++){
p.abilities.push(abilities[i]);
}
for(uint i = 0; i < _types.length; i++){
p.types.push(Types[_types[i]]);
}
for(uint i = 0; i < _debilities.length; i++){
p.debilities.push(Types[_debilities[i]]);
}
p.id = _id;
p.name = _name;
pokemonToOwner[_id] = msg.sender;
ownerPokemonCount[msg.sender]++;
emit eventNewPokemon(_id, _name);
}

function createAbility (string memory _name, string memory _desc) public {
abilities.push(Ability(_name, _desc));
}

function getResult() public pure returns(uint product, uint sum){
uint a = 1;
uint b = 2;
product = a * b;
sum = a + b;
}
function getAllPokemons() public view returns (Pokemon[] memory) {
return pokemons;
}

}