flokati/modules/coffee.go

105 lines
1.9 KiB
Go
Raw Normal View History

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