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

Solved Challenge #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
80 changes: 65 additions & 15 deletions PokemonFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,82 @@ pragma solidity >=0.7.0 <0.9.0;

contract PokemonFactory {

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

struct Ability {
string name;
string description;
}

enum Type {
BUG,
DARK,
DRAGON,
ELECTRIC,
FAIRY,
FIGHTING,
FIRE,
FLYING,
GHOST,
GRASS,
GROUND,
ICE,
NORMAL,
POISON,
PSYCHIC,
ROCK,
STEEL,
WATER
}

Pokemon[] private pokemons;

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

function createPokemon (string memory _name, uint _id) public {
pokemons.push(Pokemon(_id, _name));
event eventNewPokemon (Pokemon _pokemon);

function createPokemon (uint8 _id, string memory _name, string[] memory _abilitiesNames, string[] memory _abilitiesDesc, Type[] memory _types, Type[] memory _weaknesses) public {
require(_id > 0, "Pokemon id must be greater than 0");
require(pokemonToOwner[_id] == address(0), "Id already used");
require(bytes(_name).length > 2, "Pokemon name must be longer than 2 characters");
require(_abilitiesNames.length == _abilitiesDesc.length, "Abilities names and description should have the same size");

pokemons.push();
uint256 index = pokemons.length - 1;
pokemons[index].id = _id;
pokemons[index].name = _name;
addAbilities(index, _abilitiesNames, _abilitiesDesc);

for(uint8 i=0; i < _types.length; i++) {
pokemons[index].types.push(_types[i]);
}

for(uint8 i=0; i < _weaknesses.length; i++) {
pokemons[index].weaknesses.push(_weaknesses[i]);
}

pokemonToOwner[_id] = msg.sender;
ownerPokemonCount[msg.sender]++;
emit eventNewPokemon(pokemons[index]);
}

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;
}
function addAbilities(uint256 _index, string[] memory _abilitiesNames, string[] memory _abilitiesDesc) private {
for(uint8 i=0; i < _abilitiesNames.length; i++) {
pokemons[_index].abilities.push(
Ability(_abilitiesNames[i], _abilitiesDesc[i])
);
}
}

}