version 2.0.0, simplified usage, custom channl name

This commit is contained in:
Paul Liverman III 2018-04-12 22:42:54 -07:00
parent 2facdb610e
commit e2df6b75dd
4 changed files with 51 additions and 45 deletions

View File

@ -9,37 +9,37 @@ Just copy `check.lua` to where you want in your source.
## Usage ## Usage
Start it in its own thread, and send a table on the "send-itchy" channel with Start it as a thread with a configuration table. Wait for "itchy" channel to
information on what you're looking for. Wait for a response on the respond with a table of version information.
"receive-itchy" channel.
```lua ```lua
-- initialize versionChecker = love.thread.newThread("lib/itchy/check.lua") -- wherever you save it..
versionCheck = love.thread.newThread("lib/itchy/check.lua") versionChecker:start({
versionCheckSend = thread.getChannel("send-itchy") target = "guard13007/asteroid-dodge", -- target/url must be defined
versionCheck:start() version = "1.0.0" -- optional, config options listed below
versionCheckSend:push({
target = "guard13007/asteroid-dodge", -- target/url must be defined, see Options
version = current_version -- defined elsewhere (also, optional)
-- other options are available!! see list below
}) })
-- receive info
versionCheckReceive = thread.getChannel("receive-itchy") newVersion = love.thread.getChannel("itchy")
if versionCheckReceive:getCount() > 0 then if newVersion:getCount() > 0 then
data = versionCheckReceive:demand() -- this is a table of info, format specified below local data = newVersion:demand() -- see example data below
-- easiest usage is to just print something like this to the user somewhere -- easiest usage is to just print something like this to the user
print("Version: " .. current_version .. " Latest version: " .. data.message) print("Version: 1.0.0 Latest version: " .. data.message)
end end
``` ```
Since it is run as a thread, you can cancel it with `versionChecker:kill()` and
start it again or with a different configuration later with `:start()`.
### Version Information Example
Returned data example: Returned data example:
```lua ```lua
{ {
status = 200, -- nil or status code of an HTTP request status = 200, -- nil or status code of an HTTP request
body = '{"latest":"0.2.0"}', -- raw resulting body from the HTTP request body = '{"latest":"0.2.0"}', -- raw result body from the HTTP request
version = "0.2.0", -- a number or string version = "0.2.0", -- number/string (if body was parsed correctly)
latest = true, -- nil or boolean your version == latest version? latest = true, -- nil/boolean: your version == latest version?
message = "0.2.0, you have the latest version" -- an error or status message message = "0.2.0, you have the latest version" -- an error or status message
} }
``` ```
@ -54,9 +54,9 @@ based on this value. If it is unable to extract it or compare, `version` and
If HTTP status 200 (OK) is encountered or a version is successfully parsed from If HTTP status 200 (OK) is encountered or a version is successfully parsed from
a response, the script terminates (or moves on to checking on an `interval`.) a response, the script terminates (or moves on to checking on an `interval`.)
Otherwise, it will keep trying with an exponential back-off starting at a 1 Otherwise, it will keep trying with an exponential back-off starting at a 1
second delay. second delay, capped at retrying every 10 minutes.
### Options ### Configuration Options
At minimum a `url` or `target` must be specified. At minimum a `url` or `target` must be specified.
@ -71,7 +71,9 @@ At minimum a `url` or `target` must be specified.
* `version` (string/number) Version of the game running right now. * `version` (string/number) Version of the game running right now.
* `proxy` (string) This library uses an [HTTP proxy](https://github.com/Guard13007/insecure-proxy) * `proxy` (string) This library uses an [HTTP proxy](https://github.com/Guard13007/insecure-proxy)
for the HTTPS call to itch.io's API. By default it uses `https://104.236.139.220:16343` for the HTTPS call to itch.io's API. By default it uses `https://104.236.139.220:16343`
which is a DigitalOcean VPS I own. If you'd rather use a different proxy which is on a DigitalOcean VPS I own. If you'd rather use a different proxy
server, you can specify it here. server, you can specify it here.
* `interval` (number) If specified, a check for the latest version will happen * `interval` (number) If specified, a check for the latest version will happen
again every `interval` seconds. again every `interval` seconds.
* `thread_channel` (string) If specified, will use a different named thread
channel to return results to.

View File

@ -5,10 +5,9 @@ do
thread, timer = _obj_0.thread, _obj_0.timer thread, timer = _obj_0.thread, _obj_0.timer
end end
local http = require("socket.http") local http = require("socket.http")
local receive = thread.getChannel("send-itchy")
local send = thread.getChannel("receive-itchy")
local check local check
check = function(data) check = function(data)
local send = thread.getChannel(data.thread_channel or "itchy")
local exponential_backoff = 1 local exponential_backoff = 1
while true do while true do
local _continue_0 = false local _continue_0 = false
@ -40,6 +39,9 @@ check = function(data)
send:push(result) send:push(result)
timer.sleep(exponential_backoff) timer.sleep(exponential_backoff)
exponential_backoff = exponential_backoff * 2 exponential_backoff = exponential_backoff * 2
if exponential_backoff > 10 * 60 then
exponential_backoff = 10 * 60
end
_continue_0 = true _continue_0 = true
break break
elseif result.latest ~= nil then elseif result.latest ~= nil then
@ -62,8 +64,7 @@ check = function(data)
end end
end end
local start local start
start = function() start = function(data)
local data = receive:demand()
if not (data.proxy or data.url) then if not (data.proxy or data.url) then
data.proxy = "http://45.55.113.149:16343" data.proxy = "http://45.55.113.149:16343"
end end
@ -89,12 +90,8 @@ start = function()
if data.interval then if data.interval then
while true do while true do
timer.sleep(data.interval) timer.sleep(data.interval)
if receive:getCount() > 0 then check(data)
return start()
else
check(data)
end
end end
end end
end end
return start() return start(...)

View File

@ -2,10 +2,10 @@ require "love.timer"
import thread, timer from love import thread, timer from love
http = require "socket.http" http = require "socket.http"
receive = thread.getChannel "send-itchy"
send = thread.getChannel "receive-itchy"
check = (data) -> check = (data) ->
send = thread.getChannel data.thread_channel or "itchy"
exponential_backoff = 1 exponential_backoff = 1
while true while true
result = {} result = {}
@ -33,6 +33,7 @@ check = (data) ->
send\push result send\push result
timer.sleep exponential_backoff timer.sleep exponential_backoff
exponential_backoff *= 2 exponential_backoff *= 2
exponential_backoff = 10 * 60 if exponential_backoff > 10 * 60 -- maximum backoff is 10 minutes
continue continue
elseif result.latest != nil elseif result.latest != nil
if result.latest if result.latest
@ -45,9 +46,8 @@ check = (data) ->
send\push result send\push result
return true return true
start = -> -- data should be a table of information
-- data should be a table of information start = (data) ->
data = receive\demand!
data.proxy = "http://45.55.113.149:16343" unless data.proxy or data.url data.proxy = "http://45.55.113.149:16343" unless data.proxy or data.url
-- channel can be autodetected if not specified -- channel can be autodetected if not specified
@ -74,11 +74,6 @@ start = ->
if data.interval if data.interval
while true while true
timer.sleep data.interval timer.sleep data.interval
check data
-- if we are sent new data, start over entirely start(...)
if receive\getCount! > 0
return start!
else
check data
start!

12
usage.lua Normal file
View File

@ -0,0 +1,12 @@
versionChecker = love.thread.newThread("lib/itchy/check.lua") -- wherever you save it..
versionChecker:start({
target = "guard13007/asteroid-dodge", -- target/url must be defined
version = "1.0.0" -- optional
})
newVersion = love.thread.getChannel("itchy")
if newVersion:getCount() > 0 then
local data = newVersion:demand()
-- easiest usage is to just print something like this to the user
print("Version: 1.0.0 Latest version: " .. data.message)
end