contract TrypCrowdsale is StandardToken, Ownable {
	
    using SafeMath for uint256;

	string public name = "Tryp"; 
	string public symbol = "Tryp";
	uint public decimals = 18;
	uint256 public constant INITIAL_SUPPLY = 1000000000000000000000000;

  // The token being sold

	StandardToken public token = this;

  // Addresses where funds are collected

	address private constant prizewallet = (0x6eFd9391Db718dEff494C2199CD83E0EFc8102f6);
	address private constant prize2wallet = (0x426570e5b796A2845C700B4b49058E097f7dCb54);
	address private constant adminwallet = (0xe7d718cc663784480EBB62A672180fbB68f89424);

  // How many token units a buyer gets per wei

    uint256 public weiPerToken = 15000000000000000;
 //   uint256 public ethPriceToday;


  // Amount of wei raised

	uint256 public weiRaised;
    uint256 public totalSupply_;
    uint256 public remainingSupply_;
 
  /**
   * Event for token purchase logging
   * @param purchaser who paid for the tokens
   * @param beneficiary who got the tokens
   * @param value weis paid for purchase
   * @param amount amount of tokens purchased
   */

    event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);


	
    function TrypCrowdsale () public payable {
		
		totalSupply_ = INITIAL_SUPPLY;
		remainingSupply_ = INITIAL_SUPPLY;
		balances[msg.sender] = INITIAL_SUPPLY;
		emit Transfer(0x0, msg.sender, INITIAL_SUPPLY);
			
	}


  // -----------------------------------------
  // Crowdsale external interface
  // -----------------------------------------

  /**
   * @dev fallback function ***DO NOT OVERRIDE***
   */

    function () external payable {
        buyTokens(msg.sender);
    }

  /**
   * @dev low level token purchase ***DO NOT OVERRIDE***
   * @param _beneficiary Address performing the token purchase
   */
    function buyTokens(address _beneficiary) public payable {

        uint256 weiAmount = msg.value;
        _preValidatePurchase(_beneficiary, weiAmount);

    // calculate token amount to be sent
        uint256 tokens = _getTokenAmount(weiAmount);
        require(tokens <= remainingSupply_);

    // update state
        weiRaised = weiRaised.add(weiAmount);

        _deliverTokens(_beneficiary, tokens);
        emit TokenPurchase(msg.sender, _beneficiary, weiAmount, tokens);

        _forwardFunds();

    }

  // -----------------------------------------
  // Internal interface (extensible)
  // -----------------------------------------

  // Set rate in ETH * 1000

    function setRate (uint256 _ethPriceToday) public onlyOwner {

        require(_ethPriceToday != 0);
        weiPerToken = (_ethPriceToday * 1e18)/1000;

    }    

  /**
   * @dev Validation of an incoming purchase. Use require statemens to revert state when conditions are not met. Use super to concatenate validations.
   * @param _beneficiary Address performing the token purchase
   * @param _weiAmount Value in wei involved in the purchase
   */

    function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal pure {
        
        require(_beneficiary != address(0));
        require(_weiAmount != 0);
    }

  /**
   * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
   * @param _beneficiary Address performing the token purchase
   * @param _tokenAmount Number of tokens to be emitted
   */

    function _deliverTokens(address _beneficiary, uint256 _tokenAmount) internal {

        token.transfer(_beneficiary, _tokenAmount);
        remainingSupply_ -= _tokenAmount;
    }

  // Use to correct any token transaction errors
  
    function deliverTokensAdmin(address _beneficiary, uint256 _tokenAmount) public onlyOwner {

        token.transfer(_beneficiary, _tokenAmount);
        remainingSupply_ -= _tokenAmount;
    }


  /**
   * @dev Override to extend the way in which ether is converted to tokens.
   * @param _weiAmount Value in wei to be converted into tokens
   * @return Number of tokens that can be purchased with the specified _weiAmount
   */

    function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
		uint256 _tokens = ((_weiAmount/weiPerToken) * 1e18);
        return (_tokens);
    }

  /**
   * @dev Determines how ETH is stored/forwarded on purchases.
   */
    function _forwardFunds() internal {

    	uint total_eth = (msg.value);
    	uint prize_pool = (total_eth * 50)/100; // 50% to Main Prize Pool
    	uint prize_pool_sec = (total_eth * 10)/100; // 10% to Secondary Prize Pool
    	uint admin_pool = (total_eth-prize_pool-prize_pool_sec); // Remainder to Admin Pool

    	prizewallet.transfer(prize_pool);
    	prize2wallet.transfer(prize_pool_sec);
    	adminwallet.transfer(admin_pool);
    }

}
////////////////////////////////////////////////////////////////////////
function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
nt256 _tokens = ((_weiAmount/weiPerToken) * 1e18);
    return (_tokens);
}

/**
 * @dev Override to extend the way in which ether is converted to tokens.
 * @param _weiAmount Value in wei to be converted into tokens
 * @return Number of tokens that can be purchased with the specified _weiAmount
 */