PtokaX forum

Archive => Archived 5.1 boards => Help with scripts => Topic started by: Syphrone-NL on 04 August, 2007, 19:15:06

Title: Waittime
Post by: Syphrone-NL on 04 August, 2007, 19:15:06
I have this script:


-- ChatStats v3 Made By Optimus
-- Based on Tezlo chats
-- Added Send Commands By TiMeTrAVelleR
-- Madman fixed some in commands
-- Converted to lua5 by Madman with very little help by Jelf
-- with some help by ?
-- fixed stats saving on exit by jiten
---- Modded by Madman
-- Added so it's creates ChatStatsFile, if it's missing
-- Fixed so it dont counts commands
-- Added a IgnoreTable, users in that wont be counted
---- Madman Return's
-- Changed: New ChatArrival
-- Changed: Some Commands
-- Changed: Some small code...
-- Changed: Modded 4 my hub
---- Madman strikes again
-- Added: a lowerchatter cmd, use it to lower someones chatstats post
-- Changed to lua 5.1 by TT
-- Added: chatter of Month, Madman, requested by BrotherBear
-- Added: not case senastive, all nicks will be loged in lower case, Madman, requested by Yahoo
-- Madman does it again
-- Added: Total (char+words+post), request by -SkA-
-- Adedd: Option for Total
-- Changed: Cleaned up code, chatstats,chatmonth and topchat now use same function
-- Added: Sorting by Total, request by -SkA-
-- Changed: Shortstats Option
---- Updated again, by Madman
-- Added: UseMonthStats, so you don't have to use the month stats
-- Added: Profile ranking code..., request by Syphrone-NL
-- Changed: New ranking code
-- Added: Kill command, request by Syphrone-NL
-- Fixed: bugs in kill command

sBot = frmHub:GetHubBotName() -- Name of Bot
SendComm = 1 -- Send UserCommands, 1 = On  0 = Off
pMenu = "? CrimeLife Stats ?" -- Name of Menu
xMenu = "CrimeLife Game Menu"   -- Name of the OP Menu
UseMonthStats = 0 -- 1 = On / 0 = Off
UseTotal = 0 -- Add Total (char+words+post) in the chatstats, 1 = On  0 = Off
Sortstats = 2 -- 2=posts / 3 = chars / 4 = words / 5 = Total
WaitTime = 86400

Chatstats = {}
ChatstatsMonth = {}

ChatStatsFile = "chatstats.tbl"

IgnoreTable = {
-- 0=dont ignore/1=ignore
["-=FakeKiller=-"] = 1,
}

EnableChatStats = {
[0] = 0, --Operator
[1] = 1, --Game-Owner
[2] = 0, --Game Admin
[3] = 1, --Scum
[4] = 0, --Game Mod
[5] = 1, --Pee Wee
[6] = 1, --Thug
[7] = 1, --Gangster
[8] = 1, --Hitman
[9] = 1, --Assassin
[10] = 1, --Boss
[11] = 1, --Godfather
[12] = 1, --Legendary Godfather
[13] = 1, --Don
[14] = 1, --Respectable Don
[15] = 1, --Legendary Don
[-1] = 1,       -- Users (UnRegged)
}

AllowedProfiles = {
[0] = 0, --Operator
[1] = 1, --Game-Owner
[2] = 1, --Game Admin
[3] = 0, --Scum
[4] = 1, --Game Mod
}

rankProfiles = {
-- Posts = Rank to get
["0"] = "3",
["10"] = "5",
["25"] = "6",
["50"] = "7",
["100"] = "8",
["200"] = "9",
["350"] = "10",
["500"] = "11",
["750"] = "12",
["1000"] = "13",
["1500"] = "14",
["2500"] = "15",
}

---------- Warning! Do Not Edit! -----------
ChatStatsFileMonth = "chatstats - " ..os.date("%y-%m").. ".tbl"
---------- Warning! Do Not Edit! -----------
Players = {}

function Main()
frmHub:RegBot(sBot)
CheckFile(ChatStatsFile)
dofile(ChatStatsFile)
if UseMonthStats == 1 then
CheckFile(ChatStatsFileMonth)
dofile(ChatStatsFileMonth)
end
end

function CheckFile(File)
local file = io.open(File, "r")
if file then
file:close()
else
local file = io.open(File, "w+")
file:write()
file:close()
end
end

