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

Challenge Pokemon Solidity #24

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
35 changes: 0 additions & 35 deletions PokemonFactory.sol

This file was deleted.

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1> Pokemon Factory </h1>

<ul>
<li> Add your Pokemon with a name, id, abilities and types with createPokemon function</li>
<li> You can see the Pokemon List with getAll Pokemon function</li>
<li> You can see weaknesses of a Pokemon from the List with getWeaknesses Pokemon function</li>
<li> You can see the owner of a Pokemon with pokemonToOwner</li>
</ul>

See the contract in Rinkeby <a href="https://rinkeby.etherscan.io/address/0x1affec6c3f0f51ce735835baa63382f9f5fe0988">0x1AfFec6C3F0f51ce735835BAa63382f9F5Fe0988</a>

Have fun and catch 'em all!

124 changes: 124 additions & 0 deletions contracts/PokemonFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "./StringUtils.sol";
import "./TypesUtils.sol";

contract PokemonFactory {

struct Pokemon {
uint id;
string name;
Type[] typesList;
Ability[] abilities;
}

struct Ability {
string name;
string description;
}

struct Type {
Types id;
string name;
}

enum Types{
Normal,
Fighting,
Flying,
Poison,
Ground,
Rock,
Bug,
Ghost,
Steel,
Fire,
Water,
Grass,
Electric,
Psychic,
Ice,
Dragon,
Fairy,
Dark
}

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

event eventNewPokemon(string name, uint id);

modifier validateEntries(string memory _name, uint _id, Ability[] memory _abilities, Types[] memory _type) {
require(_id != 0, "Id must be greater than 0");
require(StringUtils.strlen(_name) > 2, "Name must have more than 2 characters");
require(_abilities.length > 0 && _abilities.length < 4, "Must have between 1 to 3 Abilities");
require(_type.length > 0 && _type.length < 3, "Must have 1 or 2 Types");

for (uint i = 0; i < _abilities.length; i++) {
for (uint j = 1; j <_abilities.length; j++) {
if(j != i && keccak256(abi.encodePacked((_abilities[i].name))) == keccak256(abi.encodePacked((_abilities[j].name)))){
revert("You have duplicated Abilities");
}
}
}

for (uint i = 0; i < _type.length; i++) {
for (uint j = 1; j <_type.length; j++) {
if(j != i && _type[i] == _type[j]){
revert("You have duplicated Types");
}
}
}
_;
}

/**
* @dev create a pokemon
*
* @param _name Name of the Pokemon
* @param _id id of the Pokemon
* @param _abilities Abilities of the Pokemon
* @param _type Type of Pokemon
*/
function createPokemon (string memory _name, uint _id, Ability[] memory _abilities, Types[] memory _type) public validateEntries(_name, _id, _abilities, _type){
//create first pokemon struct in pokemons
Pokemon storage pokemon = pokemons.push();
pokemon.id = _id;
pokemon.name = _name;
for (uint i = 0; i <_abilities.length; i++) {
pokemon.abilities.push(Ability(_abilities[i].name, _abilities[i].description));
}
for (uint i = 0; i <_type.length; i++) {
pokemon.typesList.push(Type(_type[i], TypesUtils.typeName(uint8(_type[i]))));
}
pokemonToOwner[_id] = msg.sender;
ownerPokemonCount[msg.sender]++;
emit eventNewPokemon(_name, _id);
}

/**
* @dev get weaknesses of a Pokemon
*
* @param _pokemonIndex index of pokemons array
* @return array of weaknesses
*/
function getWeaknesses (uint _pokemonIndex) public view returns (string[] memory) {
Pokemon memory pokemon = pokemons[_pokemonIndex];
string[] memory weaknesses = new string[](pokemon.typesList.length);
for (uint i = 0; i < pokemon.typesList.length; i++) {
weaknesses[i] = TypesUtils.Weakness(uint8(pokemon.typesList[i].id));
}
return weaknesses;
}

/**
* @dev get all Pokemons of pokemons array
* @return array pokemons
*/
function getAllPokemons() public view returns (Pokemon[] memory) {
return pokemons;
}
}
35 changes: 35 additions & 0 deletions contracts/StringUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
// Source:
// https://github.com/ensdomains/ens-contracts/blob/master/contracts/ethregistrar/StringUtils.sol
pragma solidity >=0.7.0 <0.9.0;

library StringUtils {
/**
* @dev Returns the length of a given string
*
* @param s The string to measure the length of
* @return The length of the input string
*/
function strlen(string memory s) internal pure returns (uint) {
uint len;
uint i = 0;
uint bytelength = bytes(s).length;
for(len = 0; i < bytelength; len++) {
bytes1 b = bytes(s)[i];
if(b < 0x80) {
i += 1;
} else if (b < 0xE0) {
i += 2;
} else if (b < 0xF0) {
i += 3;
} else if (b < 0xF8) {
i += 4;
} else if (b < 0xFC) {
i += 5;
} else {
i += 6;
}
}
return len;
}
}
98 changes: 98 additions & 0 deletions contracts/TypesUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

library TypesUtils {

/**
* @dev Returns Name
*
* @param id Type of pokemon
* @return string Name
*/
function typeName(uint8 id) internal pure returns (string memory){
if (id == 0) {
return "Normal";
} else if (id == 1) {
return "Fighting";
} else if (id == 2) {
return "Flying";
} else if (id == 3) {
return "Poison";
} else if (id == 4) {
return "Ground";
} else if (id == 5) {
return "Rock";
} else if (id == 6) {
return "Bug";
} else if (id == 7) {
return "Ghost";
} else if (id == 8) {
return "Steel";
} else if (id == 9) {
return "Fire";
} else if (id == 10) {
return "Water";
} else if (id == 11) {
return "Grass";
} else if (id == 12) {
return "Electric";
} else if (id == 13) {
return "Psychic";
} else if (id == 14) {
return "Ice";
} else if (id == 15) {
return "Dragon";
} else if (id == 16) {
return "Fairy";
} else {
return "Dark";
}
}

/**
* @dev Returns string of weaknesses per type
*
* @param id Type of pokemon
* @return string of weaknesses
*/
function Weakness(uint8 id) internal pure returns (string memory) {
if (id == 0) {
return "Rock, Ghost, Steel";
} else if (id == 1) {
return "Flying, Poison, Psychic, Bug, Ghost, Fairy";
} else if (id == 2) {
return "Rock, Steel, Electric";
} else if (id == 3) {
return "Poison, Ground, Rock, Ghost, Steel";
} else if (id == 4) {
return "Flying, Bug, Grass";
} else if (id == 5) {
return "Fighting, Ground, Steel";
} else if (id == 6) {
return "Fighting, Flying, Poison, Ghost, Steel, Fire, Fairy";
} else if (id == 7) {
return "Normal, Dark";
} else if (id == 8) {
return "Steel, Fire, Water, Electric";
} else if (id == 9) {
return "Rock, Fire, Water, Dragon";
} else if (id == 10) {
return "Water, Grass, Dragon";
} else if (id == 11) {
return "Flying, Poison, Bug, Steel, Fire, Grass, Dragon";
} else if (id == 12) {
return "Flying, Steel, Electric";
} else if (id == 13) {
return "Steel, Psychic, Dark";
} else if (id == 14) {
return "Steel, Fire, Water, Ice";
} else if (id == 15) {
return "Steel, Fairy";
} else if (id == 16) {
return "Poison, Steel, Fire";
} else {
return "Fighting, Dark, Fairy";
}
}
}