contract MonsterChampionship is Ownable {

    bool public isMonsterChampionship = true;

    ChainMonstersCore core;

    // list of top ten
    address[10] topTen;

    // holds the address current "world" champion
    address public currChampion;

    mapping (address => uint256) public addressToPowerlevel;
    mapping (uint256 => address) public rankToAddress;
    
    // try to beat every other player in the top10 with your strongest monster!
    // effectively looping through all top10 players, beating them one by one
    // and if strong enough placing your in the top10 as well
    function contestChampion(uint256 _tokenId) external {
        //uint maxIndex = 9;

        // fail tx if player is already champion!
        // in theory players could increase their powerlevel by contesting themselves but
        // this check stops that from happening so other players have the chance to
        // become the temporary champion!
        if (currChampion == msg.sender) {
            revert();
        }

        require(core.isTrainer(msg.sender));
        require(core.monsterIndexToOwner(_tokenId) == msg.sender);

       
        
        var (n, m, stats, l, k, d) =  core.getMonster(_tokenId);
        //uint8[7] ivs = core.monsterIdToIVs(_tokenId);
        
        uint256 myPowerlevel = uint256(stats[0]) + uint256(stats[1]) + uint256(stats[2]) + uint256(stats[3]) + uint256(stats[4]) + uint256(stats[5]);
        

        // checks if this transaction is useless
        // since we can't fight against ourself!
        // also stops reentrancy attacks
        require(myPowerlevel > addressToPowerlevel[msg.sender]);

        uint myRank = 0;

        for (uint i = 0; i <= 9; i++) {
            if (myPowerlevel > addressToPowerlevel[topTen[i]]) {
                // you have beaten this one so increase temporary rank
                myRank = i;

                if (myRank == 9) {
                    currChampion = msg.sender;
                }
            }
        }

        addressToPowerlevel[msg.sender] = myPowerlevel;

        address[10] storage newTopTen = topTen;

        if (currChampion == msg.sender) {
            for (uint j = 0; j < 9; j++) {
                // remove ourselves from this list in case
                if (newTopTen[j] == msg.sender) {
                    newTopTen[j] = 0x0;
                    break;
                }
            }
        }

        for (uint x = 0; x <= myRank; x++) {
            if (x == myRank) {
                newTopTen[x] = msg.sender;
            } else {
                if (x < 9)
                    newTopTen[x] = topTen[x+1];
            }
        }

        topTen = newTopTen;
    }

    function getTopPlayers() external view returns (address[10] players) {
        players = topTen;
    }

    function MonsterChampionship(address coreContract) public {
        core = ChainMonstersCore(coreContract);
    }

    function withdrawBalance() external onlyOwner {
        uint256 balance = this.balance;
        owner.transfer(balance);
    }
}
// used during launch for world championship
// can and will be upgraded during development with new battle system!
// this is just to give players something to do and test their monsters
// also demonstrates how we can build up more mechanics on top of our locked core contract!
function contestChampion(uint256 _tokenId) external {
    //uint maxIndex = 9;

    // fail tx if player is already champion!
    // in theory players could increase their powerlevel by contesting themselves but
    // this check stops that from happening so other players have the chance to
    // become the temporary champion!
    if (currChampion == msg.sender) {
        revert();
    }

    require(core.isTrainer(msg.sender));
    require(core.monsterIndexToOwner(_tokenId) == msg.sender);

       
        
    var (n, m, stats, l, k, d) =  core.getMonster(_tokenId);
    //uint8[7] ivs = core.monsterIdToIVs(_tokenId);
        
    uint256 myPowerlevel = uint256(stats[0]) + uint256(stats[1]) + uint256(stats[2]) + uint256(stats[3]) + uint256(stats[4]) + uint256(stats[5]);
        

    // checks if this transaction is useless
    // since we can't fight against ourself!
    // also stops reentrancy attacks
    require(myPowerlevel > addressToPowerlevel[msg.sender]);

    uint myRank = 0;

    for (uint i = 0; i <= 9; i++) {
        if (myPowerlevel > addressToPowerlevel[topTen[i]]) {
            // you have beaten this one so increase temporary rank
            myRank = i;

            if (myRank == 9) {
                currChampion = msg.sender;
            }
        }
    }

    addressToPowerlevel[msg.sender] = myPowerlevel;

    address[10] storage newTopTen = topTen;

    if (currChampion == msg.sender) {
        for (uint j = 0; j < 9; j++) {
            // remove ourselves from this list in case
            if (newTopTen[j] == msg.sender) {
                newTopTen[j] = 0x0;
                break;
            }
        }
    }

    for (uint x = 0; x <= myRank; x++) {
        if (x == myRank) {
            newTopTen[x] = msg.sender;
        } else {
            if (x < 9)
                newTopTen[x] = topTen[x+1];
        }
    }

    topTen = newTopTen;
}

// try to beat every other player in the top10 with your strongest monster!
// effectively looping through all top10 players, beating them one by one
// and if strong enough placing your in the top10 as well