58 lines
817 B
Go
58 lines
817 B
Go
package question
|
|
|
|
import (
|
|
"math/rand"
|
|
"strconv"
|
|
"text/template"
|
|
|
|
"github.com/hhhapz/codequest/models"
|
|
)
|
|
|
|
var Questions []*Question
|
|
|
|
func Register(q *Question) {
|
|
Questions = append(Questions, q)
|
|
}
|
|
|
|
func QuestionByID(id string) *Question {
|
|
for _, q := range Questions {
|
|
if q.ID == id {
|
|
return q
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Question struct {
|
|
ID string
|
|
Name string
|
|
Text *template.Template
|
|
Level Level
|
|
|
|
Generate func(user *models.User) string
|
|
Validate func(user *models.User, part Part, solution string) bool
|
|
}
|
|
|
|
type Level int
|
|
|
|
const (
|
|
Level1 Level = iota
|
|
Level2
|
|
)
|
|
|
|
type Part int
|
|
|
|
const (
|
|
Part1 Part = iota + 1
|
|
Part2
|
|
)
|
|
|
|
func UserRandom(u *models.User) *rand.Rand {
|
|
id := u.ID
|
|
if len(id) > 17 {
|
|
id = id[:17]
|
|
}
|
|
seed, _ := strconv.ParseInt(id, 10, 64)
|
|
return rand.New(rand.NewSource(seed))
|
|
}
|