Skip to content

Commit

Permalink
Make breaks between questions
Browse files Browse the repository at this point in the history
  • Loading branch information
vojta001 committed Apr 25, 2021
1 parent 5dae8a7 commit 339af2e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/model/ent/schema/askedQuestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
43 changes: 25 additions & 18 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
38 changes: 36 additions & 2 deletions pkg/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
5 changes: 5 additions & 0 deletions pkg/rtcomm/stateUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rtcomm
type StateUpdate struct {
Players []Player `json:"players,omitempty"`
Question *QuestionUpdate `json:"question,omitempty"`
Break *BreakUpdate `json:"break,omitempty"`
Results bool `json:"results,omitempty"`
}

Expand All @@ -21,3 +22,7 @@ type Answer struct {
ID string `json:"id"`
Title string `json:"title"`
}

type BreakUpdate struct {
//TODO add interim results
}
4 changes: 4 additions & 0 deletions ui/html/game.page.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ <h1 class="question"></h1>
}
}

if ('break' in data) {
questionSection.innerHTML = '';
}

if ('results' in data && data.results === true) {
window.location.pathname = "/results/" + encodeURIComponent(playerId);
}
Expand Down

0 comments on commit 339af2e

Please sign in to comment.