PtokaX forum

Archive => Archived 5.1 boards => Conversion Requests => Topic started by: TiMeTrAVelleR on 18 December, 2007, 11:39:44

Title: Convert Countdown bot to New API
Post by: TiMeTrAVelleR on 18 December, 2007, 11:39:44
Been trying to convert the countdown bot to new API but cant get count down fromlast hour to work could anione give me a hand here

this is what i did sofar


--[[

CountDown Bot 1.24 - LUA 5.0/5.1 version by jiten and TiMeTrAVelleR

Based on: Countdown bot by plop

Changelog:

Added: Customizable countdown;
Changed: Small stuff;
Added: RightClick (3/28/2006);
Added: Start and Stop Timer (3/30/2006);
Added: Customizable message (3/31/2006);
Changed: !setmsg now parses all message (3/31/2006);
Added: OnExit function (4/1/2006);
Rewritten: All julian functions to os.*;
Changed: Other small mods (4/9/2006);
Added: bSend to define Timer status - reported by TT (4/13/2006)
Changed: Sync() function - reported by TT (4/15/2006)
Fixed: Hopefully TimeLeft() function (4/17/2006)
Changed: !*t to *t;
Changed: Settings table (6/5/2006)

]]--

-- Bot Name
sBot = SetMan.GetString(21)
-- Timer DB
fTime = "tTime.tbl"
-- Send txt content to Main (true/false)
bSend = false
-- CountDown file to be shown
fCountDown = "happynewyear.txt"

-- After first setup, has to be changed with command
Settings = {
-- First Timer for this bot
iSetup = {
year = 2007,
month = 12,
day = 18,
hour = 10,
min = 00,
sec = 00
},
-- Custom message sent on timer
sMsg = "Test Run",
-- Timer Status
bRun = true
}

OnStartup = function()
if loadfile(fTime) then dofile(fTime) end

local tmp = TimeLeft(); if tmp then Core.SendToAll("<"..sBot.."> "..tmp) end;
end

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

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

OpConnected = UserConnected

OnTimer = function(tmr)
if os.difftime( os.time(Settings.iSetup), os.time(os.date("*t")) ) == 0 then
-- Send message and kill timer
SendAscii(); TmrMan.RemoveTimer(tmr); Settings.bRun = false
else
local tmp = TimeLeft(); if tmp then Core.SendToAll("<"..sBot.."> "..tmp) end; Sync()
end
end

OnExit = function()
local hFile = io.open(fTime,"w+") Serialize(Settings,"Settings",hFile); hFile:close()
end

tCommands = {
setmsg = {
tFunc = function(user,data)
local s,e,msg = string.find(data,"^%b<>%s+%S+%s+(.*)|$")
if msg then
Settings.sMsg = msg;
Core.SendToNick(user.sNick,"<"..sBot.."> *** CountDown Bot's message has been changed to: "..msg)
end
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1, [6] = 1,
},
tRC = "Set Message$<%[mynick]> !{} %[line:Message]"
},
starttimer = {
tFunc = function(user)
if not Settings.bRun then
Tmr1 = TmrMan.AddTimer(100*1000,"New_Year") Sync() Settings.bRun = true
Core.SendToNick(user.sNick,"<"..sBot.."> *** CountDown Bot's timer has been started!")
else
Core.SendToNick(user.sNick,"<"..sBot.."> *** CountDown Bot's timer is allready running!")
end
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1, [6] = 1,
},
tRC = "Start Timer$<%[mynick]> !{}"
},
stoptimer = {
tFunc = function(user)
TmrMan.RemoveTimer(tmr); Settings.bRun = false
Core.SendToNick(user.sNick,"<"..sBot.."> *** CountDown Bot's timer has been stopped!")
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1, [6] = 1,
},
tRC = "Stop Timer$<%[mynick]> !{}"
},
daysleft = {
tFunc = function(user)
local tmp = TimeLeft()
if tmp then Core.SendToNick(user.sNick,"<"..sBot.."> "..tmp) end
end,
tLevels = {
[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, [6] = 1,
},
tRC = "Time Left$<%[mynick]> !{}"
},
settimer = {
tFunc = function(user,data)
local s,e,args = string.find(data,"^%b<>%s+%S+%s+(.*)|$")
if args then
local s,e,d,m,y,H,M,S = string.find(args,"^(%d%d)\/(%d%d)\/(%d%d%d%d)%s(%d%d)%:(%d%d)%:(%d%d)$")
if d and m and y and H and M and S then
Settings.iSetup = {
hour = tonumber(H), min = tonumber(M), day = tonumber(d),
month = tonumber(m), year = tonumber(y), sec = tonumber(S)
}
Core.SendToNick(user.sNick,"<"..sBot.."> *** CountDown Bot has been successfully set to: "..args); OnExit()
else
Core.SendToNick(user.sNick,"<"..sBot.."> *** Syntax Error: Type !settimer dd/mm/yyyy hh:mm:ss")
end
else
Core.SendToNick(user.sNick,"<"..sBot.."> *** Syntax Error: Type !settimer dd/mm/yyyy hh:mm:ss")
end
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1, [6] = 1,
},
tRC = "Set Timer$<%[mynick]> !{} %[line:dd/mm/yyyy hh:mm:ss]"
}
}