function NewUserConnected(user)
if SendComm == 1 and EnableChatStats[user.iProfile] == 1 then
if Chatstats[string.lower(user.sName)] then
user:SendData(sBot, "---===[ Your CrimeLife Stats:  You Made "..Chatstats[string.lower(user.sName)]["post"].." Crimes, You Made ?"..Chatstats[string.lower(user.sName)]["chars"].." Money On The Crimes, And You Stole "..Chatstats[string.lower(user.sName)]["words"].." Vehicles ]===---")
end
user:SendData("$UserCommand 1 3 "..pMenu.."\\CrimeLife Stats Off All Users$<%[mynick]> !chatstats&#124;")
user:SendData("$UserCommand 1 3 "..pMenu.."\\Try To Kill This User$<%[mynick]> !kill %[nick]&#124;")
if UseMonthStats == 1 then
user:SendData("$UserCommand 1 3 "..pMenu.."\\Chat stats Year-Month$<%[mynick]> !chatmonth %[line:YY-MM]&#124;")
end
user:SendData("$UserCommand 1 3 "..pMenu.."\\My CrimeLife Stats$<%[mynick]> !mystats&#124;")
if AllowedProfiles[user.iProfile] == 1 then
user:SendData("$UserCommand 1 3 "..pMenu.."\\"..xMenu.."\\Del CrimeLife User$<%[mynick]> !delchatter %[line:Nick]&#124;")
user:SendData("$UserCommand 1 3 "..pMenu.."\\"..xMenu.."\\Lower CrimeLife User$<%[mynick]> !lowerchatter %[line:Nick] %[line:New posts]&#124;")
user:SendData("$UserCommand 1 3 "..pMenu.."\\"..xMenu.."\\Clear CrimeLife Stats$<%[mynick]> !clearstats&#124;")
end
if user.bOperator then
user:SendData("$UserCommand 1 3 "..pMenu.."\\"..xMenu.."\\Top CrimeLife Users In Main$<%[mynick]> !topchat&#124;")
end
end
end

OpConnected = NewUserConnected

function OnExit()
if isEmpty(Chatstats) then
else
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
if UseMonthStats == 1 then
saveTableToFile(ChatStatsFileMonth, ChatstatsMonth, "ChatstatsMonth")
end
end
end

function IsCmd(str)
return string.sub(str, 1, 1) == "!" or string.sub(str, 1, 1) == "?" or string.sub(str, 1, 1) == "+" or string.sub(str, 1, 1) == "$"
end

