hackathon/question/question.go

52 lines
1.0 KiB
Go

2021-12-19 15:30:21 +07:00
package question
import (
"math/rand"
"github.com/hhhapz/codequest/models"
)
var bank Bank
// Bank is a map of different questions registered.
// The key of the map is the ID of the question.
//
// A custom type was created for convenience for picking random questions based
// on difficulties, and for registration.
type Bank [][]*Question
func Register(q *Question) {
bank[q.Level] = append(bank[q.Level], q)
}
func Questions(user *models.User, level Level) []*Question {
qs := make([]*Question, len(bank))
r := rand.New(rand.NewSource(user.CreatedAt.Time().Unix()))
for level := range bank {
idx := r.Intn(len(bank[level]))
qs[level] = bank[level][idx]
}
return qs
}
type Question struct {
ID string
Text string
Level Level
Generate func(user *models.User) string
Validate func(user *models.User, solution string) bool
}
// Level represents the difficulty of each question.
// As the level gets higher, the difficulty also gets higher.
type Level int
// Allowed difficulty levels.
const (
Level1 Level = iota
Level2
Level3
)