Lots of changes in quiz module.
This commit is contained in:
parent
41164153c8
commit
cb87e33ead
|
@ -18,19 +18,20 @@ import (
|
|||
)
|
||||
|
||||
type quizQuestion struct {
|
||||
category, question, answer, regexp string
|
||||
category, question, answer, regexp, level string
|
||||
}
|
||||
|
||||
var (
|
||||
quizCtrlCh = make(chan string, 1024)
|
||||
quizAnswerCh = make(chan *irc.Message, 1024)
|
||||
quizQuestions []quizQuestion
|
||||
quizQuestionValues = map[string]int{"extreme": 5, "hard": 4, "medium": 3, "easy": 2, "baby": 1}
|
||||
)
|
||||
|
||||
func init() {
|
||||
MsgFuncs["quiz"] = quizHandleMessage
|
||||
RunFuncs["quiz"] = quiz
|
||||
quizQuestions = quizLoadQuestions("./modules/quiz/questions.txt")
|
||||
quizQuestions = quizLoadQuestions("questions.txt")
|
||||
rand.Seed(time.Now().Unix())
|
||||
}
|
||||
|
||||
|
@ -40,7 +41,7 @@ func quizHandleMessage(m *irc.Message) {
|
|||
return
|
||||
}
|
||||
switch tok[0] {
|
||||
case "!quizstart":
|
||||
case "!quiz", "!quizstart":
|
||||
quizCtrlCh <- "start"
|
||||
break
|
||||
case "!quizstop":
|
||||
|
@ -53,6 +54,7 @@ func quizHandleMessage(m *irc.Message) {
|
|||
}
|
||||
|
||||
func quiz() {
|
||||
time.Sleep(5 * time.Second)
|
||||
SayCh <- fmt.Sprintf("%s\nquiz mod test. !quizstart to start, !quizstop to end.", "*")
|
||||
for {
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
|
@ -68,32 +70,55 @@ func quiz() {
|
|||
}
|
||||
|
||||
func quizRun() {
|
||||
ranklist := make(map[string]int)
|
||||
SayCh <- fmt.Sprintf("%s\nQuiz gestartet.", "*")
|
||||
for {
|
||||
if !quizAskQuestion() {
|
||||
solved, cont, solver, gain := quizAskQuestion()
|
||||
if !cont {
|
||||
SayCh <- fmt.Sprintf("%s\nQuiz beendet.", "*")
|
||||
return
|
||||
} 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
|
||||
}
|
||||
}
|
||||
}
|
||||
quizPrintRanklist(ranklist)
|
||||
}
|
||||
}
|
||||
|
||||
func quizAskQuestion() bool {
|
||||
func quizPrintRanklist(ranklist map[string]int) {
|
||||
SayCh <- fmt.Sprintf("%s\nAktueller Punktestand:", "*")
|
||||
for k, v := range ranklist {
|
||||
SayCh <- fmt.Sprintf("%s\n%s: %d", "*", k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func quizAskQuestion() (bool, bool, string, int) {
|
||||
q := quizQuestions[rand.Intn(len(quizQuestions))]
|
||||
SayCh <- fmt.Sprintf("%s\nEine Frage aus der Kategorie \"%s\":", "*", q.category)
|
||||
SayCh <- fmt.Sprintf("%s\nEine Frage aus der Kategorie \"%s\" (%s):", "*", q.category, q.level)
|
||||
SayCh <- fmt.Sprintf("%s\n>>> %s <<<", "*", q.question)
|
||||
m, solved, cont := quizWaitForAnswer(q)
|
||||
if solved {
|
||||
mm := m
|
||||
m = mm
|
||||
} else {
|
||||
solved, cont, solver, gain := quizWaitForAnswer(q)
|
||||
if !solved {
|
||||
SayCh <- fmt.Sprintf("%s\nDie richtige Antwort wäre gewesen:", "*")
|
||||
SayCh <- fmt.Sprintf("%s\n%s [%s]", "*", q.answer, q.regexp)
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
return cont
|
||||
return solved, cont, solver, gain
|
||||
}
|
||||
|
||||
func quizWaitForAnswer(q quizQuestion) (*irc.Message, bool, bool) {
|
||||
func quizWaitForAnswer(q quizQuestion) (bool, bool, string, int) {
|
||||
i := 0
|
||||
haveAnswer := false
|
||||
timer := time.Now().Unix()
|
||||
|
@ -108,19 +133,21 @@ func quizWaitForAnswer(q quizQuestion) (*irc.Message, bool, bool) {
|
|||
re, err := regexp.Compile(q.regexp)
|
||||
if err != nil {
|
||||
xlog.Error(err.Error())
|
||||
return nil, false, false
|
||||
return false, false, "", 0
|
||||
}
|
||||
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
|
||||
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
|
||||
}
|
||||
break
|
||||
case s := <-quizCtrlCh:
|
||||
if s == "stop" {
|
||||
return nil, false, false
|
||||
return false, false, "", 0
|
||||
}
|
||||
break
|
||||
case <-time.After(15 * time.Second):
|
||||
|
@ -128,10 +155,10 @@ func quizWaitForAnswer(q quizQuestion) (*irc.Message, bool, bool) {
|
|||
if i > 3 {
|
||||
if haveAnswer {
|
||||
SayCh <- fmt.Sprintf("%s\nNiemand konnte die Frage korrekt beantworten.", "*")
|
||||
return nil, false, true
|
||||
return false, true, "", 0
|
||||
} else {
|
||||
SayCh <- fmt.Sprintf("%s\nNiemand hat auf die Frage geantwortet.", "*")
|
||||
return nil, false, false
|
||||
return false, false, "", 0
|
||||
}
|
||||
} else {
|
||||
quizGiveHint(q, i)
|
||||
|
@ -172,7 +199,7 @@ func quizLoadQuestions(path string) []quizQuestion {
|
|||
defer file.Close()
|
||||
|
||||
questions := make([]quizQuestion, 0)
|
||||
q := quizQuestion{"", "", "", ""}
|
||||
q := quizQuestion{"", "", "", "", ""}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
|
@ -180,7 +207,7 @@ func quizLoadQuestions(path string) []quizQuestion {
|
|||
if len(line) == 0 || line[0] == '#' || line[0] == '\n' {
|
||||
if q.category != "" {
|
||||
questions = append(questions, q)
|
||||
q = quizQuestion{"", "", "", ""}
|
||||
q = quizQuestion{"", "", "", "", "medium"}
|
||||
}
|
||||
} else {
|
||||
tok := strings.Split(line, ":: ")
|
||||
|
@ -192,7 +219,7 @@ func quizLoadQuestions(path string) []quizQuestion {
|
|||
q.question = tok[1]
|
||||
break
|
||||
case "Answer":
|
||||
q.answer = strings.Replace(tok[1], "#", "", -1)
|
||||
q.answer = strings.Replace(util.ReplaceUmlauts(tok[1]), "#", "", -1)
|
||||
if q.regexp == "" {
|
||||
regexp := strings.Split(strings.ToLower(util.ReplaceUmlauts(tok[1])), "#")
|
||||
if len(regexp) > 1 {
|
||||
|
@ -205,6 +232,9 @@ func quizLoadQuestions(path string) []quizQuestion {
|
|||
case "Regexp":
|
||||
q.regexp = util.ReplaceUmlauts(strings.ToLower(tok[1]))
|
||||
break
|
||||
case "Level":
|
||||
q.level = tok[1]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue