Improved coffee module

This commit is contained in:
Gogs 2016-03-15 18:05:55 +01:00
parent 813939cddc
commit 3fcf313799
1 changed files with 13 additions and 35 deletions

View File

@ -8,7 +8,6 @@ package modules
import ( import (
"fmt" "fmt"
"log"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -31,6 +30,7 @@ var (
func init() { func init() {
MsgHandlers["coffee"] = coffeeHandleMessage MsgHandlers["coffee"] = coffeeHandleMessage
r.names = make(map[string]bool)
xlog.Info("Coffee module initialized") xlog.Info("Coffee module initialized")
} }
@ -54,7 +54,6 @@ func coffeeHandleMessage(m *irc.Message) {
func (r *Receivers) addReceivers(newNames []string) { func (r *Receivers) addReceivers(newNames []string) {
r.mux.Lock() r.mux.Lock()
if r.running { if r.running {
SayCh <- "*\nEs wird gerade Kaffee zubereitet."
if newNames != nil { if newNames != nil {
r.addNames(newNames) r.addNames(newNames)
} }
@ -62,34 +61,40 @@ func (r *Receivers) addReceivers(newNames []string) {
if newNames != nil { if newNames != nil {
r.addNames(newNames) r.addNames(newNames)
} }
go makeCoffee() go r.makeCoffee()
} }
r.mux.Unlock() r.mux.Unlock()
} }
func (r *Receivers) makeCoffee() { func (r *Receivers) makeCoffee() {
r.mux.Lock()
r.running = true
r.mux.Unlock()
printAction("setzt Kaffee auf.") printAction("setzt Kaffee auf.")
time.Sleep(time.Second * 30) time.Sleep(time.Second * 30)
printAction("stellt eine frische Kanne Kaffee in den Raum.") printAction("stellt eine frische Kanne Kaffee in den Raum.")
r.mux.Lock() r.mux.Lock()
if len(r.names) != 0 { if len(r.names) != 0 {
var users string var users string
var count int = 0
for i, _ := range r.names { for i, _ := range r.names {
count++
users += i users += i
/*if i < len(rec)-2 { if count < len(r.names)-1 {
users += ", " users += ", "
} else if i == len(rec)-2 { } else if count == len(r.names)-1 {
users += " und " users += " und "
}*/ }
} }
SayCh <- "*\ngibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee." printAction("gibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee.")
} }
r.mux.Unlock()
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
SayCh <- "*\nProst! (c)" SayCh <- "*\nProst! (c)"
r.mux.Lock()
r.names = make(map[string]bool) r.names = make(map[string]bool)
r.running = false r.running = false
r.mux.Unlock() r.mux.Unlock()
} }
@ -104,30 +109,3 @@ func printAction(s string) {
SayCh <- fmt.Sprintf("%s\n%s", "*", msg) 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)"
// }
//}