From 47485db3913082d8fff77d83f5bbcca669a866fb Mon Sep 17 00:00:00 2001 From: insweater Date: Thu, 5 Feb 2015 19:27:44 +0300 Subject: [PATCH] Add fetchTrophy, fetchAllTrophies, fetchTrophiesByStatus .. also fixes for keypair parser --- gamejolt.lua | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/gamejolt.lua b/gamejolt.lua index 7332e82..9840307 100644 --- a/gamejolt.lua +++ b/gamejolt.lua @@ -21,7 +21,7 @@ local function req(s, f, pu, pt) return r end -function parseKeypair(s, on) +local function parseKeypair(s, on) local c, len = 0, string.len(s) local b, k, v @@ -32,11 +32,27 @@ function parseKeypair(s, on) c = b + 2 b = string.find(s, '"', c) v = string.sub(s, c, b - 1) - c = b + 2 + c = b + 3 on(k, v) end end +local function handleTrophies(str) + local d = req("trophies/?" .. str, "keypair", true, true) + local t, f = {} + + parseKeypair(d, function(k, v) + if k ~= "success" then + if k == "id" then + f = {} + table.insert(t, f) + end + f[k] = v + end + end) + return t +end + function GJ.init(id, key) GJ.gameID = id GJ.gameKey = key @@ -140,4 +156,22 @@ function GJ.giveTrophy(id) return string.find(req("trophies/add-achieved/?trophy_id=" .. tostring(id), "dump", true, true), "SUCCESS") ~= nil end +function GJ.fetchTrophy(id) + local d = req("trophies/?trophy_id=" .. tostring(id), "keypair", true, true) + + local t = {} + parseKeypair(d, function(k, v) + if k ~= "success" then t[k] = v end + end) + return t +end + +function GJ.fetchTrophiesByStatus(achieved) + return handleTrophies("achieved=" .. tostring(achieved)) +end + +function GJ.fetchAllTrophies() + return handleTrophies("") +end + return GJ \ No newline at end of file