// File: mainnet/Latest/mainnet/0xe5219480626C4176a77046C3703Cbc260752a6E6/gamecontract.sol



// Note: 5,000,000 $HILO sent to old gamedeployer (0xf474671f80e0d1ad8a66e5bd24bd5b1b412bf750) are burned.
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

interface IERC20 {
    function totalSupply() external view returns (uint);

    function balanceOf(address account) external view returns (uint);

    function transfer(address recipient, uint amount) external returns (bool);

    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint amount) external returns (bool);

    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owner, address indexed spender, uint value);
}

contract HiloBetGameLauncher {
    uint public fee;
    uint256 public maxBet;
    uint256 public minBet;
    address public owner;
    IERC20 public immutable token;


    constructor(address _tradeToken, uint initialFee, uint256 initalMaxBet, uint256 initalMinBet) {
        fee = initialFee;
        maxBet = initalMaxBet;
        minBet = initalMinBet;
        owner = msg.sender;
        token = IERC20(_tradeToken);
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Not owner");
        // Underscore is a special character only used inside
        // a function modifier and it tells Solidity to
        // execute the rest of the code.
        _;
    }

    function changeFee(uint newFee) public onlyOwner  {
        fee = newFee;
    }

    function changeMaxBet(uint newMaxBet) public onlyOwner  {
        maxBet = newMaxBet;
    }

    function changeMinBet(uint newMinBet) public onlyOwner {
        minBet = newMinBet;
    }

    function bet(uint card, string memory choice, uint256 amount, uint256 multiplier) external returns (uint nextCard) {
        require(amount <= maxBet, "amount is greater than the maxBet");
        require(amount >= minBet, "amount is smaller than minBet");
        uint256 newAmount = amount * (fee / 1000);
        require(token.balanceOf(msg.sender) >= newAmount, "Token transfer failed.");
        token.transferFrom(msg.sender, address(this), 10250);
        uint next = uint(keccak256(abi.encodePacked(block.timestamp, block.difficulty,  
        msg.sender))) % 13 + 1; 
        if (keccak256(abi.encodePacked(choice)) == keccak256(abi.encodePacked('L'))) {
            if (card > next) {
                token.transfer(msg.sender, amount * (multiplier / 100));
                return next;
            } 
        } else if (keccak256(abi.encodePacked(choice)) == keccak256(abi.encodePacked('H'))) {
            if (card < next) {
                token.transfer(msg.sender, amount * (multiplier / 100));
                return next;
            }
        }

        return next;
    }

    receive() external payable {}

}