contract FootballerBase is FootballerAccessControl {
  using SafeMath for uint256;
  /*** events ***/
  event Create(address owner, uint footballerId);
  event Transfer(address _from, address _to, uint256 tokenId);

  uint private randNonce = 0;

  //球员/球星 属性
  struct footballer {
    uint price; //球员-价格 ， 球星-一口价 单位wei
    //球员的战斗属性
    uint defend; //防御
    uint attack; //进攻
    uint quality; //素质
  }

  //存球星和球员
  footballer[] public footballers;
  //将球员的id和球员的拥有者对应起来
  mapping (uint256 => address) public footballerToOwner;

  //记录拥有者有多少球员，在balanceOf（）内部使用来解决所有权计数
  mapping (address => uint256) public ownershipTokenCount;

  //从footballID 到 已批准调用transferFrom（）的地址的映射
  //每个球员只能有一个批准的地址。零值表示没有批准
  mapping (uint256 => address) public footballerToApproved;

  // 将特定球员的所有权 赋给 某个地址
  function _transfer(address _from, address _to, uint256 _tokenId) internal {
    footballerToApproved[_tokenId] = address(0);
    ownershipTokenCount[_to] = ownershipTokenCount[_to].add(1);
    footballerToOwner[_tokenId] = _to;
    ownershipTokenCount[_from] = ownershipTokenCount[_from].sub(1);
    emit Transfer(_from, _to, _tokenId);
  }

  //管理员用于投放球星,和createStar函数一起使用，才能将球星完整信息保存起来
  function _createFootballerStar(uint _price,uint _defend,uint _attack, uint _quality) internal onlyManager returns(uint) {
      footballer memory _player = footballer({
        price:_price,
        defend:_defend,
        attack:_attack,
        quality:_quality
      });
      uint newFootballerId = footballers.push(_player) - 1;
      footballerToOwner[newFootballerId] = managerAddress;
      ownershipTokenCount[managerAddress] = ownershipTokenCount[managerAddress].add(1);
      //记录这个球星可以进行交易
      footballerToApproved[newFootballerId] = managerAddress;
      require(newFootballerId == uint256(uint32(newFootballerId)));
      emit Create(managerAddress, newFootballerId);
      return newFootballerId;
    }


    //用于当用户买卡包时，随机生成球员
    function createFootballer () internal returns (uint) {
        footballer memory _player = footballer({
          price: 0,
          defend: _randMod(20,80),
          attack: _randMod(20,80),
          quality: _randMod(20,80)
        });
        uint newFootballerId = footballers.push(_player) - 1;
      //  require(newFootballerId == uint256(uint32(newFootballerId)));
        footballerToOwner[newFootballerId] = msg.sender;
        ownershipTokenCount[msg.sender] =ownershipTokenCount[msg.sender].add(1);
        emit Create(msg.sender, newFootballerId);
        return newFootballerId;
    }

  // 生成一个从 _min 到 _max 范围内的随机数（不包括 _max）
  function _randMod(uint _min, uint _max) private returns(uint) {
      randNonce++;
      uint modulus = _max - _min;
      return uint(keccak256(now, msg.sender, randNonce)) % modulus + _min;
  }

}
function createFootballer () internal returns (uint) {
        footballer memory _player = footballer({
          price: 0,
          defend: _randMod(20,80),
          attack: _randMod(20,80),
          quality: _randMod(20,80)
        });
        uint newFootballerId = footballers.push(_player) - 1;
      //  require(newFootballerId == uint256(uint32(newFootballerId)));
        footballerToOwner[newFootballerId] = msg.sender;
        ownershipTokenCount[msg.sender] =ownershipTokenCount[msg.sender].add(1);
        emit Create(msg.sender, newFootballerId);
        return newFootballerId;
    }
//用于当用户买卡包时，随机生成球员