ULX

Author Topic: I Can't Get This Chat Calculator To Work  (Read 4124 times)

0 Members and 1 Guest are viewing this topic.

Offline lavacano201014

  • Jr. Member
  • **
  • Posts: 83
  • Karma: 9
  • Archbishop of Cristina Scabbia worship
I Can't Get This Chat Calculator To Work
« 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):

Code: [Select]
-- 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.
Circle reasoning works because circle reasoning works because...

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6213
  • Karma: 394
  • Project Lead
Re: I Can't Get This Chat Calculator To Work
« Reply #1 on: July 15, 2011, 11:47:37 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):

Code: [Select]
-- 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:

Code: [Select]
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")

Experiencing God's grace one day at a time.

Offline lavacano201014

  • Jr. Member
  • **
  • Posts: 83
  • Karma: 9
  • Archbishop of Cristina Scabbia worship
Re: I Can't Get This Chat Calculator To Work
« Reply #2 on: July 16, 2011, 12:36:09 AM »
Progress! Here's a chatlog with me and one of my admins trying to get it working.

Code: [Select]
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".
« Last Edit: July 16, 2011, 12:54:13 AM by lavacano201014 »
Circle reasoning works because circle reasoning works because...

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Re: I Can't Get This Chat Calculator To Work
« Reply #3 on: July 16, 2011, 02:59:48 AM »
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

Edit: I am pretty sure that this should fix all of your troubles if not IDK  :-[
I cry every time I see that I am not a respected member of this community.

Offline lavacano201014

  • Jr. Member
  • **
  • Posts: 83
  • Karma: 9
  • Archbishop of Cristina Scabbia worship
Re: I Can't Get This Chat Calculator To Work
« Reply #4 on: July 19, 2011, 09:13:30 PM »
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

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.
Circle reasoning works because circle reasoning works because...

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: I Can't Get This Chat Calculator To Work
« Reply #5 on: July 19, 2011, 10:06:12 PM »
Have you ever thought of breaking the string down and doing this all through Lua?  No google.

Offline lavacano201014

  • Jr. Member
  • **
  • Posts: 83
  • Karma: 9
  • Archbishop of Cristina Scabbia worship
Re: I Can't Get This Chat Calculator To Work
« Reply #6 on: July 19, 2011, 10:48:27 PM »
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!
« Last Edit: July 19, 2011, 10:50:01 PM by lavacano201014 »
Circle reasoning works because circle reasoning works because...