PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Finished Scripts => Topic started by: ATAG on 06 December, 2008, 20:53:53

Title: CountDown2 Lua5.1 API2
Post by: ATAG on 06 December, 2008, 20:53:53
--[[
CountDown2 Lua 5.1 API2 by ATAG
v0.1.1 @ 2008.12.06.

Based on CountDown Lua 5.1 by TiMeTrAVelleR
]]--

-- script path
sPath = Core.GetPtokaXPath().."scripts/"

Settings = {
-- Bot Name - leave empty ("") if you want to use the hub bot
sBot = "-Rock-",
-- nick to receive error messages
sOpNick = "[SU]ATAG",
-- First Timer for this bot (2 digits)
-- After first setup, has to be changed with command
iSetup = os.time(),
-- Start timer automatically (true/false)
bStart = true,
-- Timer DB
fTime = sPath.."tTime.tbl",
-- Send txt content to Main (true/false)
bSend = false,
-- Custom message sent on timer
sMsg = "Time Left ",
-- CountDown file to be shown
fCountDown = sPath.."happynewyear.txt",
}

OnStartup = function()
if Settings.sBot == "" then Settings.sBot = SetMan.GetString(21) end
if loadfile(Settings.fTime) then dofile(Settings.fTime) end
if Settings.bStart then
if Settings.iSetup > os.time() then
local iTimer = Sync()
if iTimer then
tmr = TmrMan.AddTimer(iTimer)
end
local tmp = TimeLeft()
Core.SendToAll("<"..Settings.sBot.."> "..tmp or "No timer set")
end
end
end

ChatArrival = function(user, data)
local s,e,cmd = data:find("^%b<>%s+[%!%+](%S+).*|$")
if cmd then
if tCommands[cmd:lower()] then
cmd = cmd:lower()
if tCommands[cmd].tLevels[user.iProfile] == 1 then
return tCommands[cmd].tFunc(user, data), true
else
return Core.SendToUser(user, "<"..Settings.sBot.."> *** Error: You are not allowed to use this command!"), true
end
end
end
end

OnExit = function()
local hFile = assert(io.open(Settings.fTime,"w+"))
hFile:write("Settings = {\n")
for i, v in pairs(Settings) do
hFile:write("\t"..i.." = "..(type(v) == "string" and "[["..v.."]]" or tostring(v))..",\n")
end
hFile:write("}")
hFile:flush()
hFile:close()
end

tCommands = {
setmsg = {
tFunc = function(user,data)
local s,e,msg = data:find("^%b<>%s+%S+%s+(.*)|$")
if msg then
Settings.sMsg = msg;
Core.SendToUser(user, "<"..Settings.sBot.."> *** CountDown Bot's message has been changed to: "..msg)
end
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1,
},
tRC = "Set Message$<%[myNI]> !{} %[line:Message]"
},
starttimer = {
tFunc = function(user)
local iTimer = Sync()
if iTimer then
tmr = TmrMan.AddTimer(iTimer)
Core.SendToUser(user, "<"..Settings.sBot.."> *** CountDown Bot's timer has been started!")
end
Core.SendToUser(user,"<"..Settings.sBot.."> ".. (TimeLeft() or "Timer is outdated"))
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1,
},
tRC = "Start Timer$<%[myNI]> !{}"
},
stopttimer = {
tFunc = function(user)
if tmr then TmrMan.RemoveTimer(tmr) end
Core.SendToUser(user, "<"..Settings.sBot.."> *** CountDown Bot's timer has been stopped!")
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1,
},
tRC = "Stop Timer$<%[myNI]> !{}"
},
daysleft = {
tFunc = function(user)
Core.SendToUser(user, "<"..Settings.sBot.."> ".. (TimeLeft() or "Timer is outdated"))
end,
tLevels = {
[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
},
tRC = "Time Left$<%[myNI]> !{}"
},
settimer = {
tFunc = function(user,data)
local _,_,args = data:find("^%b<>%s+%S+%s+(.*)|$")
local _,_,d,m,y,H,M,S = args:find("^(%d+)\/(%d+)\/(%d+)%s(%d+)%:(%d+)%:(%d+)$")
if d and m and y and H and M and S then
y=tonumber(y)
if y < 100 then
y = y<70 and 2000+y or 1900+y
end
Settings.iSetup = os.time({year=y,month=m,day=d,hour=H,min=M,sec=S})
Core.SendToUser(user, "<"..Settings.sBot.."> *** CountDown Bot has been successfully set to: "..args); OnExit()
local bRestart = false
if tmr then
bRestart = true
TmrMan.RemoveTimer(tmr)
tmr = nil
end

if Settings.bStart then
local iTimer = Sync()
if iTimer then
tmr = TmrMan.AddTimer(iTimer)
if bRestart then
Core.SendToUser(user, "<"..Settings.sBot.."> *** CountDown Bot's timer has been restarted!")
else
Core.SendToUser(user, "<"..Settings.sBot.."> *** CountDown Bot's timer has been started!")
end
else
Core.SendToUser(user, "<"..Settings.sBot.."> *** CountDown Bot's timer start failed: Timer is outdated!")
end
else
if bRestart then
Core.SendToUser(user, "<"..Settings.sBot.."> *** Old timer has been stopped. Don't forget to start the new timer: !starttimer")
else
Core.SendToUser(user, "<"..Settings.sBot.."> *** Don't forget to start the new timer: !starttimer")
end
end
else
Core.SendToUser(user, "<"..Settings.sBot.."> *** Syntax Error: Type !settimer dd/mm/yy hh:mm:ss")
end
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1,
},
tRC = "Set Timer$<%[myNI]> !{} %[line:dd/mm/yy hh:mm:ss]"
}
}

