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