Make breaks between questions
This commit is contained in:
@@ -15,7 +15,7 @@ func (AskedQuestion) Fields() []ent.Field {
|
|||||||
return []ent.Field{
|
return []ent.Field{
|
||||||
field.UUID("id", uuid.Nil).Immutable(),
|
field.UUID("id", uuid.Nil).Immutable(),
|
||||||
field.Time("asked").Immutable(),
|
field.Time("asked").Immutable(),
|
||||||
field.Time("ended"),
|
field.Time("ended").Optional().Nillable(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -178,22 +178,27 @@ func (m *Model) GetQuestionStateUpdate(sessionId uuid.UUID, now time.Time, c con
|
|||||||
defer tx.Commit()
|
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 {
|
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
|
// either show the current question or hide the old one
|
||||||
var qu rtcomm.QuestionUpdate
|
if aq.Ended == nil {
|
||||||
qu.Title = q.Title
|
var q = aq.Edges.Question
|
||||||
if !aq.Ended.After(now) {
|
var qu rtcomm.QuestionUpdate
|
||||||
qu.RemainingTime = 0
|
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 {
|
} 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) {
|
} else if ent.IsNotFound(err) {
|
||||||
// There is simply no current question, which is not an error
|
// There is simply no current question, which is not an error
|
||||||
return rtcomm.StateUpdate{}, nil
|
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 {
|
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))
|
query.Where(question.OrderGT(current.Edges.Question.Order))
|
||||||
if current.Ended.After(now) {
|
if current.Ended == nil {
|
||||||
if _, err := current.Update().SetEnded(now).Save(c); err != nil {
|
if _, err := current.Update().SetEnded(now).Save(c); err == nil {
|
||||||
|
return tx.Commit()
|
||||||
|
} else {
|
||||||
return err
|
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 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
|
return err
|
||||||
}
|
}
|
||||||
} else if ent.IsNotFound(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
|
// check if the question is open
|
||||||
// Asked[0] is guaranteed to exist thanks to the previous query
|
// 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
|
return nil, QuestionClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ func newTestModelWithData(t *testing.T) *Model {
|
|||||||
players := tx.Player.CreateBulk(playersC...).SaveX(c)
|
players := tx.Player.CreateBulk(playersC...).SaveX(c)
|
||||||
|
|
||||||
var askedQuestionsC = []*ent.AskedQuestionCreate{
|
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)
|
askedQuestions := tx.AskedQuestion.CreateBulk(askedQuestionsC...).SaveX(c)
|
||||||
@@ -122,8 +122,16 @@ func TestModel_NextQuestion_noNextQuestion(t *testing.T) {
|
|||||||
m := newTestModelWithData(t)
|
m := newTestModelWithData(t)
|
||||||
c := context.Background()
|
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 {
|
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 {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package rtcomm
|
|||||||
type StateUpdate struct {
|
type StateUpdate struct {
|
||||||
Players []Player `json:"players,omitempty"`
|
Players []Player `json:"players,omitempty"`
|
||||||
Question *QuestionUpdate `json:"question,omitempty"`
|
Question *QuestionUpdate `json:"question,omitempty"`
|
||||||
|
Break *BreakUpdate `json:"break,omitempty"`
|
||||||
Results bool `json:"results,omitempty"`
|
Results bool `json:"results,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,3 +22,7 @@ type Answer struct {
|
|||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BreakUpdate struct {
|
||||||
|
//TODO add interim results
|
||||||
|
}
|
||||||
|
|||||||
@@ -127,6 +127,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ('break' in data) {
|
||||||
|
questionSection.innerHTML = '';
|
||||||
|
}
|
||||||
|
|
||||||
if ('results' in data && data.results === true) {
|
if ('results' in data && data.results === true) {
|
||||||
window.location.pathname = "/results/" + encodeURIComponent(playerId);
|
window.location.pathname = "/results/" + encodeURIComponent(playerId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user