• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua manipulating os.time()

darkmu

Well-Known Member
Joined
Aug 26, 2007
Messages
274
Solutions
1
Reaction score
50
Location
Paraná,Brazil
I'm trying to make a system that he will be signaling the time for the next opening of the castle, could someone help me with this solution?

TFS 1.X

Code:
local warCastle = {
    ['Sunday'] = { timeOpen = '22:00' },
    ['Tuesday'] = { timeOpen = '22:00' },
    ['Friday'] = { timeOpen = '22:00' },
}

function onThink(interval)
    local currentTime = os.time()
    local targetTime  = os.time()
    
    local targetDate = os.date('*t', targetTime)
    local targetDay = warCastle[os.date('%A', os.time(targetDate))]
    
    if targetDay then
        for timeStr, openTime in pairs(targetDay) do
            local split = timeStr:split(':')
            targetDate.hour, targetDate.min = split[1], split[2]

            local timeLeft = os.time(targetDate) - currentTime           
            if timeLeft > 0 then
                local spectators = Game.getSpectators(settings.position, false, true, 7, 7, 5, 5)
                if #spectators > 0 then
                    if settings.text then
                        for i = 1, #spectators do
                            spectators[i]:say(string.format("Next War Castle Open %s.", showTimeLeft(timeLeft, true)), TALKTYPE_MONSTER_SAY, false, spectators[i], Position(81, 315, 6))
                        end
                    end           
                end
                break
            end
        end   
    end
    
    return true
end
 
I'm trying to make a system that he will be signaling the time for the next opening of the castle, could someone help me with this solution?

TFS 1.X

Code:
local warCastle = {
    ['Sunday'] = { timeOpen = '22:00' },
    ['Tuesday'] = { timeOpen = '22:00' },
    ['Friday'] = { timeOpen = '22:00' },
}

function onThink(interval)
    local currentTime = os.time()
    local targetTime  = os.time()
  
    local targetDate = os.date('*t', targetTime)
    local targetDay = warCastle[os.date('%A', os.time(targetDate))]
  
    if targetDay then
        for timeStr, openTime in pairs(targetDay) do
            local split = timeStr:split(':')
            targetDate.hour, targetDate.min = split[1], split[2]

            local timeLeft = os.time(targetDate) - currentTime         
            if timeLeft > 0 then
                local spectators = Game.getSpectators(settings.position, false, true, 7, 7, 5, 5)
                if #spectators > 0 then
                    if settings.text then
                        for i = 1, #spectators do
                            spectators[i]:say(string.format("Next War Castle Open %s.", showTimeLeft(timeLeft, true)), TALKTYPE_MONSTER_SAY, false, spectators[i], Position(81, 315, 6))
                        end
                    end         
                end
                break
            end
        end 
    end
  
    return true
end
I only tested this out with lua demo so let me know if it works. I also didn't get involved in adding it to onThink cause I'm not exactly sure what all you need beyond the time check.

Lua:
-- warCastle index [0-6 = Sunday-Saturday]

warCastle = {
    [0] = { weekday = 'Sunday', time = '22:00' },
    [2] = { weekday = 'Tuesday', time = '22:00' },
    [5] = { weekday = 'Friday', time = '22:00' },
}

local currentWeekday = os.date("%w")
local currentTime = os.date('%H%M', os.time())

local nextCastle
for i = currentWeekday, 6 do
    if warCastle[i] then
        local warCastleTime = warCastle[i].time:gsub(":", "")
        if currentTime < warCastleTime then
            nextCastle = warCastle[i]
            break
        end
    end
end

nextCastle = nextCastle or warCastle[0] -- make sure this index is always set to the first iteration in warCastle table

print("The next War Castle will be open on " .. nextCastle.weekday .. " at " .. nextCastle.time .. ".")
 
Back
Top