SolidityLabs/zombie-project/zombieattack.sol

30 lines
826 B
Solidity
Raw Permalink Normal View History

2022-08-05 16:05:36 -03:00
pragma solidity ^0.4.19;
2022-08-05 13:44:20 -03:00
import "./zombiehelper.sol";
2022-08-05 15:08:20 -03:00
contract ZombieBattle is ZombieHelper {
uint randNonce = 0;
2022-08-05 15:30:11 -03:00
uint attackVictoryProbability = 70;
2022-08-05 15:08:20 -03:00
2022-08-05 15:30:11 -03:00
function randMod(uint _modulus) internal returns(uint) {
2022-08-05 15:08:20 -03:00
randNonce++;
return uint(keccak256(now, msg.sender, randNonce)) % _modulus;
}
2022-08-05 15:30:11 -03:00
2022-08-05 16:57:48 -03:00
function attack(uint _zombieId, uint _targetId) external onlyOwnerOf(_zombieId) {
2022-08-05 16:05:36 -03:00
Zombie storage myZombie = zombies[_zombieId];
Zombie storage enemyZombie = zombies[_targetId];
uint rand = randMod(100);
if (rand <= attackVictoryProbability) {
myZombie.winCount++;
myZombie.level++;
enemyZombie.lossCount++;
feedAndMultiply(_zombieId, enemyZombie.dna, "zombie");
} else {
myZombie.lossCount++;
enemyZombie.winCount++;
_triggerCooldown(myZombie);
}
2022-08-05 15:30:11 -03:00
}
2022-08-05 15:08:20 -03:00
}