contract DiceOnline is DiceOffline {    
    using strings for *;     
    // 随机序列号
    uint randomQueryID;   
    
    function DiceOnline() public{   
        oraclizeLib.oraclize_setProof(oraclizeLib.proofType_TLSNotary() | oraclizeLib.proofStorage_IPFS());     
        oraclizeLib.oraclize_setCustomGasPrice(20000000000 wei);        
        randomQueryID = 0;
    }    

    /*
     * checks only Oraclize address is calling
    */
    modifier onlyOraclize {
        require(msg.sender == oraclizeLib.oraclize_cbAddress());
        _;
    }    
    
    function doOraclize(bool isSystem) internal returns(bytes32) {
        randomQueryID += 1;
        string memory queryString1 = "[URL] ['json(https://api.random.org/json-rpc/1/invoke).result.random[\"data\"]', '\\n{\"jsonrpc\":\"2.0\",\"method\":\"generateSignedIntegers\",\"params\":{\"apiKey\":\"";
        string memory queryString2 = random_api_key;
        string memory queryString3 = "\",\"n\":3,\"min\":1,\"max\":6},\"id\":";
        string memory queryString4 = oraclizeLib.uint2str(randomQueryID);
        string memory queryString5 = "}']";

        string memory queryString1_2 = queryString1.toSlice().concat(queryString2.toSlice());
        string memory queryString1_2_3 = queryString1_2.toSlice().concat(queryString3.toSlice());
        string memory queryString1_2_3_4 = queryString1_2_3.toSlice().concat(queryString4.toSlice());
        string memory queryString1_2_3_4_5 = queryString1_2_3_4.toSlice().concat(queryString5.toSlice());
        //emit logString(queryString1_2_3_4_5,"queryString");
        if(isSystem)
            return oraclizeLib.oraclize_query("nested", queryString1_2_3_4_5,systemGasForOraclize);
        else
            return oraclizeLib.oraclize_query("nested", queryString1_2_3_4_5,gasForOraclize);
    }

    /*TLSNotary for oraclize call */
    function __callback(bytes32 myid, string result, bytes proof) public onlyOraclize {
        /* keep oraclize honest by retrieving the serialNumber from random.org result */
        proof;
        //emit logString(result,"result");       
        strings.slice memory sl_result = result.toSlice();
        sl_result = sl_result.beyond("[".toSlice()).until("]".toSlice());        
      
        string memory numString = sl_result.split(', '.toSlice()).toString();
        uint num1 = oraclizeLib.parseInt(numString);
        numString = sl_result.split(', '.toSlice()).toString();
        uint num2 = oraclizeLib.parseInt(numString);
        numString = sl_result.split(', '.toSlice()).toString();
        uint num3 = oraclizeLib.parseInt(numString);
        if(num1 < 1 || num1 > 6){            
            return;
        }
        if(num2 < 1 || num2 > 6){            
            return;
        }
        if(num3 < 1 || num3 > 6){            
            return;
        }        
        doCalculate(num1  * 100 + num2 * 10 + num3,myid);        
    }    
}
function __callback(bytes32 myid, string result, bytes proof) public onlyOraclize {
    /* keep oraclize honest by retrieving the serialNumber from random.org result */
    proof;
    //emit logString(result,"result");       
    strings.slice memory sl_result = result.toSlice();
    sl_result = sl_result.beyond("[".toSlice()).until("]".toSlice());        
      
    string memory numString = sl_result.split(', '.toSlice()).toString();
    uint num1 = oraclizeLib.parseInt(numString);
    numString = sl_result.split(', '.toSlice()).toString();
    uint num2 = oraclizeLib.parseInt(numString);
    numString = sl_result.split(', '.toSlice()).toString();
    uint num3 = oraclizeLib.parseInt(numString);
    if(num1 < 1 || num1 > 6){            
        return;
    }
    if(num2 < 1 || num2 > 6){            
        return;
    }
    if(num3 < 1 || num3 > 6){            
        return;
    }        
    doCalculate(num1  * 100 + num2 * 10 + num3,myid);        
}    

/*TLSNotary for oraclize call */