Skip to content

Commit

Permalink
Added handling for lockout period
Browse files Browse the repository at this point in the history
  • Loading branch information
DaltonSW committed Aug 11, 2024
1 parent ef5e791 commit e4c0979
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/charmbracelet/x/input v0.1.3 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -35,11 +36,14 @@ require (
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 13 additions & 2 deletions internal/resources/Puzzle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

"github.com/PuerkitoBio/goquery"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -74,6 +75,10 @@ const (
)

func (p *Puzzle) SubmitAnswer(answer string) (int, string) {
if !time.Now().After(p.LockoutEnd) {
return WarningAnswer, fmt.Sprintf("Still within lockout period of last submission. Lockout End: %s", p.LockoutEnd.Format(time.Stamp))
}

var part int
if p.AnswerOne == "" {
part = 1
Expand All @@ -84,9 +89,7 @@ func (p *Puzzle) SubmitAnswer(answer string) (int, string) {
}

// TODO: Check past submissions and lockout period before allowing submission
// - Lockout period
// - Past submissions
// - Equal to
// - Too high / too low

for _, pastSub := range p.Submissions[part] {
Expand Down Expand Up @@ -118,6 +121,7 @@ func (p *Puzzle) SubmitAnswer(answer string) (int, string) {

p.Submissions[part] = outList
if submission.correct {
p.ReloadPage()

if p.AnswerOne == "" {
p.AnswerOne = answer
Expand All @@ -126,8 +130,15 @@ func (p *Puzzle) SubmitAnswer(answer string) (int, string) {
p.AnswerTwo = answer
return CorrectAnswer, "Second star obtained! That's all for today, good luck tomorrow!"
}

} else {
// TODO: Parse the response message for lockout period
lockoutDuration, err := utils.ParseDuration(submission.message)
if err != nil {
return IncorrectAnswer, submission.message + "\nUnable to parse lockout duration from message."
}

p.LockoutEnd = time.Now().Add(lockoutDuration)

return IncorrectAnswer, submission.message
}
Expand Down

0 comments on commit e4c0979

Please sign in to comment.