flokati/modules/coffee.go

134 lines
2.6 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"
"log"
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-12 12:09:25 +00:00
"code.dnix.de/an/xlog"
2016-02-15 23:17:42 +00:00
"github.com/sorcix/irc"
2016-03-15 16:31:31 +00:00
"github.com/sorcix/irc/ctcp"
)
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() {
MsgHandlers["coffee"] = coffeeHandleMessage
xlog.Info("Coffee module initialized")
2016-02-15 23:17:42 +00:00
}
func coffeeHandleMessage(m *irc.Message) {
tok := strings.Split(strings.Trim(m.Trailing, " "), " ")
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:
2016-03-15 16:31:31 +00:00
go r.addReceivers(nil)
2016-02-15 23:17:42 +00:00
default:
2016-03-15 16:31:31 +00:00
go r.addReceivers(tok[1:])
2016-02-15 23:17:42 +00:00
}
default:
}
}
2016-03-15 16:31:31 +00:00
func (r *Receivers) addReceivers(newNames []string) {
r.mux.Lock()
if r.running {
SayCh <- "*\nEs wird gerade Kaffee zubereitet."
if newNames != nil {
r.addNames(newNames)
}
} else {
if newNames != nil {
r.addNames(newNames)
}
go makeCoffee()
}
r.mux.Unlock()
}
func (r *Receivers) makeCoffee() {
printAction("setzt Kaffee auf.")
time.Sleep(time.Second * 30)
printAction("stellt eine frische Kanne Kaffee in den Raum.")
r.mux.Lock()
if len(r.names) != 0 {
2016-02-15 23:17:42 +00:00
var users string
2016-03-15 16:31:31 +00:00
for i, _ := range r.names {
users += i
/*if i < len(rec)-2 {
2016-02-15 23:17:42 +00:00
users += ", "
} else if i == len(rec)-2 {
users += " und "
2016-03-15 16:31:31 +00:00
}*/
2016-02-15 23:17:42 +00:00
}
SayCh <- "*\ngibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee."
2016-02-15 23:17:42 +00:00
}
time.Sleep(10 * time.Second)
SayCh <- "*\nProst! (c)"
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 {
r.names[v] = true
}
}
func printAction(s string) {
msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf(s))
SayCh <- fmt.Sprintf("%s\n%s", "*", msg)
}
//func (r *Receivers) coffeeMake(rec []string) {
// if *running {
// // TODO: add new recipients to map
// // no if rec == nil add the sender, else add the sender and rec
// SayCh <- "*\nEs wird gerade Kaffee zubereitet."
// } else {
// printAction("setzt Kaffee auf.")
// time.Sleep(30 * time.Second)
// printAction("stellt eine frische Kanne Kaffee in den Raum.")
// if rec != nil {
// var users string
// for i, v := range rec {
// users += v
// if i < len(rec)-2 {
// users += ", "
// } else if i == len(rec)-2 {
// users += " und "
// }
// }
// SayCh <- "*\ngibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee."
// }
// time.Sleep(10 * time.Second)
// SayCh <- "*\nProst! (c)"
// }
//}