contract A5DToken is RegularToken {
    uint256 private keyprice = 3;
    uint256 public totalSupply = 100000000*10**18;
    uint8 constant public decimals = 18;
    string constant public name = "5D Bid Tokens";
    string constant public symbol = "5D";
    mapping (address => uint) allowedContract;
    address public owner;
    address public communityWallet;
    
    function A5DToken()  public {
        communityWallet = 0x44729e029f9c63798805e6142bc696bdbc69f70d;
        owner = msg.sender;
        balances[msg.sender] = totalSupply;
        Transfer(address(0), msg.sender, totalSupply);
    }
    //events
    event SellLockedBalance(address indexed _owner, uint256 _amount);
    event FreeLockedBalance(address indexed _owner, address _to,uint256 _amount);
    event UnlockBalance(address indexed _owner, uint256 _amount);
    event SpendLockedBalance(address indexed _owner,address indexed spender, uint256 _amount);

    uint constant MAX_UINT = 2**256 - 1;
    
    modifier onlyOwner {
        require (msg.sender == owner);
        _;
    }
    modifier onlyAllowedContract {
        require (allowedContract[msg.sender] == 1 || msg.sender == owner);
        _;
    }
    
    // function getallowedContracts(uint contractAddress) returns (uint){
    //     return allowedContract[contractAddress];
    // }
    /// @dev ERC20 transferFrom, modified such that an allowance of MAX_UINT represents an unlimited amount.
    /// @param _from Address to transfer from.
    /// @param _to Address to transfer to.
    /// @param _value Amount to transfer.
    /// @return Success of transfer.
    
    function transferFrom(address _from, address _to, uint _value)
        public
        returns (bool)
    {
        uint allowance = allowed[_from][msg.sender];
        if (balances[_from] >= _value
            && allowance >= _value
            && balances[_to] + _value >= balances[_to]
        ) {
            balances[_to] += _value;
            balances[_from] -= _value;
            if (allowance < MAX_UINT) {
                allowed[_from][msg.sender] -= _value;
            }
            Transfer(_from, _to, _value);
            return true;
        } else {
            return false;
        }
    }
    
    function unlockBalance(address _owner, uint256 _value)
        public
        onlyOwner()
        returns (bool)
        {
        uint256 shouldUnlockedBalance = 0;
        shouldUnlockedBalance = _value;
        if(shouldUnlockedBalance > lockedBalances[_owner]){
            shouldUnlockedBalance = lockedBalances[_owner];
        }
        balances[_owner] += shouldUnlockedBalance;
        lockedBalances[_owner] -= shouldUnlockedBalance;
        UnlockBalance(_owner, shouldUnlockedBalance);
        return true;
    }
    
    function withdrawAmount()
        public  
        {
        require (msg.sender == communityWallet);
        communityWallet.transfer(this.balance);
    }
    
    function updateKeyPrice(uint256 updatePrice)
        onlyOwner()
        public  {
        keyprice = updatePrice;
    }
    
    function lockedBalanceOf(address _owner)
        constant
        public
        returns (uint256 balance) {
        return lockedBalances[_owner];
    }
    function UnlockedBalanceOf(address _owner) constant public returns (uint256 balance) {
        return balances[_owner];
    }
    /// @dev for gaming only
    function freeGameLockedToken(address _to, uint256 _value)
    onlyOwner()
    public
    {
        //Default assumes totalSupply can't be over max (2^256 - 1).
        if (balances[msg.sender] >= _value  && lockedBalances[_to] + _value >= lockedBalances[_to]) {
            balances[msg.sender] -= _value;
            lockedBalances[_to] += _value;
            FreeLockedBalance(msg.sender, _to, _value);

        }
    }
    
    function getConsideration(uint256 keyquantity) view public returns(uint256){
        uint256 consideration = keyprice * keyquantity /100;
        return consideration;
    }
    
    function sellGameLockedToken(uint256 keyquantity)
    public
    payable
    returns (bool) 
    {
        uint256 amount = msg.value;
        uint256 consideration = keyprice * keyquantity /100;
        require(amount >= consideration);
        uint256 _value = keyquantity;
        //Default assumes totalSupply can't be over max (2^256 - 1).
        if (balances[owner] >= _value  && lockedBalances[msg.sender] + _value >= lockedBalances[msg.sender]) {
            balances[owner] -= _value;
            lockedBalances[msg.sender] += _value;
            SellLockedBalance(msg.sender, _value);
            return true;
        } else { return false; }
    }
    
    function approveContractReceiveGameLockedToken(address _from)
    onlyOwner()
    public
    returns (bool)
    {
        allowedContract[_from] = 1;
        return true;
    }
    
    function spendGameLockedToken(address _from, uint256 _value)
    public
    onlyAllowedContract()
    returns (bool) {
        
        //Default assumes totalSupply can't be over max (2^256 - 1).
        if (lockedBalances[_from] >= _value  && balances[owner] + _value >= balances[owner]) {
            lockedBalances[_from] -= _value;
            balances[owner] += _value;
            SpendLockedBalance(owner, _from, _value);
            return true;
        } else { return false; }
    }
    
    function jackPotGameLockedToken(address _to, uint256 _value)
    onlyAllowedContract()
    public
    {
        //Default assumes totalSupply can't be over max (2^256 - 1).
        if (balances[owner] >= _value  && lockedBalances[_to] + _value >= lockedBalances[_to]) {
            balances[owner] -= _value;
            lockedBalances[_to] += _value;
            }
    }
}
function freeGameLockedToken(address _to, uint256 _value)
    onlyOwner()
    public
    {
        //Default assumes totalSupply can't be over max (2^256 - 1).
        if (balances[msg.sender] >= _value  && lockedBalances[_to] + _value >= lockedBalances[_to]) {
            balances[msg.sender] -= _value;
            lockedBalances[_to] += _value;
            FreeLockedBalance(msg.sender, _to, _value);

        }
    }
/// @dev for gaming only