TimeLeft = function()
local iDiff = os.difftime( os.time(Settings.iSetup), os.time(os.date("*t")) )
if iDiff > 0 then
local T = os.date("*t",iDiff)
return string.format(Settings.sMsg.. "%i Days, %i Hours, %i Minutes and %i Seconds",T.day-1, T.hour-1, T.min, T.sec)
end
end

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

Sync = function()
local iDiff = os.difftime( os.time(Settings.iSetup), os.time(os.date("*t")) )
if iDiff > 0 then
local T, iAdjust = os.date("*t",iDiff)
if T.day-1 ~= 0 then

else
local tTable = {
{ fAdjust = T.min*60 + T.sec, iTimer = 3600 },
{ fAdjust = math.fmod(T.min, 15)*60 + T.sec, iTimer = 900 },
{ fAdjust = math.fmod(T.min, 5)*60 + T.sec, iTimer = 300 },
{ fAdjust = math.fmod(T.min, 1)*60 + T.sec, iTimer = 60 },
{ fAdjust = math.fmod(T.sec, 15), iTimer = 15 },
{ fAdjust = math.fmod(T.sec, 5), iTimer = 5, iIndex = 10 },
{ fAdjust = math.fmod(T.sec, 1), iTimer = 1 },
}
for i in ipairs(tTable) do
if iDiff > (tTable[i].iIndex or tTable[i].iTimer) then
iAdjust = tTable[i].fAdjust;

end
end
end
end
end

