70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package question
|
|
|
|
import (
|
|
"math/rand"
|
|
"strconv"
|
|
|
|
"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 = append(bank, q)
|
|
}
|
|
|
|
func QuestionByID(id string) *Question {
|
|
for _, q := range bank {
|
|
if q.ID == id {
|
|
return q
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Questions(user *models.User, level Level) []*Question {
|
|
// TODO: player skill level should be used to determine which problems to return
|
|
return bank
|
|
}
|
|
|
|
type Question struct {
|
|
ID string
|
|
Name string
|
|
Text string
|
|
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
|
|
Part2
|
|
)
|
|
|
|
// TODO: Internal autoincrement id
|
|
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))
|
|
}
|