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
Start it in its own thread, and send a table on the "send-itchy" channel with
information on what you're looking for. Wait for a response on the
"receive-itchy" channel.
Start it as a thread with a configuration table. Wait for "itchy" channel to
respond with a table of version information.
```lua
-- initialize
versionCheck = love.thread.newThread("lib/itchy/check.lua")
versionCheckSend = thread.getChannel("send-itchy")
versionCheck:start()
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
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, config options listed below
})
-- receive info
versionCheckReceive = thread.getChannel("receive-itchy")
if versionCheckReceive:getCount() > 0 then
data = versionCheckReceive:demand() -- this is a table of info, format specified below
-- easiest usage is to just print something like this to the user somewhere
print("Version: " .. current_version .. " Latest version: " .. data.message)
newVersion = love.thread.getChannel("itchy")
if newVersion:getCount() > 0 then
local data = newVersion:demand() -- see example data below
-- easiest usage is to just print something like this to the user
print("Version: 1.0.0 Latest version: " .. data.message)
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:
```lua
{
status = 200, -- nil or status code of an HTTP request
body = '{"latest":"0.2.0"}', -- raw resulting body from the HTTP request
version = "0.2.0", -- a number or string
latest = true, -- nil or boolean your version == latest version?
body = '{"latest":"0.2.0"}', -- raw result body from the HTTP request
version = "0.2.0", -- number/string (if body was parsed correctly)
latest = true, -- nil/boolean: your version == latest version?
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
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
second delay.
second delay, capped at retrying every 10 minutes.
### Options
### Configuration Options
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.
* `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`
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.
* `interval` (number) If specified, a check for the latest version will happen
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
end
local http = require("socket.http")
local receive = thread.getChannel("send-itchy")
local send = thread.getChannel("receive-itchy")
local check
check = function(data)
local send = thread.getChannel(data.thread_channel or "itchy")
local exponential_backoff = 1
while true do
local _continue_0 = false
@ -40,6 +39,9 @@ check = function(data)
send:push(result)
timer.sleep(exponential_backoff)
exponential_backoff = exponential_backoff * 2
if exponential_backoff > 10 * 60 then
exponential_backoff = 10 * 60
end
_continue_0 = true
break
elseif result.latest ~= nil then
@ -62,8 +64,7 @@ check = function(data)
end
end
local start
start = function()
local data = receive:demand()
start = function(data)
if not (data.proxy or data.url) then
data.proxy = "http://45.55.113.149:16343"
end
@ -89,12 +90,8 @@ start = function()
if data.interval then
while true do
timer.sleep(data.interval)
if receive:getCount() > 0 then
return start()
else
check(data)
end
check(data)
end
end
end
return start()
return start(...)

View File

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