美文网首页
以太猫CryptoKitties源码(七):生成0代猫与促销猫

以太猫CryptoKitties源码(七):生成0代猫与促销猫

作者: 雪飘千里 | 来源:发表于2019-02-24 22:45 被阅读0次

12、生成0代猫与促销猫

/// 游戏刚开始时,有宣传猫和初代猫两种,宣传猫直接给账号,初代猫放大市场拍卖
contract KittyMinting is KittyAuction {

    // 宣传猫5000只,可以直接发给自己的朋友
    uint256 public constant PROMO_CREATION_LIMIT = 5000;
    // 初代猫45000只
    uint256 public constant GEN0_CREATION_LIMIT = 45000;

    // 初代猫 最初的价格 和 存续期,相当于一个最低价
    uint256 public constant GEN0_STARTING_PRICE = 10 finney;
    uint256 public constant GEN0_AUCTION_DURATION = 1 days;

    // 宣传猫和初代猫的数量
    uint256 public promoCreatedCount;
    uint256 public gen0CreatedCount;

    ///产生宣传猫,只有COO才能调用,可以直接指定基因和拥有者
    /// @param _genes 基因
    /// @param _owner 拥有者
    function createPromoKitty(uint256 _genes, address _owner) external onlyCOO {
        address kittyOwner = _owner;
        if (kittyOwner == address(0)) {
             kittyOwner = cooAddress;
        }
        require(promoCreatedCount < PROMO_CREATION_LIMIT);

        promoCreatedCount++;
        _createKitty(0, 0, 0, _genes, kittyOwner);
    }

    /// 创建初代猫
    ///  直接的放入拍卖所
    function createGen0Auction(uint256 _genes) external onlyCOO {
        require(gen0CreatedCount < GEN0_CREATION_LIMIT);

        uint256 kittyId = _createKitty(0, 0, 0, _genes, address(this));
        // 授权给拍卖交易所
        _approve(kittyId, saleAuction);
        // 创建拍卖
        saleAuction.createAuction(
            kittyId,
            _computeNextGen0Price(),
            0,
            GEN0_AUCTION_DURATION,
            address(this)
        );
        // 初代猫数量加1
        gen0CreatedCount++;
    }

    /// 计算拍卖的初始价格
    function _computeNextGen0Price() internal view returns (uint256) {
        // 计算最近五次拍卖的平均价格
        uint256 avePrice = saleAuction.averageGen0SalePrice();

        // 验证
        require(avePrice == uint256(uint128(avePrice)));
        // 价格上涨了50%
        uint256 nextPrice = avePrice + (avePrice / 2);

        // 价格不能低于 初始价格
        if (nextPrice < GEN0_STARTING_PRICE) {
            nextPrice = GEN0_STARTING_PRICE;
        }
        // 返回最新的价格
        return nextPrice;
    }
}

KittyCore.sol

/// 
contract KittyCore is KittyMinting {

    // 设置新的合约地址,这是在游戏项目出现巨大的问题时,我们可以设置新的合约来升级
    address public newContractAddress;

    /// 构造函数
    function KittyCore() public {
        // 暂停
        paused = true;

        // 合约的部署地址就是CEO
        ceoAddress = msg.sender;

        // 合约的部署地址也是CEO
        cooAddress = msg.sender;

        // 创建了一只猫
        _createKitty(0, 0, 0, uint256(-1), address(0));
    }

    /// 设置新的合约地址
    function setNewAddress(address _v2Address) external onlyCEO whenPaused {
       
        newContractAddress = _v2Address;
        // 触发合约变更事件
        ContractUpgrade(_v2Address);
    }

    /// 转账,必须是拍卖所的合约
    // 匿名函数,用户直接给合约地址转账时,报错,这是为了防止误操作,只有交易所才能给合约转账
    function() external payable {
        // 
        require(
            msg.sender == address(saleAuction) ||
            msg.sender == address(siringAuction)
        );
    }

    /// 获取代币(猫)的信息
    /// @param _id 猫的id 
    function getKitty(uint256 _id)
        external
        view
        returns (
        bool isGestating,
        bool isReady,
        uint256 cooldownIndex,
        uint256 nextActionAt,
        uint256 siringWithId,
        uint256 birthTime,
        uint256 matronId,
        uint256 sireId,
        uint256 generation,
        uint256 genes
    ) {
        Kitty storage kit = kitties[_id];

        // if this variable is 0 then it's not gestating
        isGestating = (kit.siringWithId != 0);
        isReady = (kit.cooldownEndBlock <= block.number);
        cooldownIndex = uint256(kit.cooldownIndex);
        nextActionAt = uint256(kit.cooldownEndBlock);
        siringWithId = uint256(kit.siringWithId);
        birthTime = uint256(kit.birthTime);
        matronId = uint256(kit.matronId);
        sireId = uint256(kit.sireId);
        generation = uint256(kit.generation);
        genes = kit.genes;
    }

    /// 解锁暂停,只有ceo才能调用
    function unpause() public onlyCEO whenPaused {
        // 要求交易所地址不能为 0 
        require(saleAuction != address(0));
        require(siringAuction != address(0));
        // 要求基因组成地址不能为 0 
        require(geneScience != address(0));
         // 要求新的合约地址不能为 0 
        require(newContractAddress == address(0));

        // 解除暂停
        super.unpause();
    }

    // 转账到CFO address
    function withdrawBalance() external onlyCFO {
        // 本合约地址的金额
        uint256 balance = this.balance;
        // 计算怀孕猫的总的保证金
        uint256 subtractFees = (pregnantKitties + 1) * autoBirthFee;

        if (balance > subtractFees) {
            // 总的金额减去 怀孕猫的保证金,这个是要返还的
            cfoAddress.send(balance - subtractFees);
        }
    }
}

相关文章

网友评论

      本文标题:以太猫CryptoKitties源码(七):生成0代猫与促销猫

      本文链接:https://www.haomeiwen.com/subject/copvyqtx.html