From 486d2474b64ffaba3b80934327d598f739acde07 Mon Sep 17 00:00:00 2001 From: John Russell Date: Sat, 10 Nov 2012 11:46:49 -0800 Subject: [PATCH 1/6] Chess Script! --- src/scripts/chess.coffee | 101 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/scripts/chess.coffee diff --git a/src/scripts/chess.coffee b/src/scripts/chess.coffee new file mode 100644 index 000000000..092c396f5 --- /dev/null +++ b/src/scripts/chess.coffee @@ -0,0 +1,101 @@ +# Description: +# Play a game of chess! +# +# Dependencies: +# "chess": "0.1.3" +# +# Configuration: +# None +# +# Commands: +# hubot chess me - Creates a new game between yourself and another person in the room +# hubot chess status - Gets the current state of the board +# hubot chess move - Moves a piece to the coordinate position using standard chess notation +# hubot chess undo - Takes back the previous move +# +# Author: +# thallium205 +# + +Chess = require 'chess' + +module.exports = (robot) -> + robot.respond /chess me$/i, (msg) -> + robot.brain.data.chess = Chess.create() + boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> + msg.send 'White\'s turn.' + msg.send 'http://www.eddins.net/steve/chess/ChessImager/ChessImager.php?fen=' + fen + '.png' + + robot.respond /chess status/i, (msg) -> + try + boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> + msg.send status + msg.send 'http://www.eddins.net/steve/chess/ChessImager/ChessImager.php?fen=' + fen + '.png' + catch e + msg.send e + + robot.respond /chess undo/i, (msg) -> + try + robot.brain.data.chess.undo + boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> + msg.send status + msg.send 'http://www.eddins.net/steve/chess/ChessImager/ChessImager.php?fen=' + fen + '.png' + catch e + msg.send e + + robot.respond /chess move (.*)/i, (msg) -> + try + robot.brain.data.chess.move msg.match[1] + boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> + msg.send status + msg.send 'http://www.eddins.net/steve/chess/ChessImager/ChessImager.php?fen=' + fen + '.png' + catch e + msg.send e + +boardToFen = (status, callback) -> + fen = [[],[],[],[],[],[],[],[]] + blank = 0 + lastRank = 0 + for square in status.board.squares + if lastRank isnt square.rank + if blank isnt 0 + fen[lastRank - 1].push(blank) + blank = 0 + if square.piece is null + blank = blank + 1 + else + if square.piece.type is 'pawn' + if blank is 0 + fen[square.rank - 1].push(if square.piece.side.name is 'white' then 'P' else 'p') + else + fen[square.rank - 1].push(blank) + fen[square.rank - 1].push(if square.piece.side.name is 'white' then 'P' else 'p') + blank = 0 + else + if blank is 0 + fen[square.rank - 1].push(if square.piece.side.name is 'white' then square.piece.notation.toUpperCase() else square.piece.notation.toLowerCase()) + else + fen[square.rank - 1].push(blank) + fen[square.rank - 1].push(if square.piece.side.name is 'white' then square.piece.notation.toUpperCase() else square.piece.notation.toLowerCase()) + blank = 0 + lastRank = square.rank + for rank in fen + rank = rank.join() + fen = fen.reverse().join('/').replace(/,/g,'') + + msg = '' + if status.isCheck + msg += 'Check! ' + if status.isCheckmate + msg += 'Checkmate! ' + if status.isRepetition + msg += 'Threefold Repetition! A draw can be called. ' + if status.isStalemate + msg += 'Stalemate! ' + if Object.keys(status.notatedMoves).length > 0 + if status.notatedMoves[Object.keys(status.notatedMoves)[0]].src.piece.side.name is 'white' then msg += 'White\'s turn. ' else msg += 'Black\'s turn.' + + + callback msg, fen + + From 545befe4224465de7318a9d7742b72242b851d37 Mon Sep 17 00:00:00 2001 From: John Russell Date: Sat, 10 Nov 2012 12:04:07 -0800 Subject: [PATCH 2/6] Remove undo --- src/scripts/chess.coffee | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/scripts/chess.coffee b/src/scripts/chess.coffee index 092c396f5..21864045e 100644 --- a/src/scripts/chess.coffee +++ b/src/scripts/chess.coffee @@ -11,7 +11,6 @@ # hubot chess me - Creates a new game between yourself and another person in the room # hubot chess status - Gets the current state of the board # hubot chess move - Moves a piece to the coordinate position using standard chess notation -# hubot chess undo - Takes back the previous move # # Author: # thallium205 @@ -34,15 +33,6 @@ module.exports = (robot) -> catch e msg.send e - robot.respond /chess undo/i, (msg) -> - try - robot.brain.data.chess.undo - boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> - msg.send status - msg.send 'http://www.eddins.net/steve/chess/ChessImager/ChessImager.php?fen=' + fen + '.png' - catch e - msg.send e - robot.respond /chess move (.*)/i, (msg) -> try robot.brain.data.chess.move msg.match[1] From d03d422dbe10d4bc78634bcfa61030d37c6af8a8 Mon Sep 17 00:00:00 2001 From: John Russell Date: Sat, 10 Nov 2012 23:24:36 -0800 Subject: [PATCH 3/6] chess board now has coordinates --- src/scripts/chess.coffee | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/scripts/chess.coffee b/src/scripts/chess.coffee index 21864045e..5a763e734 100644 --- a/src/scripts/chess.coffee +++ b/src/scripts/chess.coffee @@ -23,13 +23,12 @@ module.exports = (robot) -> robot.brain.data.chess = Chess.create() boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> msg.send 'White\'s turn.' - msg.send 'http://www.eddins.net/steve/chess/ChessImager/ChessImager.php?fen=' + fen + '.png' - + msg.send 'http://chessup.net/php/pictureParser.php?fen=' + fen + '&coord=on&.png' robot.respond /chess status/i, (msg) -> try boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> msg.send status - msg.send 'http://www.eddins.net/steve/chess/ChessImager/ChessImager.php?fen=' + fen + '.png' + msg.send 'http://chessup.net/php/pictureParser.php?fen=' + fen + '&coord=on&.png' catch e msg.send e @@ -38,7 +37,7 @@ module.exports = (robot) -> robot.brain.data.chess.move msg.match[1] boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> msg.send status - msg.send 'http://www.eddins.net/steve/chess/ChessImager/ChessImager.php?fen=' + fen + '.png' + msg.send 'http://chessup.net/php/pictureParser.php?fen=' + fen + '&coord=on&.png' catch e msg.send e From 92128ac344999a1e6581965ef8470bcb9ffa875e Mon Sep 17 00:00:00 2001 From: John Russell Date: Wed, 5 Dec 2012 13:04:28 -0800 Subject: [PATCH 4/6] Update src/scripts/chess.coffee Updated the chess board as the last website broke --- src/scripts/chess.coffee | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/scripts/chess.coffee b/src/scripts/chess.coffee index 5a763e734..7981b535a 100644 --- a/src/scripts/chess.coffee +++ b/src/scripts/chess.coffee @@ -22,13 +22,13 @@ module.exports = (robot) -> robot.respond /chess me$/i, (msg) -> robot.brain.data.chess = Chess.create() boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> - msg.send 'White\'s turn.' - msg.send 'http://chessup.net/php/pictureParser.php?fen=' + fen + '&coord=on&.png' + msg.send 'http://webchess.freehostia.com/diag/chessdiag.php?fen=' + encodeURIComponent(fen) + '&size=large&coord=yes&cap=yes&stm=yes&fb=no&theme=classic&format=auto&color1=E3CEAA&color2=635147&color3=000000&.png' robot.respond /chess status/i, (msg) -> try boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> - msg.send status - msg.send 'http://chessup.net/php/pictureParser.php?fen=' + fen + '&coord=on&.png' + if status + msg.send status + msg.send 'http://webchess.freehostia.com/diag/chessdiag.php?fen=' + encodeURIComponent(fen) + '&size=large&coord=yes&cap=yes&stm=yes&fb=no&theme=classic&format=auto&color1=E3CEAA&color2=635147&color3=000000&.png' catch e msg.send e @@ -36,8 +36,9 @@ module.exports = (robot) -> try robot.brain.data.chess.move msg.match[1] boardToFen robot.brain.data.chess.getStatus(), (status, fen) -> - msg.send status - msg.send 'http://chessup.net/php/pictureParser.php?fen=' + fen + '&coord=on&.png' + if status + msg.send status + msg.send 'http://webchess.freehostia.com/diag/chessdiag.php?fen=' + encodeURIComponent(fen) + '&size=large&coord=yes&cap=yes&stm=yes&fb=no&theme=classic&format=auto&color1=E3CEAA&color2=635147&color3=000000&.png' catch e msg.send e @@ -82,9 +83,9 @@ boardToFen = (status, callback) -> if status.isStalemate msg += 'Stalemate! ' if Object.keys(status.notatedMoves).length > 0 - if status.notatedMoves[Object.keys(status.notatedMoves)[0]].src.piece.side.name is 'white' then msg += 'White\'s turn. ' else msg += 'Black\'s turn.' - - - callback msg, fen - + if status.notatedMoves[Object.keys(status.notatedMoves)[0]].src.piece.side.name is 'white' + fen += ' w'; + else + fen += ' b'; + callback msg, fen \ No newline at end of file From d54d77a2ebd99c5db8288ee4fcb919cf9f06ccee Mon Sep 17 00:00:00 2001 From: John Russell Date: Thu, 6 Dec 2012 12:50:50 -0800 Subject: [PATCH 5/6] added a library that makes this way better --- src/scripts/twss.coffee | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/scripts/twss.coffee b/src/scripts/twss.coffee index c87bbd4d2..18519e58f 100644 --- a/src/scripts/twss.coffee +++ b/src/scripts/twss.coffee @@ -2,7 +2,7 @@ # Hubot will respond to (in)appropriate lines with "That's what she said" # # Dependencies: -# None +# twss # # Configuration: # None @@ -13,6 +13,8 @@ # Author: # dhchow +var twss = require('twss'); module.exports = (robot) -> - robot.respond /.*(big|small|long|hard|soft|mouth|face|good|fast|slow|in there|on there|in that|on that|wet|dry|on the|in the|suck|blow|jaw|all in|fit that|fit it|hurts|hot|huge|balls|stuck)/i, (msg) -> - msg.send "THAT'S WHAT SHE SAID!" + robot.hear //, (msg) -> + if (twss.is(robot.msg)) { + msg.send "... that's what she said." From 3f810b16e124573f763f453220bacc304fed1c98 Mon Sep 17 00:00:00 2001 From: John Russell Date: Thu, 6 Dec 2012 13:41:36 -0800 Subject: [PATCH 6/6] adds the twss threshhold of 90% --- src/scripts/twss.coffee | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/scripts/twss.coffee b/src/scripts/twss.coffee index 18519e58f..3384473b6 100644 --- a/src/scripts/twss.coffee +++ b/src/scripts/twss.coffee @@ -8,13 +8,14 @@ # None # # Commands: -# hubot - Hubot will "that's what she said" that ish +# Will listen for good opportunities to drop a twss joke. # # Author: -# dhchow +# thallium205 -var twss = require('twss'); +twss = require('twss') +twss.threshold = .9 module.exports = (robot) -> - robot.hear //, (msg) -> - if (twss.is(robot.msg)) { - msg.send "... that's what she said." + robot.hear /\S/, (msg) -> + if twss.is(msg.message.text) + msg.send "... that's what she said." \ No newline at end of file