PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Conversion Requests => Topic started by: Highlander Back on 03 December, 2008, 17:00:13

Title: Countdown
Post by: Highlander Back on 03 December, 2008, 17:00:13
Its been a while sind i was on this board  and runned a hub  most scripts i found wat i would like to have if possible one of my fav scripts that i cant find here converted to new api

--[[

------------------------------------------------------------------------------
------- Lua 5 version by jiten   Lua 5.1 version by TiMeTrAVelleR      -------
-------                  countdown bot by plop                         -------
-------             original julian day made by tezlo                  -------
-------      modifyd by chilla 2 also handle hours, mins, seconds      -------
------------------------------------------------------------------------------

------------------------------------------------------------------------------
-------           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               -------
------------------------------------------------------------------------------

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)

]]--

Settings = {
-- Bot Name
sBot = "-Rock-",
-- First Timer for this bot (2 digits)
-- After first setup, has to be changed with command
iSetup = {
iYear = 06,
iMonth = 3,
iDay = 28,
iHour = 21,
iMinute = 00,
iSecond = 00,
},
-- Start timer automatically (true/false)
bStart = false,
-- Timer DB
fTime = "tTime.tbl",
-- Send txt content to Main (true/false)
bSend = false,
-- Custom message sent on timer
sMsg = "Time Til NewYear ",
-- CountDown file to be shown
fCountDown = "happynewyear.txt",
}

Main = function()
if loadfile(Settings.fTime) then dofile(Settings.fTime) end
SetTimer(100 * 1000);
if Settings.bStart then
StartTimer(); Sync()
local tmp = TimeLeft()
if tmp then SendToAll(Settings.sBot,tmp) end;
end
end

ChatArrival = function(user, data)
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 user:SendData(Settings.sBot,"*** Error: You are not allowed to use this command!"), 1
end
end
end
end

