Skip to content

Instantly share code, notes, and snippets.

@alexroan
Created March 27, 2020 15:56
Show Gist options
  • Save alexroan/efb4ad36a344b2c6011628dae7cb55ca to your computer and use it in GitHub Desktop.
Save alexroan/efb4ad36a344b2c6011628dae7cb55ca to your computer and use it in GitHub Desktop.
ethermon.sol@4
pragma solidity ^0.5.5;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract Ethermon is ERC721 {
struct Monster {
string name;
uint level;
}
Monster[] public monsters;
address public gameOwner;
constructor() public {
gameOwner = msg.sender;
}
modifier onlyOwnerOf(uint _monsterId) {
require(ownerOf(_monsterId) == msg.sender, "Must be owner of monster to battle");
_;
}
function battle(uint _attackingMonster, uint _defendingMonster) public onlyOwnerOf(_attackingMonster) {
Monster storage attacker = monsters[_attackingMonster];
Monster storage defender = monsters[_defendingMonster];
if (attacker.level >= defender.level) {
attacker.level += 2;
defender.level += 1;
}
else{
attacker.level += 1;
attacker.level += 2;
}
}
function createNewMonster(string memory _name, address _to) public {
require(msg.sender == gameOwner, "Only game owner can create new monsters");
uint id = monsters.length;
monsters.push(Monster(_name, 1));
_safeMint(_to, id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment