105 lines
1.9 KiB
Go
105 lines
1.9 KiB
Go
// vi:ts=4:sts=4:sw=4:noet:tw=72
|
|
//
|
|
// flokatirc coffee module
|
|
//
|
|
// Copyright (c) 2016 Daniel Aberger <da@ixab.de>
|
|
|
|
package modules
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Receivers struct {
|
|
names map[string]bool
|
|
running bool
|
|
mux sync.Mutex
|
|
}
|
|
|
|
var (
|
|
r Receivers
|
|
)
|
|
|
|
func init() {
|
|
MsgFuncs["coffee"] = coffeeHandleMessage
|
|
r.names = make(map[string]bool)
|
|
}
|
|
|
|
func coffeeHandleMessage(m *Message) {
|
|
tok := strings.Split(strings.Trim(m.Text, " "), " ")
|
|
if len(tok) < 1 {
|
|
return
|
|
}
|
|
switch strings.ToLower(tok[0]) {
|
|
case "!kaffee":
|
|
switch len(tok) {
|
|
case 1:
|
|
go r.addReceivers(nil, m.Channel)
|
|
default:
|
|
go r.addReceivers(tok[1:], m.Channel)
|
|
}
|
|
default:
|
|
}
|
|
}
|
|
|
|
func (r *Receivers) addReceivers(newNames []string, channel string) {
|
|
r.mux.Lock()
|
|
if r.running {
|
|
if newNames != nil {
|
|
r.addNames(newNames)
|
|
}
|
|
} else {
|
|
if newNames != nil {
|
|
r.addNames(newNames)
|
|
}
|
|
go r.makeCoffee(channel)
|
|
}
|
|
r.mux.Unlock()
|
|
}
|
|
|
|
func (r *Receivers) makeCoffee(channel string) {
|
|
r.mux.Lock()
|
|
r.running = true
|
|
r.mux.Unlock()
|
|
printAction(channel, "setzt Kaffee auf.")
|
|
time.Sleep(time.Second * 30)
|
|
printAction(channel, "stellt eine frische Kanne Kaffee in den Raum.")
|
|
r.mux.Lock()
|
|
if len(r.names) != 0 {
|
|
var users string
|
|
var count int = 0
|
|
for i, _ := range r.names {
|
|
count++
|
|
users += i
|
|
if count < len(r.names)-1 {
|
|
users += ", "
|
|
} else if count == len(r.names)-1 {
|
|
users += " und "
|
|
}
|
|
}
|
|
printAction(channel, "gibt "+users+" einen frischen, richtig schwarzen, richtig leckeren Kaffee.")
|
|
}
|
|
r.mux.Unlock()
|
|
time.Sleep(10 * time.Second)
|
|
SayCh <- channel + "\nProst! (c)"
|
|
|
|
r.mux.Lock()
|
|
r.names = make(map[string]bool)
|
|
r.running = false
|
|
r.mux.Unlock()
|
|
}
|
|
|
|
func (r *Receivers) addNames(newNames []string) {
|
|
for _, v := range newNames {
|
|
r.names[strings.ToLower(v)] = true
|
|
}
|
|
}
|
|
|
|
func printAction(channel, s string) {
|
|
//msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf(s))
|
|
SayCh <- fmt.Sprintf("%s\n%s", channel, s)
|
|
}
|