OnExit = function()
local hFile = io.open(Settings.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;
user:SendData(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$<%[mynick]> !{} %[line:Message]"
},
starttimer = {
tFunc = function(user)
StartTimer(); Sync();
user:SendData(Settings.sBot,"*** CountDown Bot's timer has been started!")
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1,
},
tRC = "Start Timer$<%[mynick]> !{}"
},
stopttimer = {
tFunc = function(user)
StopTimer()
user:SendData(Settings.sBot,"*** CountDown Bot's timer has been started!")
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1,
},
tRC = "Stop Timer$<%[mynick]> !{}"
},
daysleft = {
tFunc = function(user)
local tmp = TimeLeft()
if tmp then user:SendData(Settings.sBot,tmp) end
end,
tLevels = {
[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
},
tRC = "Time Left$<%[mynick]> !{}"
},
settimer = {
tFunc = function(user,data)
local s,e,args = string.find(data,"^%b<>%s+%S+%s+(.*)|$")
local s,e,d,m,y,H,M,S = string.find(args,"^(%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 = {
iDay = tonumber(d), iMonth = tonumber(m), iYear = tonumber(y),
iHour = tonumber(H), iMinute = tonumber(M), iSecond = tonumber(S),
}
user:SendData(Settings.sBot,"*** CountDown Bot has been successfully set to: "..args); OnExit()
else
user:SendData(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$<%[mynick]> !{} %[line:dd/mm/yy hh:mm:ss]"
}
}

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

OpConnected = NewUserConnected

OnTimer = function()
if last then
-- Send message and kill timer
SendAscii(); StopTimer()
else
SendToAll(Settings.sBot, TimeLeft()); Sync()
end
end

jdatehms = function(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

TimeLeft = function()
local tmp = Settings.iSetup
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 iday,isec = jdatehms(tmp.iDay,tmp.iMonth,tmp.iYear,tmp.iHour,tmp.iMinute,tmp.iSecond)
local tmp = isec-cursec
local hours, minutes,seconds = math.floor(math.mod(tmp/3600, 60)), math.floor(math.mod(tmp/60, 60)), math.floor(math.mod(tmp/1, 60))
local day = iday-curday
if day >= 0 then
local line = Settings.sMsg
if day ~= 0 then line = line.." "..day.." Days" 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
return line
end
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
SendToAll(Settings.sBot,text)
end
SendToAll(Settings.sBot, "Oki Here We Go    ;)")
end

Sync = function()
local tmp = Settings.iSetup
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 iday,isec = jdatehms(tmp.iDay,tmp.iMonth,tmp.iYear,tmp.iHour,tmp.iMinute,tmp.iSecond)
local tmp = isec-cursec
local hours, minutes,seconds = math.floor(math.mod(tmp/3600, 60)), math.floor(math.mod(tmp/60, 60)), math.floor(math.mod(tmp/1, 60))
local day = iday-curday
if day ~= 0 then
adjust = (math.floor(math.mod(minutes, 60))*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(3600 * 1000) end
else
if tmp > 3600 then  --- every hours a msg
adjust = (math.floor(math.mod(minutes, 60))*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(3600 * 1000) end
elseif tmp > 900 then  -- every 15 mins a msg
adjust = (math.floor(math.mod(minutes, 15))*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(900 * 1000) end
elseif tmp > 300 then  -- every 5 mins a msg
adjust = (math.floor(math.mod(minutes, 5))*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(300 * 1000) end
elseif tmp > 60 then  -- every min a msg
adjust = (math.floor(math.mod(minutes, 1))*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(60 * 1000) end
elseif tmp > 15 then  -- every 15 secs a msg
adjust = math.floor(math.mod(seconds, 15))
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(15 * 1000) end
elseif tmp > 10 then  -- every 10 secs a msg
adjust = math.floor(math.mod(seconds, 10))
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(5 * 1000) end
elseif tmp > 1 then
SetTimer(1 * 1000)
else
last = true
SetTimer(1 * 1000)
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


I hope sombody can help me here

Title: Re: Countdown
Post by: Highlander Back on 05 December, 2008, 19:09:37
Nobody that could help out here Please
Title: Re: Countdown
Post by: ATAG on 05 December, 2008, 22:12:08
Pls test it and report any problems :)

--[[

------------------------------------------------------------------------------
------- Lua 5 version by jiten   Lua 5.1 version by TiMeTrAVelleR      -------
-------                  countdown bot by plop                         -------
-------             original julian day made by tezlo                  -------
-------      modifyd by chilla 2 also handle hours, mins, seconds      -------
------------------------------------------------------------------------------

------------------------------------------------------------------------------
-------           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               -------
------------------------------------------------------------------------------

Changelog:
Upgrade to API2 (12/4/2008)
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)

]]--

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

Settings = {
-- Bot Name - leave empty ("") if you want to use the hub bot
sBot = "-Rock-",
-- First Timer for this bot (2 digits)
-- After first setup, has to be changed with command
iSetup = {
iYear = 06,
iMonth = 3,
iDay = 28,
iHour = 21,
iMinute = 00,
iSecond = 00,
},
-- Start timer automatically (true/false)
bStart = false,
-- 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
SetTimer(100*1000)
if Settings.bStart then
StartTimer(); Sync()
local tmp = TimeLeft()
if tmp then Core.SendToAll("<"..Settings.sBot.."> "..tmp) end;
end
end

SetTimer = function(iInt)
iTimer = iInt
end

StartTimer = function()
tmr = TmrMan.AddTimer(100*1000)
end

StopTimer = function()
if type(tmr) == "number" then
TmrMan.RemoveTimer(tmr)
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+")) Serialize(Settings, "Settings", hFile) 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)
StartTimer(); Sync();
Core.SendToUser(user, "<"..Settings.sBot.."> *** CountDown Bot's timer has been started!")
end,
tLevels = {
[0] = 1, [1] = 1, [4] = 1, [5] = 1,
},
tRC = "Start Timer$<%[myNI]> !{}"
},
stopttimer = {
tFunc = function(user)
StopTimer()
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)
local tmp = TimeLeft()
if tmp then Core.SendToUser(user, "<"..Settings.sBot.."> "..tmp) end
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 s,e,args = data:find("^%b<>%s+%S+%s+(.*)|$")
local s,e,d,m,y,H,M,S = string.find(args,"^(%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 = {
iDay = tonumber(d), iMonth = tonumber(m), iYear = tonumber(y),
iHour = tonumber(H), iMinute = tonumber(M), iSecond = tonumber(S),
}
Core.SendToUser(user, "<"..Settings.sBot.."> *** CountDown Bot has been successfully set to: "..args); OnExit()
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()
if last then
-- Send message and kill timer
SendAscii(); StopTimer()
else
Core.SendToAll("<"..Settings.sBot.."> "..TimeLeft()); Sync()
end
end

jdatehms = function(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

TimeLeft = function()
local tmp = Settings.iSetup
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 iday,isec = jdatehms(tmp.iDay,tmp.iMonth,tmp.iYear,tmp.iHour,tmp.iMinute,tmp.iSecond)
local tmp = isec-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 = iday-curday
if day >= 0 then
local line = Settings.sMsg
if day > 0 then line = line.." "..day.." Days" 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
return line ~= Settings.sMsg and line or "There's no currently running countdown"
end
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()
local tmp = Settings.iSetup
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 iday,isec = jdatehms(tmp.iDay,tmp.iMonth,tmp.iYear,tmp.iHour,tmp.iMinute,tmp.iSecond)
local tmp = isec-cursec
local hours, minutes,seconds = math.fmod(tmp/3600, 60), math.fmod(tmp/60, 60), math.fmod(tmp/1, 60)
local day = iday-curday
if day ~= 0 then
adjust = (math.fmod(minutes, 60)*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(3600 * 1000) end
else
if tmp > 3600 then  --- every hours a msg
adjust = (math.fmod(minutes, 60)*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(3600 * 1000) end
elseif tmp > 900 then  -- every 15 mins a msg
adjust = (math.fmod(minutes, 15)*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(900 * 1000) end
elseif tmp > 300 then  -- every 5 mins a msg
adjust = (math.fmod(minutes, 5)*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(300 * 1000) end
elseif tmp > 60 then  -- every min a msg
adjust = (minutes*60)+seconds
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(60 * 1000) end
elseif tmp > 15 then  -- every 15 secs a msg
adjust = math.fmod(seconds, 15)
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(15 * 1000) end
elseif tmp > 10 then  -- every 10 secs a msg
adjust = math.fmod(seconds, 10)
if adjust ~= 0 then SetTimer(adjust * 1000) else SetTimer(5 * 1000) end
elseif tmp > 1 then
SetTimer(1 * 1000)
else
last = true
SetTimer(1 * 1000)
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 "[["..value.."]]" or tostring(value)
hFile:write(sTab.."\t"..sKey.." = "..sValue);
end
hFile:write(",\n");
end
end
hFile:write(sTab.."}");
end
Title: Re: Countdown
Post by: Highlander Back on 05 December, 2008, 22:38:26
Wildo  thank you :)
Title: Re: Countdown
Post by: Highlander Back on 05 December, 2008, 22:56:04
 Time Til NewYear  26 Days

script only gives remaining days not hours min and sec

for rest al commands work :)

Title: Re: Countdown
Post by: Highlander Back on 06 December, 2008, 06:46:58
thats a nice script Mutor but not what i am looking for i like the option from countdown much to set message and timer tru commands i used that almost every week 
Title: Re: Countdown
Post by: ATAG on 06 December, 2008, 11:31:00
OK, script is really broken.. i'm going to write a new one :)
Title: Re: Countdown
Post by: ATAG on 06 December, 2008, 13:05:02
New version, pls update and try again :)

Quote[12:58:14] <-Rock-> *** CountDown Bot has been successfully set to: 6/12/08 12:59:00
[12:58:17] <-Rock-> *** CountDown Bot's timer has been started!
[12:58:30] <-Rock-> Time Left  0 Hours 0 Minutes 30 Seconds ;)
[12:58:45] <-Rock-> Time Left  0 Hours 0 Minutes 15 Seconds ;)
[12:58:50] <-Rock-> Time Left  0 Hours 0 Minutes 10 Seconds ;)
[12:58:51] <-Rock-> Time Left  0 Hours 0 Minutes 9 Seconds ;)
[12:58:52] <-Rock-> Time Left  0 Hours 0 Minutes 8 Seconds ;)
[12:58:54] <-Rock-> Time Left  0 Hours 0 Minutes 7 Seconds ;)
[12:58:54] <-Rock-> Time Left  0 Hours 0 Minutes 6 Seconds ;)
[12:58:55] <-Rock-> Time Left  0 Hours 0 Minutes 5 Seconds ;)
[12:58:56] <-Rock-> Time Left  0 Hours 0 Minutes 4 Seconds ;)
[12:58:57] <-Rock-> Time Left  0 Hours 0 Minutes 3 Seconds ;)
[12:58:59] <-Rock-> Time Left  0 Hours 0 Minutes 2 Seconds ;)
[12:58:59] <-Rock-> Time Left  0 Hours 0 Minutes 1 Seconds ;)
[12:59:00] <-Rock-> Oki Here We Go    ;)




Final code has moved here (http://forum.ptokax.org/index.php?topic=8160.0)
Title: Re: Countdown
Post by: Highlander Back on 06 December, 2008, 17:55:18
Works  great ATAG  thank you :)