PtokaX forum

Archive => Archived 5.0 boards => Finished Scripts => Topic started by: Madman on 27 February, 2006, 00:53:48

Title: Custom ShareLimter
Post by: Madman on 27 February, 2006, 00:53:48

-- Custom ShareLimter
-- Made by Madman
-- Based on a requst by Northwind and Pablo.zz

Set = { -- Profiles allowed to set sharelimit
[0] = 1,
[1] = 0,
}

function Main()
-- Check For file
local file = io.open("CustomShares.dat")
if file then
file:close()
else
local file = io.open("CustomShares.dat", "w+")
file:write("CustomShare = {}")
file:close()
end
end

function MyINFOArrival(curUser, data)
LoadFromFile("CustomShares.dat") -- Load table
if CustomShare[curUser.sName] then -- If user in table
if ConvShare(curUser.iShareSize, "B") < (ConvShare(CustomShare[curUser.sName]["Share"], CustomShare[curUser.sName]["Unit"])) then
-- Compar and if share less then in table, disconnect
curUser:SendData(frmHub:GetHubBotName(), "Your customshare is " ..CustomShare[curUser.sName]["Share"].. " " ..CustomShare[curUser.sName]["Unit"].. ", please get more share")
curUser:Disconnect()
end
end
CustomShare = nil -- "Unload table"
end

function ChatArrival(curUser, data)
local data = string.sub(data, 1, -2)
local s,e,cmd = string.find(data, "%b<>%s+%p(%S+)")
if cmd and Set[curUser.iProfile] == 1 then
local tCmds = {
["addsharelimit"] = function(curUser, data)
local s,e,Nick,Share,Unit = string.find(data, "%b<>%s+%S+%s+(%S+)%s+(%d+)%s+(%S+)")
if Nick and Share and Unit then
LoadFromFile("CustomShares.dat")
if CustomShare[Nick] == nil then
CustomShare[Nick] = {}
CustomShare[Nick]["Share"] = {}
CustomShare[Nick]["Unit"] = {}
CustomShare[Nick]["Share"] = tonumber(Share)
CustomShare[Nick]["Unit"] = string.upper(Unit)
else
CustomShare[Nick]["Share"] = tonumber(Share)
CustomShare[Nick]["Unit"] = string.upper(Unit)
end
curUser:SendData(frmHub:GetHubBotName(), Nick.." added with the share " ..Share.." "..string.upper(Unit))
SaveToFile("CustomShares.dat", CustomShare, "CustomShare")
CustomShare = nil
else
curUser:SendData(frmHub:GetHubBotName(), "Syntax: !" ..cmd.. " <Nick> <Share> <Unit>")
end
return 1
end,
["delsharelimit"] = function(curUser, data)
local s,e,Nick = string.find(data, "%b<>%s+%S+%s+(%S+)")
if Nick then
LoadFromFile("CustomShares.dat")
if CustomShare[Nick] == nil then
curUser:SendData(frmHub:GetHubBotName(), Nick.. " not in table")
else
CustomShare[Nick] = nil
SaveToFile("CustomShares.dat", CustomShare, "CustomShare")
curUser:SendData(frmHub:GetHubBotName(), Nick.. " removed from table")
end
CustomShare = nil
else
curUser:SendData(frmHub:GetHubBotName(), "Syntax: !" ..cmd.. " <Nick>")
end
return 1
end,
["viewsharelimit"] = function(curUser, data)
LoadFromFile("CustomShares.dat")
Msg = "\r\nCustom Shares\r\n"
for Nick,_ in CustomShare do
Msg = Msg.."Nick: " ..Nick
for That,Set in CustomShare[Nick] do
Msg = Msg.." " ..That.. ": " ..Set
end
Msg = Msg.."\r\n"
end
curUser:SendData(frmHub:GetHubBotName(), Msg) CustomShare = nil
return 1
end,
}
if tCmds[cmd] then
return tCmds[cmd](curUser, data)
end
end
end

-- Created by Corayzon
function ConvShare(iShareSize, Type) -- Convert Share Size...
while (iShareSize >= 1024) and (Type ~= "PB") do
if Type == "B" then
Type = "KB"
elseif Type == "KB" then
Type = "MB"
elseif Type == "MB" then
Type = "GB"
elseif Type == "GB" then
Type = "TB"
elseif Type == "TB" then
Type = "PB"
end
iShareSize = iShareSize/1024
end
return string.format("%.0f", iShareSize).." "..Type
end

-- Serialize by nErBoS
function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end

function SaveToFile(file , table , tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end

function LoadFromFile(filename)
local f = io.open(filename)
if f then
local r = f:read("*a")
f:flush()
f:close()
local func,err = loadstring(r)
if func then x,err = pcall(func) end
end
end