Implement switching to next question
This commit is contained in:
41
pkg/model/ent/schema/askedQuestion.go
Normal file
41
pkg/model/ent/schema/askedQuestion.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"github.com/facebook/ent"
|
||||
"github.com/facebook/ent/schema/edge"
|
||||
"github.com/facebook/ent/schema/field"
|
||||
)
|
||||
|
||||
type AskedQuestion struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (AskedQuestion) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Time("asked").Immutable(),
|
||||
field.Time("ended"),
|
||||
}
|
||||
}
|
||||
|
||||
func (AskedQuestion) Indexes() []ent.Index {
|
||||
return []ent.Index{}
|
||||
}
|
||||
|
||||
func (AskedQuestion) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("session", Session.Type).
|
||||
Ref("askedQuestions").
|
||||
Unique().
|
||||
Required(),
|
||||
edge.From("question", Question.Type).
|
||||
Ref("asked").
|
||||
Unique().
|
||||
Required(),
|
||||
}
|
||||
}
|
||||
|
||||
func (AskedQuestion) Config() ent.Config {
|
||||
return ent.Config{
|
||||
Table: "asked_questions",
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ func (Question) Fields() []ent.Field {
|
||||
field.UUID("id", uuid.New()).Immutable(),
|
||||
field.Text("title").MaxLen(256).MinLen(1),
|
||||
field.Int("order"),
|
||||
field.Uint64("defaultLength"), // in milliseconds
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +34,7 @@ func (Question) Edges() []ent.Edge {
|
||||
Unique().
|
||||
Required(),
|
||||
edge.To("choices", Choice.Type),
|
||||
edge.To("current_sessions", Session.Type),
|
||||
edge.To("asked", AskedQuestion.Type),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,14 +16,12 @@ func (Session) Fields() []ent.Field {
|
||||
field.UUID("id", uuid.New()).Immutable(),
|
||||
field.Time("created").Immutable(),
|
||||
field.Time("started").Nillable().Optional(), // TODO remove?
|
||||
field.Time("current_question_until").Nillable().Optional(),
|
||||
field.String("code").MinLen(6).MaxLen(6).Immutable().Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
func (Session) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
}
|
||||
return []ent.Index{}
|
||||
}
|
||||
|
||||
func (Session) Edges() []ent.Edge {
|
||||
@@ -33,9 +31,7 @@ func (Session) Edges() []ent.Edge {
|
||||
Unique().
|
||||
Required(),
|
||||
edge.To("players", Player.Type),
|
||||
edge.From("current_question", Question.Type).
|
||||
Ref("current_sessions").
|
||||
Unique(),
|
||||
edge.To("askedQuestions", AskedQuestion.Type),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package model
|
||||
|
||||
//TODO loose transaction levels wherever possible
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
@@ -7,6 +9,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"time"
|
||||
"vkane.cz/tinyquiz/pkg/model/ent"
|
||||
"vkane.cz/tinyquiz/pkg/model/ent/askedquestion"
|
||||
"vkane.cz/tinyquiz/pkg/model/ent/game"
|
||||
"vkane.cz/tinyquiz/pkg/model/ent/player"
|
||||
"vkane.cz/tinyquiz/pkg/model/ent/question"
|
||||
"vkane.cz/tinyquiz/pkg/model/ent/session"
|
||||
@@ -134,7 +138,8 @@ func (m *Model) GetQuestionStateUpdate(sessionId uuid.UUID, c context.Context) (
|
||||
}
|
||||
defer tx.Commit()
|
||||
|
||||
if q, err := tx.Question.Query().Where(question.HasCurrentSessionsWith(session.ID(sessionId))).WithChoices().Only(c); err == nil {
|
||||
if aq, err := tx.Question.Query().WithChoices().Where(question.HasAskedWith(askedquestion.HasSessionWith(session.ID(sessionId)))).QueryAsked().WithQuestion(func(q *ent.QuestionQuery) { q.WithChoices() }).Order(ent.Desc(askedquestion.FieldAsked)).First(c); err == nil {
|
||||
var q = aq.Edges.Question
|
||||
var qu rtcomm.QuestionUpdate
|
||||
qu.Title = q.Title
|
||||
qu.Answers = make([]rtcomm.Answer, 0, len(q.Edges.Choices))
|
||||
@@ -166,3 +171,47 @@ func (m *Model) GetFullStateUpdate(sessionId uuid.UUID, c context.Context) (rtco
|
||||
}
|
||||
return su, nil
|
||||
}
|
||||
|
||||
var NoNextQuestion = errors.New("there is no next question") // TODO fill
|
||||
|
||||
// TODO retry on serialization failure
|
||||
// TODO validate sessionId
|
||||
func (m *Model) NextQuestion(sessionId uuid.UUID, c context.Context) error {
|
||||
tx, err := m.c.BeginTx(c, &sql.TxOptions{
|
||||
Isolation: sql.LevelSerializable,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO rollback only if not yet committed
|
||||
defer tx.Rollback()
|
||||
|
||||
var now = time.Now()
|
||||
|
||||
var query = tx.Question.Query().Where(question.HasGameWith(game.HasSessionsWith(session.ID(sessionId)))).Order(ent.Asc(question.FieldOrder))
|
||||
|
||||
if current, err := tx.AskedQuestion.Query().Where(askedquestion.HasSessionWith(session.ID(sessionId))).WithQuestion().Order(ent.Desc(askedquestion.FieldAsked)).First(c); err == nil {
|
||||
query.Where(question.OrderGT(current.Edges.Question.Order))
|
||||
// TODO make sure we do not extend the deadline by slow processing
|
||||
if current.Ended.After(now) {
|
||||
if _, err := current.Update().SetEnded(now).Save(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if !ent.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if next, err := query.First(c); err == nil {
|
||||
if _, err := tx.AskedQuestion.Create().SetAsked(now).SetSessionID(sessionId).SetQuestion(next).SetEnded(now.Add(time.Duration(int64(next.DefaultLength)) * time.Millisecond)).Save(c); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if ent.IsNotFound(err) {
|
||||
return NoNextQuestion
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user