Skip to content

Instantly share code, notes, and snippets.

@AntonNguyen
Created July 3, 2014 21:00
Show Gist options
  • Save AntonNguyen/4ffa46c33a8965073bc6 to your computer and use it in GitHub Desktop.
Save AntonNguyen/4ffa46c33a8965073bc6 to your computer and use it in GitHub Desktop.
# Description:
# Manipulate the queue for releasing
#
# Commands:
# singles? - Start looking for a singles partner
# doubles? - Start looking for doubles partners
# hyper <singles/doubles> - Start looking for hyper pong partners
# stop - Stop searching for partners
# o/ or \o or \o/ - Join a game
# add <username> - Manually add a player
# remove <username> - Manually add a player
# players - Shows players waitings to play
module.exports = (robot) ->
# initialize the redis store.
robot.brain.data.pong_manager = robot.brain.data.pong_manager || []
pong_manager = robot.brain.data.pong_manager
robot.brain.data.game_type = robot.brain.data.game_type || 'doubles'
game_type = robot.brain.data.game_type
robot.brain.data.partner_searching = robot.brain.data.partner_searching || false
partner_searching = robot.brain.data.partner_searching
robot.hear /stahp$/i, (msg) ->
reset_search()
msg.send "Stopping the partner search"
robot.hear /stop$/i, (msg) ->
reset_search()
msg.send "Stopping the partner search"
robot.hear /singles\?$/i, (msg) ->
msg.send init_singles msg.message.user.name
robot.hear /doubles\?$/i, (msg) ->
msg.send init_doubles msg.message.user.name
robot.hear /hyper\s*(.*)\?$/i, (msg) ->
user = msg.message.user.name
if msg.match[1]
type = msg.match[1].toLowerCase()
if type == "doubles"
msg.send init_doubles user, true
else if type == "singles"
msg.send init_singles user, true
else
msg.send init_doubles user, true
robot.hear /players\?$/i, (msg) ->
msg.send show()
robot.hear /add\s*(.*)$/i, (msg) ->
if msg.match[1]
msg.send join_game msg.match[1].toLowerCase()
else
msg.send "You didn't tell me who to add"
robot.hear /remove\s(.*)$/i, (msg) ->
if msg.match[1]
username = msg.match[1].toLowerCase()
return msg.send "No one is looking for a partner." if not partner_searching
return msg.send "#{username} is not signed up" if username not in pong_manager
index = pong_manager.indexOf(username)
pong_manager.splice index, 1
if pong_manager.length == 0
reset_search()
msg.send "#{ username } is removed. #{ show() }"
else
msg.send "You didn't tell me who you want to remove"
robot.hear /ping$/i, (msg) ->
msg.send 'Pong!'
robot.hear /pong$/i, (msg) ->
msg.send 'Ping!'
robot.hear /o\/$/i, (msg) ->
msg.send join_game msg.message.user.name
robot.hear /\\o$/i, (msg) ->
msg.send join_game msg.message.user.name
init_singles = (user, is_hyper = false) ->
game_type = 'singles'
return init_game user, game_type, is_hyper
init_doubles = (user, is_hyper = false) ->
game_type = 'doubles'
return init_game user, game_type, is_hyper
init_game = (user, game, is_hyper) ->
if not partner_searching
pong_manager = [user.toLowerCase()]
partner_searching = true
if is_hyper
game = "hyper #{game}"
return "#{user} is looking for a #{game} game. Say 'o/' to accept!"
return "Someone else is already looking for #{game} partners: #{ show() }"
join_game = (username) ->
return "No one is looking for a partner." if not partner_searching
return "#{username} is already signed up" if username in pong_manager
if game_type == 'singles'
if pong_manager.length < 2
pong_manager.push username.toLowerCase()
return game_start()
else
if pong_manager.length < 4
pong_manager.push username.toLowerCase()
if pong_manager.length == 4
return game_start()
else
return show()
game_start = ->
if game_type == 'singles'
message = "Singles match starting! #{pong_manager[0]} vs #{pong_manager[1]}"
else
pong_manager.sort () -> return 0.5 - Math.random()
message = "Doubles match starting! #{pong_manager[0]} and #{pong_manager[1]} vs #{pong_manager[2]} and #{pong_manager[3]}"
reset_search()
return message
reset_search = ->
pong_manager = []
partner_searching = false
show = ->
"Current players are: [#{pong_manager.join(', ')}]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment