PtokaX forum

Archive => Archived 5.0 boards => Request for scripts => Topic started by: amenay on 11 June, 2005, 23:12:21

Title: Exchange-Convert, yea, also by Chill to LUA 5
Post by: amenay on 11 June, 2005, 23:12:21
Can someone convert Exchange-Convert (yet another request for one of Chilla's scripts heh =) into LUA 5?  Haven't been able to get it done on my own. Many thanks!

Exchange-Convert (still in LUA 4) (http://www.plop.nl/ptokaxbots/Chilla/Exchange-Convert-V.1.lua)
Title:
Post by: ??????Hawk?????? on 11 June, 2005, 23:46:47
Removed Auto update  as Socket support is no longer supported in this version of ptokax.

use your old   txt/curRates.txt  file  and update manually



--Exchange-Converter V.1 written by chill
--Based on an idea from Kotten
-- LUA5 by ??????Hawk??????
--You need a "txt" folder, "scripts/txt"

bot1 = "Converter"

EPrefix = "%!%+%-%?"

MainCurrency = "EUR"

cmd1 = "changehelp"
cmd2 = "change"
cmd3 = "converthelp"
cmd4 = "convert"

--10 USD to EUR = 10/USD-EUR == EUR


Converters = {
["?F-?C"] = "([x]-32)*5/9",
["?C-?F"] = "[x]*5/9+32",
["IN-CM"] = "[x]*2.54",
["CM-IN"] = "[x]/2.54",
["FT-CM"] = "[x]*2.54*12",
["CM-FT"] = "[x]/(2.54*12)",
["MI-CM"] = "[x]*1.6093*1000*100",
["CM-MI"] = "[x]/(1.6093*1000*100)",
["KM-CM"] = "[x]*1000*100",
["CM-KM"] = "[x]/(1000*100)",
["M-CM"] = "[x]*100",
["CM-M"] = "[x]/100",
["LBS-KG"] = "[x]*0.4536",
["KG-LBS"] = "[x]/0.4536",
}

convhelp = "\tMiles = MI\r\n"..
"\tFoot = FT\r\n"..
"\tInch = IN\r\n"..
"\tCentimeter = CM\r\n"..
"\tMeter = M\r\n"..
"\tKilometer = KM\r\n"..
"\tPound = LBS\r\n"..
"\tKilogramms = KG\r\n"..
"\tFahrenheit = ?F\r\n"..
"\tCelsius = ?C\r\n"

FindRates = {
{ "American Dollar ","USD" },
{ "Australian Dollar ","AUD" },
{ "Botswana Pula ","BWP" },
{ "Brazilian Real ","BRL" },
{ "British Pound ","GBP" },
{ "Canadian Dollar ","CAD" },
{ "Chinese Yuan ","CNY" },
{ "Danish Krone ","DKK" },
{ "Hong Kong Dollar ","HKD" },
{ "Hungarian Forint ","HUF" },
{ "Indian Rupee ","INR" },
{ "Japanese Yen ","JPY" },
{ "Malaysian Ringgit ","MYR" },
{ "Mexican Peso ","MXN" },
{ "New Zealand Dollar ","NZD" },
{ "Norwegian Kroner ","NOK" },
{ "Singapore Dollar ","SGD" },
{ "South African Rand ","ZAR" },
{ "South Korean Won ","KRW" },
{ "Sri Lanka Rupee ","LKR" },
{ "Swedish Krona ","SEK" },
{ "Swiss Franc ","CHF" },
{ "Taiwan Dollar ","TWD" },
{ "Thai Baht ","THB" },
{ "Venezuelan Bolivar ","VEB" },
}

curday = 0

curRates = {}

dofile("txt/curRates.txt")


function Main()
local var = 0
curhelp = ""
for i,_ in FindRates do
curhelp = curhelp.."\tCurrency:    "..string.sub(FindRates[i][1],1,string.len(FindRates[i][1])-5).."     Shorten:     "..FindRates[i][2].."\r\n"
end
end

function ChatArrival(curUser,data)
data = string.sub(data,1,string.len(data)-1)
local _,_,cmd = string.find( data, "%b<>%s["..EPrefix.."](%S+)" )

if not cmd then
return 0
end

if (string.lower(cmd) == string.lower(cmd1)) then
curUser:SendData(bot1,"Syntax: Prefix\""..cmd2.."\" [Amount] [CurrentCurrency] [WantedCurrrency], Prefixa = + - ! ?\r\n\r\n"..
"\tAvailable Currencies =\r\n\r\n"..curhelp)
return 1

elseif (string.lower(cmd) == string.lower(cmd2)) then
local _,_,str1,str2,str3 = string.find(data,"%b<>%s+%S+%s+(%S+)%s+(%S+)%s+(%S+)")
if tonumber(str1) then
str2, str3 = string.upper(str2),string.upper(str3)
if curRates[str2.."-"..str3] then
local change = string.format("%0.2f",tonumber(str1)/curRates[str2.."-"..str3])
curUser:SendData(bot1,str1.." "..str2.." = "..change.." "..str3)
elseif tonumber(str1) and curRates[str2.."-"..MainCurrency] and curRates[MainCurrency.."-"..str3] then
local change = string.format("%0.2f",tonumber(str1)/(curRates[str2.."-"..MainCurrency]*curRates[MainCurrency.."-"..str3]))
curUser:SendData(bot1,str1.." "..str2.." = "..change.." "..str3)
end
else
curUser:SendData(bot1,"Syntax: Prefixa \""..cmd2.."\" [Amount] [CurrentCurrency] [WantedCurrrency], Prefixa = + - ! ?")
end
return 1

elseif (string.lower(cmd) == string.lower(cmd3)) then
curUser:SendData(bot1,"Syntax: Prefix\""..cmd4.."\" [Number] [CurrentValue] [WantedValue], Prefixa = + - ! ?\r\n\r\n"..convhelp)

elseif (string.lower(cmd) == string.lower(cmd4)) then
local _,_,str1,str2,str3 = string.find(data,"%b<>%s+%S+%s+(%S+)%s+(%S+)%s+(%S+)")
if tonumber(str1) then
str2, str3 = string.upper(str2),string.upper(str3)
conv = ""
if Converters[str2.."-"..str3] then
local x = string.gsub(Converters[str2.."-"..str3],"%b[]",str1)
assert(loadstring("conv="..x))
curUser:SendData(bot1,str1.." "..str2.." = "..string.format("%0.2f",conv).." "..str3)
elseif tonumber(str1) and Converters[str2.."-CM"] and Converters["CM-"..str3] then
local x1 = string.gsub(Converters[str2.."-CM"],"%b[]",str1)
assert(loadstring("conv="..x1))
local x2 = string.gsub(Converters["CM-"..str3],"%b[]",conv)
assert(loadstring("conv="..x2))
curUser:SendData(bot1,str1.." "..str2.." = "..string.format("%0.2f",conv).." "..str3)
end
else
curUser:SendData(bot1,"Syntax: Prefixa \""..cmd4.."\" [Number] [CurrentValue] [WantedValue], Prefixa = + - ! ?")
end
return 1
end
end
----------------------------------------------------------------------------------------------------------------------------------------------
**** UNTESTED AS i dont have a  txt/curRates.txt file and i dont know the layout. ****


??????Hawk??????
Title:
Post by: amenay on 12 June, 2005, 00:44:46
Nice fast response.  Tested it out with the curRates.txt and changeconvert is working fine.  But getting errors on the distance/temp/weight conversions.

I get this error whenever I try various conversions, including temp, KG to LBS, CM to M, and M to CM:

Syntax ...6\scripts\002-Exchange-Convert-V.1.lua:260: bad argument #2 to `format' (number expected, got string)

curUser:SendData(bot1,str1.." "..str2.." = "..string.format("%0.2f",conv).." "..str3)
And for their respective conversions these errors:

 Syntax ...6\scripts\002-Exchange-Convert-V.1.lua:270: [string "conv=/(2.54*12)"]:1: unexpected symbol near `/'

Syntax ...6\scripts\002-Exchange-Convert-V.1.lua:270: [string "conv=/2.54"]:1: unexpected symbol near `/'

Syntax ...6\scripts\002-Exchange-Convert-V.1.lua:270: [string "conv=/(1000*100)"]:1: unexpected symbol near `/'

Syntax ...6\scripts\002-Exchange-Convert-V.1.lua:270: [string "conv=/100"]:1: unexpected symbol near `/'

I haven't tried all combinations, but I'm sure you get the idea.  If you need curRates.txt for any future reference then just give word.  Appreciate the help.