contract FootBall is Ownable,ERC721{
    using SafeMath for uint256;
    uint public drawFee = 0.05 ether;
    uint public defendFee = 0.01 ether;
    uint public inviteRate = 5;
    uint coolDownTime = 24 hours;
    uint public playerInDraw = 0;
    uint backFee = 75;
    event newPlayer(uint _id,uint16 _attack);
    event drawedPlayer(uint _id,address indexed _to,uint _time);
    event battleLog(uint _playerA,uint _playerB,uint _result,uint _rdId,address indexed _addressA,address indexed _addressB);
    event playerDefend(uint _id,uint _time);
    event playerBack(uint _id , address indexed _address);
    event purChase(uint _id, address _newowner, address _oldowner);
    event inviteBack(address _from,address _to, uint _fee);
    //name&pic store in db;
    struct Player{
        uint256 sellPrice;
        uint256 readytime;
        uint16 attack;
        uint16 winCount;
        uint16 lossCount;
        uint8 isSell;
        uint8 isDraw;
    }
    Player[] public players; 
    mapping(uint=>address) playerToOwner;
    mapping(address=>uint) ownerPlayerCount;
    mapping (uint => address) playerApprovals;
    //modifier
    modifier onlyOwnerOf(uint _id) {
        require(msg.sender == playerToOwner[_id]);
        _;
    }
    //owner draw _money
    function withdraw() external onlyOwner {
        owner.transfer(address(this).balance);
    }
    function() payable public{}
    //change fee
    function changeDrawFee(uint _money)public onlyOwner{ 
        drawFee = _money;
    }
    function changeDefendFee(uint _money) public onlyOwner{
        defendFee = _money;
    }
    function changeCoolDownTime(uint _time) public onlyOwner{
        coolDownTime = _time;
    }
    function changeInviteRate(uint _rate) public onlyOwner{
        inviteRate = _rate;
    }
    //create player;
    function createPlayer(uint16 _attack) public onlyOwner{ 
      uint id = players.push (Player(0 ether,0,_attack,0,0,0,0)) - 1;
      playerInDraw = playerInDraw.add(1);
      emit newPlayer(id,_attack);
    }
    //draw card
    function drawPlayer(address _address) public payable returns (uint playerId){
        require(msg.value == drawFee && playerInDraw > 0);
        for(uint i =0;i < players.length;i++){ 
            if(players[i].isDraw == 0){ 
                players[i].isDraw = 1;
                playerInDraw  = playerInDraw.sub(1);
                playerToOwner[i] = msg.sender;
                ownerPlayerCount[msg.sender] = ownerPlayerCount[msg.sender].add(1);
                if(_address != 0){ 
                 uint inviteFee = msg.value * 5 / 100;
                 _address.transfer(inviteFee);
                 emit inviteBack(msg.sender,_address,inviteFee);
                }
                emit drawedPlayer(i,msg.sender,now);
                return i;
            }
        }
    }
    //battle 
    function playerAttack(uint _playerA,uint _playerB) external{
        require(playerToOwner[_playerA] == msg.sender && players[_playerB].isDraw == 1 && playerToOwner[_playerA] != playerToOwner[_playerB]);
        require(now >= players[_playerB].readytime);
        uint rdId = uint256(keccak256(block.difficulty,now))%1000;
        uint attackA;
        uint attackB;
        address ownerOfB = playerToOwner[_playerB];
        if(rdId >= players[_playerA].attack){
            attackA = rdId -  players[_playerA].attack;
        }else{ 
            attackA =  players[_playerA].attack - rdId;
        }
        if(rdId >= players[_playerB].attack){
            attackB =  rdId -  players[_playerB].attack;
        }else{
            attackB =  players[_playerB].attack - rdId;
        }
        uint8 result= 0;
        if(attackA < attackB){
            result = 1;
            playerToOwner[_playerB] = msg.sender;
            ownerPlayerCount[msg.sender] = ownerPlayerCount[msg.sender].add(1);
            ownerPlayerCount[playerToOwner[_playerB]] = ownerPlayerCount[playerToOwner[_playerB]].sub(1);
        }else{ 
            result = 2;
            playerToOwner[_playerA] = playerToOwner[_playerB];
            ownerPlayerCount[msg.sender] = ownerPlayerCount[msg.sender].sub(1);
            ownerPlayerCount[playerToOwner[_playerB]] = ownerPlayerCount[playerToOwner[_playerB]].add(1);
        }
        emit battleLog(_playerA,_playerB,result,rdId,msg.sender,ownerOfB);
    }
    //defend
    function getPlayerDefend(uint _id) public payable{
        require(msg.value == defendFee && msg.sender == playerToOwner[_id]);
        players[_id].readytime = uint256(now + coolDownTime);
        emit playerDefend(_id,players[_id].readytime);
    }
    //sendback
    function sendPlayerBack(uint[] _id) public {
        for(uint i=0;i<_id.length;i++){ 
            uint256 id = _id[i];
            require(playerToOwner[id] == msg.sender);
            uint fee = drawFee * backFee/100;
            //init player info 
            players[id].isDraw = 0;
            players[id].isSell = 0;
            players[id].readytime = 0;
            players[id].sellPrice = 0 ether;
            playerToOwner[id] = 0;
            ownerPlayerCount[msg.sender] = ownerPlayerCount[msg.sender].sub(1);
            playerInDraw  = playerInDraw.add(1);
            if(address(this).balance >= fee){ 
                msg.sender.transfer(fee);    
            }  
            emit playerBack(id,msg.sender);
        }

    }
    //ERC721 functions;
    function balanceOf(address _owner) public view returns (uint256 _balance) {
        return ownerPlayerCount[_owner];
    }

    function ownerOf(uint256 _tokenId) public view returns (address _owner) {
        return playerToOwner[_tokenId];
    }
    function _transfer(address _from, address _to, uint256 _tokenId) private {
        require(_from != _to);
        ownerPlayerCount[_to] = ownerPlayerCount[_to].add(1) ;
        ownerPlayerCount[_from] = ownerPlayerCount[_from].sub(1);
        playerToOwner[_tokenId] = _to;
        emit Transfer(_from, _to, _tokenId);
    }
    function transfer(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
        _transfer(msg.sender, _to, _tokenId);
    }

    function approve(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
        playerApprovals[_tokenId] = _to;
        emit Approval(msg.sender, _to, _tokenId);
    }
    function takeOwnership(uint256 _tokenId) public {
        require(playerApprovals[_tokenId] == msg.sender && playerToOwner[_tokenId] != msg.sender);
        address owner = ownerOf(_tokenId);
        _transfer(owner, msg.sender, _tokenId);
    }
    //market functions
    function setPlayerPrice(uint _id,uint _price) public payable onlyOwnerOf(_id){ 
        require(msg.value == defendFee);
        players[_id].isSell = 1;
        players[_id].sellPrice = _price;
        players[_id].readytime = uint256(now + coolDownTime);
    }
    function playerTakeOff(uint _id) public onlyOwnerOf(_id){
        players[_id].isSell = 0;
    }
    function purchase(uint _id) public payable{
        require(players[_id].isSell == 1 && msg.value == players[_id].sellPrice &&msg.sender != playerToOwner[_id]);
        address owner = playerToOwner[_id];
        ownerPlayerCount[owner] = ownerPlayerCount[owner].sub(1) ;
        ownerPlayerCount[msg.sender] = ownerPlayerCount[msg.sender].add(1);
        playerToOwner[_id] = msg.sender;
        owner.transfer(msg.value);
        emit purChase(_id,msg.sender,owner);
    }
}
function createPlayer(uint16 _attack) public onlyOwner{ 
  uint id = players.push (Player(0 ether,0,_attack,0,0,0,0)) - 1;
  playerInDraw = playerInDraw.add(1);
  emit newPlayer(id,_attack);
}

//create player;