{"action":"created","comment":{"url":"https://api.github.com/repos/gtracy/slack-stuart/pulls/comments/74164640","id":74164640,"diff_hunk":"@@ -0,0 +1,187 @@\n+/********************************************\n+ *\n+ * Play a game of 5 card draw\n+ *\n+ * Author : Dan Schumann @dasch\n+ *\n+ ********************************************/\n+\n+var _ = require(\"underscore\");\n+\n+var SECOND = 1000;\n+var MINUTE = SECOND * 60;\n+\n+var DISCARD_HELP = \n+  '`/stuart poker discard 1 3 5` to discard your 1st, 3rd, and 5th card' + \n+  ' or `/stuart poker discard` to discard no cards. Game ends after everyone does this';\n+\n+// single game at a time\n+var game;\n+\n+var Game = function(stuart, user_names, channel_name){\n+  this.id = 'Game #' + (''+Math.random()).substring(10);\n+  var _game = this;\n+\n+  // stuart is not saved to instance for garbage collection purposes\n+  _game.user_names   = _.unique(user_names);\n+  _game.channel_name = channel_name;\n+\n+  _game.players = {};\n+\n+  // Expire games after 5 minutes in case someone is afk\n+  _game.timeout = setTimeout(function(){\n+    _game.reveal(stuart);\n+  }, 5*MINUTE);\n+\n+};\n+\n+_.extend(Game.prototype, {\n+\n+  // Shuffles a deck and deals 5 cards to each player\n+  deal: function(stuart){\n+\n+    var _game = this;\n+\n+    _game.deck = _.flatten(_.map([':hearts:', ':diamonds:', ':spades:', ':clubs:'], function(suit){\n+      return _.map(_.range(2,11).concat('Jack', 'Queen', 'King', 'Ace'), function(c){\n+        return c + suit;\n+      });\n+    }));\n+    _game.deck = _.shuffle(_game.deck);\n+\n+    _.each(_game.user_names, function(user_name){\n+      stuart.slack_post(\n+        _game.id + ' - ' +\n+        'Dealing' + \n+        _.range(0,5).map(function(){return ':flower_playing_cards:'}).join(' ') +\n+        'to ' + user_name,\n+        '#'+_game.channel_name, user_name\n+      );\n+      \n+      _game.players[user_name] = {\n+        cards: _.range(0,5).map(function(){\n+          return _game.deck.pop();\n+        }),\n+      };\n+\n+      stuart.slack_post(\n+        DISCARD_HELP,\n+        user_name, user_name\n+      );\n+\n+      setTimeout(function(){\n+        _game.echoCards(stuart, user_name);\n+      }, 1*SECOND);\n+\n+    });\n+  },\n+\n+  echoCards: function(stuart, user_name){\n+    var _game = this;\n+    stuart.slack_post(\n+      _game.id + ' - ' +\n+      'Your cards are          ' + \n+      this.players[user_name].cards.join('      '),\n+      user_name, user_name\n+    );\n+  },\n+\n+  discard: function(stuart, user_name, cards){\n+    var _game = this;\n+    var player = _game.players[user_name];\n+\n+    // sanitize\n+    cards = _.unique(_.compact(_.map(cards, function(c){\n+      if (1 <= +c && +c <= 5) return +c;\n+    })));\n+\n+    if (!player)\n+      return stuart.slack_post(\n+        _game.id + ' - ' +\n+        \"You're not in the game! `/stuart poker deal @me @otherPerson` -- but be careful about interrupting another game!\",\n+        user_name, user_name\n+      );\n+\n+    if (player.discarded)\n+      return stuart.slack_post(\n+        _game.id + ' - ' +\n+        'You can\\'t discard twice!',\n+        user_name, user_name\n+      );\n+\n+    var pre = _game.deck.length;\n+    _.each(cards, function(n){\n+      player.cards[+n-1] = _game.deck.pop();\n+    });\n+\n+    player.discarded = true;\n+\n+    stuart.slack_post(\n+      _game.id + ' - ' +\n+      user_name + ' took ' + cards.length + ' cards ' + \n+      _.range(0,cards.length).map(function(){return ':flower_playing_cards:'}).join(' '),\n+      '#'+_game.channel_name, user_name\n+    );\n+\n+    _game.echoCards(stuart, user_name);\n+\n+    if (! _.any(_game.players, function(p){return !p.discarded;}))\n+      setTimeout(function(){\n+        _game.reveal(stuart);\n+      }, .5*SECOND);\n+\n+  },\n+\n+  reveal: function(stuart){\n+    clearTimeout(this.timeout);\n+\n+    var _game = this;\n+    _.each(_game.players, function(player, user_name){\n+      stuart.slack_post(\n+        _game.id + ' - ' +\n+        user_name + ' shows ' + player.cards.join('      '),\n+        '#'+_game.channel_name, user_name\n+      );\n+    });\n+    game = undefined;\n+  },\n+\n+});\n+\n+module.exports.run = function(request, _args, stuart, plugin) {\n+  var args = _.clone(_args);\n+\n+  var channel_name = request.channel_name == 'directmessage' ? 'general' : request.channel_name;\n+  switch (args.shift()) {\n+    case 'deal':\n+      game = new Game(stuart, args, channel_name);\n+      game.deal(stuart);\n+      break;\n+    case 'discard':\n+      if (game)\n+        game.discard(stuart, '@' + request.user_name, args);\n+      break;\n+    case 'reveal':\n+      if (game)\n+        game.reveal(stuart);\n+      break;\n+  }\n+};\n+\n+module.exports.help = function(request, stuart) {\n+  stuart.slack_post(\n+    'Play a game of poker with your friends. ' +\n+    ' `/stuart poker deal @you @player2 @player3 @player4`',\n+    '@'+request.user_name\n+  );\n+\n+  setTimeout(function(){","path":"lib/plugins/slash/poker.js","position":177,"original_position":178,"commit_id":"4561076ddc53ede4011062699224cfc67c7eb8ab","original_commit_id":"65148d795be62ca13d588e16824773b65842c957","user":{"login":"gtracy","id":169253,"avatar_url":"https://avatars.githubusercontent.com/u/169253?v=3","gravatar_id":"","url":"https://api.github.com/users/gtracy","html_url":"https://github.com/gtracy","followers_url":"https://api.github.com/users/gtracy/followers","following_url":"https://api.github.com/users/gtracy/following{/other_user}","gists_url":"https://api.github.com/users/gtracy/gists{/gist_id}","starred_url":"https://api.github.com/users/gtracy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gtracy/subscriptions","organizations_url":"https://api.github.com/users/gtracy/orgs","repos_url":"https://api.github.com/users/gtracy/repos","events_url":"https://api.github.com/users/gtracy/events{/privacy}","received_events_url":"https://api.github.com/users/gtracy/received_events","type":"User","site_admin":false},"body":"@danschumann that's a nice way to make this feel conversational. ","created_at":"2016-08-09T23:44:21Z","updated_at":"2016-08-09T23:44:21Z","html_url":"https://github.com/gtracy/slack-stuart/pull/30#discussion_r74164640","pull_request_url":"https://api.github.com/repos/gtracy/slack-stuart/pulls/30","_links":{"self":{"href":"https://api.github.com/repos/gtracy/slack-stuart/pulls/comments/74164640"},"html":{"href":"https://github.com/gtracy/slack-stuart/pull/30#discussion_r74164640"},"pull_request":{"href":"https://api.github.com/repos/gtracy/slack-stuart/pulls/30"}}},"pull_request":{"url":"https://api.github.com/repos/gtracy/slack-stuart/pulls/30","id":80190846,"html_url":"https://github.com/gtracy/slack-stuart/pull/30","diff_url":"https://github.com/gtracy/slack-stuart/pull/30.diff","patch_url":"https://github.com/gtracy/slack-stuart/pull/30.patch","issue_url":"https://api.github.com/repos/gtracy/slack-stuart/issues/30","number":30,"state":"open","locked":false,"title":"Add poker plugin","user":{"login":"danschumann","id":1516973,"avatar_url":"https://avatars.githubusercontent.com/u/1516973?v=3","gravatar_id":"","url":"https://api.github.com/users/danschumann","html_url":"https://github.com/danschumann","followers_url":"https://api.github.com/users/danschumann/followers","following_url":"https://api.github.com/users/danschumann/following{/other_user}","gists_url":"https://api.github.com/users/danschumann/gists{/gist_id}","starred_url":"https://api.github.com/users/danschumann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danschumann/subscriptions","organizations_url":"https://api.github.com/users/danschumann/orgs","repos_url":"https://api.github.com/users/danschumann/repos","events_url":"https://api.github.com/users/danschumann/events{/privacy}","received_events_url":"https://api.github.com/users/danschumann/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-08-05T05:27:42Z","updated_at":"2016-08-09T23:44:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"162bdd60ef85cf927cad1393845bc05ec4807088","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/gtracy/slack-stuart/pulls/30/commits","review_comments_url":"https://api.github.com/repos/gtracy/slack-stuart/pulls/30/comments","review_comment_url":"https://api.github.com/repos/gtracy/slack-stuart/pulls/comments{/number}","comments_url":"https://api.github.com/repos/gtracy/slack-stuart/issues/30/comments","statuses_url":"https://api.github.com/repos/gtracy/slack-stuart/statuses/4561076ddc53ede4011062699224cfc67c7eb8ab","head":{"label":"danschumann:add-poker-plugin","ref":"add-poker-plugin","sha":"4561076ddc53ede4011062699224cfc67c7eb8ab","user":{"login":"danschumann","id":1516973,"avatar_url":"https://avatars.githubusercontent.com/u/1516973?v=3","gravatar_id":"","url":"https://api.github.com/users/danschumann","html_url":"https://github.com/danschumann","followers_url":"https://api.github.com/users/danschumann/followers","following_url":"https://api.github.com/users/danschumann/following{/other_user}","gists_url":"https://api.github.com/users/danschumann/gists{/gist_id}","starred_url":"https://api.github.com/users/danschumann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danschumann/subscriptions","organizations_url":"https://api.github.com/users/danschumann/orgs","repos_url":"https://api.github.com/users/danschumann/repos","events_url":"https://api.github.com/users/danschumann/events{/privacy}","received_events_url":"https://api.github.com/users/danschumann/received_events","type":"User","site_admin":false},"repo":{"id":64990494,"name":"slack-stuart","full_name":"danschumann/slack-stuart","owner":{"login":"danschumann","id":1516973,"avatar_url":"https://avatars.githubusercontent.com/u/1516973?v=3","gravatar_id":"","url":"https://api.github.com/users/danschumann","html_url":"https://github.com/danschumann","followers_url":"https://api.github.com/users/danschumann/followers","following_url":"https://api.github.com/users/danschumann/following{/other_user}","gists_url":"https://api.github.com/users/danschumann/gists{/gist_id}","starred_url":"https://api.github.com/users/danschumann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danschumann/subscriptions","organizations_url":"https://api.github.com/users/danschumann/orgs","repos_url":"https://api.github.com/users/danschumann/repos","events_url":"https://api.github.com/users/danschumann/events{/privacy}","received_events_url":"https://api.github.com/users/danschumann/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/danschumann/slack-stuart","description":"An extendable bot server for Slack","fork":true,"url":"https://api.github.com/repos/danschumann/slack-stuart","forks_url":"https://api.github.com/repos/danschumann/slack-stuart/forks","keys_url":"https://api.github.com/repos/danschumann/slack-stuart/keys{/key_id}","collaborators_url":"https://api.github.com/repos/danschumann/slack-stuart/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/danschumann/slack-stuart/teams","hooks_url":"https://api.github.com/repos/danschumann/slack-stuart/hooks","issue_events_url":"https://api.github.com/repos/danschumann/slack-stuart/issues/events{/number}","events_url":"https://api.github.com/repos/danschumann/slack-stuart/events","assignees_url":"https://api.github.com/repos/danschumann/slack-stuart/assignees{/user}","branches_url":"https://api.github.com/repos/danschumann/slack-stuart/branches{/branch}","tags_url":"https://api.github.com/repos/danschumann/slack-stuart/tags","blobs_url":"https://api.github.com/repos/danschumann/slack-stuart/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/danschumann/slack-stuart/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/danschumann/slack-stuart/git/refs{/sha}","trees_url":"https://api.github.com/repos/danschumann/slack-stuart/git/trees{/sha}","statuses_url":"https://api.github.com/repos/danschumann/slack-stuart/statuses/{sha}","languages_url":"https://api.github.com/repos/danschumann/slack-stuart/languages","stargazers_url":"https://api.github.com/repos/danschumann/slack-stuart/stargazers","contributors_url":"https://api.github.com/repos/danschumann/slack-stuart/contributors","subscribers_url":"https://api.github.com/repos/danschumann/slack-stuart/subscribers","subscription_url":"https://api.github.com/repos/danschumann/slack-stuart/subscription","commits_url":"https://api.github.com/repos/danschumann/slack-stuart/commits{/sha}","git_commits_url":"https://api.github.com/repos/danschumann/slack-stuart/git/commits{/sha}","comments_url":"https://api.github.com/repos/danschumann/slack-stuart/comments{/number}","issue_comment_url":"https://api.github.com/repos/danschumann/slack-stuart/issues/comments{/number}","contents_url":"https://api.github.com/repos/danschumann/slack-stuart/contents/{+path}","compare_url":"https://api.github.com/repos/danschumann/slack-stuart/compare/{base}...{head}","merges_url":"https://api.github.com/repos/danschumann/slack-stuart/merges","archive_url":"https://api.github.com/repos/danschumann/slack-stuart/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/danschumann/slack-stuart/downloads","issues_url":"https://api.github.com/repos/danschumann/slack-stuart/issues{/number}","pulls_url":"https://api.github.com/repos/danschumann/slack-stuart/pulls{/number}","milestones_url":"https://api.github.com/repos/danschumann/slack-stuart/milestones{/number}","notifications_url":"https://api.github.com/repos/danschumann/slack-stuart/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/danschumann/slack-stuart/labels{/name}","releases_url":"https://api.github.com/repos/danschumann/slack-stuart/releases{/id}","deployments_url":"https://api.github.com/repos/danschumann/slack-stuart/deployments","created_at":"2016-08-05T05:16:59Z","updated_at":"2016-08-05T05:17:00Z","pushed_at":"2016-08-05T05:28:36Z","git_url":"git://github.com/danschumann/slack-stuart.git","ssh_url":"git@github.com:danschumann/slack-stuart.git","clone_url":"https://github.com/danschumann/slack-stuart.git","svn_url":"https://github.com/danschumann/slack-stuart","homepage":"","size":67,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"gtracy:master","ref":"master","sha":"7e2a1369aba462393ab85c8d12e10743d3003e2b","user":{"login":"gtracy","id":169253,"avatar_url":"https://avatars.githubusercontent.com/u/169253?v=3","gravatar_id":"","url":"https://api.github.com/users/gtracy","html_url":"https://github.com/gtracy","followers_url":"https://api.github.com/users/gtracy/followers","following_url":"https://api.github.com/users/gtracy/following{/other_user}","gists_url":"https://api.github.com/users/gtracy/gists{/gist_id}","starred_url":"https://api.github.com/users/gtracy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gtracy/subscriptions","organizations_url":"https://api.github.com/users/gtracy/orgs","repos_url":"https://api.github.com/users/gtracy/repos","events_url":"https://api.github.com/users/gtracy/events{/privacy}","received_events_url":"https://api.github.com/users/gtracy/received_events","type":"User","site_admin":false},"repo":{"id":19888288,"name":"slack-stuart","full_name":"gtracy/slack-stuart","owner":{"login":"gtracy","id":169253,"avatar_url":"https://avatars.githubusercontent.com/u/169253?v=3","gravatar_id":"","url":"https://api.github.com/users/gtracy","html_url":"https://github.com/gtracy","followers_url":"https://api.github.com/users/gtracy/followers","following_url":"https://api.github.com/users/gtracy/following{/other_user}","gists_url":"https://api.github.com/users/gtracy/gists{/gist_id}","starred_url":"https://api.github.com/users/gtracy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gtracy/subscriptions","organizations_url":"https://api.github.com/users/gtracy/orgs","repos_url":"https://api.github.com/users/gtracy/repos","events_url":"https://api.github.com/users/gtracy/events{/privacy}","received_events_url":"https://api.github.com/users/gtracy/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/gtracy/slack-stuart","description":"An extendable bot server for Slack","fork":false,"url":"https://api.github.com/repos/gtracy/slack-stuart","forks_url":"https://api.github.com/repos/gtracy/slack-stuart/forks","keys_url":"https://api.github.com/repos/gtracy/slack-stuart/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gtracy/slack-stuart/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gtracy/slack-stuart/teams","hooks_url":"https://api.github.com/repos/gtracy/slack-stuart/hooks","issue_events_url":"https://api.github.com/repos/gtracy/slack-stuart/issues/events{/number}","events_url":"https://api.github.com/repos/gtracy/slack-stuart/events","assignees_url":"https://api.github.com/repos/gtracy/slack-stuart/assignees{/user}","branches_url":"https://api.github.com/repos/gtracy/slack-stuart/branches{/branch}","tags_url":"https://api.github.com/repos/gtracy/slack-stuart/tags","blobs_url":"https://api.github.com/repos/gtracy/slack-stuart/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gtracy/slack-stuart/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gtracy/slack-stuart/git/refs{/sha}","trees_url":"https://api.github.com/repos/gtracy/slack-stuart/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gtracy/slack-stuart/statuses/{sha}","languages_url":"https://api.github.com/repos/gtracy/slack-stuart/languages","stargazers_url":"https://api.github.com/repos/gtracy/slack-stuart/stargazers","contributors_url":"https://api.github.com/repos/gtracy/slack-stuart/contributors","subscribers_url":"https://api.github.com/repos/gtracy/slack-stuart/subscribers","subscription_url":"https://api.github.com/repos/gtracy/slack-stuart/subscription","commits_url":"https://api.github.com/repos/gtracy/slack-stuart/commits{/sha}","git_commits_url":"https://api.github.com/repos/gtracy/slack-stuart/git/commits{/sha}","comments_url":"https://api.github.com/repos/gtracy/slack-stuart/comments{/number}","issue_comment_url":"https://api.github.com/repos/gtracy/slack-stuart/issues/comments{/number}","contents_url":"https://api.github.com/repos/gtracy/slack-stuart/contents/{+path}","compare_url":"https://api.github.com/repos/gtracy/slack-stuart/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gtracy/slack-stuart/merges","archive_url":"https://api.github.com/repos/gtracy/slack-stuart/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gtracy/slack-stuart/downloads","issues_url":"https://api.github.com/repos/gtracy/slack-stuart/issues{/number}","pulls_url":"https://api.github.com/repos/gtracy/slack-stuart/pulls{/number}","milestones_url":"https://api.github.com/repos/gtracy/slack-stuart/milestones{/number}","notifications_url":"https://api.github.com/repos/gtracy/slack-stuart/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gtracy/slack-stuart/labels{/name}","releases_url":"https://api.github.com/repos/gtracy/slack-stuart/releases{/id}","deployments_url":"https://api.github.com/repos/gtracy/slack-stuart/deployments","created_at":"2014-05-17T14:21:02Z","updated_at":"2016-08-09T21:24:25Z","pushed_at":"2016-08-05T05:27:42Z","git_url":"git://github.com/gtracy/slack-stuart.git","ssh_url":"git@github.com:gtracy/slack-stuart.git","clone_url":"https://github.com/gtracy/slack-stuart.git","svn_url":"https://github.com/gtracy/slack-stuart","homepage":"","size":66,"stargazers_count":92,"watchers_count":92,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":27,"mirror_url":null,"open_issues_count":1,"forks":27,"open_issues":1,"watchers":92,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/gtracy/slack-stuart/pulls/30"},"html":{"href":"https://github.com/gtracy/slack-stuart/pull/30"},"issue":{"href":"https://api.github.com/repos/gtracy/slack-stuart/issues/30"},"comments":{"href":"https://api.github.com/repos/gtracy/slack-stuart/issues/30/comments"},"review_comments":{"href":"https://api.github.com/repos/gtracy/slack-stuart/pulls/30/comments"},"review_comment":{"href":"https://api.github.com/repos/gtracy/slack-stuart/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/gtracy/slack-stuart/pulls/30/commits"},"statuses":{"href":"https://api.github.com/repos/gtracy/slack-stuart/statuses/4561076ddc53ede4011062699224cfc67c7eb8ab"}}}}
{"actor":{"display_login":"gtracy"}}