go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/kana-server/pkg/types/quiz_result.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package types
     9  
    10  import (
    11  	"math/rand"
    12  	"strings"
    13  	"time"
    14  
    15  	"go.charczuk.com/sdk/uuid"
    16  
    17  	"go.charczuk.com/projects/kana-server/pkg/kana"
    18  )
    19  
    20  // QuizResult is a quiz result.
    21  type QuizResult struct {
    22  	ID          uuid.UUID `db:"id,pk"`
    23  	QuizID      uuid.UUID `db:"quiz_id"`
    24  	UserID      uuid.UUID `db:"user_id"`
    25  	CreatedUTC  time.Time `db:"created_utc"`
    26  	AnsweredUTC time.Time `db:"answered_utc"`
    27  	Prompt      string    `db:"prompt"`
    28  	Expected    string    `db:"expected"`
    29  	Actual      string    `db:"actual"`
    30  }
    31  
    32  // TableName returns the database tablename for the type.
    33  func (qr QuizResult) TableName() string { return "quiz_result" }
    34  
    35  // Elapsed returns the elapsed time as a duration from the answered to the created times.
    36  func (qr QuizResult) Elapsed() time.Duration {
    37  	return qr.AnsweredUTC.Sub(qr.CreatedUTC)
    38  }
    39  
    40  // Correct returns if the actual answer matches the expected.
    41  //
    42  // It will trim space, and use a case insensitive equals.
    43  func (qr QuizResult) Correct() bool {
    44  	return strings.EqualFold(
    45  		strings.TrimSpace(qr.Expected),
    46  		strings.TrimSpace(qr.Actual),
    47  	)
    48  }
    49  
    50  // NewTestQuizResultCorrect returns a new correct quiz result.
    51  func NewTestQuizResultCorrect(quiz *Quiz) *QuizResult {
    52  	prompt, expected := kana.SelectWeighted(quiz.Prompts, quiz.PromptWeights)
    53  	now := time.Now().UTC()
    54  	answerElapsed := time.Duration(rand.Int63n(int64(5 * time.Second)))
    55  	return &QuizResult{
    56  		ID:          uuid.V4(),
    57  		UserID:      quiz.UserID,
    58  		QuizID:      quiz.ID,
    59  		CreatedUTC:  now.Add(-answerElapsed),
    60  		AnsweredUTC: now,
    61  		Prompt:      prompt,
    62  		Expected:    expected,
    63  		Actual:      expected,
    64  	}
    65  }
    66  
    67  // NewTestQuizResultIncorrect returns a new correct quiz result.
    68  func NewTestQuizResultIncorrect(quiz *Quiz) *QuizResult {
    69  	prompt, expected := kana.SelectWeighted(quiz.Prompts, quiz.PromptWeights)
    70  	now := time.Now().UTC()
    71  	answerElapsed := time.Duration(rand.Int63n(int64(5 * time.Second)))
    72  	return &QuizResult{
    73  		ID:          uuid.V4(),
    74  		UserID:      quiz.UserID,
    75  		QuizID:      quiz.ID,
    76  		CreatedUTC:  now.Add(-answerElapsed),
    77  		AnsweredUTC: now,
    78  		Prompt:      prompt,
    79  		Expected:    expected,
    80  		Actual:      "not-" + expected,
    81  	}
    82  }