+;;; krakenbot-thegame.el -- misc helpers around the !game command
+;; -*- coding: utf-8 -*-
+;; Copyright (C) 2013 Fabrice Gabolde.
+;; Author: Fabrice Gabolde <email@example.com>
+;; Maintainer: Fabrice Gabolde <firstname.lastname@example.org>
+;; 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
+;; 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.
+(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)
+ (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)))))
+(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)))
+ (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))))
+(add-to-list 'kbot-commands '("games" kbot-command-games))
+;;; krakenbot-thegame.el ends here