Re: Lua GameJolt API [Highscore tables / achievements]
Posted: Mon Feb 09, 2015 5:51 am
This looks pretty cool and I'm glad to see that it's really being worked on. I can't wait to try it out. ^^
Code: Select all
return string.find(req(...),"SUCCESS")
Code: Select all
local s = req(...) --This is the same function you could find inside the returned string.find
print(s)
return string.find(s,"SUCCESS") --String.find, the same values but instead of a new request, the value you got before
Code: Select all
function GJ.authUser(name, token)
GJ.username = name
GJ.userToken = token
local r = req("users/auth/?", "dump", true, true)
print(r)
local s = string.find(r, "SUCCESS") ~= nil
GJ.isLoggedIn = s
return s
end
Code: Select all
console.addCommand("GJinit", function(args)
if args then
if GJ.init(args[1], args[2]) then
console.success("Initialised GameJolt API.")
else
console.error("Didn't initialise GameJolt API.")
end
else
console.error("No arguments! - [gameid] [gamekey]")
end
end, "Initialise GameJolt API - Arguments: [gameid] [gamekey]")
console.addCommand("GJauthUser", function(args)
if args then
if GJ.authUser(args[1], args[2]) then
console.success(args[1] .. " is now connected to GameJolt.")
else
console.error("Didn't connect, try again.")
end
else
console.error("No arguments! - [username] [token]")
end
end, "Authenticate user - Arguments: [username] [token]")
console.addCommand("GJopenSession", function(args)
if GJ.isLoggedIn then
if GJ.openSession() then
console.success("Opened session.")
else
console.error("Didn't open session")
end
else
console.error("You are not logged in to GameJolt.")
end
end, "Open Session")
console.addCommand("GJpingSession", function(args)
if args then
if args[1] == "active" then
if GJ.pingSession(true) then
console.success("Pinged as active.")
else
console.error("Didn't ping")
end
elseif args[1] == "idle" then
if GJ.pingSession(false) then
console.success("Pinged as active.")
else
console.error("Didn't ping")
end
else
console.error("Wrong arguments! - [idle/active]")
end
else
console.error("No arguments! [idle/active]")
end
end, "Ping Session - Arguments: [idle/active]")
console.addCommand("GJcloseSession", function(args)
if GJ.closeSession() then
console.success("Closed session.")
else
console.error("Did not close session.")
end
end, "Close Session")
console.addCommand("GJisLoggedIn", function(args)
if GJ.isLoggedIn then
if userInfo then
console.print(userInfo.username .. " is logged in to GameJolt.")
else
console.print("You are logged in to GameJolt.")
end
else
console.print("You are not logged in to GameJolt.")
end
end, "Check if user is logged in to GameJolt")
console.addCommand("GJfetchUserBy", function(args)
if args then
if args[1] == "id" and tonumber(args[2]) then
if GJ.isLoggedIn then
local userInfo = GJ.fetchUserByID(args[2])
console.print("Fetching user info...")
console.print("ID: " .. userInfo.id)
console.print("Type: " .. userInfo.type)
console.print("Username: " .. userInfo.username)
console.print("Avatar URL: " .. userInfo.avatar_url)
console.print("Signed up: " .. userInfo.signed_up)
console.print("Last logged in: " .. userInfo.last_logged_in)
console.print("Status: " .. userInfo.status)
if userInfo.type == "Developer" then
console.print("Developer name: " .. userInfo.developer_name)
console.print("Developer website: " .. userInfo.developer_website)
console.print("Developer description: " .. userInfo.developer_describtion)
end
else
console.error("You are not logged in to GameJolt!")
end
elseif args[1] == "name" then
if GJ.isLoggedIn then
userInfo = GJ.fetchUserByName(args[2])
console.print("Fetching user info...")
console.print("ID: " .. userInfo.id)
console.print("Type: " .. userInfo.type)
console.print("Username: " .. userInfo.username)
console.print("Avatar URL: " .. userInfo.avatar_url)
console.print("Signed up: " .. userInfo.signed_up)
console.print("Last logged in: " .. userInfo.last_logged_in)
console.print("Status: " .. userInfo.status)
if userInfo.type == "Developer" then
console.print("Developer name: " .. userInfo.developer_name)
console.print("Developer website: " .. userInfo.developer_website)
console.print("Developer description: " .. userInfo.developer_description)
end
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("Wrong arguments! - [id/name] [actual id or name]")
end
else
console.error("No arguments! - [id/name] [actual id or name]")
end
end, "Fetch User Info - Arguments: [id/name] [actual id or name]")
console.addCommand("GJgiveTrophy", function(args)
if args then
if GJ.isLoggedIn then
if tonumber(args[1]) then
if GJ.giveTrophy(args[1]) then
console.success("Gave trophy with ID: ." .. args[1])
else
console.error("Didn't give trophy")
end
else
console.error("You did not enter a number!")
end
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("Wrong argument! - [trophy id]")
end
end, "Give trophy by ID - Arguments: [trophy id]")
console.addCommand("GJfetchTrophy", function(args)
if args and tonumber(args[1]) then
if GJ.isLoggedIn then
if tonumber(args[1]) then
console.print("Fetching trophy info...")
trophyInfo = GJ.fetchTrophy(args[1])
for i,v in ipairs(trophyInfo) do
console.print("")
console.print("ID: " .. v.id)
console.print("Title: " .. v.title)
console.print("Description: " .. v.description)
console.print("Difficulty: " .. v.difficulty)
console.print("Image URL: " .. v.image_url)
console.print("Achieved: " .. v.achieved)
end
else
console.error("You did not enter a number!")
end
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("Wrong argument! - [trophy id]")
end
end, "Fetch trophy by ID - Arguments: [trophy id]")
console.addCommand("GJfetchTrophiesByStatus", function(args)
if args then
if GJ.isLoggedIn then
if args[1] == "achieved" then
console.print("Fetching trophies...")
trophiesStatus = GJ.fetchTrophiesByStatus(true)
elseif args[1] == "unachieved" then
console.print("Fetching trophies...")
trophiesStatus = GJ.fetchTrophiesByStatus(false)
else
console.error("Wrong argument! - [achieved/unachieved]")
end
for i,v in ipairs(trophiesStatus) do
console.print("")
console.print("ID: " .. v.id)
console.print("Title: " .. v.title)
console.print("Description: " .. v.description)
console.print("Difficulty: " .. v.difficulty)
console.print("Image URL: " .. v.image_url)
console.print("Achieved: " .. v.achieved)
end
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("No arguments! - [achieved/unachieved]")
end
end, "Fetch trophies by status - Arguments: [achieved/unachieved]")
console.addCommand("GJfetchAllTrophies", function(args)
if GJ.isLoggedIn then
console.print("Fetching trophies info...")
trophies = GJ.fetchAllTrophies()
for i,v in ipairs(trophies) do
console.print("")
console.print("ID: " .. v.id)
console.print("Title: " .. v.title)
console.print("Description: " .. v.description)
console.print("Difficulty: " .. v.difficulty)
console.print("Image URL: " .. v.image_url)
console.print("Achieved: " .. v.achieved)
end
else
console.error("You are not logged in to GameJolt!")
end
end, "Fetch all trophies")
console.addCommand("GJaddScore", function(args)
if args then
if GJ.isLoggedIn then
if GJ.addScore(args[1], args[2], args[3], args[4], args[5] ) then
console.success("Score added.")
else
console.error("Score not added.")
end
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("No arguments! - [score] [desc] [tableID] [guestName] [extraData]")
end
end, "Add score - Arguments: [score] [desc] [tableID] [guestName] [extraData]")
console.addCommand("GJfetchScores", function(args)
if args then
if GJ.isLoggedIn then
if args[2] and tonumber(args[1]) and tonumber(args[2]) then
console.print("Fetching scores...")
scores = GJ.fetchScores(args[1], args[2])
console.print("")
elseif not tonumber(args[2]) and args[2] then
console.error("You did not enter a number! (Argument 2)")
elseif not args[2] then
console.print("Fetching scores...")
scores = GJ.fetchScores(args[1])
console.print("")
end
for i,v in ipairs(scores) do
console.print(
i .. ". " ..
" Score: " .. v.score ..
" Sort: " .. v.sort ..
" ExtDat: " .. v.extra_data ..
" User: " .. v.user ..
" UserID: " .. v.user_id ..
" Guest: " .. v.guest ..
" Stored: " .. v.stored
)
end
elseif not tonumber(args[1]) then
console.error("You did not enter a number!(Argument 1)")
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("No arguments! - [How many scores to fetch]")
end
end, "Fetch scores - Arguments: [How many scores to fetch]")
console.addCommand("GJfetchTables", function(args)
if GJ.isLoggedIn then
console.print("Fetching score tables...")
tables = GJ.fetchTables()
console.print("")
for i,v in ipairs(tables) do
console.print(
"ID: " .. v.id ..
" Name: " .. v.name ..
" Description: " .. v.description ..
" Primary: " .. v.primary
)
end
else
console.error("You are not logged in to GameJolt!")
end
end, "Fetch Score Tables")
console.addCommand("GJfetchData", function(args)
if args then
if GJ.isLoggedIn then
console.print("Fetching data...")
data = GJ.fetchData(args[1], args[2])
console.print("Key: " .. args[1] .. " Data: " .. data)
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("No arguments! - [key] [true/false]")
end
end, "Fetch Data - Arguments: [key] [true/false]")
console.addCommand("GJsetData", function(args)
if args then
if GJ.isLoggedIn then
console.print("Storing data...")
if GJ.setData(args[1], args[2], args[3]) then
console.success("Data is stored.")
else
console.error("Data is not stored.")
end
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("No arguments! - [key] [data] [true/false]")
end
end, "Set Data - Arguments: [key] [data] [true/false]")
console.addCommand("GJupdateData", function(args)
if args then
if GJ.isLoggedIn then
console.print("Updating data...")
if GJ.updateData(args[1], args[2], args[3], args[4]) then
console.success("Data is updated.")
else
console.error("Data is not updated.")
end
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("No arguments! - [key] [value] [operation] [true/false]")
end
end, "Update Data - Arguments: [key] [value] [operation] [true/false]")
console.addCommand("GJremoveData", function(args)
if args then
if GJ.isLoggedIn then
console.print("Removing data...")
if GJ.removeData(args[1], args[2]) then
console.success("Data removed.")
else
console.error("Data was not removed.")
end
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("No arguments! - [key] [true/false]")
end
end, "Remove Data - Arguments: [key] [true/false]")
console.addCommand("GJfetchStorageKeys", function(args)
if args then
if GJ.isLoggedIn then
console.print("Fetching storage keys...")
keys = GJ.fetchStorageKeys(args[1])
console.print("")
for _, v in ipairs(keys) do console.print(v) end
else
console.error("You are not logged in to GameJolt!")
end
else
console.error("No arguments! - [true/false]")
end
end, "Fetch storage keys - Arguments: [true/false]")
Well that is good, anyway, I want to fix this someday hahaDreanh wrote:Today when I was going try debugging like you suggested, the error doesn't happen anymore. Haven't changed any code, so it's probably something with my connection. I've been printing username and token all along, so they should be right.
Yeah exactly that!Dreanh wrote:If it returns though it was something like this you meant?
Yeah, you need to make it asynchronous, but that is not in the scope of this lib, since the lib is not made for LÖVE but Lua in general, so I'm working on a rework of this lib which works asynchronously in LÖVEDreanh wrote:Another thing though I tried GJ.pingSesion() every 30 sec as wiki suggested, it freezes the game for a second every time. Do I have to run another thread?(I know almost nothing about threads yet) And is it even necessary if I don't have trophies? Could I do GJ.openSession() before I GJ.fetchScores() or GJ.addScores()?
Currently it only works in Windows but the issue has been reported to the GameJolt team and we hope it will be fixedManInBlackSweater wrote: - Added basic Quick Play support (tested with LÖVE)
Yeah and, if I'm not wrong, that means something like 40000% faster hashing!!. A really big impact for this lib, since all the request need to be hashed (and that means MANY requests!)ManInBlackSweater wrote: - Updated md5.lua to latest version
Glad to being part of this, an amazing library!ManInBlackSweater wrote: Thanks to amazing team.