function ChatArrival(user, data)
if EnableChatStats[user.iProfile] == 1 then
local s,e,cmd = string.find(data,"%b<>%s+(%S+)")
if IsCmd(cmd) then
elseif IgnoreTable[string.lower(user.sName)] == 1 then
else
local s,e,str = string.find(data, "%b<>%s+(.*)%|")
updStats(string.lower(user.sName), str)
end
end
local data = string.sub(data,1, -2)
local s,e,cmd = string.find(data, "%b<>%s+[%!%+%?](%S+)")
if cmd then
cmd = string.lower(cmd)
local tCmds = {
["kill"] = function(user,data)
local s,e,nick = string.find(data,"%b<>%s+%S+%s+(%S+)")
Z,u,Hit = nil,nil,0
if nick then
Zombie = GetItemByName(nick)
if Zombie then
if user.iProfile >= Zombie.iProfile then
for posts,prof in pairs(rankProfiles) do
prof = tonumber(prof)
if prof == Zombie.iProfile then
Z = 1
end
if prof == user.iProfile then
u = 1
end
end
Players[user.sName]=start
local start = os.clock()
if Players[user.sName] then
local interval = Players[user.sName]
if os.difftime(os.clock(), interval) < WaitTime then
local reply = "Take it easy. You must wait "..WaitTime.." seconds between kills."
user:SendData(sBot,reply)
return 1
end
end
if Z and u then
math.randomseed(os.clock())
Hit = math.random(0,10)
SendToAll(Hit)
if Hit >= 5 then
Posts = math.random(1,20)
local tmp = Chatstats[string.lower(Zombie.sName)]
tmp["post"] = tmp["post"] - Posts
for posts,prof in pairs(rankProfiles) do
prof = tonumber(prof)
if Zombie.iProfile == prof then
-- Do Nothing atm
else
ChangeRegUser(Zombie.sName,frmHub:GetUserPassword(Zombie.sName),prof)
Zombie:SendPM(sBot,"You have be downgraded to " ..GetProfileName(prof).. ". Please reconnect")
end
break
end
Chatstats[string.lower(Zombie.sName)] = tmp
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
SendToAll(sBot,Zombie.sName.. " got killed by " ..user.sName.. ", and lost " ..Posts)
else
SendToAll(sBot,user.sName.. " tried to kill " .. Zombie.sName.. " but failed")
end
else
user:SendData(sBot,"Sorry, you can't play")
end
else
user:SendData(sBot, nick.. " has a higher rank you cant kill him")
end
else
user:SendData(sBot,nick.. " is not online")
end
else
user:SendData(sBot,"Syntax: !" ..cmd.." <nick>")
end
return 1
end,
["mystats"] = function(user, data)
if Chatstats[string.lower(user.sName)] then
user:SendData(sBot, "---===[ Your CrimeLife Stats:  You Made "..Chatstats[string.lower(user.sName)]["post"].." Crimes, You Made ?"..Chatstats[string.lower(user.sName)]["chars"].." Money On The Crimes, And You Stole "..Chatstats[string.lower(user.sName)]["words"].." Vehicles ]===---")
else
user:SendData(sBot, "*** No CrimeLife Statics Found!")
end
return 1
end,
["chatstats"] = function(user, data)
Topic = "Current Top CrimeLife Users"
Msg = Stats(Topic)
user:SendPM(sBot,Msg)
return 1
end,
["chatmonth"] = function(user,data)
if UseMonthStats == 1 then
local _,_,Y,M = string.find(data,"%b<>%s+%S+%s+(%d%d)%-(%d%d)")
if Y and M then
Topic = "Current Top Chatters of Month"
Msg = Stats(Topic,Y,M)
user:SendPM(sBot,Msg)
else
user:SendData(sBot, "Syntax: !chatmonth YY-MM i.e 07-02 for feb, 07")
end
else
user:SendData(sBot,"This function is disabled")
end
return 1
end,
["topchat"] = function(user, data)
if user.bOperator then
Topic = "Current Top CrimeLife Users"
Msg = Stats(Topic)
SendToAll(sBot,Msg)
return 1
end
end,
["lowerchatter"] = function(user, data)
if AllowedProfiles[user.iProfile] == 1 then
local s,e,name,chat = string.find(data, "%b<>%s+%S+%s+(%S+)%s+(%d+)")
if name and chat then
if Chatstats[name] then
chat = tonumber(chat)
if Chatstats[name]["post"] <= chat then
user:SendData(sBot, "*** You can not raise the stats!")
else
local OldChat = Chatstats[name]["post"]
Chatstats[name]["post"] = chat
SendToOps(sBot, "*** Chatstats posts for " ..name.. " has been lowered to " ..chat.. " from " ..OldChat.. " by " ..string.lower(user.sName))
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
else
user:SendData(sBot, "*** " ..name.. " is not in CrimeLife stats")
end
else
user:SendData(sBot, "*** Usage: !lowerchatter <name> <new posts>")
end
return 1
end
end,
["delchatter"] = function(user, data)
if AllowedProfiles[user.iProfile] == 1 then
local s,e,name = string.find(data, "%b<>%s+%S+%s+(%S+)" )
if name then
if Chatstats[name] then
Chatstats[name] = nil
SendToOps(sBot, "CrimeLife stats from user "..name.." are now removed!")
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
else
user:SendData(sBot, "*** CrimeLife stats from user "..name.." not found!")
end
else
user:SendData(sBot, "*** Usage: !delchatter <name>")
end
return 1
end
end,
["clearstats"] = function(user, data)
if AllowedProfiles[user.iProfile] == 1 then
Chatstats = {}
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
SendToAll(sBot, "CrimeLife stats are cleared by "..user.sName)
return 1
end
end,
}
if tCmds[cmd] then
return tCmds[cmd](user, data)
end
end
end

