diff --git a/main.go b/main.go index 8dfcb1f..d371ed1 100644 --- a/main.go +++ b/main.go @@ -83,6 +83,7 @@ func main() { } }() + go Ping(bot) RegisterHandlers(bot) bot.HandleLoop() xlog.Info("Exiting") @@ -95,6 +96,13 @@ func RegisterHandlers(bot *ircx.Bot) { bot.HandleFunc(irc.PRIVMSG, PrivmsgHandler) } +func Ping(bot *ircx.Bot) { + for { + time.Sleep(1 * time.Minute) + bot.Sender.Send(&irc.Message{Command: irc.PING}) + } +} + func ConnectHandler(s ircx.Sender, m *irc.Message) { if *nspass != "" { xlog.Info("Authenticating with NickServ: %v, %v", *name, *nspass) diff --git a/modules/modules.go b/modules/modules.go index c7fc4cc..cdc02c1 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -8,6 +8,7 @@ package modules import ( "strings" + "time" "github.com/sorcix/irc" ) @@ -20,6 +21,7 @@ var ( ) func Init(ch chan string, mods, params string) { + time.Sleep(5 * time.Second) SayCh = ch for mod, _ := range MsgFuncs { if !contains(strings.Split(mods, ","), mod) { diff --git a/modules/quiz.go b/modules/quiz.go index 10aa87a..9d2c6b7 100644 --- a/modules/quiz.go +++ b/modules/quiz.go @@ -6,6 +6,7 @@ import ( "bufio" "flokatirc/util" "fmt" + "math/rand" "os" "regexp" "strings" @@ -29,7 +30,8 @@ var ( func init() { MsgFuncs["quiz"] = quizHandleMessage RunFuncs["quiz"] = quiz - quizQuestions = quizLoadQuestions("./modules/quiz/questions.de") + quizQuestions = quizLoadQuestions("./modules/quiz/questions.txt") + rand.Seed(time.Now().Unix()) } func quizHandleMessage(m *irc.Message) { @@ -66,23 +68,26 @@ func quiz() { } func quizRun() { + SayCh <- fmt.Sprintf("%s\nQuiz gestartet.", "*") for { if !quizAskQuestion() { + SayCh <- fmt.Sprintf("%s\nQuiz beendet.", "*") return } } } func quizAskQuestion() bool { - q := quizQuestions[util.Random(0, len(quizQuestions))] - SayCh <- fmt.Sprintf("%s\n%s (Kategorie: %s)", "*", q.question, q.category) + q := quizQuestions[rand.Intn(len(quizQuestions))] + SayCh <- fmt.Sprintf("%s\nEine Frage aus der Kategorie \"%s\":", "*", q.category) + SayCh <- fmt.Sprintf("%s\n>>> %s <<<", "*", q.question) m, solved, cont := quizWaitForAnswer(q) if solved { - SayCh <- fmt.Sprintf("%s\n%s hat die Frage beantwortet.", "*", m.Prefix) + mm := m + m = mm } else { - SayCh <- fmt.Sprintf("%s\nNiemand konnte die Frage beantworten.", "*") SayCh <- fmt.Sprintf("%s\nDie richtige Antwort wäre gewesen:", "*") - SayCh <- fmt.Sprintf("%s\n%s", "*", q.answer) + SayCh <- fmt.Sprintf("%s\n%s [%s]", "*", q.answer, q.regexp) } time.Sleep(5 * time.Second) return cont @@ -90,16 +95,26 @@ func quizAskQuestion() bool { func quizWaitForAnswer(q quizQuestion) (*irc.Message, bool, bool) { i := 0 - t := 0 + haveAnswer := false + timer := time.Now().Unix() for { select { case m := <-quizAnswerCh: + haveAnswer = true + points := 10 - ((time.Now().Unix() - timer) / 10) + if points < 1 { + points = 1 + } re, err := regexp.Compile(q.regexp) if err != nil { xlog.Error(err.Error()) return nil, false, false } if re.MatchString(strings.ToLower(util.ReplaceUmlauts(m.Trailing))) { + bold := byte(0x02) + solver := strings.Split(m.Prefix.String(), "!")[0] + solver = string(bold) + solver + string(bold) + SayCh <- fmt.Sprintf("%s\n%s hat die Frage korrekt beantwortet und erhält dafür %d Punkte.", "*", solver, points) return m, true, true } break @@ -108,24 +123,45 @@ func quizWaitForAnswer(q quizQuestion) (*irc.Message, bool, bool) { return nil, false, false } break - default: - time.Sleep(1 * time.Millisecond) - t++ - if t > 20000 { - t = 0 - i++ - if i > 3 { + case <-time.After(15 * time.Second): + i++ + if i > 3 { + if haveAnswer { + SayCh <- fmt.Sprintf("%s\nNiemand konnte die Frage korrekt beantworten.", "*") return nil, false, true } else { - quizGiveHint(q) + SayCh <- fmt.Sprintf("%s\nNiemand hat auf die Frage geantwortet.", "*") + return nil, false, false } + } else { + quizGiveHint(q, i) } } } } -func quizGiveHint(q quizQuestion) { - //SayCh <- fmt.Sprintf("%s\nHint: %s | %s", "*", q.answer, q.regexp) +func quizGiveHint(q quizQuestion, n int) { + var hint string + haveHint := false + for { + hint = "" + for i := 0; i < len(q.answer); i++ { + if string(q.answer[i]) == " " { + hint += " " + } else { + if rand.Intn(10) < 2 { + haveHint = true + hint += string(q.answer[i]) + } else { + hint += "-" + } + } + } + if haveHint { + break + } + } + SayCh <- fmt.Sprintf("%s\nTipp: %s", "*", hint) } func quizLoadQuestions(path string) []quizQuestion { @@ -147,7 +183,7 @@ func quizLoadQuestions(path string) []quizQuestion { q = quizQuestion{"", "", "", ""} } } else { - tok := strings.Split(line, ": ") + tok := strings.Split(line, ":: ") switch tok[0] { case "Category": q.category = tok[1] @@ -157,18 +193,17 @@ func quizLoadQuestions(path string) []quizQuestion { break case "Answer": q.answer = strings.Replace(tok[1], "#", "", -1) - q.answer = util.ReplaceUmlauts(q.answer) - regexp := strings.Split(tok[1], "#") if q.regexp == "" { + regexp := strings.Split(strings.ToLower(util.ReplaceUmlauts(tok[1])), "#") if len(regexp) > 1 { - q.regexp = strings.ToLower(regexp[1]) + q.regexp = regexp[1] } else { - q.regexp = strings.ToLower(regexp[0]) + q.regexp = regexp[0] } } break case "Regexp": - q.regexp = util.ReplaceUmlauts(tok[1]) + q.regexp = util.ReplaceUmlauts(strings.ToLower(tok[1])) break } } diff --git a/modules/stoll.go b/modules/stoll.go index 8b2a840..32dd122 100644 --- a/modules/stoll.go +++ b/modules/stoll.go @@ -3,9 +3,10 @@ package modules import ( - "flokatirc/util" "fmt" + "math/rand" "strings" + "time" "github.com/sorcix/irc" ) @@ -521,6 +522,7 @@ var quotes = [][]string{ func init() { MsgFuncs["stoll"] = stollHandleMessage + rand.Seed(time.Now().Unix()) } func stollHandleMessage(m *irc.Message) { @@ -531,7 +533,7 @@ func stollHandleMessage(m *irc.Message) { if tok[0] == "!stoll" { line := "" for i := 0; i < 3; i++ { - line += quotes[i][util.Random(0, len(quotes[i]))] + line += quotes[i][rand.Intn(len(quotes[i]))] line += " " } line += "[Dr. Axel Stoll, promovierter Naturwissenschaftler]" diff --git a/util/util.go b/util/util.go index 90c5357..e39e041 100644 --- a/util/util.go +++ b/util/util.go @@ -4,10 +4,8 @@ package util import ( "bytes" - "math/rand" "strconv" "strings" - "time" ) func ToInt(v interface{}) int { @@ -49,11 +47,6 @@ func NumberToString(n int, sep rune) string { return buf.String() } -func Random(min, max int) int { - rand.Seed(time.Now().Unix()) - return rand.Intn(max-min) + min -} - func ReplaceUmlauts(s string) string { ret := strings.Replace(s, "Ä", "Ae", -1) ret = strings.Replace(ret, "Ö", "Oe", -1)