<commit_before>gl.setup(1280, 720)

sys.set_flag("slow_gc")

local json = require "json"
local schedule
local current_room

util.resource_loader{
    "progress.frag",
}

local white = resource.create_colored_texture(1,1,1)

util.file_watch("schedule.json", function(content)
    print("reloading schedule")
    schedule = json.decode(content)
    print("loaded schedule. Len: ", #schedule)
end)

local rooms
local spacer = white

node.event("config_update", function(config)
    rooms = {}
    for idx, room in ipairs(config.rooms) do
        if room.serial == sys.get_env("SERIAL") then
            print("found my room")
            current_room = room
        end
        rooms[room.name] = room
    end
    spacer = resource.create_colored_texture(CONFIG.foreground_color.rgba())
end)

hosted_init()

local base_time = N.base_time or 0
local current_talk
local all_talks = {}
local day = 0

function get_now()
    return base_time + sys.now()
end

function check_next_talk()
    local now = get_now()
    local room_next = {}
    for idx, talk in ipairs(schedule) do
        if rooms[talk.place] and not room_next[talk.place] and talk.start_unix + 25 * 60 > now then 
            room_next[talk.place] = talk
        end
    end

    for room, talk in pairs(room_next) do
        talk.slide_lines = wrap(talk.title, 30)

        if #talk.title > 25 then
            talk.lines = wrap(talk.title, 60)
            if #talk.lines == 1 then
                talk.lines[2] = table.concat(talk.speakers, ", ")
            end
        end
    end

    if current_room and room_next[current_room.name] then
        current_talk = room_next[current_room.name]
    else
        current_talk = nil
    end

    print("PARSING talks")
    all_talks = {}
    for idx,talk in ipairs(schedule) do
        if talk.start_unix + 25 * 60 > now and talk.start_unix < now + 24 * 3600 then
            if not current_talk or talk.place ~= current_talk.place then
                all_talks[#all_talks + 1] = talk
            end
        end
    end
    table.sort(all_talks, function(a, b)
        if a.start_unix < b.start_unix then
            return true
        elseif a.start_unix > b.start_unix then
            return false
        else
            return a.place < b.place
        end
    end)
    print("PARSED talks. Count: ", #all_talks)
end

function wrap(str, limit, indent, indent1)
    limit = limit or 72
    local here = 1
    local wrapped = str:gsub("(%s+)()(%S+)()", function(sp, st, word, fi)
        if fi-here > limit then
            here = st
            return "\n"..word
        end
    end)
    local splitted = {}
    for token in string.gmatch(wrapped, "[^\n]+") do
        splitted[#splitted + 1] = token
    end
    return splitted
end

local clock = (function()
    local base_time = N.base_time or 0

    local function set(time)
        base_time = tonumber(time) - sys.now()
    end

    util.data_mapper{
        ["clock/midnight"] = function(since_midnight)
            print("NEW midnight", since_midnight)
            set(since_midnight)
        end;
    }

    local function get()
        local time = (base_time + sys.now()) % 86400
        return string.format("%d:%02d", math.floor(time / 3600), math.floor(time % 3600 / 60))
    end

    return {
        get = get;
        set = set;
    }
end)()

util.data_mapper{
    ["clock/set"] = function(time)
        base_time = tonumber(time) - sys.now()
        N.base_time = base_time
        check_next_talk()
        print("UPDATED TIME", base_time)
    end;
    ["clock/day"] = function(new_day)
        day = new_day
        print("UPDATED DAY", new_day)
    end;
}

function switcher(get_screens)
    local current_idx = 0
    local current
    local current_state
    local switch = sys.now()
    local screens = get_screens()

    local function prepare()
        local now = sys.now()
        if now > switch then
            print("Switching screen currently at ", current_idx)
            -- find next screen
            repeat
                current_idx = current_idx + 1
                if current_idx > #screens then
                    screens = get_screens()
                    current_idx = 1
                end
                current = screens[current_idx]
            until current.time ~= 0
            print("Switched to ", current_idx, " will stay there ", current.time, " seconds")
            switch = now + current.time
            current_state = current.prepare()
        end
    end

    local function draw()
        current.draw(current_state)
    end
    return {
        prepare = prepare;
        draw = draw;
    }
end

local content = switcher(function()
    return {{
        -- Announcement, eg Plenum.
        -- Update date in the prepare function and text in the draw function
        -- use date -d 'May 22 23:00:00 2015' +%s
        -- to get timestamp
        time = 0,
        prepare = function()
        end;
        draw = function()
            CONFIG.font:write(40, 10, "Ankündigung", 70, CONFIG.foreground_color.rgba())
            spacer:draw(0, 120, WIDTH, 122, 0.6)

            local start_date = 1432329600
            local difference = start_date - get_now()

            local time_to_event = ""
            if difference <= 0 then
                time_to_event = string.format("Seit %d Minuten:", difference / -60)
            else
                time_to_event = string.format("In %d Minuten:", difference / 60)
            end
            print("TIME TO EVENT: ", time_to_event, " START: ", start_date, " NOW: ", get_now())
            CONFIG.font:write(40, 180, time_to_event, 90, CONFIG.foreground_color.rgba())
            CONFIG.font:write(40, 300, "Test Event", 90, CONFIG.foreground_color.rgba())
        end
    },
    {
        time = CONFIG.other_rooms,
        prepare = function()
            local content = {}

            local function add_content(func)
                content[#content+1] = func
            end

            local function mk_spacer(y)
                return function()
                    spacer:draw(0, y, WIDTH, y+2, 0.6)
                end
            end

            local function mk_talkmulti(y, talk, is_running)
                local alpha
                if is_running then
                    alpha = 0.5
                else
                    alpha = 1.0
                end<commit_msg>New announcement<commit_after>gl.setup(1280, 720)

sys.set_flag("slow_gc")

local json = require "json"
local schedule
local current_room

util.resource_loader{
    "progress.frag",
}

local white = resource.create_colored_texture(1,1,1)

util.file_watch("schedule.json", function(content)
    print("reloading schedule")
    schedule = json.decode(content)
    print("loaded schedule. Len: ", #schedule)
end)

local rooms
local spacer = white

node.event("config_update", function(config)
    rooms = {}
    for idx, room in ipairs(config.rooms) do
        if room.serial == sys.get_env("SERIAL") then
            print("found my room")
            current_room = room
        end
        rooms[room.name] = room
    end
    spacer = resource.create_colored_texture(CONFIG.foreground_color.rgba())
end)

hosted_init()

local base_time = N.base_time or 0
local current_talk
local all_talks = {}
local day = 0

function get_now()
    return base_time + sys.now()
end

function check_next_talk()
    local now = get_now()
    local room_next = {}
    for idx, talk in ipairs(schedule) do
        if rooms[talk.place] and not room_next[talk.place] and talk.start_unix + 25 * 60 > now then 
            room_next[talk.place] = talk
        end
    end

    for room, talk in pairs(room_next) do
        talk.slide_lines = wrap(talk.title, 30)

        if #talk.title > 25 then
            talk.lines = wrap(talk.title, 60)
            if #talk.lines == 1 then
                talk.lines[2] = table.concat(talk.speakers, ", ")
            end
        end
    end

    if current_room and room_next[current_room.name] then
        current_talk = room_next[current_room.name]
    else
        current_talk = nil
    end

    print("PARSING talks")
    all_talks = {}
    for idx,talk in ipairs(schedule) do
        if talk.start_unix + 25 * 60 > now and talk.start_unix < now + 24 * 3600 then
            if not current_talk or talk.place ~= current_talk.place then
                all_talks[#all_talks + 1] = talk
            end
        end
    end
    table.sort(all_talks, function(a, b)
        if a.start_unix < b.start_unix then
            return true
        elseif a.start_unix > b.start_unix then
            return false
        else
            return a.place < b.place
        end
    end)
    print("PARSED talks. Count: ", #all_talks)
end

function wrap(str, limit, indent, indent1)
    limit = limit or 72
    local here = 1
    local wrapped = str:gsub("(%s+)()(%S+)()", function(sp, st, word, fi)
        if fi-here > limit then
            here = st
            return "\n"..word
        end
    end)
    local splitted = {}
    for token in string.gmatch(wrapped, "[^\n]+") do
        splitted[#splitted + 1] = token
    end
    return splitted
end

local clock = (function()
    local base_time = N.base_time or 0

    local function set(time)
        base_time = tonumber(time) - sys.now()
    end

    util.data_mapper{
        ["clock/midnight"] = function(since_midnight)
            print("NEW midnight", since_midnight)
            set(since_midnight)
        end;
    }

    local function get()
        local time = (base_time + sys.now()) % 86400
        return string.format("%d:%02d", math.floor(time / 3600), math.floor(time % 3600 / 60))
    end

    return {
        get = get;
        set = set;
    }
end)()

util.data_mapper{
    ["clock/set"] = function(time)
        base_time = tonumber(time) - sys.now()
        N.base_time = base_time
        check_next_talk()
        print("UPDATED TIME", base_time)
    end;
    ["clock/day"] = function(new_day)
        day = new_day
        print("UPDATED DAY", new_day)
    end;
}

function switcher(get_screens)
    local current_idx = 0
    local current
    local current_state
    local switch = sys.now()
    local screens = get_screens()

    local function prepare()
        local now = sys.now()
        if now > switch then
            print("Switching screen currently at ", current_idx)
            -- find next screen
            repeat
                current_idx = current_idx + 1
                if current_idx > #screens then
                    screens = get_screens()
                    current_idx = 1
                end
                current = screens[current_idx]
            until current.time ~= 0
            print("Switched to ", current_idx, " will stay there ", current.time, " seconds")
            switch = now + current.time
            current_state = current.prepare()
        end
    end

    local function draw()
        current.draw(current_state)
    end
    return {
        prepare = prepare;
        draw = draw;
    }
end

local content = switcher(function()
    return {{
        -- Announcement, eg Plenum.
        -- Update date in the prepare function and text in the draw function
        -- use date -d 'May 22 23:00:00 2015' +%s
        -- to get timestamp
        time = 15,
        prepare = function()
        end;
        draw = function()
            CONFIG.font:write(40, 10, "Ankündigung", 70, CONFIG.foreground_color.rgba())
            spacer:draw(0, 120, WIDTH, 122, 0.6)

            local start_date = 1432329600
            local difference = start_date - get_now()

            local time_to_event = ""
            if difference <= 0 then
                time_to_event = string.format("Seit %d Minuten:", difference / -60)
            else
                time_to_event = string.format("In %d Minuten:", difference / 60)
            end
            print("TIME TO EVENT: ", time_to_event, " START: ", start_date, " NOW: ", get_now())
            CONFIG.font:write(40, 180, "Die Person mit Schluessel fuer 24a moege sich bitte im Infocafe melden", 35, CONFIG.foreground_color.rgba())
            -- CONFIG.font:write(40, 180, time_to_event, 90, CONFIG.foreground_color.rgba())
            CONFIG.font:write(40, 300, "Die Person mit Schluessel fuer 66 moege sich bitte bei Bungalow 74 melden", 35, CONFIG.foreground_color.rgba())
        end
    },
    {
        time = CONFIG.other_rooms,
        prepare = function()
            local content = {}

            local function add_content(func)
                content[#content+1] = func
            end

            local function mk_spacer(y)
                return function()
                    spacer:draw(0, y, WIDTH, y+2, 0.6)
                end
            end

            local function mk_talkmulti(y, talk, is_running)
                local alpha
                if is_running then
                    alpha = 0.5
                else
                    alpha = 1.0
                end
{'fraction_non_alphanumeric': 0.09952935730267726, 'fraction_numerical': 0.020831725947071986, 'mean_word_length': 1.8683337021464925, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '544051', 'n_tokens_mistral': 4127, 'n_tokens_neox': 3838, 'n_words': 1065}