contract PowerOne is PowerEtherHelper, usingOraclize {
    
    /// @dev checks only Oraclize address is calling
    modifier onlyOraclize {
        require(msg.sender == oraclize_cbAddress());
        _;
    }
    
    /**
     * @dev Makes the bid to the PowerOne game.
     */
    function makePowerOneBid(uint256 numberOne)
        public
        payable
        isHuman
        isActivated {
            
            require(numberOne >= minNumber && numberOne <= maxNumber, "The number chosen is invalid!");
            
            uint256 payment = SafeMath.add(powerOneBid, powerOneFee);
            uint256 totalPayment = SafeMath.add(payment, megaJackpotFee);
            
            require(msg.value == totalPayment, "Wrong payment value!");
            
            randomQueryId += 1;
            
            powerOneFeesToCollect ++;
            
            // Compose the Oraclize query
            string memory queryStringOne = "[URL] ['json(https://api.random.org/json-rpc/1/invoke).result.random[\"serialNumber\",\"data\"]', '\\n{\"jsonrpc\":\"2.0\",\"method\":\"generateSignedIntegers\",\"params\":{\"apiKey\":\"${[decrypt] BEna2ojyJ8x3euQmExkugHrukwYeMH2Z7o3e9XEqATmN1ApOokRElT5IJEp1JNFhbn3dvdEo3wLaDaZJu5PqRUaoI4ZnbDTwAmMtkfLP1jBD7OldcYReDzG4cc5tdjCdP2KbzhIOEuXskoW3PzkqHzGq641e}\",\"n\":1,\"min\":1,\"max\":10,\"replacement\":false,\"base\":10${[identity] \"}\"},\"id\":";
            string memory queryStringTwo = uint2str(randomQueryId);
            string memory queryStringThree = "${[identity] \"}\"}']";
            
            string memory queryStringOne_Two = queryStringOne.toSlice().concat(queryStringTwo.toSlice());
            string memory queryStringOne_Two_Three = queryStringOne_Two.toSlice().concat(queryStringThree.toSlice());
            
            bytes32 queryId = oraclize_query("nested", queryStringOne_Two_Three, gasForOraclize);
            
            senderAddresses[queryId] = msg.sender;
            
            gameTypes[queryId] = 1;
            
            powerNumberOne[queryId] = numberOne;
            
            powerOneJackpot += powerOneBid;
            
            megaJackpot += megaJackpotFee;

            totalGamesPlayed ++;
            
            emit LogQuery(
                msg.sender,
                1,
                randomQueryId,
                numberOne,
                0,
                0,
                0
                );
                
        }
    
    /**
     * @dev Internal core logic of the PowerOne game
     */
    function _powerOne(
        string result,
        uint256 pnOne,
        address playerAddress
        ) internal {
        // Sanity check
        require(pnOne != 0, "Invalid game, refunded!");
                
        require(powerOneJackpot <= address(this).balance, "Insufficient funds!");
                
        strings.slice memory res = result.toSlice();
        strings.slice memory delim = " ".toSlice();
        uint256[] memory parts = new uint256[](res.count(delim) + 1);
        for (uint256 i = 0; i < parts.length; i ++) {
            parts[i] = parseInt(res.split(delim).toString());
        }
                
        // Refunding if the result is 0 or no proof is provided.       
        if (bytes(result).length == 0) {
                    
            emit Refund(
                playerAddress,
                1
                );
                
            if (!playerAddress.send(SafeMath.add(powerOneBid, powerOneFee))) {
                
                playerFundsToWithdraw[playerAddress] = SafeMath.add(powerOneBid, powerOneFee);                     
            }
                    
            playerAddress = 0x0;
                    
            return;
                    
        }
                
        if (parts[1] == pnOne) {
                    
            if(_checkMegaJackpotCap(playerAddress)) {
                bool checkResult = true;
            } else {
                checkResult = false;
            }
                       
            powerOneWinCounter ++;
                    
            // Calculating the eligible payout
            uint256 eligiblePayout = SafeMath.div(SafeMath.mul(powerOneJackpot, platformCut), 100);
            uint256 platformCutPayout = SafeMath.sub(powerOneJackpot, eligiblePayout);
                    
            playerAddress.transfer(eligiblePayout);
                    
            ceoAddress.transfer(platformCutPayout);

            emit PowerEtherResults(
                playerAddress,
                parts[0],
                1,
                pnOne,
                0,
                0,
                0,
                eligiblePayout,
                true,
                checkResult
                );

            totalEtherWon += eligiblePayout;
                    
            powerOneJackpot = 0;
                    
            playerAddress = 0x0;            

        } else if (parts[1] != pnOne) {
                    
            emit PowerEtherResults(
                playerAddress,
                parts[0],
                1,
                pnOne,
                0,
                0,
                0,
                eligiblePayout,
                false,
                false
                );
                    
            playerAddress = 0x0;

        }
        
    }
    
}
function _powerOne(
        string result,
        uint256 pnOne,
        address playerAddress
        ) internal {
        // Sanity check
        require(pnOne != 0, "Invalid game, refunded!");
                
        require(powerOneJackpot <= address(this).balance, "Insufficient funds!");
                
        strings.slice memory res = result.toSlice();
        strings.slice memory delim = " ".toSlice();
        uint256[] memory parts = new uint256[](res.count(delim) + 1);
        for (uint256 i = 0; i < parts.length; i ++) {
            parts[i] = parseInt(res.split(delim).toString());
        }
                
        // Refunding if the result is 0 or no proof is provided.       
        if (bytes(result).length == 0) {
                    
            emit Refund(
                playerAddress,
                1
                );
                
            if (!playerAddress.send(SafeMath.add(powerOneBid, powerOneFee))) {
                
                playerFundsToWithdraw[playerAddress] = SafeMath.add(powerOneBid, powerOneFee);                     
            }
                    
            playerAddress = 0x0;
                    
            return;
                    
        }
                
        if (parts[1] == pnOne) {
                    
            if(_checkMegaJackpotCap(playerAddress)) {
                bool checkResult = true;
            } else {
                checkResult = false;
            }
                       
            powerOneWinCounter ++;
                    
            // Calculating the eligible payout
            uint256 eligiblePayout = SafeMath.div(SafeMath.mul(powerOneJackpot, platformCut), 100);
            uint256 platformCutPayout = SafeMath.sub(powerOneJackpot, eligiblePayout);
                    
            playerAddress.transfer(eligiblePayout);
                    
            ceoAddress.transfer(platformCutPayout);

            emit PowerEtherResults(
                playerAddress,
                parts[0],
                1,
                pnOne,
                0,
                0,
                0,
                eligiblePayout,
                true,
                checkResult
                );

            totalEtherWon += eligiblePayout;
                    
            powerOneJackpot = 0;
                    
            playerAddress = 0x0;            

        } else if (parts[1] != pnOne) {
                    
            emit PowerEtherResults(
                playerAddress,
                parts[0],
                1,
                pnOne,
                0,
                0,
                0,
                eligiblePayout,
                false,
                false
                );
                    
            playerAddress = 0x0;

        }
        
    }
/**
 * @dev Internal core logic of the PowerOne game
 */