Quiz module: Earn points for solved questions.

This commit is contained in:
Andreas Neue 2016-03-20 23:01:04 +01:00
parent cc4050e87e
commit 41164153c8
5 changed files with 72 additions and 32 deletions

View File

@ -83,6 +83,7 @@ func main() {
} }
}() }()
go Ping(bot)
RegisterHandlers(bot) RegisterHandlers(bot)
bot.HandleLoop() bot.HandleLoop()
xlog.Info("Exiting") xlog.Info("Exiting")
@ -95,6 +96,13 @@ func RegisterHandlers(bot *ircx.Bot) {
bot.HandleFunc(irc.PRIVMSG, PrivmsgHandler) 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) { func ConnectHandler(s ircx.Sender, m *irc.Message) {
if *nspass != "" { if *nspass != "" {
xlog.Info("Authenticating with NickServ: %v, %v", *name, *nspass) xlog.Info("Authenticating with NickServ: %v, %v", *name, *nspass)

View File

@ -8,6 +8,7 @@ package modules
import ( import (
"strings" "strings"
"time"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
@ -20,6 +21,7 @@ var (
) )
func Init(ch chan string, mods, params string) { func Init(ch chan string, mods, params string) {
time.Sleep(5 * time.Second)
SayCh = ch SayCh = ch
for mod, _ := range MsgFuncs { for mod, _ := range MsgFuncs {
if !contains(strings.Split(mods, ","), mod) { if !contains(strings.Split(mods, ","), mod) {

View File

@ -6,6 +6,7 @@ import (
"bufio" "bufio"
"flokatirc/util" "flokatirc/util"
"fmt" "fmt"
"math/rand"
"os" "os"
"regexp" "regexp"
"strings" "strings"
@ -29,7 +30,8 @@ var (
func init() { func init() {
MsgFuncs["quiz"] = quizHandleMessage MsgFuncs["quiz"] = quizHandleMessage
RunFuncs["quiz"] = quiz 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) { func quizHandleMessage(m *irc.Message) {
@ -66,23 +68,26 @@ func quiz() {
} }
func quizRun() { func quizRun() {
SayCh <- fmt.Sprintf("%s\nQuiz gestartet.", "*")
for { for {
if !quizAskQuestion() { if !quizAskQuestion() {
SayCh <- fmt.Sprintf("%s\nQuiz beendet.", "*")
return return
} }
} }
} }
func quizAskQuestion() bool { func quizAskQuestion() bool {
q := quizQuestions[util.Random(0, len(quizQuestions))] q := quizQuestions[rand.Intn(len(quizQuestions))]
SayCh <- fmt.Sprintf("%s\n%s (Kategorie: %s)", "*", q.question, q.category) 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) m, solved, cont := quizWaitForAnswer(q)
if solved { if solved {
SayCh <- fmt.Sprintf("%s\n%s hat die Frage beantwortet.", "*", m.Prefix) mm := m
m = mm
} else { } else {
SayCh <- fmt.Sprintf("%s\nNiemand konnte die Frage beantworten.", "*")
SayCh <- fmt.Sprintf("%s\nDie richtige Antwort wäre gewesen:", "*") 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) time.Sleep(5 * time.Second)
return cont return cont
@ -90,16 +95,26 @@ func quizAskQuestion() bool {
func quizWaitForAnswer(q quizQuestion) (*irc.Message, bool, bool) { func quizWaitForAnswer(q quizQuestion) (*irc.Message, bool, bool) {
i := 0 i := 0
t := 0 haveAnswer := false
timer := time.Now().Unix()
for { for {
select { select {
case m := <-quizAnswerCh: case m := <-quizAnswerCh:
haveAnswer = true
points := 10 - ((time.Now().Unix() - timer) / 10)
if points < 1 {
points = 1
}
re, err := regexp.Compile(q.regexp) re, err := regexp.Compile(q.regexp)
if err != nil { if err != nil {
xlog.Error(err.Error()) xlog.Error(err.Error())
return nil, false, false return nil, false, false
} }
if re.MatchString(strings.ToLower(util.ReplaceUmlauts(m.Trailing))) { 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 return m, true, true
} }
break break
@ -108,24 +123,45 @@ func quizWaitForAnswer(q quizQuestion) (*irc.Message, bool, bool) {
return nil, false, false return nil, false, false
} }
break break
default: case <-time.After(15 * time.Second):
time.Sleep(1 * time.Millisecond) i++
t++ if i > 3 {
if t > 20000 { if haveAnswer {
t = 0 SayCh <- fmt.Sprintf("%s\nNiemand konnte die Frage korrekt beantworten.", "*")
i++
if i > 3 {
return nil, false, true return nil, false, true
} else { } 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) { func quizGiveHint(q quizQuestion, n int) {
//SayCh <- fmt.Sprintf("%s\nHint: %s | %s", "*", q.answer, q.regexp) 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 { func quizLoadQuestions(path string) []quizQuestion {
@ -147,7 +183,7 @@ func quizLoadQuestions(path string) []quizQuestion {
q = quizQuestion{"", "", "", ""} q = quizQuestion{"", "", "", ""}
} }
} else { } else {
tok := strings.Split(line, ": ") tok := strings.Split(line, ":: ")
switch tok[0] { switch tok[0] {
case "Category": case "Category":
q.category = tok[1] q.category = tok[1]
@ -157,18 +193,17 @@ func quizLoadQuestions(path string) []quizQuestion {
break break
case "Answer": case "Answer":
q.answer = strings.Replace(tok[1], "#", "", -1) q.answer = strings.Replace(tok[1], "#", "", -1)
q.answer = util.ReplaceUmlauts(q.answer)
regexp := strings.Split(tok[1], "#")
if q.regexp == "" { if q.regexp == "" {
regexp := strings.Split(strings.ToLower(util.ReplaceUmlauts(tok[1])), "#")
if len(regexp) > 1 { if len(regexp) > 1 {
q.regexp = strings.ToLower(regexp[1]) q.regexp = regexp[1]
} else { } else {
q.regexp = strings.ToLower(regexp[0]) q.regexp = regexp[0]
} }
} }
break break
case "Regexp": case "Regexp":
q.regexp = util.ReplaceUmlauts(tok[1]) q.regexp = util.ReplaceUmlauts(strings.ToLower(tok[1]))
break break
} }
} }

View File

@ -3,9 +3,10 @@
package modules package modules
import ( import (
"flokatirc/util"
"fmt" "fmt"
"math/rand"
"strings" "strings"
"time"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
@ -521,6 +522,7 @@ var quotes = [][]string{
func init() { func init() {
MsgFuncs["stoll"] = stollHandleMessage MsgFuncs["stoll"] = stollHandleMessage
rand.Seed(time.Now().Unix())
} }
func stollHandleMessage(m *irc.Message) { func stollHandleMessage(m *irc.Message) {
@ -531,7 +533,7 @@ func stollHandleMessage(m *irc.Message) {
if tok[0] == "!stoll" { if tok[0] == "!stoll" {
line := "" line := ""
for i := 0; i < 3; i++ { 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 += " "
} }
line += "[Dr. Axel Stoll, promovierter Naturwissenschaftler]" line += "[Dr. Axel Stoll, promovierter Naturwissenschaftler]"

View File

@ -4,10 +4,8 @@ package util
import ( import (
"bytes" "bytes"
"math/rand"
"strconv" "strconv"
"strings" "strings"
"time"
) )
func ToInt(v interface{}) int { func ToInt(v interface{}) int {
@ -49,11 +47,6 @@ func NumberToString(n int, sep rune) string {
return buf.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 { func ReplaceUmlauts(s string) string {
ret := strings.Replace(s, "Ä", "Ae", -1) ret := strings.Replace(s, "Ä", "Ae", -1)
ret = strings.Replace(ret, "Ö", "Oe", -1) ret = strings.Replace(ret, "Ö", "Oe", -1)