Skip to content

Instantly share code, notes, and snippets.

@bmwalters
Last active January 13, 2016 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmwalters/9736a9d2fbcfd5554530 to your computer and use it in GitHub Desktop.
Save bmwalters/9736a9d2fbcfd5554530 to your computer and use it in GitHub Desktop.
Counts my B1s
local function GetB1Count(sid, callback)
http.Fetch(string.format("http://steamcommunity.com/profiles/%d/inventory/json/753/6", sid), function(c, b)
if c ~= 200 then print("bad code", c) callback(false) return end
local data = json.decode(b)
if data and data.rgInventory then
local count = 0
for _, info in pairs(data.rgInventory) do
if info and tonumber(info.classid) == 171857344 then
count = count + 1
end
end
callback(count)
else
callback(false)
end
end)
end
zerf = zerf or {}
function zerf.B1Count()
GetB1Count(76561198052589582, function(data)
if not data then return end
print("Zerf has " .. data .. " :B1:s.")
end)
return "Counting :B1:s..."
end
zerf.lastB1Count = 0
--[[
local function checkB1s()
GetB1Count(76561198052589582, function(s, c)
if s and (c ~= zerf.lastB1Count) then
print("Zerf has obtained " .. (c - zerf.lastB1Count) .. " new :B1:s!")
zerf.lastB1Count = c
end
end)
end
checkB1s()
timer.Create("zerfb1counter", 30, 0, checkB1s)
--]]
--[[
local function FetchGroupMembers(gid, callback, page, sids)
page = page or 1
sids = sids or {}
http.Fetch(string.format("http://steamcommunity.com/gid/%d/memberslistxml/?xml=1&p=%d", gid, page), function(c, b)
if c ~= 200 then callback(false, c) return end
local membersblock = string.match(b, "<members>(.+)</members>")
if not membersblock then print("couldn't find members block") return end
for sid64 in string.gmatch(membersblock, "<steamID64>(.-)</steamID64>") do
sids[#sids + 1] = sid64
end
if string.find(b, "<nextPageLink>") then
print("GOING TO NEXT PAGE: ", page + 1)
FetchGroupMembers(gid, callback, page + 1, sids)
else
callback(sids)
end
end)
end
local B1Counts = {}
local function PrintLeaderboard()
table.sort(B1Counts, function(a, b)
return a[2] > b[2]
end)
print(":B1: LEADERBOARD")
print("-----------------")
table.print(B1Counts)
for i = 1, 5 do
print(B1Counts[i][1], B1Counts[i][2] .. " :B1:s")
end
end
-- 103582791434445273 = B1 Group
-- 103582791439031144 = glua
FetchGroupMembers(103582791438967254, function(data, err)
if data then
print("Found " .. #data .. " members.")
--[=[
print("This will take " .. string.format("%.2f", (#data * 0.05) / 60) .. " seconds to complete.")
local i = 0
timer.Create("zerf_fetchB1counts", 0.05, #data, function()
i = i + 1
local sid = data[i]
GetB1Count(sid, function(count, err)
if err then
print("Failure fetching data for SID " .. sid .. ". Code: " .. err)
else
B1Counts[#B1Counts + 1] = {sid, count}
end
end)
end)
--]=]
local done = 0
for _, sid in pairs(data) do -- steam locks me out for this
GetB1Count(sid, function(count)
if count then
B1Counts[#B1Counts + 1] = {sid, count}
end
done = done + 1
if done == #data then
PrintLeaderboard()
end
end)
end
else
print("Failure. Code: " .. err)
end
end)
--]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment