Exchange-Convert, yea, also by Chill to LUA 5
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Exchange-Convert, yea, also by Chill to LUA 5

Started by amenay, 11 June, 2005, 23:12:21

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

amenay

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)

??????Hawk??????

#1
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??????

amenay

#2
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.

SMF spam blocked by CleanTalk