Award points via participant ID, instead of team ID

This commit is contained in:
John Donaldson 2022-01-27 16:52:26 -08:00
parent afb3cde38d
commit c4cfee94f4
2 changed files with 27 additions and 5 deletions

View File

@ -57,8 +57,9 @@ type StateProvider interface {
PointsLog() award.List
TeamName(teamID string) (string, error)
SetTeamName(teamID, teamName string) error
ParticipantTeam(participantID string) (string, error)
AssignParticipant(participantID string, teamID string) error
AwardPoints(teamID string, cat string, points int) error
AwardPoints(participantID string, cat string, points int) error
LogEvent(event, participantID, teamID, cat string, points int, extra ...string)
Maintainer
}
@ -155,7 +156,12 @@ func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string)
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 {
if _, err := mh.State.ParticipantTeam(mh.participantID); err != nil {
return fmt.Errorf("invalid participant ID")
}
if err := mh.State.AwardPoints(mh.participantID, cat, points); err != nil {
return fmt.Errorf("error awarding points: %s", err)
}

View File

@ -177,6 +177,18 @@ func (s *State) SetTeamName(teamID, teamName string) error {
return nil
}
func (s *State) ParticipantTeam(participantID string) (string, error) {
s.lock.RLock()
defer s.lock.RUnlock()
teamID, ok := s.participantTeams[participantID]
if !ok {
return "", fmt.Errorf("participant (%s) has not registered with a team", participantID)
}
return teamID, nil
}
// AssignParticipant associated a participant with a team
// A participant can only be associated with one team at a time
func (s *State) AssignParticipant(participantID string, teamID string) error {
@ -235,15 +247,19 @@ func (s *State) Messages() string {
return s.messages
}
// AwardPoints gives points to teamID in category.
// AwardPoints gives points to the team of participantID 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.
// The update task makes sure we never have duplicate points in the log.
func (s *State) AwardPoints(teamID, category string, points int) error {
func (s *State) AwardPoints(participantID, category string, points int) error {
if teamID, err := s.ParticipantTeam(participantID); err != nil {
return err
} else {
return s.awardPointsAtTime(time.Now().Unix(), teamID, category, points)
}
}
func (s *State) awardPointsAtTime(when int64, teamID string, category string, points int) error {
a := award.T{