Skip to content

Commit

Permalink
team score is now working, the scores of the members count in teams s…
Browse files Browse the repository at this point in the history
…core.

Fixes #276
  • Loading branch information
santiaago committed Mar 2, 2014
1 parent b13c7c3 commit 8257421
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
1 change: 1 addition & 0 deletions helpers/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
ErrorCodeMatchNotFound = "Match not found"
ErrorCodeMatchNotFoundCannotSetPrediction = "Match not found, unable to set prediction"
ErrorCodeCannotSetPrediction = "Something went wrong, unable to set prediction"
ErrorCodeTeamsCannotUpdate = "Could not update teams"

// invite
ErrorCodeInviteNoEmailAddr = "No email address has been entered"
Expand Down
16 changes: 14 additions & 2 deletions models/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Team struct {
Created time.Time
UserIds []int64 // ids of Users <=> members of the team.
TournamentIds []int64 // ids of Tournaments <=> Tournaments the team subscribed.
score int64 // Team score
Score int64 // Team score
}

type TeamJson struct {
Expand All @@ -50,7 +50,7 @@ type TeamJson struct {
Created *time.Time `json:",omitempty"`
UserIds *[]int64 `json:",omitempty"`
TournamentIds *[]int64 `json:",omitempty"`
score *int64 `json:",omitempty"`
Score *int64 `json:",omitempty"`
}

// Create a team given a name, an admin id and a private mode.
Expand Down Expand Up @@ -333,3 +333,15 @@ func (t *Team) ContainsUserId(id int64) (bool, int) {
}
return false, -1
}

// Update an array of teams.
func UpdateTeams(c appengine.Context, teams []*Team) error {
keys := make([]*datastore.Key, len(teams))
for i, _ := range keys {
keys[i] = TeamKeyById(c, teams[i].Id)
}
if _, err := datastore.PutMulti(c, keys, teams); err != nil {
return err
}
return nil
}
39 changes: 26 additions & 13 deletions models/tournament_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,13 @@ func (t *Tournament) UpdateUsersScore(c appengine.Context, m *Tmatch) error {
users := t.Participants(c)
usersToUpdate := make([]*User, 0)
for i, u := range users {
var p *Predict
var err1 error
if p, err1 = u.PredictFromMatchId(c, m.Id); err1 == nil && p == nil {
// nothing to update
continue
} else if err1 != nil {
log.Errorf(c, "%s unable to get predict for current user %v: %v", desc, u.Id, err1)
continue
}
score := computeScore(m, p)
if score > 0 {
if score, err := u.ScoreForMatch(c, m); err != nil {
log.Errorf(c, "%s unable udpate user %v score: %v", desc, u.Id, err)
} else if score > 0 {
users[i].Score += score
usersToUpdate = append(usersToUpdate, users[i])
}
}
// update users
if err := UpdateUsers(c, usersToUpdate); err != nil {
log.Errorf(c, "%s unable udpate users scores: %v", desc, err)
return errors.New(helpers.ErrorCodeUsersCannotUpdate)
Expand All @@ -57,7 +48,29 @@ func (t *Tournament) UpdateUsersScore(c appengine.Context, m *Tmatch) error {

// Update the score of the teams members of the tournament.
func (t *Tournament) UpdateTeamsScore(c appengine.Context, m *Tmatch) error {
//desc := "Update Teams score:"
desc := "Update Teams score:"
teams := t.Teams(c)

teamsToUpdate := make([]*Team, 0)
for i, team := range teams {
teamScore := int64(0)
for _, u := range team.Players(c) {
if score, err := u.ScoreForMatch(c, m); err != nil {
log.Errorf(c, "%s unable udpate user %v score: %v", desc, u.Id, err)
} else {
teamScore += score
}
}
if teamScore > 0 {
teams[i].Score += teamScore
teamsToUpdate = append(teamsToUpdate, teams[i])
}
}
if err := UpdateTeams(c, teamsToUpdate); err != nil {
log.Errorf(c, "%s unable udpate teams scores: %v", desc, err)
return errors.New(helpers.ErrorCodeTeamsCannotUpdate)
}

return nil
}

Expand Down
15 changes: 14 additions & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (u *User) ContainsTeamId(id int64) (bool, int) {
return false, -1
}

// Update an array of groups.
// Update an array of users.
func UpdateUsers(c appengine.Context, users []*User) error {
keys := make([]*datastore.Key, len(users))
for i, _ := range keys {
Expand All @@ -331,3 +331,16 @@ func (u *User) PredictFromMatchId(c appengine.Context, mId int64) (*Predict, err
}
return nil, nil
}

func (u *User) ScoreForMatch(c appengine.Context, m *Tmatch) (int64, error) {
desc := "Score for match:"
var p *Predict
var err1 error
if p, err1 = u.PredictFromMatchId(c, m.Id); err1 == nil && p == nil {
return 0, nil
} else if err1 != nil {
log.Errorf(c, "%s unable to get predict for current user %v: %v", desc, u.Id, err1)
return 0, nil
}
return computeScore(m, p), nil
}

0 comments on commit 8257421

Please sign in to comment.