PtokaX forum

Archive => Archived 5.0 boards => Finished Scripts => Topic started by: TiMeTrAVelleR on 23 March, 2006, 22:28:11

Title: Countdown from Plop to Lua 5.1
Post by: TiMeTrAVelleR on 23 March, 2006, 22:28:11

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


------------------------------------------------------------------------------
Bot = "-T?M????-"

--This the date the timer has 2 stop @ midnight
-- year (2 numbers), month, day
SylYear,SylMonth,SylDay = 06,3,23
-- this is the file 2 be shown
file = "happynewyear.txt"
------------------------------------------------------------------------------
------------------------------------------------------------------------------
function OnTimer()
if last == 0 then
SendToAll(Bot, TimeLeft())
Sync()
elseif last == 1 then
SendAscii() -- send the msg
StopTimer() -- 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()
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,21,00,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
line = "LiVe SeT FroM TiMeTrAVelleR At: http://stream01.pcextreme.nl:8035/stream In "
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
------------------------------------------------------------------------------
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()
SendToAll(Bot, "Oki Here We Go? ? ;)|")
end
------------------------------------------------------------------------------
function ChatArrival(user, data)
data=string.sub(data,1,-2)
s,e,cmd = string.find(data,"%b<>%s+(%S+)")
if cmd == "!daysleft" then
local tmp = TimeLeft()
if tonumber(tmp) == nil then user:SendData(Bot, tmp.."|") end
return 1
end
end
------------------------------------------------------------------------------
function NewUserConnected(user)
local tmp = TimeLeft()
if tonumber(tmp) == nil then user:SendData(Bot, tmp.."|") end
end
------------------------------------------------------------------------------
OpConnected = NewUserConnected
------------------------------------------------------------------------------
function Main()
SetTimer(100 * 1000)
StartTimer()
local tmp = TimeLeft()
if tonumber(tmp) == nil then 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,21,00,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 SetTimer(adjust * 1000) else SetTimer(3600 * 1000) end
else
if tmp > 3600 then? --- every hours a msg
adjust = (math.floor(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.floor(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.floor(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 = (math.floor(math.fmod(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.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.floor(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 = 1
SetTimer(1 * 1000)
end
end
end



greetzz TT
Title: Countdown 1.1d - LUA 5.1
Post by: jiten on 27 March, 2006, 20:17:08
--[[

------------------------------------------------------------------------------
------- 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 = "-T?M????-",
-- 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 = "LiVe SeT FroM TiMeTrAVelleR At: http://stream01.pcextreme.nl:8035/stream In",
-- 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
Title: CountDown 1.1a - LUA 5.1
Post by: jiten on 28 March, 2006, 13:01:16
First post updated with rightclick commands.
Title: Re: Countdown from Plop to Lua 5.1
Post by: plop on 29 March, 2006, 01:02:59
hints:

tTime = os.date("*t") returns a table with the current date.
stuff the table in
iJdate = os.time(tTime) and you get the julian date.
now if you stuff iJdate in
os.date("*t", iJdate) you get tTime again.
and next os.date("%c", iJdate) returns the time string.
but there's more.
os.difftime( iNow , iThen) and you get the time between 2 julian date's.

plop

(ps. timetraveller: check a.i. v2, i'm using all this in it)
Title: Re: Countdown from Plop to Lua 5.1
Post by: bastya_elvtars on 29 March, 2006, 02:00:54
os.difftime(t1,t2) is t2-t1
Title: Re: Countdown from Plop to Lua 5.1
Post by: jiten on 30 March, 2006, 13:36:02
Thank you for your hints.
In fact, initially, I modified the script by request only to make the countdown more customizable. But, will try implement this when I get some spare time.

Cheers
Title: CountDown 1.1b - LUA 5.1
Post by: jiten on 30 March, 2006, 22:22:16
First post updated with a quick mod upon request.

Best regards
Title: CountDown 1.1c - LUA 5.1
Post by: jiten on 31 March, 2006, 13:47:24
First updated once more with another request.

Best regards
Title: CountDown 1.1d - LUA 5.1
Post by: jiten on 01 April, 2006, 10:20:36
First post updated with customizable message and related stuff.
Title: Re: Countdown from Plop to Lua 5.1
Post by: TiMeTrAVelleR on 01 April, 2006, 11:52:50
Great job jiten thanks  :D

Greetzz TT
Title: Re: Countdown from Plop to Lua 5.1
Post by: philoum on 05 June, 2006, 05:57:46
Hello, I use this script, it's run but at the time the file  happynewyear.txt  doen't run !
Have you a solution ?
merci

respectfuuly