hackathon/question/question.go

59 lines
848 B
Go

2021-12-19 15:30:21 +07:00
package question
import (
"math/rand"
2021-12-20 13:00:56 +07:00
"strconv"
2021-12-22 14:38:22 +07:00
"text/template"
2021-12-19 15:30:21 +07:00
"github.com/hhhapz/codequest/models"
)
2021-12-22 14:38:22 +07:00
var Questions []*Question
2021-12-19 15:30:21 +07:00
func Register(q *Question) {
2021-12-22 14:38:22 +07:00
Questions = append(Questions, q)
2021-12-19 15:30:21 +07:00
}
2021-12-20 13:00:56 +07:00
func QuestionByID(id string) *Question {
2021-12-22 14:38:22 +07:00
for _, q := range Questions {
2021-12-20 13:00:56 +07:00
if q.ID == id {
return q
}
2021-12-19 15:30:21 +07:00
}
2021-12-20 13:00:56 +07:00
return nil
}
2021-12-19 15:30:21 +07:00
type Question struct {
ID string
Name string
2021-12-22 14:38:22 +07:00
Text *template.Template
2021-12-19 15:30:21 +07:00
Level Level
Generate func(user *models.User) string
2021-12-20 13:00:56 +07:00
Validate func(user *models.User, part Part, solution string) bool
2021-12-19 15:30:21 +07:00
}
type Level int
const (
Level1 Level = iota
Level2
)
2021-12-20 13:00:56 +07:00
type Part int
const (
Part1 Part = iota
Part2
)
// TODO: Internal autoincrement id
func UserRandom(u *models.User) *rand.Rand {
2021-12-20 13:00:56 +07:00
id := u.ID
if len(id) > 17 {
id = id[:17]
}
seed, _ := strconv.ParseInt(id, 10, 64)
return rand.New(rand.NewSource(seed))
}