Devel server: allow any team ID to score ponts.

This commit is contained in:
Neale Pickett 2020-12-02 19:31:34 -07:00
parent 788ce7c3b6
commit d4b37d1efa
7 changed files with 70 additions and 7 deletions

View File

@ -56,7 +56,6 @@ func main() {
osfs := afero.NewOsFs()
theme := NewTheme(afero.NewBasePathFs(osfs, *themePath))
state := NewState(afero.NewBasePathFs(osfs, *statePath))
config := Configuration{}
@ -68,6 +67,12 @@ func main() {
log.Println("-=- You are in development mode, champ! -=-")
}
var state StateProvider
state = NewState(afero.NewBasePathFs(osfs, *statePath))
if config.Devel {
state = NewDevelState(state)
}
// Set random seed
if *seed == "" {
*seed = os.Getenv("SEED")

View File

@ -151,6 +151,9 @@ func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string)
mh.State.LogEvent("correct", mh.participantID, mh.teamID, cat, points)
if _, err := mh.State.TeamName(mh.teamID); err != nil {
return fmt.Errorf("Invalid team ID")
}
if err := mh.State.AwardPoints(mh.teamID, cat, points); err != nil {
return fmt.Errorf("Error awarding points: %s", err)
}

View File

@ -124,6 +124,9 @@ func TestProdServer(t *testing.T) {
r.Close()
}
if err := anonHandler.CheckAnswer("pategory", 1, "answer123"); err == nil {
t.Error("Invalid team ID was able to get points with correct answer")
}
if err := handler.CheckAnswer("pategory", 1, "answer123"); err != nil {
t.Error("Right answer marked wrong", err)
}

View File

@ -160,7 +160,7 @@ func (s *State) SetTeamName(teamID, teamName string) error {
return err
}
defer teamFile.Close()
log.Println("Setting team name to:", teamName, teamFilename, teamFile)
log.Printf("Setting team name [%s] in file %s", teamName, teamFilename)
fmt.Fprintln(teamFile, teamName)
teamFile.Close()
return nil
@ -197,6 +197,7 @@ func (s *State) Messages() string {
}
// AwardPoints gives points to teamID in category.
// This doesn't attempt to ensure the teamID has been registered.
// It first checks to make sure these are not duplicate points.
// This is not a perfect check, you can trigger a race condition here.
// It's just a courtesy to the user.
@ -209,11 +210,6 @@ func (s *State) AwardPoints(teamID, category string, points int) error {
Points: points,
}
_, err := s.TeamName(teamID)
if err != nil {
return err
}
for _, e := range s.PointsLog() {
if a.Equal(e) {
return fmt.Errorf("Points already awarded to this team in this category")
@ -434,3 +430,26 @@ func (s *State) Maintain(updateInterval time.Duration) {
}
}
}
// DevelState is a StateProvider for use by development servers
type DevelState struct {
StateProvider
}
// NewDevelState returns a new state object that can be used by the development server.
//
// This makes it possible to use the server without having to register a team.
func NewDevelState(sp StateProvider) *DevelState {
return &DevelState{sp}
}
// TeamName returns a valid team name for any teamID
//
// If one's registered, it will use it.
// Otherwise, it returns sprintf("Devel Server Team %s", teamID)
func (ds *DevelState) TeamName(teamID string) (string, error) {
if name, err := ds.StateProvider.TeamName(teamID); err == nil {
return name, nil
}
return fmt.Sprintf("Devel Server Team %s", teamID), nil
}

View File

@ -265,3 +265,17 @@ func TestStateMaintainer(t *testing.T) {
t.Error("Event log didn't end with newline")
}
}
func TestDevelState(t *testing.T) {
s := NewTestState()
ds := NewDevelState(s)
if n, err := ds.TeamName("boog"); err != nil {
t.Error("Devel State returned error on team name lookup")
} else if n != "Devel Server Team boog" {
t.Error("Wrong team name", n)
}
if err := ds.AwardPoints("blerg", "dog", 82); err != nil {
t.Error("Devel State AwardPoints returned an error", err)
}
}

1
go.mod
View File

@ -6,5 +6,6 @@ require (
github.com/russross/blackfriday/v2 v2.0.1
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/spf13/afero v1.4.0
golang.org/x/tools v0.0.0-20201202200335-bef1c476418a // indirect
gopkg.in/yaml.v2 v2.3.0
)

18
go.sum
View File

@ -12,15 +12,33 @@ github.com/spf13/afero v1.4.0 h1:jsLTaI1zwYO3vjrzHalkVcIHXTNmdQFepW4OI8H3+x8=
github.com/spf13/afero v1.4.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201202200335-bef1c476418a h1:TYqOq/v+Ri5aADpldxXOj6PmvcPMOJbLjdALzZDQT2M=
golang.org/x/tools v0.0.0-20201202200335-bef1c476418a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=