Skip to content

Commit

Permalink
Challenges gelopfalcon#2 and gelopfalcon#3 are solved
Browse files Browse the repository at this point in the history
  • Loading branch information
UchihaCFC committed Jul 26, 2022
1 parent 63878e8 commit d75432d
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions PokemonFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,69 @@ contract PokemonFactory {
struct Pokemon {
uint id;
string name;
Ability[] abilities;
}

struct Ability{
string name;
string description;
}

Pokemon[] private pokemons;
Ability[] private abilities;

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

event eventNewPokemon(
uint id,
string name
);

function createPokemon (string memory _name, uint _id) public {
pokemons.push(Pokemon(_id, _name));
event eventNewPokemonAbility(
string name,
string description
);

modifier checkPokemonData(string memory _name, uint _id){
require(_id > 0, "The id must be bigger than 0!");
require(bytes(_name).length > 2 || bytes(_name).length == 0, "The name must be bigger than 2 characters!");
_;
}

modifier checkPokemonAbilityData(string memory name, string memory description, uint256 id){
require(id > 0, "The id must be bigger than 0!");
require(bytes(name).length > 2 || bytes(name).length == 0, "The name must be bigger than 2 characters!");
require(bytes(description).length > 10, "The description must be bigger than 10 characters!");
_;
}

modifier checkPokemonExist(uint8 id) {
require(globalId[id] > 0, "The pokemon with this id doesn't exist, please check that it is correct and try again");
_;
}

function createPokemon (string memory _name, uint8 _id) public checkPokemonData(_name, _id) {
pokemons.push();
uint pokemonId = pokemons.length - 1;
pokemons[pokemonId].id = _id;
pokemons[pokemonId].name = _name;
pokemonToOwner[_id] = msg.sender;
ownerPokemonCount[msg.sender]++;
globalId[_id] = pokemonId + 1;
emit eventNewPokemon(_id, _name);
}

function getAllPokemons() public view returns (Pokemon[] memory) {
return pokemons;
}

function addPokemonAbility(string memory name, string memory description, uint8 id) public checkPokemonAbilityData(name, description, id) checkPokemonExist(id){
uint idx = globalId[id];
pokemons[idx - 1].abilities.push(Ability(name, description));
emit eventNewPokemonAbility(name, description);
}

function getResult() public pure returns(uint product, uint sum){
uint a = 1;
Expand Down

0 comments on commit d75432d

Please sign in to comment.