From ad2c1c586b2d828b0d2090015b3e3ee17337bce5 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Sun, 25 Mar 2018 13:47:39 -0700 Subject: [PATCH] forgot to save ReadMe file before commiting... >.> --- ReadMe.md | 45 ++++++++++++++++++++++++++++++++++++--------- check.moon | 6 +++--- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 67da3df..aa443ad 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -10,7 +10,8 @@ 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. +information on what you're looking for. Wait for a response on the +"receive-itchy" channel. ```lua -- initialize @@ -18,33 +19,59 @@ versionCheck = love.thread.newThread("lib/itchy/check.lua") versionCheckSend = thread.getChannel("send-itchy") versionCheck:start() versionCheckSend:push({ - target = "guard13007/asteroid-dodge" - -- other options are available!! + 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") if versionCheckReceive:getCount() > 0 then - -- this could be a version or an error message! - latest_version = versionCheckReceive:demand() + 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) end ``` +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? + message = "0.2.0, you have the latest version" -- an error or status message +} +``` + +Errors from LuaSocket will be returned as `"socket.http.request error: " .. err` + +The library tries to parse a response body for valid JSON of the format +`{"latest":"VERSION"}` (itch.io's format) and will do basic version comparisons +based on this value. If it is unable to extract it or compare, `version` and +`latest` will be `nil`. + +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. + ### Options +At minimum a `url` or `target` must be specified. + * `url` (string) If you have a different URL to check for the latest version from, you can specify it here. -* `target` REQUIRED (string) The target string of your game on itch.io +* `target` (string) The target string of your game on itch.io (username/game-slug) * `channel` (string) If you do not specify the channel name to look for on itch.io, it will use `osx` for Mac OS / OS X, `win32` for Windows, `linux` for Linux, `android` for Android, `ios` for iOS, and if any other OS is returned by `love.system.getOS()` it will use that string as-is. -* `version` (any) 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) 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 server, you can specify it here. * `interval` (number) If specified, a check for the latest version will happen again every `interval` seconds. -* `send_interval_errors` DEFAULT false (boolean) Whether or not checks happening - on an interval will report errors. diff --git a/check.moon b/check.moon index 78c8a2e..982171a 100644 --- a/check.moon +++ b/check.moon @@ -19,7 +19,7 @@ check = (data) -> result.body, result.status = http.request "#{data.proxy}/get/https://itch.io/api/1/x/wharf/latest?target=#{data.target}&channel_name=#{data.channel}" unless result.body - result.message = "socket.http.request error: #{status}" + result.message = "socket.http.request error: #{result.status}" send\push result return false @@ -28,8 +28,8 @@ check = (data) -> result.latest = if data.version result.version == data.version - if status != 200 and (not version) - result.message = "unknown, error getting latest version: HTTP #{status}, trying again in #{exponential_backoff} seconds" + 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 timer.sleep exponential_backoff exponential_backoff *= 2