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

Solidity eth pokesolutions #66

Open
wants to merge 5 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
109 changes: 94 additions & 15 deletions PokemonFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,112 @@
pragma solidity >=0.7.0 <0.9.0;

contract PokemonFactory {
struct Pokemon {
uint256 id;
string name;
Skill[] skills;
Aweakness[] aweakness;
}

struct Pokemon {
uint id;
string name;
}
struct Skill {
string name;
string description;
}

struct Aweakness {
WeaknessStatus weaknesName;
}

enum WeaknessStatus {
Fire,
Psychic,
Flying,
Ice
}

Pokemon[] private pokemons;

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

// logic for pokemons
mapping(uint256 => uint256) public pokemonIndexed;

event eventNewPokemon(Pokemon _pokemonCreated);

// event eventAllPokemons(Pokemon[] _pokemonCreated);

function createPokemon(
string memory _name,
uint256 _id,
string memory _skillName,
string memory _skillDescription,
WeaknessStatus _weaknessStatus
) public NameMinimunTwoCharacters(_name) IsGreaterThanZero(_id) {
pokemons.push();
uint256 lastIndex = pokemons.length - 1;
pokemons[lastIndex].id = _id;
pokemons[lastIndex].name = _name;
addSkill(_id, _skillName, _skillDescription);
// pokemons[lastIndex].skills.push(Skill(_skillName, _skillDescription));
addAweakness(_id, _weaknessStatus);
// pokemons[lastIndex].aweakness.push(Aweakness(_weaknessStatus));
pokemonIndexed[_id] = lastIndex;

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

emit eventNewPokemon(pokemons[lastIndex]);
}

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

modifier IsGreaterThanZero(uint256 _id) {
require(_id > 0, "_id field should be greater than zero");
_;
}

modifier NameMinimunTwoCharacters(string memory _name) {
// this rule include bytes(_name).length > 0 that enssures "_name field should not be empty"
require(
bytes(_name).length >= 2,
"_name field should be have at least 2 characteres"
);
_;
}

function getResult() public pure returns(uint product, uint sum){
uint a = 1;
uint b = 2;
product = a * b;
sum = a + b;
}
function addSkill(
uint256 _pokemonId,
string memory _skillName,
string memory _skillDescription
) public IsGreaterThanZero(_pokemonId) {
// Solución al buscar directamente en un mapping
pokemons[pokemonIndexed[_pokemonId]].skills.push(
Skill(_skillName, _skillDescription)
);

// emit eventAllPokemons(pokemons);
// Solución al recorrer un for
// for (uint256 counter = 0; counter < pokemons.length; counter++) {
// if (pokemons[counter].id == _pokemonId) {
// pokemons[pokemonIndexed[_pokemonId]].skills.push(
// Skill(_skillName, _skillDescription)
// );

// emit eventAllPokemons(pokemons);
// }
// }
}

function addAweakness(uint256 _pokemonId, WeaknessStatus _weaknessStatus)
public
IsGreaterThanZero(_pokemonId)
{
// Solución al buscar directamente en un mapping
pokemons[pokemonIndexed[_pokemonId]].aweakness.push(
Aweakness(_weaknessStatus)
);
}
}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Pokemon Factory

Smart contract focused to implement Events, Validation, Types, in a programing way within a simulation of creation of Pokemon with his skills, weakness.

To run this contract

The best way to review this contract use `remix.ethereum.org`

Create a new contract `copy/paste` this sample

- `Ctrl+S` to compile
- Deploy
- Play on `createPokemon` button
- Then you can add new skills with `addSkill` button and weakness with `addAweakness` button

Enjoy it!