Average Share 1.0a LUA 5.1x [API 2]
 

Average Share 1.0a LUA 5.1x [API 2]

Started by CyCLoNe, 24 March, 2010, 05:33:02

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

CyCLoNe

--Average Share 1.0a LUA 5.1x [API 2]
--By CyCLoNe - 03/21/10 at 11:36 am
--Fixed the display of MB, GB, TB
--Added categorized messages for Below (10 GiB, 25 GiB, 50 GiB, 100 GiB)
--and Above( 50 GiB, 100 GiB, 250 GiB, 500 GiB)
--API 2 version tested on Ptokax v.0.4.1.2 New release


AverageCfg = {
-- "Botname" ["" = hub bot]
Bot = "",
-- Hub Name ["" = pull from hub]
Hub = "",
-- Should bot have a key?
BotIsOp = true,
-- Bot description
BotDesc = "Average Share",
-- Bot Email address
BotMail = "user@domain.com",
}

local Timer = 0
OnStartup = function()
	assert(_VERSION == "Lua 5.1","Incorrect Lua version.")
	AverageCfg.Scp = "Average Share"
	if AverageCfg.Bot == "" then AverageCfg.Bot = SetMan.GetString(21) end
	if AverageCfg.Hub == "" then AverageCfg.Hub = SetMan.GetString(0) end
	if not Core then
		local hub = ""
		local app,ver = getHubVersion()
		if app and ver then hub = app.." "..ver.." " end
		OnError("This script is incompatible with "..hub.._VERSION)
	end
	local Path = Core.GetPtokaXPath().."scripts/"
	if AverageCfg.Bot ~= SetMan.GetString(21) then
		Core.RegBot(AverageCfg.Bot, AverageCfg.BotDesc, AverageCfg.BotMail, AverageCfg.BotIsOp)
	end

end


UserConnected = function(user)
	if (Core.GetUsersCount() > 2) and (Core.GetCurrentSharedSize() > 0) and (Core.GetUserValue(user,16) > 0) then
		local ProperUnits = function( i )
			local i = tonumber(i)
			if i == 0 then return "0.00 KiB"; end
			if i >= (1024^2) then
				if i >= (1024^3) then
					if i >= (1024^4) then return string.format("%.2f", (i/(1024^4))).." TiB"; end
					return string.format("%.2f", (i/(1024^3))).." GiB"
				end; return string.format("%.2f", (i/(1024^2))).." MiB"
			end;return string.format("%.2f", (i/(1024))).." KiB"
		end
		local SafeDivide = function( a, b )
			if ( a == 0 and b ~= 0 ) or ( a == 0 and b == 0 )then return false; end
			if b == 0 and a ~= 0 then return true; end
			return a/b
		end
		local iAverageShare = SafeDivide( Core.GetCurrentSharedSize(), Core.GetUsersCount() )
		local m = "\r\n\r\n\t================================================================================== "
		m = m.."\r\n\t\t\t\t       "..SetMan.GetString(0).."\r\n\t==================================================================================\n\n\t"..Core.GetUsersCount().." Users in the hub are currently sharing "..ProperUnits(Core.GetCurrentSharedSize()).." giving an average of "..ProperUnits(iAverageShare).." each."
		if Core.GetUserValue(user,16) >= iAverageShare then
			m = m.."\r\n\tYou are "..(ProperUnits( Core.GetUserValue(user,16) - iAverageShare )).." above the Average per user share... you're immense !!!"
			local iShare = SafeDivide(Core.GetUserValue(user,16), 1024^3)
			if iShare >= 50 and iShare < 100 then m = m.."\r\n\tYou have boosted the average up, you're amazing !!!"
			elseif iShare >= 100 and iShare < 250 then m = m.."\r\n\tYou have boosted the average up, There are people that will pay for your share !!!"
			elseif iShare >=250 and iShare < 500 then m = m.."\r\n\tYou have boosted the average up, There are people that will kill for your share !!!"
			elseif iShare >= 500 then m = m.."\r\n\tThere are people that will die for your share, or even a slot !!!"
			end
			m = m.."\r\n\t ... and yes we need you here !  ;)"
		elseif Core.GetUserValue(user,16) < iAverageShare then
			m = m.."\r\n\tYou are "..(ProperUnits( iAverageShare - Core.GetUserValue(user,16)) ).." below the Average share.. Try to get more stuff in your share and be above average !"
			local iShare = SafeDivide(Core.GetUserValue(user,16), 1024^3)
			if iShare >= 10 and iShare < 25 then m = m.."\r\n\tYou are VERY small !!!"
			elseif iShare >= 25 and iShare < 50 then m = m.."\r\n\tYou are small !!!"
			elseif iShare >= 50 and iShare < 100 then m = m.."\r\n\tI bet you can try harder to raise your sharesize !!!"
			elseif iShare >= 100 then m = m.."\r\n\tIt's not that your share ain't big, it's that there are bigger !!!"
			end
			m = m.."\r\n\tStill.. You are most welcome !  Enjoy your stay here and have a great download time !  ;) "
		end
		Core.SendToUser(user,"<"..AverageCfg.Bot.."> ".. m.."\r\n\r\n\t==================================================================================\n\n " )
	end
end

OpConnected = UserConnected


Regards,

CyCLoNe

CrazyGuy

Nice work and welcome to this board  :)

A small remark though about your OnStartup function.

if AverageCfg.Bot == "" then AverageCfg.Bot = SetMan.GetString(21) end
	if AverageCfg.Hub == "" then AverageCfg.Hub = SetMan.GetString(0) end
	if not Core then
		local hub = ""
		local app,ver = getHubVersion()
		if app and ver then hub = app.." "..ver.." " end
		OnError("This script is incompatible with "..hub.._VERSION)
	end


If "if not Core" would ever be true, the script would halt on the 2 lines above because if Core doesn't exist, neither does SetMan (attempt to index global 'SetMan', a nil value). The condition "if not Core" will therefor never produce the result you are looking for.

I'd suggest the following:
OnStartup = function()
	assert(_VERSION == "Lua 5.1","Incorrect Lua version.")
	if not Core then
		local hub = ""
		local app,ver = getHubVersion()
		if app and ver then hub = app.." "..ver.." " end
		OnError("This script is incompatible with "..hub.._VERSION)
	else
		AverageCfg.Scp = "Average Share"
		if AverageCfg.Bot == "" then AverageCfg.Bot = SetMan.GetString(21) end
		if AverageCfg.Hub == "" then AverageCfg.Hub = SetMan.GetString(0) end
		local Path = Core.GetPtokaXPath().."scripts/"
		if AverageCfg.Bot ~= SetMan.GetString(21) then
			Core.RegBot(AverageCfg.Bot, AverageCfg.BotDesc, AverageCfg.BotMail, AverageCfg.BotIsOp)
		end
	end
end

SMF spam blocked by CleanTalk