itchy/check.moon

85 lines
2.4 KiB
Plaintext
Raw Permalink Normal View History

2018-03-25 16:46:03 +00:00
require "love.timer"
import thread, timer from love
http = require "socket.http"
receive = thread.getChannel "send-itchy"
send = thread.getChannel "receive-itchy"
check = (data) ->
2018-03-25 16:46:03 +00:00
exponential_backoff = 1
while true
result = {}
2018-03-25 16:46:03 +00:00
if data.url
result.body, result.status = http.request data.url
2018-03-25 16:46:03 +00:00
elseif data.proxy
unless data.target
result.message = "'target' or 'url' must be defined!"
send\push result
return false
result.body, result.status = http.request "#{data.proxy}/get/https://itch.io/api/1/x/wharf/latest?target=#{data.target}&channel_name=#{data.channel}"
2018-03-25 16:46:03 +00:00
unless result.body
result.message = "socket.http.request error: #{result.status}"
send\push result
return false
result.version = result.body\match '%s*{%s*"latest"%s*:%s*"(.+)"%s*}%s*'
result.version = tonumber(result.version) or result.version
result.latest = if data.version
result.version == data.version
if result.status != 200 and (not result.version)
result.message = "unknown, error getting latest version: HTTP #{result.status}, trying again in #{exponential_backoff} seconds"
send\push result
2018-03-25 16:46:03 +00:00
timer.sleep exponential_backoff
exponential_backoff *= 2
continue
2018-03-25 20:51:59 +00:00
elseif result.latest != nil
if result.latest
result.message = "#{result.version}, you have the latest version"
else
result.message = "#{result.version}, there is a newer version available!"
else
result.message = result.version
send\push result
return true
2018-03-25 16:46:03 +00:00
start = ->
-- data should be a table of information
data = receive\demand!
data.proxy = "http://45.55.113.149:16343" unless data.proxy or data.url
2018-03-25 16:46:03 +00:00
-- channel can be autodetected if not specified
unless data.channel
require "love.system"
os = love.system.getOS!
switch os
when "OS X"
data.channel = "osx"
when "Windows"
data.channel = "win32"
when "Linux"
data.channel = "linux"
when "Android"
data.channel = "android"
when "iOS"
data.channel = "ios"
else
data.channel = os
check data
-- if we should check again every x seconds, wait, and do so
if data.interval
while true
timer.sleep data.interval
-- if we are sent new data, start over entirely
if receive\getCount! > 0
return start!
else
check data
2018-03-25 16:46:03 +00:00
start!