Ulysses
General => Developers Corner => Topic started by: lavacano201014 on July 15, 2011, 10:55:28 PM
-
So I haven't done any Lua work in a while. So I made a chat calculator - because it'd be easy to make.
After finally realizing the only way I could do this was an interface to Google Calculator, I realized I could still do that. And it sort of works. If I do "calculate 2-2" I get 0 like I'm supposed to. But if I do anything else (even put spaces between the numbers and the minus), I get "Invalid Capture Index". That's all it tells me.
Here's the code (I'm sorry there's only one comment):
-- Supplementary Function: The http.Get callback
local function TheCalculatorCallback(result, _)
local jsontable = string.Explode("\"", result)
ULib.tsayColor(nil, true, Color(0,255,0), jsontable[2], Color(255,255,255), " = ", Color(255,255,0), jsontable[4])
end
function ulx.madhouse_calculator( calling_ply, equation )
http.Get("http://www.google.com/ig/calculator?q="..string.gsub(string.gsub(equation,"+", "%2B"), " ", ""), "", TheCalculatorCallback)
end
local calculator = ulx.command( CATEGORY_NAME, "ulx calculate", ulx.madhouse_calculator, "calculate")
calculator:addParam{type=ULib.cmds.StringArg, hint="equation", ULib.cmds.takeRestOfLine}
calculator:defaultAccess(ULib.ACCESS_ALL)
calculator:help("Calculate something, via Google Calculator")
The line it's saying this error is on is the http.Get line. CATEGORY_NAME is defined as "Madhouse" (separate Lua file). The one other command ("loudspeaker", highlights a message with a bunch of red asterisks) works fine.
-
So I haven't done any Lua work in a while. So I made a chat calculator - because it'd be easy to make.
After finally realizing the only way I could do this was an interface to Google Calculator, I realized I could still do that. And it sort of works. If I do "calculate 2-2" I get 0 like I'm supposed to. But if I do anything else (even put spaces between the numbers and the minus), I get "Invalid Capture Index". That's all it tells me.
Here's the code (I'm sorry there's only one comment):
-- Supplementary Function: The http.Get callback
local function TheCalculatorCallback(result, _)
local jsontable = string.Explode("\"", result)
ULib.tsayColor(nil, true, Color(0,255,0), jsontable[2], Color(255,255,255), " = ", Color(255,255,0), jsontable[4])
end
function ulx.madhouse_calculator( calling_ply, equation )
http.Get("http://www.google.com/ig/calculator?q="..string.gsub(string.gsub(equation,"+", "%2B"), " ", ""), "", TheCalculatorCallback)
end
local calculator = ulx.command( CATEGORY_NAME, "ulx calculate", ulx.madhouse_calculator, "calculate")
calculator:addParam{type=ULib.cmds.StringArg, hint="equation", ULib.cmds.takeRestOfLine}
calculator:defaultAccess(ULib.ACCESS_ALL)
calculator:help("Calculate something, via Google Calculator")
The line it's saying this error is on is the http.Get line. CATEGORY_NAME is defined as "Madhouse" (separate Lua file). The one other command ("loudspeaker", highlights a message with a bunch of red asterisks) works fine.
I haven't tried it or anything, but out of sheer gut instinct try this and let us know if it gives the same issue:
function ulx.madhouse_calculator( calling_ply, equation )
http.Get("http://www.google.com/ig/calculator?q="..(string.gsub((string.gsub(equation,"+", "%2B")), " ", "")), "", TheCalculatorCallback)
end
local calculator = ulx.command( CATEGORY_NAME, "ulx calculate", ulx.madhouse_calculator, "calculate")
calculator:addParam{type=ULib.cmds.StringArg, hint="equation", ULib.cmds.takeRestOfLine}
calculator:defaultAccess(ULib.ACCESS_ALL)
calculator:help("Calculate something, via Google Calculator")
-
Progress! Here's a chatlog with me and one of my admins trying to get it working.
Lavacano: calculate 2 + 2
Lavacano: RAGE.
Jakk: calculate 2 + 1
Lavacano: SO. MUCH. RAGE.
Lavacano: calculate 2 - 2
2 - 2 = 0
Jakk: calculate 2 - 2
2 - 2 = 0
Lavacano: calculate 2 ^2
2^2 = 4
Jakk: Huh.
Lavacano: calculate 6*2
6 * 2 = 12
Lavacano: calculate 6 * 2
6 * 2 = 12
Jakk: 2 / 3
Lavacano: calculate (6 * 2) - 1
(6 * 2) - 1 = 11
Lavacano: calculate 2 + 3
Jakk: calculate 2 / 0
=
Jakk: Aww.
Running any equation with + causes it to say "Invalid Capture Index", so something to do with the + symbol or it's URL escape.
Aha! If I change it so spaces are replaced with %20, spaces throw "invalid capture index".
-
A quick look at the URL when you type 1 + 1.
http://www.google.ca/search?sourceid=chrome&ie=UTF-8&q=1+%2B+1
Now with 1+1
http://www.google.ca/search?aq=f&sourceid=chrome&ie=UTF-8&q=1%2B1
Now with 1 * 1
http://www.google.ca/search?aq=f&sourceid=chrome&ie=UTF-8&q=1+*+1
And finally with 1*1
http://www.google.ca/search?aq=f&sourceid=chrome&ie=UTF-8&q=1*1
If you have not noticed the difference already, is that Google is using a Hex value for the + sign and replaces spaces with + I hope this helps.
For a list of the Hex values of the symbols go here.
http://www.ascii.cl/htmlcodes.htm (http://www.ascii.cl/htmlcodes.htm)
Edit: I am pretty sure that this should fix all of your troubles if not IDK :-[
-
A quick look at the URL when you type 1 + 1.
http://www.google.ca/search?sourceid=chrome&ie=UTF-8&q=1+%2B+1
Now with 1+1
http://www.google.ca/search?aq=f&sourceid=chrome&ie=UTF-8&q=1%2B1
Now with 1 * 1
http://www.google.ca/search?aq=f&sourceid=chrome&ie=UTF-8&q=1+*+1
And finally with 1*1
http://www.google.ca/search?aq=f&sourceid=chrome&ie=UTF-8&q=1*1
If you have not noticed the difference already, is that Google is using a Hex value for the + sign and replaces spaces with + I hope this helps.
For a list of the Hex values of the symbols go here.
http://www.ascii.cl/htmlcodes.htm (http://www.ascii.cl/htmlcodes.htm)
Edit: I am pretty sure that this should fix all of your troubles if not IDK :-[
I already figured that one out, but I figured out that it's the codes themselves (%2B) that are causing these errors. And I think it's because of the % sign itself.
-
Have you ever thought of breaking the string down and doing this all through Lua? No google.
-
Have you ever thought of breaking the string down and doing this all through Lua? No google.
Ideally I'd just check for "bad words" in the string and run a sanitized version through loadstring() but Garry removed that. Any other method would be too much hassle for me to maintain.
--edit--
uh HUH. So apparently the proper way to insert % in a Lua string is %%. So obvious and yet I'm so dumb for not thinking of it.
Thanks for all your help anyway guys!