Skip to content

Instantly share code, notes, and snippets.

@coderoshi
Created May 18, 2012 23:29
Show Gist options
  • Save coderoshi/2728125 to your computer and use it in GitHub Desktop.
Save coderoshi/2728125 to your computer and use it in GitHub Desktop.
Challonge Hubot plugin
# Usage: (bracket followed by a comma-separated list of names)
# hubot bracket Joe, Fred, Tony, Eddy
https = require('https')
# uses challonge to create a bracket with the given names
module.exports = (robot) ->
robot.respond /bracket\s+(.+?)$/i, (msg) ->
names = msg.match[1]
return unless names
names = names.split(',').map((x)-> (x || '').trim())
api_key = process.env.CHALLONGE_API_KEY
challonge_api_host = "challonge.com"
# first create the tourney
tourney_name = "hubot_#{randomString()}"
console.log tourney_name
create_body = "tournament[name]=#{tourney_name}&tournament[url]=#{tourney_name}"
create = https.request {
method: 'POST'
host: challonge_api_host
path: "/api/tournaments.json?api_key=#{api_key}"
headers:
'Content-Length': create_body.length
}, (create_res)->
buffer = ''
create_res.on 'data', (chunk)-> buffer += chunk
create_res.on 'end', ()->
console.log buffer
name_counter = names.length
# now that t is made, add players
for player_name in names
player_body = "participant[name]=#{player_name}"
player = https.request {
method: 'POST'
host: challonge_api_host
path: "/api/tournaments/#{tourney_name}/participants.json?api_key=#{api_key}"
headers:
'Content-Length': player_body.length
}, (player_res)->
pbuffer = ''
player_res.on 'data', (chunk)-> pbuffer += chunk
player_res.on 'end', ()->
console.log pbuffer
# when we've completed all player creations, start the t
return unless --name_counter == 0
publish = https.request {
method: 'POST'
host: challonge_api_host
path: "/api/tournaments/publish/#{tourney_name}.json?api_key=#{api_key}"
headers:
'Content-Length': 0
}, (publish_res)->
pubuffer = ''
publish_res.on 'data', (chunk)-> pubuffer += chunk
publish_res.on 'end', ()->
console.log pubuffer
start = https.request {
method: 'POST'
host: challonge_api_host
path: "/api/tournaments/start/#{tourney_name}.json?api_key=#{api_key}"
headers:
'Content-Length': 0
}, (start_res)->
sbuffer = ''
start_res.on 'data', (chunk)-> sbuffer += chunk
start_res.on 'end', ()->
console.log sbuffer
msg.send "http://challonge.com/#{tourney_name}"
start.on 'error', (e)-> console.error e
start.end("")
publish.on 'error', (e)-> console.error e
publish.end("")
player.on 'error', (e)->
--name_counter
console.error e
player.end(player_body)
create.on 'error', (e)-> console.error e
create.end(create_body)
randomString = ()->
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"
(chars.charAt(Math.floor(Math.random() * chars.length)) for i in [0...8]).join('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment