Created
February 22, 2016 03:47
-
-
Save tannerjfco/7be2f554a0253286685e to your computer and use it in GitHub Desktop.
hubot-ping-pong.coffee
This file contains hidden or 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
# Description: | |
# ping teh pongs | |
# | |
# Dependencies: | |
# probably hubot | |
# | |
# Configuration: | |
# nope | |
# | |
# Commands: | |
# hubot <player> won <singles/doubles> against <player> - logs a win and a loss for given players | |
# hubot who <wins/loses> <singles/doubles>? - see who's doing well (or not) | |
# | |
# Notes: | |
# nope | |
# | |
# Author: | |
# tannerjfco | |
module.exports = (robot) -> | |
pongScore = (gameType, player, result) -> | |
pongStore = JSON.parse(robot.brain.get "pingpong-#{gameType}") or {} | |
if not pongStore[player]? | |
pongStore[player] = { wins: 0, loses: 0 } | |
pongStore[player]["#{result}"]++ | |
robot.brain.set "pingpong-#{gameType}", JSON.stringify(pongStore) | |
robot.brain.save() | |
robot.respond /(.*) won (singles|doubles) against (.*)/i, (res)-> | |
winner = res.match[1] | |
gameType = res.match[2] | |
notWinner = res.match[3] | |
res.reply "congrats #{winner}" | |
pongScore(gameType, winner, "wins") | |
pongScore(gameType, notWinner, "loses") | |
robot.respond /who (wins|loses) (singles|doubles)?/i, (res) -> | |
resultType = res.match[1] | |
gameType = res.match[2] | |
pongStore = JSON.parse(robot.brain.get "pingpong-#{gameType}") | |
if not pongStore? | |
res.reply "eh, I got nothing. you should go play some ping-pong and let me know how it goes" | |
results = [] | |
response = "" | |
for k,v of pongStore | |
results.push({"name": k, "score": v[resultType]}) | |
sortNumber = (a, b) -> | |
b.score - a.score | |
results.sort(sortNumber) | |
for result in results | |
response += "#{result["name"]}: #{result["score"]}\n" | |
res.reply response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment