Make breaks between questions

This commit is contained in:
Vojtěch Káně
2021-04-25 14:09:08 +02:00
parent 5dae8a7ad7
commit 339af2ebca
5 changed files with 71 additions and 21 deletions

View File

@@ -15,7 +15,7 @@ func (AskedQuestion) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.Nil).Immutable(),
field.Time("asked").Immutable(),
field.Time("ended"),
field.Time("ended").Optional().Nillable(),
}
}

View File

@@ -178,22 +178,27 @@ func (m *Model) GetQuestionStateUpdate(sessionId uuid.UUID, now time.Time, c con
defer tx.Commit()
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
if !aq.Ended.After(now) {
qu.RemainingTime = 0
// either show the current question or hide the old one
if aq.Ended == nil {
var q = aq.Edges.Question
var qu rtcomm.QuestionUpdate
qu.Title = q.Title
if ends := aq.Asked.Add(time.Duration(q.DefaultLength) * time.Millisecond); !now.Before(ends) {
qu.RemainingTime = 0
} else {
qu.RemainingTime = uint64(ends.Sub(now).Round(time.Millisecond).Milliseconds())
}
qu.Answers = make([]rtcomm.Answer, 0, len(q.Edges.Choices))
for i := 0; i < len(q.Edges.Choices); i++ {
qu.Answers = append(qu.Answers, rtcomm.Answer{
ID: q.Edges.Choices[i].ID.String(),
Title: q.Edges.Choices[i].Title,
})
}
return rtcomm.StateUpdate{Question: &qu}, nil
} else {
qu.RemainingTime = uint64(aq.Ended.Sub(now).Round(time.Millisecond).Milliseconds())
return rtcomm.StateUpdate{Break: &rtcomm.BreakUpdate{}}, nil
}
qu.Answers = make([]rtcomm.Answer, 0, len(q.Edges.Choices))
for i := 0; i < len(q.Edges.Choices); i++ {
qu.Answers = append(qu.Answers, rtcomm.Answer{
ID: q.Edges.Choices[i].ID.String(),
Title: q.Edges.Choices[i].Title,
})
}
return rtcomm.StateUpdate{Question: &qu}, nil
} else if ent.IsNotFound(err) {
// There is simply no current question, which is not an error
return rtcomm.StateUpdate{}, nil
@@ -238,8 +243,10 @@ func (m *Model) NextQuestion(sessionId uuid.UUID, now time.Time, c context.Conte
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))
if current.Ended.After(now) {
if _, err := current.Update().SetEnded(now).Save(c); err != nil {
if current.Ended == nil {
if _, err := current.Update().SetEnded(now).Save(c); err == nil {
return tx.Commit()
} else {
return err
}
}
@@ -248,7 +255,7 @@ func (m *Model) NextQuestion(sessionId uuid.UUID, now time.Time, c context.Conte
}
if next, err := query.First(c); err == nil {
if _, err := tx.AskedQuestion.Create().SetID(uuid.New()).SetAsked(now).SetSessionID(sessionId).SetQuestion(next).SetEnded(now.Add(time.Duration(int64(next.DefaultLength)) * time.Millisecond)).Save(c); err != nil {
if _, err := tx.AskedQuestion.Create().SetID(uuid.New()).SetAsked(now).SetSessionID(sessionId).SetQuestion(next).Save(c); err != nil {
return err
}
} else if ent.IsNotFound(err) {
@@ -292,7 +299,7 @@ func (m *Model) SaveAnswer(playerId uuid.UUID, choiceId uuid.UUID, now time.Time
// check if the question is open
// Asked[0] is guaranteed to exist thanks to the previous query
if !q.Edges.Asked[0].Ended.After(now) {
if q.Edges.Asked[0].Ended != nil || q.Edges.Asked[0].Asked.Add(time.Duration(q.DefaultLength)*time.Millisecond).Before(now) {
return nil, QuestionClosed
}

View File

@@ -86,7 +86,7 @@ func newTestModelWithData(t *testing.T) *Model {
players := tx.Player.CreateBulk(playersC...).SaveX(c)
var askedQuestionsC = []*ent.AskedQuestionCreate{
tx.AskedQuestion.Create().SetID(uuid.MustParse("72a1bb9c-67e7-4d59-80fa-80ce729629d3")).SetAsked(time.Unix(1613387996, 0)).SetQuestion(questions[0]).SetSession(sessions[0]).SetEnded(time.Unix(1613388001, 0)),
tx.AskedQuestion.Create().SetID(uuid.MustParse("72a1bb9c-67e7-4d59-80fa-80ce729629d3")).SetAsked(time.Unix(1613387996, 0)).SetQuestion(questions[0]).SetSession(sessions[0]),
}
askedQuestions := tx.AskedQuestion.CreateBulk(askedQuestionsC...).SaveX(c)
@@ -122,8 +122,16 @@ func TestModel_NextQuestion_noNextQuestion(t *testing.T) {
m := newTestModelWithData(t)
c := context.Background()
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), time.Unix(1613388005, 0), c); err != nil {
t.Fatalf("Unexpected error when switching to next question (closing the current one): %v", err)
}
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), time.Unix(1613388006, 0), c); err != nil {
t.Fatalf("Unexpected error when switching to next question: %v", err)
t.Fatalf("Unexpected error when switching to next question (from a break): %v", err)
}
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), time.Unix(1613388007, 0), c); err != nil {
t.Fatalf("Unexpected error when switching to next question (from a break): %v", err)
}
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), time.Unix(1613388008, 0), c); err == nil {
@@ -166,3 +174,29 @@ func TestModel_SaveAnswer_again(t *testing.T) {
t.Fatalf("Saving answer again failed with unexpected error type: %v", err)
}
}
func TestModel_SaveAnswer_late(t *testing.T) {
m := newTestModelWithData(t)
c := context.Background()
if _, err := m.SaveAnswer(uuid.MustParse("321f3bb4-f789-49db-ad14-45299a4725a0"), uuid.MustParse("5155b997-eb2c-4cd0-a067-2bb01379730f"), time.Unix(1613388026, 1), c); err == nil {
t.Fatalf("Saving answer too late succeeded")
} else if !errors.Is(err, QuestionClosed) {
t.Fatalf("Saving answer too late failed with unexpected error type: %v", err)
}
}
func TestModel_SaveAnswer_closed(t *testing.T) {
m := newTestModelWithData(t)
c := context.Background()
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), time.Unix(1613387997, 0), c); err != nil {
t.Fatalf("Unexpected error when switching to next question (closing the current one): %v", err)
}
if _, err := m.SaveAnswer(uuid.MustParse("321f3bb4-f789-49db-ad14-45299a4725a0"), uuid.MustParse("5155b997-eb2c-4cd0-a067-2bb01379730f"), time.Unix(1613387998, 0), c); err == nil {
t.Fatalf("Saving answer to closed question succeeded")
} else if !errors.Is(err, QuestionClosed) {
t.Fatalf("Saving answer to closed question failed with unexpected error type: %v", err)
}
}