Created
July 20, 2012 17:26
-
-
Save zacstewart/3152018 to your computer and use it in GitHub Desktop.
Hubot scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = require 'underscore' | |
module.exports = (robot) -> | |
class Leaderboard | |
constructor: -> | |
robot.brain.once 'loaded', => | |
robot.brain.data.leaderboard ?= {} | |
@games = robot.brain.data.leaderboard.games ?= [] | |
addGame: (winner, loser) -> | |
@games.push | |
winner: winner | |
loser: loser | |
time: Date.now() | |
"Okay, #{winner.name} beat #{loser.name}" | |
getGames: -> | |
@games | |
getPlayers: -> | |
_(@games.map((g) -> g.winner).concat @games.map((g) -> g.loser)).uniq(false, _.isEqual) | |
getPlayerGames: (player) -> | |
_(@games).select((g) -> g.winner.id is player.id or g.loser.id is player.id) | |
getPlayerKdr: (player) -> | |
wins = _(@games).select((g) -> g.winner.id is player.id).length || 0 | |
losses = _(@games).select((g) -> g.loser.id is player.id).length || 0 | |
wins / (wins + losses) | |
getBoard: -> | |
@getPlayers().map (p) => | |
player: p | |
kdr: @getPlayerKdr(p) | |
games: @getPlayerGames(p) | |
@leaderboard = new Leaderboard() | |
# Add a game | |
robot.respond /@?([\w .-]+) beat @?([\w .-]+)/i, (msg) -> | |
wname = msg.match[1].trim() | |
lname = msg.match[2].trim() | |
winner = robot.usersForFuzzyName wname | |
loser = robot.usersForFuzzyName lname | |
if winner.length is 1 and loser.length is 1 | |
msg.send leaderboard.addGame(winner[0], loser[0]) | |
else | |
msg.send "I don't care. I've been drinking" | |
# Show whole leaderboard | |
robot.respond /leaderboard$/i, (msg) -> | |
lb = _(leaderboard.getBoard()).chain() | |
.sortBy('kdr') | |
.map((l) -> "#{l.player.name} has won #{l.kdr * 100}% of #{l.games.length} games") | |
.value() | |
msg.send """ | |
Leaderboard: | |
Win % | Player | |
#{lb.join("\n")} | |
""" | |
# Show all players | |
robot.respond /leaderboard players$/i, (msg) -> | |
players = leaderboard.getPlayers() | |
msg.send "Players: #{(player.name for player in players).join(', ')}" | |
# Show player stats | |
robot.respond /leaderboard @([\w .-]+)/i, (msg) -> | |
name = msg.match[1].trim() | |
player = robot.usersForFuzzyName name | |
if player.length is 1 | |
player = player[0] | |
kdr = leaderboard.getPlayerKdr(player) | |
msg.send "#{player.name} has won #{kdr * 100}% of their games." | |
else | |
msg.send "Either #{name} is really awful or I don't care" | |
robot.respond /leaderboard games$/i, (msg) -> | |
games = _(leaderboard.getGames()).map((g) -> "#{g.time}: #{new Date(g.time)} #{g.winner.name} beat #{g.loser.name}") | |
msg.send """ | |
Leaderboard games: | |
Game id: date winner beats loser | |
#{games.join("\n")} | |
""" | |
getAmbiguousUserText = (users) -> | |
"Be more specific, I know #{users.length} people named like that: #{(user.name for user in users).join(", ")}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment