85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
package session
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
|
)
|
|
|
|
func generateLogin() (string, error) {
|
|
bs := make([]byte, 4)
|
|
|
|
if _, err := rand.Read(bs); err != nil {
|
|
return "", fmt.Errorf("error: generateLogin: rand.Read(bs): %v", err)
|
|
}
|
|
|
|
return hex.EncodeToString(bs), nil
|
|
}
|
|
|
|
func findCorrectLogin(l string, ss *[]*Session) (*Session, *BriefingParticipant, bool) {
|
|
for _, session := range *ss {
|
|
for _, p := range session.Participants {
|
|
if l == p.Login {
|
|
return session, p, true
|
|
}
|
|
}
|
|
}
|
|
return nil, nil, false
|
|
}
|
|
|
|
func newParticipant(l string) (*data.Participant, error) {
|
|
var err error
|
|
p := new(data.Participant)
|
|
|
|
p.ID, err = strconv.ParseInt(l, 10, 64)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error: newParticipant: strconv.Atoi(): %v", err)
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
func handleGivenAnswer(p *BriefingParticipant, i int64, r *http.Request) error {
|
|
answer, err := strconv.Atoi(r.PostFormValue("answer"))
|
|
if err != nil {
|
|
return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err)
|
|
}
|
|
|
|
p.GivenAnswers[i] = answer
|
|
return nil
|
|
}
|
|
|
|
func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
|
|
questions := make([]resultQuestion, len(sq))
|
|
for i, q := range sq {
|
|
question := resultQuestion{
|
|
Text: q.Text,
|
|
Answers: make([]resultAnswer, len(q.Answers)),
|
|
}
|
|
|
|
for j, a := range q.Answers {
|
|
answer := resultAnswer{Text: a.Text}
|
|
|
|
if j+1 == q.Correct {
|
|
answer.Correct = true
|
|
} else {
|
|
answer.Correct = false
|
|
}
|
|
|
|
if j+1 == givenAnswers[i] {
|
|
answer.Chosen = true
|
|
} else {
|
|
answer.Chosen = false
|
|
}
|
|
question.Answers[j] = answer
|
|
}
|
|
questions[i] = question
|
|
}
|
|
|
|
return questions
|
|
}
|