//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.26; /* Should manage relationships between cards and people, store a list of cards, generate cards and assign them to people: - selling - list them - create new ones - start fights - breed - invoke state of card */ contract PokeCore { mapping (address => uint[]) private ownerToIDs; mapping (uint => address) private IDToOwner; mapping (address => cardForSaleToAddress) private sellerToCardForSaleToAddress; Pokemon[] private cards; uint private timeoutBreeding = 30 minutes; struct cardForSaleToAddress { uint cardID; address authToBuy; bool canBuy; uint price; } struct Pokemon { uint totalHp; uint currentHp; uint atk; uint speciesID; bool isFighting; bool isBreeding; uint breedingWith; uint breedUntil; uint parent1ID; uint parent2ID; uint ID; } constructor() public { Pokemon memory pkm = Pokemon({ totalHp: 0, currentHp: 0, atk: 0, speciesID: 0, isFighting: false, isBreeding: false, breedingWith: 0, breedUntil: 0, parent1ID: 0, parent2ID: 0, ID: 0 }); ownerToIDs[0x0].push(0); IDToOwner[0] = 0x0; cards.push(pkm); } // ################ EVENTS ################ // ################ MODIFIERS ################ modifier ownsTheCard(address add, uint cardID) { require(IDToOwner[cardID] == add); _; } // ################ BREEDING ################ function _generatePokemon(uint parent1ID, uint parent2ID) private view returns (Pokemon){ Pokemon memory parent1 = cards[parent1ID]; Pokemon memory parent2 = cards[parent2ID]; uint hp = uint((parent1.totalHp + parent2.totalHp) / 2); uint atk = uint((parent1.atk + parent2.atk) / 2); uint speciesID = uint((parent1.speciesID + parent2.speciesID) / 2); return Pokemon({ totalHp: hp, currentHp: hp, atk: atk, speciesID: speciesID, isFighting: false, isBreeding: false, breedingWith: 0, breedUntil: 0, parent1ID: parent1ID, parent2ID: parent2ID, ID: 0 }); } function _birthPokemon(uint parent1ID, uint parent2ID) private returns (uint newID) { Pokemon memory newPokemon = _generatePokemon(parent1ID, parent2ID); newID = cards.push(newPokemon) - 1; cards[newID].ID = newID; _transferOwnership(newID, 0x0, msg.sender); // todo: event } function breed(uint parent1ID, uint parent2ID) external ownsTheCard(msg.sender, parent1ID) ownsTheCard(msg.sender, parent2ID) returns (uint newID) { require(parent1ID != parent2ID); require(!cards[parent1ID].isBreeding); require(!cards[parent2ID].isBreeding); cards[parent1ID].isBreeding = true; cards[parent1ID].breedingWith = parent2ID; cards[parent1ID].breedUntil = now + timeoutBreeding; cards[parent2ID].isBreeding = true; cards[parent2ID].breedingWith = parent1ID; cards[parent2ID].breedUntil = now + timeoutBreeding; newID = _birthPokemon(parent1ID, parent2ID); } // ################ TRANSFER ################ function _transferOwnership(uint ID, address from, address to) private { // not safe, the sender might not own the card if (from != 0x0) { uint[] memory allFromCards = ownerToIDs[from]; uint[] memory newFromCards = new uint[](allFromCards.length - 1); bool foundFlag = false; for (uint i=0; i < allFromCards.length; i=i+1) { if (allFromCards[i] != ID) { if (foundFlag) { newFromCards[i-1] = allFromCards[i]; } else { newFromCards[i] = allFromCards[i]; } } else { foundFlag = true; } } ownerToIDs[from] = newFromCards; } IDToOwner[ID] = to; ownerToIDs[to].push(ID); // todo: event } function authoriseToBuyCard(uint cardID, address authorisedToBuy, uint price) external ownsTheCard(msg.sender, cardID){ sellerToCardForSaleToAddress[msg.sender] = cardForSaleToAddress({ cardID: cardID, authToBuy: authorisedToBuy, canBuy: true, price: price }); } function cancelSellAuthorisation(uint cardID) external ownsTheCard(msg.sender, cardID) { sellerToCardForSaleToAddress[msg.sender].canBuy = false; } function buyPreAuthorisedCard(uint cardID, address seller) external payable ownsTheCard(seller, cardID){ require(sellerToCardForSaleToAddress[seller].canBuy); require(sellerToCardForSaleToAddress[seller].authToBuy == msg.sender); require(msg.value == sellerToCardForSaleToAddress[seller].price); seller.transfer(msg.value); _transferOwnership(cardID, seller, msg.sender); sellerToCardForSaleToAddress[seller].canBuy = false; } // ################ GETTERS ################ function getCard(uint cardID) external view returns ( uint totalHp, uint currentHp, uint atk, uint speciesID, bool isFighting, bool isBreeding, uint breedingWith, uint breedgUntil, uint parent1ID, uint parent2ID, uint ID) { require(cardID < cards.length); Pokemon memory card = cards[cardID]; return ( card.totalHp, card.currentHp, card.atk, card.speciesID, card.isFighting, card.isBreeding, card.breedingWith, card.breedUntil, card.parent1ID, card.parent2ID, card.ID ); } function getUserCardsIDs(address add) public view returns (uint[]) { return ownerToIDs[add]; } function getSenderCardsIDs() external view returns (uint[]) { return getUserCardsIDs(msg.sender); } // ################ TESTS ################ function testCreatePokemonForMe( uint totalHp, uint currentHp, uint atk, uint speciesID, bool isFighting, bool isBreeding, uint breedingWith, uint breedUntil, uint parent1ID, uint parent2ID) public { Pokemon memory newPokemon = Pokemon({ totalHp: totalHp, currentHp: currentHp, atk: atk, speciesID: speciesID, isFighting: isFighting, isBreeding: isBreeding, breedingWith: breedingWith, breedUntil: breedUntil, parent1ID: parent1ID, parent2ID: parent2ID, ID: 0 }); uint newID = cards.push(newPokemon) - 1; cards[newID].ID = newID; _transferOwnership(newID, 0x0, msg.sender); } function testBreed(uint ID1, uint ID2) public { _birthPokemon(ID1, ID2); } }
0.4.26