diff --git a/contracts/room_contract.go b/contracts/room_contract.go index fc52d53..9dfac94 100644 --- a/contracts/room_contract.go +++ b/contracts/room_contract.go @@ -57,9 +57,9 @@ type Answer struct { type Player struct { Wallet interop.Hash160 + RoundsWon int isReady bool isVotedToFinish bool - // todo: Сделать реализацию подсчета выигранных раундов } // GLOBAL PRIVATE METHODS FOR ROOM @@ -77,6 +77,7 @@ func setRoom(ctx storage.Context, room *Room) { // Function for get room from storage with deserialize func getRoom(ctx storage.Context, roomId string) Room { var roomData = storage.Get(ctx, "room:"+roomId) + if roomData == nil { panic(fmt.Sprintf("Room with roomId=%s not found", roomId)) } @@ -128,7 +129,13 @@ func JoinRoom(roomId string) bool { // todo: Добавить списание токенов за вход в комнату - var player = Player{Wallet: wallet} + var player = Player{ + Wallet: wallet, + RoundsWon: 0, + isReady: false, + isVotedToFinish: false, + } + room.Players = append(room.Players, player) setRoom(ctx, &room) return true @@ -287,8 +294,8 @@ func VoteAnswer(roomId string, answerIdx int) bool { return true } -func chooseWinner(round Round, countWinners int) []Answer { - var winners []Answer +func chooseWonAnswers(round Round, countWinners int) []Answer { + var wonAnswers []Answer var answers = round.answers sort.Slice(answers, func(a, b int) bool { return len(answers[a].votes) > len(answers[b].votes) @@ -302,21 +309,21 @@ func chooseWinner(round Round, countWinners int) []Answer { return []Answer{answers[0]} } - // Choose winners from sorted answers. If the current answer has the same number of votes as the previous one, - // we add it to the winners list. We increase the number of winners if there are multiple answers with the same + // Choose wonAnswers from sorted answers. If the current answer has the same number of votes as the previous one, + // we add it to the wonAnswers list. We increase the number of wonAnswers if there are multiple answers with the same // number of votes, as in cases where there are 5 answers with equal votes and countWinners is 3, all should be included. var lastVote = len(answers[0].votes) - winners = append(winners, answers[0]) - for i := 1; i < len(answers) && len(winners) < countWinners; i++ { + wonAnswers = append(wonAnswers, answers[0]) + for i := 1; i < len(answers) && len(wonAnswers) < countWinners; i++ { var currentVote = len(answers[i].votes) if lastVote == currentVote { countWinners++ // todo: Могут возникнуть проблемы, надо протестировать } lastVote = currentVote - winners = append(winners, answers[i]) + wonAnswers = append(wonAnswers, answers[i]) } - return winners + return wonAnswers } // GetWinner todo: Нам сказали, что ссылку на другой контракт нельзя чисто, используем nns @@ -333,10 +340,21 @@ func GetWinner(roomId string) bool { return false // Zero winners, because no answer } - var winners = chooseWinner(round, room.CountWinners) + var wonAnswers = chooseWonAnswers(round, room.CountWinners) + var players = room.Players + for _, answer := range wonAnswers { + for _, player := range players { + if answer.wallet.Equals(player.Wallet) { + player.RoundsWon++ + break + } + } + } + room.Players = players + var result string - for i, winner := range winners { - result += fmt.Sprintf("place:%d, winner:%s, votes:%s", i, winner.wallet, winner.votes) + for i, answer := range wonAnswers { + result += fmt.Sprintf("place:%d, winner:%s, votes:%s", i, answer.wallet, answer.votes) } for _, player := range room.Players {