UserConnected = function(user)
for i,v in pairs(tCommands) do
local sRC = string.gsub(v.tRC,"{}",i)
Core.SendToUser(user, "$UserCommand 1 3 CountDown Bot\\"..sRC.."&#124;")
end
local tmp = TimeLeft()
if tmp then Core.SendToUser(user, "<"..Settings.sBot.."> "..tmp) end
end
RegConnected = UserConnected
OpConnected = UserConnected

OnTimer = function(iN)
TmrMan.RemoveTimer(iN)
tmr = nil
if last then
SendAscii()
else
local left = TimeLeft()
if left then Core.SendToAll("<"..Settings.sBot.."> "..left) end

local iTimer = Sync()
if iTimer then
tmr = TmrMan.AddTimer(iTimer)
end
end
end


TimeLeft = function()
local diff = os.difftime(Settings.iSetup, os.time())
if diff < 0 then return nil end

local  tDiff = os.date("*t", diff)
local line = Settings.sMsg

if tDiff.year > 1970 then line = line.." ".. tDiff.year-1970 .." Years" end
if tDiff.month > 1 then line = line.." ".. tDiff.month-1 .." Months" end
if tDiff.day > 1 then line = line.." ".. tDiff.day-1 .." Days" end
if tDiff.hour >= 1 then line = line.." ".. tDiff.hour-1 .." Hours" end
if tDiff.min >= 0 then line = line.." ".. tDiff.min .." Minutes" end
if tDiff.min >= 0 then line = line.." ".. tDiff.sec .." Seconds ;) " end

return line ~= Settings.sMsg and line or nil
end

function OnError(sErr)
Core.SendToNick(Settings.sOpNick,"<"..Settings.sBot.."> "..sErr)
end

SendAscii = function()
if Settings.bSend then
local text
local f = io.open(Settings.fCountDown)
if f then text = f:read("*all"); f:close(); return string.gsub( text, "\n", "\r\n" ) end
Core.SendToAll("<"..Settings.sBot.."> "..text)
end
Core.SendToAll("<"..Settings.sBot.."> ".."Oki Here We Go    ;)")
end

Sync = function()
last = false
local tmp, adjust = os.difftime(Settings.iSetup, os.time())
local t = {86400,3600,900,300,60,15,10,1} -- timered messsage in every x seconds

for i, v in ipairs(t) do
if tmp > v then
adjust = math.fmod(tmp,v)
return adjust ~= 0 and adjust * 1000 or v * 1000
end
end

if tmp == 1 then
last = true
return 1000
else
return nil
end
end