function swapCardForReward(address _by, uint8 _rank)
    onlyCard
    public
    returns (uint256)
  {
    
    
    
    require(tx.origin != _by && tx.origin != msg.sender);

    
    var _randomValue = random(100, 0);
    
    
    
    uint8 _heroRankToMint = 0; 

    if (_rank == 0) { 
      if (_randomValue < 85) {
        _heroRankToMint = 3;
      } else {
        _heroRankToMint = 4;
      }
    } else if (_rank == 1) { 
      if (_randomValue < 50) {
        _heroRankToMint = 1;
      } else if (_randomValue < 80) {
        _heroRankToMint = 2;
      }  else if (_randomValue < 99) {
        _heroRankToMint = 3;
      } else {
        _heroRankToMint = 4;
      }
    } else if (_rank == 2) { 
      if (_randomValue < 50) {
        _heroRankToMint = 0;
      } else if (_randomValue < 85) {
        _heroRankToMint = 1;
      } else {
        _heroRankToMint = 2;
      }
    } else { 
      _heroRankToMint = 0;
    }

    
    uint32 _numberOfClasses = heroContract.numberOfHeroClasses();
    uint32[] memory _candidates = new uint32[](_numberOfClasses);
    uint32 _count = 0;
    for (uint32 i = 0; i < _numberOfClasses; i ++) {
      if (heroContract.getClassRank(i) == _heroRankToMint && blackList[i] != true) {
        _candidates[_count] = i;
        _count++;
      }
    }

    require(_count != 0);
    
    return heroContract.mint(tx.origin, _candidates[random(_count, 0)]);
  }