#!/usr/bin/ruby -w
# PizzaBot - IRC bot to coordinate mass pizza orders
#
# Copyright (C) 2007  Urs Ganse <urs@nerd2nerd.org>
# Some changes 2007 by Petschge <mail@petschge.de>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

$orders = Hash.new
$day = Time.now.yday

$startsingle = "" 
$stopsingle = ":"
$startall = "/privmsg "
$stopall = ""

$confirmations = ["Hmkay", "Jupp", "Geht klar", "Aber gerne doch", "Hab ich", "Jawohl!"]
$changes = ["Ok, dann halt was anders", "Na gut, dann halt das", "Hab ich geändert", "Jawohl!" ]

$stdin.each_line do |line|

    if Time.now.yday != $day
        $orders = Hash.new
        $day = Time.now.yday
        $stderr.print "------------------------------------------------\n"
    end

    if /[^>]+<([^>]+)> !help/.match(line)
        print "#{$startsingle}#{$1}#{$stopsingle} !help zeigt diese Hilfe. !pizza <details> um eine Pizza zu bestellen (ersetzt die alte Bestellung). !nopizza zum Stornieren. !show zeigt alle heutigen Bestellungen.\n"
        $stdout.flush
    end

    if /[^>]+<([^>]+)> !show/.match(line)
        if $orders.size == 0
            print "#{$startall}#{$1}#{$stopall} Heute hat noch keiner was bestellt!\n"
        else
            print "#{$startall}#{$1}#{$stopall} Die Bestellungen soweit:\n"
            $orders.each_key do |person|
                print "#{$startall}#{$1}#{$stopall} * #{person}: #{$orders[person]}\n"
            end
        end
        $stdout.flush
        next
    end

    if /[^>]+<([^>]+)> !snow/.match(line)
	print "let it sno, let it snow, let it SNOW\n"
    end

    if /[^>]+<([^>]+)> !dump/.match(line)
        if $orders.size == 0
            print "#{$startsingle}#{$1}#{$stopsingle} Heute hat noch keiner was bestellt!\n"
        else
            ordercount = $orders.size
            pizzacount = (ordercount + 2) / 3
            beitrag = 17.00 * pizzacount / ordercount
	    stueckchen = 24 * pizzacount / ordercount
            print "#{$startall}#{$1}#{$stopall} Bisher #{ordercount} Bestellungen => #{pizzacount} Pizzae\n"
            print "#{$startall}#{$1}#{$stopall} Das macht %5.2f Euro pro Nase\n"%[beitrag]
            print "#{$startall}#{$1}#{$stopall} Gibt dann %5.2f Stuecke pro Nase\n"%[stueckchen]
        end
        $stdout.flush
        next
    end

    if /[^>]+<([^>]+)> !nopizza/.match(line)
        $orders.delete($1)
        print "#{$startsingle}#{$1}#{$stopsingle} Gut, dann halt nicht.\n"
        $stdout.flush
    end

    results = /[^>]+<([^>]+)> !pizza (.*)?/.match(line) 
    if results
        nick = results[1]
        details = results[2] || ""

	if $orders[nick]
            print "#{$startsingle}#{nick}#{$stopsingle} #{$changes[rand($changes.size)]}"
	else
            print "#{$startsingle}#{nick}#{$stopsingle} #{$confirmations[rand($confirmations.size)]}"
	end
        print "\n"
        $stdout.flush
        $orders[nick] = "%s (%02i:%02i)"%[details,Time.now.hour,Time.now.min]
        $stderr.print "#{nick} bestellt: #{$orders[nick]}\n"
    end
end
