Fabrice Gabolde avatar Fabrice Gabolde committed 646e301

Misc. helpers around !game.

Comments (0)

Files changed (1)

krakenbot-thegame.el

+;;; krakenbot-thegame.el -- misc helpers around the !game command
+
+;; -*- coding: utf-8 -*-
+
+;; Copyright (C) 2013 Fabrice Gabolde.
+
+;; Author: Fabrice Gabolde <fabrice.gabolde@gmail.com>
+;; Maintainer: Fabrice Gabolde <fabrice.gabolde@gmail.com>
+;; Created: 29 Mar 2013
+;; Version: 0.1
+;; Keywords: IRC, bot
+
+;; This file is part of Krakenbot.
+
+;; Krakenbot is free software: you can redistribute it and/or modify it under
+;; the terms of the GNU General Public License as published by the Free Software
+;; Foundation, either version 3 of the License, or (at your option) any later
+;; version.
+
+;; Krakenbot is distributed in the hope that it will be useful, but WITHOUT ANY
+;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+;; A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License along with
+;; Krakenbot.  If not, see <http://www.gnu.org/licenses/>.
+
+;; See krakenbot.el for requirements and installation instructions.
+
+;;; Code:
+
+(require 'erc-log)
+
+(defun kbot-thegame-find-all-losses-in-channel (channel)
+  "Find all loss announcements in an ERC log file."
+  (with-current-buffer (erc-get-buffer channel)
+    (let ((mynick (erc-current-nick))
+	  (results (make-hash-table :test 'equal))
+          (users (erc-get-channel-nickname-alist)))
+      (with-current-buffer (find-file-noselect (erc-current-logfile) t)
+        (save-excursion
+          (goto-char (point-min))
+          (while (re-search-forward (concat "<" mynick "> \\([^:]+\\): acknowledging your loss") nil t 1)
+              (let ((loser (match-string-no-properties 1)))
+                (if (assoc loser users)
+                    (puthash loser (+ (gethash loser results 0) 1) results)))))
+	(kill-buffer))
+      results)))
+
+(defun kbot-command-games (kbot-channel-name kbot-sender &optional kbot-args)
+  "Print the leaderboard of current Game losses on this channel."
+  (let ((leaderboard (kbot-thegame-find-all-losses-in-channel kbot-channel-name)))
+    (if (hash-table-count leaderboard)
+	(concat kbot-sender ": " (mapconcat (quote identity) (kbot-format-loss leaderboard) ", "))
+      (concat kbot-sender ": no losses announced for current channel members"))))
+
+(defun kbot-format-loss (losers-hash)
+  "Format the losers hash properly for output."
+  (let ((all-nicks (quote nil)))
+    (maphash (lambda (nick losses) (setq all-nicks (cons nick all-nicks)))
+	     losers-hash)
+    (let ((sorted-nicks (sort all-nicks (lambda (a b) (> (gethash a losers-hash) (gethash b losers-hash))))))
+      (mapcar (lambda (nick) (concat nick ": " (number-to-string (gethash nick losers-hash))))
+	      sorted-nicks))))
+
+(add-to-list 'kbot-commands '("games" kbot-command-games))
+
+(provide 'krakenbot-thegame)
+
+;;; krakenbot-thegame.el ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.