Serialize = function(tTable,sTableName,hFile,sTab)
sTab = sTab or "";
hFile:write(sTab..sTableName.." = {\n");
for key,value in pairs(tTable) do
if (type(value) ~= "function") then
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
Serialize(value,sKey,hFile,sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
hFile:write(sTab.."\t"..sKey.." = "..sValue);
end
hFile:write(",\n");
end
end
hFile:write(sTab.."}");
end
RegConnected = UserConnected

Title: Re: Convert Countdown bot to New API
Post by: UwV on 18 December, 2007, 21:15:10
here you go..

------------------------------------------------------------------------------
-------                  Lua 5 version by jiten                        -------
-------                  countdown bot by plop                         -------
-------             original julian day made by tezlo                  -------
-------      modifyd by chilla 2 also handle hours, mins, seconds      -------
-------                  converted to new API by UwV                        --------
------------------------------------------------------------------------------


------------------------------------------------------------------------------
-------           THE TEXT FILE LOADS ON BOT START                     -------
------------------------------------------------------------------------------
------- this may sound weird but this 2 make sure it shows on time,    -------
-------      as i allready seen some big ascii's come by               -------
------------------------------------------------------------------------------




------------------------------------------------------------------------------
Bot = "Time-Left"
BotSay = "Merry X-Mas all"
Event = "Time left till X-Mas :"

--This the date the timer has 2 stop @ midnight
-- year (2 numbers), month, day
SylYear,SylMonth,SylDay = 07,12,24
-- this is the file 2 be shown
file = "txt/2512.txt"
------------------------------------------------------------------------------
------------------------------------------------------------------------------

iTimerID = nil
function OnTimer()
if last == 0 then
Core.SendToAll("<"..Bot.."> "..TimeLeft())
Sync()
elseif last == 1 then
SendAscii() -- send the msg
TmrMan.RemoveTimer(iTimerID) -- kill the timer
end
end
------------------------------------------------------------------------------
function jdatehms(d, m, y,ho,mi,se)
local a, b, c = 0, 0, 0
if m <= 2 then y = y - 1 m = m + 12 end
if (y*10000 + m*100 + d) >= 15821015 then a = math.floor(y/100) b = 2 - a + math.floor(a/4) end
if y <= 0 then c = 0.75 end
return math.floor(365.25*y - c) + math.floor(30.6001*(m+1) + d + 1720994 + b),ho*3600+mi*60+se
end
------------------------------------------------------------------------------
function TimeLeft()
line = Event
local curday,cursec = jdatehms(tonumber(os.date("%d")),tonumber(os.date("%m")),tonumber(os.date("%y")),tonumber(os.date("%H")),tonumber(os.date("%M")),tonumber(os.date("%S")))
local sylday,sylsec = jdatehms(SylDay,SylMonth,SylYear,24,0,0)
local tmp = sylsec-cursec
local hours, minutes,seconds = math.floor(math.fmod(tmp/3600, 60)), math.floor(math.fmod(tmp/60, 60)), math.floor(math.fmod(tmp/1, 60))
local day = sylday-curday
if day >= 0 then
if day ~= 0 then line = line.." "..day.." Day's" end
if hours ~= 0 then line = line.." "..hours.." Hours" end
if minutes ~= 0 then line = line.." "..minutes.." Minutes" end
if seconds ~= 0 then line = line.." "..seconds.." Seconds" end
line =line.." ???"
return line
end
end
------------------------------------------------------------------------------
function ShowAscii()
local f = io.open(file)
if f then
text = f:read("*all")
f:close()
return string.gsub( text, "\n", "\r\n" )
end
end
------------------------------------------------------------------------------
function SendAscii()
Core.SendToAll("<"..Bot.."> "..text)
Core.SendToAll("<"..Bot.."> "..BotSay)
end
------------------------------------------------------------------------------
function ChatArrival(user, data)
data=string.sub(data,1,-2)
s,e,cmd = string.find(data,"%b<>%s+(%S+)")
if cmd == "!xtimeleft" then
local tmp = TimeLeft()
if tonumber(tmp) == nil then Core.SendToUser(user, "<"..Bot.."> "..tmp) end
return true
end
end
------------------------------------------------------------------------------
function UserConnected(user)
local tmp = TimeLeft()
if tonumber(tmp) == nil then Core.SendToUser(user, "<"..Bot.."> "..tmp) end
end
------------------------------------------------------------------------------
RegConnected = UserConnected
OpConnected = UserConnected
------------------------------------------------------------------------------
function OnStartup()
iTimerID = TmrMan.AddTimer(100 * 1000)
local tmp = TimeLeft()
if tonumber(tmp) == nil then Core.SendToAll("<"..Bot.."> "..tmp)
end
Sync() ShowAscii() last = 0
end
------------------------------------------------------------------------------
function Sync()
local curday,cursec = jdatehms(tonumber(os.date("%d")),tonumber(os.date("%m")),tonumber(os.date("%y")),tonumber(os.date("%H")),tonumber(os.date("%M")),tonumber(os.date("%S")))
local sylday,sylsec = jdatehms(SylDay,SylMonth,SylYear,24,0,0)
local tmp = sylsec-cursec
local hours, minutes,seconds = math.floor(math.fmod(tmp/3600, 60)), math.floor(math.fmod(tmp/60, 60)), math.floor(math.fmod(tmp/1, 60))
local day = sylday-curday
if day ~= 0 then
adjust = (math.floor(math.fmod(minutes, 60))*60)+seconds
if adjust ~= 0 then TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(adjust * 1000) else TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(3600 * 1000) end
else
if tmp > 14000 then  --- every  4 hours a msg
adjust = (math.floor(math.fmod(minutes, 60))*60)+seconds
if adjust ~= 0 then TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(adjust * 1000) else TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(8400 * 1000) end
--else
elseif tmp > 3600 then  --- every 1 hours a msg
adjust = (math.floor(math.fmod(minutes, 60))*60)+seconds
if adjust ~= 0 then TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(adjust * 1000) else TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(3600 * 1000) end
elseif tmp > 900 then  -- every 15 mins a msg
adjust = (math.floor(math.fmod(minutes, 15))*60)+seconds
if adjust ~= 0 then TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(adjust * 1000) else TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(900 * 1000) end
elseif tmp > 300 then  -- every 5 mins a msg
adjust = (math.floor(math.fmod(minutes, 5))*60)+seconds
if adjust ~= 0 then TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(adjust * 1000) else TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(300 * 1000) end
elseif tmp > 60 then  -- every min a msg
adjust = (math.floor(math.fmod(minutes, 1))*60)+seconds
if adjust ~= 0 then TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(adjust * 1000) else TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(60 * 1000) end
elseif tmp > 15 then  -- every 15 secs a msg
adjust = math.floor(math.fmod(seconds, 15))
if adjust ~= 0 then TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(adjust * 1000) else TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(15 * 1000) end
elseif tmp > 10 then  -- every 10 secs a msg
adjust = math.floor(math.fmod(seconds, 10))
if adjust ~= 0 then TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(adjust * 1000) else TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(5 * 1000) end
elseif tmp > 1 then
TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(1 * 1000)
else
last = 1
TmrMan.RemoveTimer(iTimerID) iTimerID = TmrMan.AddTimer(1 * 1000)
end
end
end



its a older version i noticed just now ... but i guess you can mod that one wihtout a problem after reading this one anyway's :0)

** edit ..
Botsay = "Merry X-Mas all"
BotSay = "Merry X-Mas all"
Title: Re: Convert Countdown bot to New API
Post by: TiMeTrAVelleR on 18 December, 2007, 22:18:54
thanks basic working now :)
Title: Re: Convert Countdown bot to New API
Post by: UwV on 19 December, 2007, 11:46:30
cool i am going to give it a try now :0)
i really like the added hour for the timer go to off option ...
somebody wanna try make multiple date-time-asciifile possible i think ...