Skip to content

Instantly share code, notes, and snippets.

@badcc
Last active July 13, 2017 02:07
Show Gist options
  • Save badcc/e99df31b8c383756e5f114244e4b08a8 to your computer and use it in GitHub Desktop.
Save badcc/e99df31b8c383756e5f114244e4b08a8 to your computer and use it in GitHub Desktop.
Binary search. Some logic. Some API stuff. Quite hacked together.
// @badccvoid
var PLACE_ID = 539494517
var TARGET_PLAYERS = 7
var queue = [0]
function req(placeid, startindex, cb) {
$.get('https://www.roblox.com/games/getgameinstancesjson?placeId='+placeid+'&startindex='+startindex, cb)
}
function finish(id) {
console.log('found maximum', id)
req(PLACE_ID, id, function(res){
var collection = res.Collection
var server = collection[0]
if (server === undefined) return finish(id-10)
var first_players = server.CurrentPlayers.length
console.log('first server has', first_players)
// This is the logic for actually getting correct server to join.
// Currently ONLY joins server with TARGET_PLAYER amount.
// If none exist (e.g. only target+1 or target-1) it will cycle
// back and forth until there is one. This is maybe not intended.
// Feel free to update.
if (first_players < TARGET_PLAYERS) return finish(id-1)
else if (first_players > TARGET_PLAYERS) return finish(id+1)
else eval(server.JoinScript);
})
}
function processQueue(min, max) {
var item = queue.shift()
if (item === undefined) return;
req(PLACE_ID, item, function(res){
console.log('fetched', item)
var length = res.Collection.length
var next = length > 0 ? Math.round((max+item)*0.5) : Math.round((min+item)*0.5)
if (length <= 0) max = item;
else if (length >= 0) min = item;
queue.push(next)
if (item == next) {
return finish(item-1)
}
processQueue(min, max)
})
}
processQueue(0,5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment