Skip to content

Commit

Permalink
gelopfalcon#7 added pokemon weaknesses using a string array
Browse files Browse the repository at this point in the history
  • Loading branch information
albbeltran committed Jul 20, 2022
1 parent 615f6f5 commit 94afc99
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions contracts/PokemonFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
pragma solidity >=0.7.0 <0.9.0;

contract PokemonFactory {
uint _id;

event eventNewPokemon(Pokemon pokemon);

struct Pokemon {
uint256 id;
string name;
Ability[] abilities;
string[] pokemonType;
string[] weaknesses;
}

struct Ability {
Expand All @@ -23,33 +26,48 @@ contract PokemonFactory {
mapping(address => uint256) public ownerPokemonCount;
mapping(address => mapping(uint => Pokemon)) ownedPokemons;

function createPokemon(string memory _name, string[] memory _abilityName, string[] memory _abilityDscription, string[] memory _type) public {
require(bytes(_name).length > 2, "The name must have at least 2 characters.");
require((_abilityName).length == (_abilityDscription).length, "You have to provide the same number of abilitie and description.");
modifier checkData
(uint _id, string memory _name, string[] memory _abilityName, string[] memory _abilityDescription, string[] memory _type, string[] memory _weaknesses) {
require(bytes(_name).length > 2, "The name must have at least 2 characters.");
require((_abilityName).length == (_abilityDescription).length, "You must provide the name of each ability for each ability description.");

uint _id = pokemons.length;
require(bytes((_abilityName)[_id]).length > 3, "The name must have at least 3 characters.");
require(bytes((_abilityDscription)[_id]).length > 5, "The description must have at least 5 characters.");

pokemons.push();
pokemons[_id].id = _id;
pokemons[_id].name = _name;
for(uint i=0; i<_weaknesses.length; i++) {
require(bytes((_weaknesses)[i]).length > 3, "The weakness name must have at least 3 characters.");
}

for(uint i=0; i<_type.length; i++) {
require(bytes((_type)[i]).length > 3, "The type must have at least 3 characters.");
}
for(uint i=0; i<_type.length; i++) {
require(bytes((_type)[i]).length > 3, "The type must have at least 3 characters.");
}

pokemons[_id].pokemonType = _type;
for(uint i=0; i<_abilityName.length; i++) {
require(bytes((_abilityName)[_id]).length > 3, "The ability name must have at least 3 characters.");
require(bytes((_abilityDescription)[_id]).length > 5, "The ability description must have at least 5 characters.");
}

for(uint i=0; i<_abilityName.length; i++) {
pokemons[_id].abilities.push(
Ability(_abilityName[i], _abilityDscription[i])
);
}
_;
}

function createPokemon
(string memory _name, string[] memory _abilityName, string[] memory _abilityDescription, string[] memory _type, string[] memory _weaknesses)
public
checkData(_id, _name, _abilityName, _abilityDescription, _type, _weaknesses) {
_id = pokemons.length;

pokemons.push();
pokemons[_id].id = _id;
pokemons[_id].name = _name;
pokemons[_id].pokemonType = _type;
pokemons[_id].weaknesses = _weaknesses;

for(uint i=0; i<_abilityName.length; i++) {
pokemons[_id].abilities.push(
Ability(_abilityName[i], _abilityDescription[i])
);
}

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

function getAllPokemons() public view returns (Pokemon[] memory) {
Expand Down

0 comments on commit 94afc99

Please sign in to comment.