2016-03-19 22:51:15 +00:00
|
|
|
// vi:ts=4:sts=4:sw=4:noet:tw=72
|
|
|
|
|
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flokatirc/util"
|
|
|
|
"fmt"
|
2016-03-20 22:01:04 +00:00
|
|
|
"math/rand"
|
2016-03-19 22:51:15 +00:00
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sorcix/irc"
|
|
|
|
|
|
|
|
"code.dnix.de/an/xlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type quizQuestion struct {
|
2016-04-01 17:21:49 +00:00
|
|
|
category, question, answer, regexp, level string
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-04-01 17:21:49 +00:00
|
|
|
quizCtrlCh = make(chan string, 1024)
|
|
|
|
quizAnswerCh = make(chan *irc.Message, 1024)
|
|
|
|
quizQuestions []quizQuestion
|
2016-04-01 17:26:57 +00:00
|
|
|
quizQuestionValues = map[string]int{"extreme": 5, "hard": 4, "normal": 3, "easy": 2, "baby": 1}
|
2016-03-19 22:51:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
MsgFuncs["quiz"] = quizHandleMessage
|
|
|
|
RunFuncs["quiz"] = quiz
|
2016-03-20 22:01:04 +00:00
|
|
|
rand.Seed(time.Now().Unix())
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func quizHandleMessage(m *irc.Message) {
|
|
|
|
tok := strings.Split(m.Trailing, " ")
|
|
|
|
if len(tok) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch tok[0] {
|
2016-04-01 17:21:49 +00:00
|
|
|
case "!quiz", "!quizstart":
|
2016-03-19 22:51:15 +00:00
|
|
|
quizCtrlCh <- "start"
|
|
|
|
break
|
|
|
|
case "!quizstop":
|
|
|
|
quizCtrlCh <- "stop"
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
quizAnswerCh <- m
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func quiz() {
|
2016-04-01 17:21:49 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2016-03-19 22:51:15 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\nquiz mod test. !quizstart to start, !quizstop to end.", "*")
|
|
|
|
for {
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
select {
|
|
|
|
case s := <-quizCtrlCh:
|
|
|
|
if s == "start" {
|
|
|
|
quizRun()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func quizRun() {
|
2016-06-16 17:50:10 +00:00
|
|
|
quizQuestions = quizLoadQuestions("questions.txt")
|
2016-04-01 17:21:49 +00:00
|
|
|
ranklist := make(map[string]int)
|
2016-03-20 22:01:04 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\nQuiz gestartet.", "*")
|
2016-03-19 22:51:15 +00:00
|
|
|
for {
|
2016-04-01 17:21:49 +00:00
|
|
|
solved, cont, solver, gain := quizAskQuestion()
|
|
|
|
if !cont {
|
2016-03-20 22:01:04 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\nQuiz beendet.", "*")
|
2016-03-19 22:51:15 +00:00
|
|
|
return
|
2016-04-01 17:21:49 +00:00
|
|
|
} else {
|
|
|
|
if solved {
|
|
|
|
if _, exists := ranklist[solver]; exists {
|
|
|
|
ranklist[solver] += gain
|
|
|
|
} else {
|
|
|
|
ranklist[solver] = gain
|
|
|
|
}
|
|
|
|
if ranklist[solver] > 1000 {
|
|
|
|
bold := byte(0x02)
|
|
|
|
solver = string(bold) + solver + string(bold)
|
|
|
|
SayCh <- fmt.Sprintf("%s\n%s gewinnt!", "*", solver)
|
|
|
|
quizPrintRanklist(ranklist)
|
|
|
|
SayCh <- fmt.Sprintf("%s\nQuiz beendet.", "*")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
2016-04-01 17:21:49 +00:00
|
|
|
quizPrintRanklist(ranklist)
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 17:21:49 +00:00
|
|
|
func quizPrintRanklist(ranklist map[string]int) {
|
2017-01-21 17:20:57 +00:00
|
|
|
if len(ranklist) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2017-01-23 13:57:41 +00:00
|
|
|
ranklistTmp = copy(ranklist)
|
2016-04-01 17:21:49 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\nAktueller Punktestand:", "*")
|
2017-01-21 17:20:57 +00:00
|
|
|
for {
|
|
|
|
maxk := ""
|
|
|
|
maxv := -1
|
2017-01-23 13:57:41 +00:00
|
|
|
if len(ranklistTmp) == 0 {
|
2017-01-21 17:20:57 +00:00
|
|
|
break
|
|
|
|
}
|
2017-01-23 13:57:41 +00:00
|
|
|
for k, v := range ranklistTmp {
|
2017-01-21 17:20:57 +00:00
|
|
|
if v > maxv {
|
|
|
|
maxv = v
|
|
|
|
maxk = k
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 13:57:41 +00:00
|
|
|
delete(ranklistTmp, maxk)
|
2017-01-21 17:20:57 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n%s: %d", "*", maxk, maxv)
|
2016-04-01 17:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func quizAskQuestion() (bool, bool, string, int) {
|
2016-03-20 22:01:04 +00:00
|
|
|
q := quizQuestions[rand.Intn(len(quizQuestions))]
|
2016-04-01 17:21:49 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\nEine Frage aus der Kategorie \"%s\" (%s):", "*", q.category, q.level)
|
2016-03-20 22:01:04 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n>>> %s <<<", "*", q.question)
|
2016-04-01 17:21:49 +00:00
|
|
|
solved, cont, solver, gain := quizWaitForAnswer(q)
|
|
|
|
if !solved {
|
2016-03-19 22:51:15 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\nDie richtige Antwort wäre gewesen:", "*")
|
2016-03-20 22:01:04 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n%s [%s]", "*", q.answer, q.regexp)
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
time.Sleep(5 * time.Second)
|
2016-04-01 17:21:49 +00:00
|
|
|
return solved, cont, solver, gain
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 17:21:49 +00:00
|
|
|
func quizWaitForAnswer(q quizQuestion) (bool, bool, string, int) {
|
2016-03-19 22:51:15 +00:00
|
|
|
i := 0
|
2016-03-20 22:01:04 +00:00
|
|
|
haveAnswer := false
|
|
|
|
timer := time.Now().Unix()
|
2017-01-21 16:04:24 +00:00
|
|
|
timeHintCh := make(chan bool)
|
|
|
|
go func(chan bool) {
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
timeHintCh <- true
|
|
|
|
}
|
|
|
|
}(timeHintCh)
|
2016-03-19 22:51:15 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-quizAnswerCh:
|
2016-03-20 22:01:04 +00:00
|
|
|
haveAnswer = true
|
|
|
|
points := 10 - ((time.Now().Unix() - timer) / 10)
|
|
|
|
if points < 1 {
|
|
|
|
points = 1
|
|
|
|
}
|
2016-03-19 22:51:15 +00:00
|
|
|
re, err := regexp.Compile(q.regexp)
|
|
|
|
if err != nil {
|
|
|
|
xlog.Error(err.Error())
|
2016-04-01 17:21:49 +00:00
|
|
|
return false, false, "", 0
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
if re.MatchString(strings.ToLower(util.ReplaceUmlauts(m.Trailing))) {
|
2016-03-20 22:01:04 +00:00
|
|
|
bold := byte(0x02)
|
|
|
|
solver := strings.Split(m.Prefix.String(), "!")[0]
|
|
|
|
solver = string(bold) + solver + string(bold)
|
2016-04-01 17:21:49 +00:00
|
|
|
value := quizQuestionValues[q.level]
|
|
|
|
gain := int(points) * value
|
|
|
|
SayCh <- fmt.Sprintf("%s\n%s hat die Frage korrekt beantwortet und erhält dafür %d (%d * %d) Punkte.", "*", solver, gain, points, value)
|
|
|
|
return true, true, solver, gain
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case s := <-quizCtrlCh:
|
|
|
|
if s == "stop" {
|
2016-04-01 17:21:49 +00:00
|
|
|
return false, false, "", 0
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
break
|
2017-01-21 16:04:24 +00:00
|
|
|
case <-timeHintCh:
|
2016-03-20 22:01:04 +00:00
|
|
|
i++
|
|
|
|
if i > 3 {
|
|
|
|
if haveAnswer {
|
|
|
|
SayCh <- fmt.Sprintf("%s\nNiemand konnte die Frage korrekt beantworten.", "*")
|
2016-04-01 17:21:49 +00:00
|
|
|
return false, true, "", 0
|
2016-03-19 22:51:15 +00:00
|
|
|
} else {
|
2016-03-20 22:01:04 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\nNiemand hat auf die Frage geantwortet.", "*")
|
2016-04-01 17:21:49 +00:00
|
|
|
return false, false, "", 0
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
2016-03-20 22:01:04 +00:00
|
|
|
} else {
|
|
|
|
quizGiveHint(q, i)
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 22:01:04 +00:00
|
|
|
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)
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func quizLoadQuestions(path string) []quizQuestion {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
xlog.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
questions := make([]quizQuestion, 0)
|
2016-04-01 17:21:49 +00:00
|
|
|
q := quizQuestion{"", "", "", "", ""}
|
2016-03-19 22:51:15 +00:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if len(line) == 0 || line[0] == '#' || line[0] == '\n' {
|
|
|
|
if q.category != "" {
|
|
|
|
questions = append(questions, q)
|
2016-04-01 17:26:57 +00:00
|
|
|
q = quizQuestion{"", "", "", "", "normal"}
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-20 22:01:04 +00:00
|
|
|
tok := strings.Split(line, ":: ")
|
2016-03-19 22:51:15 +00:00
|
|
|
switch tok[0] {
|
|
|
|
case "Category":
|
|
|
|
q.category = tok[1]
|
|
|
|
break
|
|
|
|
case "Question":
|
|
|
|
q.question = tok[1]
|
|
|
|
break
|
|
|
|
case "Answer":
|
2016-04-01 17:21:49 +00:00
|
|
|
q.answer = strings.Replace(util.ReplaceUmlauts(tok[1]), "#", "", -1)
|
2016-03-19 22:51:15 +00:00
|
|
|
if q.regexp == "" {
|
2016-03-20 22:01:04 +00:00
|
|
|
regexp := strings.Split(strings.ToLower(util.ReplaceUmlauts(tok[1])), "#")
|
2016-03-19 22:51:15 +00:00
|
|
|
if len(regexp) > 1 {
|
2016-03-20 22:01:04 +00:00
|
|
|
q.regexp = regexp[1]
|
2016-03-19 22:51:15 +00:00
|
|
|
} else {
|
2016-03-20 22:01:04 +00:00
|
|
|
q.regexp = regexp[0]
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case "Regexp":
|
2016-03-20 22:01:04 +00:00
|
|
|
q.regexp = util.ReplaceUmlauts(strings.ToLower(tok[1]))
|
2016-03-19 22:51:15 +00:00
|
|
|
break
|
2016-04-01 17:21:49 +00:00
|
|
|
case "Level":
|
|
|
|
q.level = tok[1]
|
|
|
|
break
|
2016-03-19 22:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
xlog.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return questions
|
|
|
|
}
|