Advertisement
Imgoodisher

Economy Client API

Jan 13th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. ntc = {
  2.     _baseURL = "http://imgood.zapto.org/ntc/",
  3.     _escape = function(s)
  4.         return tostring(s):gsub("[&=%?%%]", function(c) return "%"..string.byte(c) end)
  5.     end,
  6.     _parseArgs = function(args)
  7.         local s = ''
  8.         for i,v in pairs(args) do
  9.             s = s .. "&" .. ntc._escape(i) .. "=" .. ntc._escape(v)
  10.         end
  11.         return "?"..s:sub(2)
  12.     end,
  13.     _decodeJSON = function(str)
  14.         s = str:gsub("[%[%]:]", {['[']='{', [']']='}', [':']='='}):gsub('"([^"]+)"%s?=', '["%1"]=')--:gsub('([%d]+)%s?=', '[%1]=')
  15.         local func, e = _G[ntc.isCraftOS and "loadstring" or "load"]( "return "..s )
  16.         if not func then
  17.             return false, "Error decoding"
  18.         else
  19.             return func()
  20.         end
  21.     end
  22. }
  23.  
  24.  
  25. if os.version and os.version():find("^CraftOS") then
  26.     ntc.isCraftOS = true
  27.     ntc._getRaw = function(page, args)
  28.         local url = ntc._baseURL .. page .. ntc._parseArgs(args)
  29.         local data = http.get(url).readAll();
  30.         local status, response = data:match("([01])%s(.+)")
  31.         if status and response then
  32.             return status=="1", response
  33.         else
  34.             return false, "Invalid Response"
  35.         end
  36.     end
  37. else
  38.     ntc.isCraftOS = false
  39.     ntc._getRaw = function(page, args)
  40.         local url = ntc._baseURL .. page .. ntc._parseArgs(args)
  41.         local data = ""
  42.         for s in http.request(url) do data = data .. s end
  43.         local status, response = data:match("([01])%s(.+)")
  44.         if status and response then
  45.             return status=="1", response
  46.         else
  47.             return false, "Invalid Response"
  48.         end
  49.     end
  50. end
  51.  
  52. ntc.login = function(username, password)
  53.     ntc._username = username
  54.     ntc._password = password
  55. end
  56.  
  57. ntc.register = function(username, password)
  58.     local ok, resp = ntc._getRaw("register.py", {username=username, password=password})
  59.     if ok then
  60.         ntc._username = username
  61.         ntc._password = password
  62.     end
  63.     return ok, resp
  64. end
  65.  
  66. ntc.transfer = function(to, amount)
  67.     return ntc._getRaw("transfer.py", {from=ntc._username, to=to, password=ntc._password, amount=amount})
  68. end
  69.  
  70. ntc.balance = function()
  71.     local ok, resp = ntc._getRaw("balance.py", {username=ntc._username, password=ntc._password})
  72.     if ok then return tonumber(resp) else return false, resp end
  73. end
  74.  
  75. ntc.getlog = function()
  76.     local ok, resp = ntc._getRaw("getlog.py", {username=ntc._username, password=ntc._password})
  77.     if ok then return ntc._decodeJSON(resp) else return false, resp end
  78. end
  79.  
  80. ntc.genkey = function(amount)
  81.     local ok, resp = ntc._getRaw("genkey.py", {username=ntc._username, password=ntc._password, amount=amount})
  82.     if ok then return resp else return false, resp end
  83. end
  84.  
  85. ntc.redeemkey = function(key)
  86.     return ntc._getRaw("redeemkey.py", {username=ntc._username, password=ntc._password, key=key})
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement