Categories
Crypto

TRON: Kingdoms

Let’s build a game using TRON, and what better game to create than rock-paper-scissors? We’ve already covered TRON, so now it’s time to put that knowledge into practice.

pragma solidity ^0.4.24;

contract Kingdoms {
    // Struct to hold the information of a Kingdom
    struct Kingdom {
        address owner;
        uint army;
        uint gold;
        uint resources;
        mapping(string => uint) structures;
    }

    // Mapping to hold all the Kingdoms
    mapping(address => Kingdom) public kingdoms;

    // Event to be emitted on attack
    event Attack(address attacker, address defender, bool success);
    //Event to be emitted on structure building
    event Build(address owner, string structureName);

    // Function to create a new Kingdom
    function createKingdom(uint initialArmy, uint initialGold, uint initialResources) public {
        require(kingdoms[msg.sender].owner == address(0));
        kingdoms[msg.sender].owner = msg.sender;
        kingdoms[msg.sender].army = initialArmy;
        kingdoms[msg.sender].gold = initialGold;
        kingdoms[msg.sender].resources = initialResources;
    }

    // Function to attack another Kingdom
    function attack(address _defender) public {
        require(kingdoms[msg.sender].army > 0);
        require(kingdoms[_defender].owner != address(0));
        //calculate the success of the attack
        bool success = calculateAttack(kingdoms[msg.sender].army, kingdoms[_defender].army);
        if (success) {
            kingdoms[_defender].army = kingdoms[_defender].army / 2;
            kingdoms[msg.sender].gold += kingdoms[_defender].gold;
            kingdoms[msg.sender].resources += kingdoms[_defender].resources;
        } else {
            kingdoms[msg.sender].army = kingdoms[msg.sender].army / 2;
        }
        emit Attack(msg.sender, _defender, success);
    }
    // Function to build structures in a Kingdom
    function buildStructure(string _structureName) public {
        require(kingdoms[msg.sender].gold >= getStructureCost(_structureName));
        require(getStructureLevel(msg.sender, _structureName) == 0);
        kingdoms[msg.sender].gold -= getStructureCost(_structureName);
        kingdoms[msg.sender].structures[_structureName] = 1;
        emit Build(msg.sender, _structureName);
    }

    // Function to upgrade a structure in a Kingdom
    function upgradeStructure(string _structureName) public {
        require(kingdoms[msg.sender].gold >= getStructureUpgradeCost(_structureName));
        uint currentLevel = getStructureLevel(msg.sender, _structureName);
        require(currentLevel != 0);
        require(currentLevel < maxStructureLevel);
        kingdoms[msg.sender].gold -= getStructureUpgradeCost(_structureName);
        kingdoms[msg.sender].structures[_structureName]++;
    }

    // Function to get the current level of a structure in a Kingdom
    function getStructureLevel(address _owner, string _structureName) public view returns (uint) {
        return kingdoms[_owner].structures[_structureName];
    }

    // Function to get the cost of building a structure
    function getStructureCost(string _structureName) public view returns (uint) {
        if (_structureName == "wall") return 1000;
        if (_structureName == "tower") return 5000;
        if (_structureName == "barracks") return 10000;
    }

    // Function to get the cost of upgrading a structure
    function getStructureUpgradeCost(string _structureName) public view returns (uint) {
        if (_structureName == "wall") return 500;
        if (_structureName == "tower") return 2500;
        if (_structureName == "barracks") return 5000;
    }

    // Function to calculate the success of the attack
    function calculateAttack(uint attackerArmy, uint defenderArmy) private view returns (bool) {
        uint random = uint(keccak256(abi.encodePacked(block.timestamp, block.difficulty))) % 2;
        if (random == 1 && attackerArmy > defenderArmy) return true;
        else return false;
    }
}

Full source code can be found here.

The smart contract allows players to create new kingdoms, attack other players, and acquire resources and gold and build structures.

The new feature allows players to build and upgrade structures in their kingdom. Structures can be walls, towers, or barracks. The function buildStructure allows a player to build a new structure, it checks if the player has enough gold and if the structure doesn’t exist in the player’s kingdom. When a player builds a new structure, the gold is subtracted from the player’s kingdom and the structure is added to the kingdom with level 1.

The function upgradeStructure allows players to upgrade an existing structure, it checks if the player has enough gold, if the structure exist and if the structure is not at the max level. Upgrading a structure will increase the level of the structure and the gold will be subtracted from the player’s kingdom.

The function getStructureLevel allows players to check the current level of a structure in their kingdom. The function getStructureCost and getStructureUpgradeCost allows players to check the cost of building or upgrading a structure.

The smart contract also includes events that are emitted when a player builds or upgrades a structure, the event includes the player’s address and the name of the structure.

Leave a Reply

Your email address will not be published. Required fields are marked *