Skip to content

Commit

Permalink
Better answer tracking 'framework'
Browse files Browse the repository at this point in the history
  • Loading branch information
DaltonSW committed Aug 11, 2024
1 parent a5a040f commit 7671a69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
2 changes: 0 additions & 2 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const DAY_URL = YEAR_URL + "/day/%v"

const REQS_PER_SEC = 10

// TODO: SubmitGuess()

var MasterClient httpClient

type httpClient struct {
Expand Down
34 changes: 26 additions & 8 deletions internal/resources/Puzzle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"io"
"strconv"
"strings"
"time"

"dalton.dog/aocgo/internal/api"
"dalton.dog/aocgo/internal/cache"

"github.com/PuerkitoBio/goquery"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
Expand All @@ -35,6 +37,8 @@ type Puzzle struct {

UserInput []byte
Submissions map[int][]*Submission

LockoutEnd time.Time
}

func (p *Puzzle) GetID() string { return p.BucketID }
Expand Down Expand Up @@ -62,12 +66,21 @@ func (p *Puzzle) Display() {
NewPuzzleViewport(p)
}

func (p *Puzzle) SubmitAnswer(answer string) (bool, string) {
// Answer Response types
const (
IncorrectAnswer int = iota
CorrectAnswer
WarningAnswer
)

func (p *Puzzle) SubmitAnswer(answer string) (int, string) {
var part int
if p.AnswerOne == "" {
part = 1
} else {
} else if p.AnswerTwo == "" {
part = 2
} else {
return WarningAnswer, "You've already gotten both stars for this level."
}

// TODO: Check past submissions and lockout period before allowing submission
Expand All @@ -76,6 +89,12 @@ func (p *Puzzle) SubmitAnswer(answer string) (bool, string) {
// - Equal to
// - Too high / too low

for _, pastSub := range p.Submissions[part] {
if pastSub.answer == answer {
return WarningAnswer, "You've already submitted that answer!"
}
}

submissionData, err := api.SubmitAnswer(p.Year, p.Day, part, p.SessionToken, answer)
if err != nil {
log.Fatal(err)
Expand All @@ -102,16 +121,15 @@ func (p *Puzzle) SubmitAnswer(answer string) (bool, string) {

if p.AnswerOne == "" {
p.AnswerOne = answer
return true, "First star obtained! Run `view` again to get part 2."
return CorrectAnswer, "First star obtained! Run `view` again to get part 2."
} else {
p.AnswerTwo = answer
return true, "Second star obtained! That's all for today, good luck tomorrow!"
return CorrectAnswer, "Second star obtained! That's all for today, good luck tomorrow!"
}
} else {
// TODO: Parse the response message for info about wrong answer
// - Lockout period
// - Too high or too low?
return false, submission.message
// TODO: Parse the response message for lockout period

return IncorrectAnswer, submission.message
}
}

Expand Down

0 comments on commit 7671a69

Please sign in to comment.