Skip to content

Commit

Permalink
Merge pull request #21 from takama/feature/result_state
Browse files Browse the repository at this point in the history
Saved result state and check for not a tournament player
  • Loading branch information
takama authored Apr 17, 2018
2 parents 08f3f51 + 2197a4d commit b33e704
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tournament/tournament.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var (
ErrPlayersAlreadyJoined = errors.New("Could not re-announce the Tournament, players already joined")
// ErrCouldNotJoinTwice appears if the same player try to join to the tournament twice
ErrCouldNotJoinTwice = errors.New("Could not join twice to the same tournament")
// ErrWinnerIsNotMember appears if among winners exist player who not a tournament member as player
ErrWinnerIsNotMember = errors.New("Not a tournament player can not be a winner")
)

// Entry implements Tournament interface
Expand Down Expand Up @@ -207,6 +209,7 @@ func (entry *Entry) Result(winners map[backer.Player]backer.Points) error {
for idx, bidder := range tournament.Bidders {
if bidder.ID == winner.ID() {
tournament.Bidders[idx].Winner = true
tournament.Bidders[idx].Prize = points
prize := float32(points / backer.Points(len(bidder.Backers)+1))
err := winner.Fund(backer.Points(prize))
if err != nil {
Expand All @@ -225,10 +228,15 @@ func (entry *Entry) Result(winners map[backer.Player]backer.Points) error {
return err
}
}
delete(winners, winner)
}
}
}

if len(winners) != 0 {
tx.Rollback()
return ErrWinnerIsNotMember
}
tournament.IsFinished = true

err = entry.Controller.SaveTournament(tournament, tx)
Expand All @@ -243,6 +251,7 @@ func (entry *Entry) Result(winners map[backer.Player]backer.Points) error {
}

entry.Tournament.IsFinished = tournament.IsFinished
entry.Tournament.Bidders = tournament.Bidders

return nil
}
8 changes: 8 additions & 0 deletions tournament/tournament_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ func TestTournamentResult(t *testing.T) {
err = tournament.Join(playerP2)
test(t, err == nil, "Expected join players, got", err)

playerP3, err := player.New("p3", store)
test(t, err == nil, "Expected creating a new player, got", err)
winners = make(map[backer.Player]backer.Points)
winners[playerP1] = 1000
winners[playerP3] = 1000
err = tournament.Result(winners)
test(t, err == ErrWinnerIsNotMember, "Expected", ErrWinnerIsNotMember, "got", err)

winners = make(map[backer.Player]backer.Points)
winners[playerP1] = 2000
err = tournament.Result(winners)
Expand Down

0 comments on commit b33e704

Please sign in to comment.