function Stats(Topic,Y,M)
if Chatstats then
if Y and M then
if loadfile("chatstats - " ..Y.. "-" ..M.. ".tbl") then
loadTableFromFile("chatstats - " ..Y.. "-" ..M.. ".tbl")
Table = ChatstatsMonth
else
return "That month has not been loged"
end
else
Table = Chatstats
end
TCopy={}
for i,v in pairs(Table) do
table.insert(TCopy,{i,v.post,v.chars,v.words,v.tot})
end
table.sort(TCopy,function(a,b) return (a[Sortstats] > b[Sortstats]) end)
local chat = Topic..":\r\n\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
if UseTotal == 1 then
chat = chat.."\t Nr.\tPosts:\tChars:\tWords:\tTotal:\tName:\r\n"
else
chat = chat.."\t Nr.\tCrimes:\tMoney:\tVehicles:\tName:\r\n"
end
chat = chat.."\t ------------------------------------------------------------------------\r\n"
for i = 1,25 do
if TCopy[i] then
if UseTotal == 1 then
chat = chat.."\t "..i..".\t "..TCopy[i][2].."\t "..TCopy[i][3].."\t "..TCopy[i][4].."\t"..TCopy[i][5].."\t"..TCopy[i][1].."\r\n"
else
chat = chat.."\t "..i..".\t "..TCopy[i][2].."\t ?"..TCopy[i][3].."\t "..TCopy[i][4].."\t"..TCopy[i][1].."\r\n"
end
end
end
return chat
end
TCopy={}
end

function updStats(nick, str)
local tmp = Chatstats[nick] or {["post"]=0, ["chars"]=0, ["words"]=0, ["time"]=os.date("%x"),["tot"]=0,}
tmp["post"], tmp["chars"], tmp["words"], tmp["time"], tmp["tot"] = tmp["post"]+1, tmp["chars"]+string.len(str), tmp["words"]+cntargs(str,"(%a+)"), os.date("%x"), tmp["tot"]+string.len(str)+cntargs(str,"(%a+)")+1
-- Ranking code
for posts,prof in pairs(rankProfiles) do
if tmp["post"] == tonumber(posts) then
user = GetItemByName(nick)
prof = tonumber(prof)
if user.iProfile == prof then
-- Do Nothing atm
else
if user.iProfile == -1 then
AddRegUser(user.sName,"temp",prof)
user:SendPM(sBot,"You have be regged as " ..GetProfileName(prof).. " with the pass temp, reconnect and then user !passwd <new password> to change it, then reconnect again, and login with the pass of you choise")
else
ChangeRegUser(user.sName,frmHub:GetUserPassword(user.sName),prof)
user:SendPM(sBot,"You have be upgraded to " ..GetProfileName(prof).. ". Please reconnect")
end
end
break
end
end
-- Ranking code
Chatstats[nick] = tmp
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
-- Update Monthfile
if UseMonthStats == 1 then
local tmpM = ChatstatsMonth[nick] or {["post"]=0, ["chars"]=0, ["words"]=0, ["time"]=os.date("%x"),["tot"]=0}
tmpM["post"], tmpM["chars"], tmpM["words"], tmpM["time"], tmpM["tot"] = tmpM["post"]+1, tmpM["chars"]+string.len(str), tmpM["words"]+cntargs(str,"(%a+)"), os.date("%x"), tmpM["tot"]+string.len(str)+cntargs(str,"(%a+)")+1
ChatstatsMonth[nick] = tmpM
saveTableToFile(ChatStatsFileMonth,ChatstatsMonth,"ChatstatsMonth")
end
end

function cntargs(str, rule)
local s,n = string.gsub(str, rule, "")
return n
end

----------------------------------------------
-- load & save Tables
----------------------------------------------
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 pairs(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 saveTableToFile(file, table, tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end
-----------------------------------------------------------
function loadTableFromFile(file)
local f = io.open(file)
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

-------------table checker by herodes
--- for an associative table, like ["smth"] = "smth else",
function isEmpty(t)
for i,v in pairs(t) do
return false;
end
return true;
end;


I added myself:

Players[user.sName]=start
local start = os.clock()
if Players[user.sName] then
local interval = Players[user.sName]
if os.difftime(os.clock(), interval) < WaitTime then
local reply = "Take it easy. You must wait "..WaitTime.." seconds between kills."
user:SendData(sBot,reply)
return 1
end
end


And

WaitTime = 86400


And

Player = {}


But the waittime doesnt work. The script works good and when i kill someone i gives no errors but then i can kill someone again right after it. It gives no waittime. And no errors in the script then.
Whats the problem?
Can somebody help me to fix the waittime?
Thx in advanced
Title: Re: Waittime
Post by: Syphrone-NL on 05 August, 2007, 13:11:04
Thx it worked :D

But can you tell me what i did wrong?