diff --git a/main.lua b/main.lua index c9f63fb..69458c2 100644 --- a/main.lua +++ b/main.lua @@ -7,6 +7,7 @@ environment = os.getenv('ENV') screens = { require 'screens.weather', require 'screens.time', + require 'screens.misery', require 'screens.screen1', } diff --git a/screens/misery.lua b/screens/misery.lua new file mode 100644 index 0000000..e4d6258 --- /dev/null +++ b/screens/misery.lua @@ -0,0 +1,50 @@ +local node = {} + +local inspect = require('vendor.inspect') + +local textFont = love.graphics.newFont('fonts/Lato-Light.ttf', 50) +local smallFont = love.graphics.newFont('fonts/Lato-Light.ttf', 30) + +local updateInterval = 2 * 60 +local lastUpdate = 0 +local state = nil + +function node.load() +end + +function node.unload() +end + +function node.render() + love.graphics.setColor( 0, 0, 0 ) + love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) + + if state then + love.graphics.setColor( 255, 255, 255 ) + love.graphics.setFont(textFont) + love.graphics.printf(state.text, 50, 180, love.graphics.getWidth() - 100, 'center') + + love.graphics.setColor( 255, 255, 255, 200 ) + love.graphics.setFont(smallFont) + love.graphics.printf(state.description, 200, love.graphics.getHeight() - 100, love.graphics.getWidth() - 400, 'center') + else + love.graphics.setColor( 255, 255, 255, 80 ) + + love.graphics.setFont(smallFont) + love.graphics.printf("Loading misery...", 0, love.graphics.getHeight() - 200, love.graphics.getWidth(), 'center') + end +end + +function node.update(dt) + if lastUpdate < love.timer.getTime() - updateInterval then + lastUpdate = love.timer.getTime() + print("Updating...") + + local updateThread = love.thread.newThread('screens/misery_thread.lua') + updateThread:start() + end + + state = love.thread.getChannel('misery'):pop() or state +end + +return node diff --git a/screens/misery_thread.lua b/screens/misery_thread.lua new file mode 100644 index 0000000..8b0c6d9 --- /dev/null +++ b/screens/misery_thread.lua @@ -0,0 +1,35 @@ +local socket = require("socket") +local http = require("socket.http") +local json = require("vendor.json") +local lume = require("vendor.lume") + +local miseryURL = 'http://oodviewer.q3k.me/term/_,' + +math.randomseed( os.time() ) + +function unescape(str) + str = string.gsub( str, '<', '<' ) + str = string.gsub( str, '>', '>' ) + str = string.gsub( str, '"', '"' ) + str = string.gsub( str, ''', "'" ) + str = string.gsub( str, '(%d+);', function(n) return string.char(n) end ) + str = string.gsub( str, '(%d+);', function(n) return string.char(tonumber(n,16)) end ) + str = string.gsub( str, '&', '&' ) -- Be sure to do this after all others + return str +end + +local r, c, h = http.request(miseryURL) +if c == 200 then + local data = {} + for item, desc in string.gmatch(r, '