This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.lua
159 lines (139 loc) · 4.51 KB
/
node.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
util.init_hosted()
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
node.alias("sold-out")
local font
local logo_asset_name
local logo
local black = resource.create_colored_texture(0,0,0,0.8)
node.event("content_update", function(filename, file)
if filename == "config.json" then
if CONFIG.auto_resolution then
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
else
gl.setup(CONFIG.width, CONFIG.height)
end
font = CONFIG.font
if CONFIG.logo.asset_name ~= logo_asset_name then
logo_asset_name = CONFIG.logo.asset_name
logo = resource.load_image(logo_asset_name, TRUE)
end
end
end)
local start = 0
local count = 0
local current_page = 1
local total_pages = 1
local next_page = sys.now() + CONFIG.switch_time
local soldoutevents = {}
soldoutevents[1] = "Checking for sold out events"
local magic_dot = " "
node.event("content_update", function(filename, file)
if filename == "soldoutevents.txt" then
soldoutevents = {}
local content = resource.load_file(file)
if content ~= "" then
for event in string.gmatch(content, "[^\r\n]+") do
soldoutevents[#soldoutevents + 1] = event
end
print("There are " .. #soldoutevents .. " sold out events")
end
if #soldoutevents == 0 then
soldoutevents[#soldoutevents + 1] = CONFIG.no_events_text
print("There are no sold out events")
end
total_pages = math.ceil(#soldoutevents / CONFIG.events_per_page)
end
end)
util.data_mapper {
["updating"] = function(value)
magic_dot = value
end;
}
local function load_next()
next_page = sys.now() + CONFIG.switch_time
start = start + count
current_page = current_page + 1
end
local function start_again()
start = 0
count = 0
current_page = 1
end
local function stringLongerThanScreen(str)
local width = font:width(str, CONFIG.font_size)
if width > WIDTH - (CONFIG.margin * 2) then
return true
end
return false
end
local function shortenString(str, no_chars)
no_chars = no_chars or 1
local length = string.len(str)
return string.sub(str, 0, length - no_chars)
end
local function write_event(event)
local str = event
while (stringLongerThanScreen(str))
do
str = shortenString(str)
event = shortenString(str, 3)
event = event .. '...'
end
font:write(CONFIG.margin, y, event, CONFIG.font_size, CONFIG.font_colour.rgba())
y = y + CONFIG.font_size
return 1
end
function node.render()
CONFIG.background_colour.clear()
-- print title
local title = "Sold out events"
if CONFIG.today_only then
title = title .. " today"
end
local title_font_size = CONFIG.font_size * 1.5
local title_width = font:width(title, title_font_size)
font:write(CONFIG.margin, CONFIG.margin, title, title_font_size, CONFIG.font_colour.rgba())
black:draw(CONFIG.margin, CONFIG.margin + title_font_size, CONFIG.margin + title_width, CONFIG.margin + title_font_size + 1)
-- draw logo
local logo_w, logo_h = logo:size()
local logo_ratio = logo_w / logo_h
util.draw_correct(logo, WIDTH - (CONFIG.margin + (logo_ratio * title_font_size)), CONFIG.margin, WIDTH - CONFIG.margin, CONFIG.margin + title_font_size)
y = title_font_size + (title_font_size / 2 ) + CONFIG.margin
count = 0
local line_count = 0
local pages = 0
local morelines = 0
for i, event in ipairs(soldoutevents) do
if i > start then
if line_count < CONFIG.events_per_page then
local lines_consumed = write_event(event)
y = y + (CONFIG.font_size / 2)
count = count + 1
line_count = line_count + lines_consumed
else
morelines = 1
end
end
end
if morelines == 1 then
if current_page >= total_pages then
total_pages = current_page + 1
end
end
-- print number of pages
local text = "Page " .. current_page .. "/" .. total_pages .. magic_dot
local size = CONFIG.font_size * 0.8
local width = font:width(text, size)
font:write(WIDTH - width - CONFIG.margin, HEIGHT - size - CONFIG.margin, text, size, CONFIG.font_colour.rgba())
-- handle loading next pages
if current_page < total_pages then
if sys.now() > next_page then
load_next()
end
else
if sys.now() > next_page then
load_next()
start_again()
end
end
end