From 87a092736dc4e98ef520833d46a7732a4b794a60 Mon Sep 17 00:00:00 2001 From: Vr61v Date: Mon, 20 Jan 2025 18:17:48 +0300 Subject: [PATCH] feat: create isActive status in Player --- contracts/room_contract.go | 62 +++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/contracts/room_contract.go b/contracts/room_contract.go index d793ce0..b30775e 100644 --- a/contracts/room_contract.go +++ b/contracts/room_contract.go @@ -51,6 +51,7 @@ type Player struct { RoundsWon int IsReady bool IsVotedToFinish bool + isActive bool } // GLOBAL PRIVATE METHODS FOR ROOM @@ -83,6 +84,16 @@ func sendMessageToPlayers(notificationName string, message string) { runtime.Notify(notificationName, message) } +func isPlayerDeactivate(room Room, wallet interop.Hash160) bool { + for _, player := range room.Players { + if player.Wallet.Equals(wallet) { + return !player.isActive + } + } + + return true // Player was not found +} + // MAIN METHODS TO PLAY IN GAME func CreateRoom(host interop.Hash160, RoundWinnersCount, GameWinnersCount int) string { @@ -127,6 +138,7 @@ func JoinRoom(roomId string) bool { RoundsWon: 0, IsReady: false, IsVotedToFinish: false, + isActive: true, } room.Players = append(room.Players, player) @@ -141,8 +153,8 @@ func ConfirmReadiness(roomId string) bool { for i, p := range room.Players { if p.Wallet.Equals(wallet) { - if p.IsReady { - return false // Player is already ready + if p.IsReady || !p.isActive { + return false // Player is already ready, player must be active } room.Players[i].IsReady = true setRoom(ctx, &room) @@ -193,7 +205,7 @@ func AskQuestion(roomId string, tokenId []byte) bool { var tokenProps = NFTQuestion.Properties(tokenId) if tokenProps == nil || tokenProps["owner"] != string(wallet) || checkingForUniqueness(room.Rounds, tokenId) { - return false // NFT was not found || Host is not the owner of question || Round must contain unique questions + return false // NFT was not found, host is not the owner of question, round must contain unique questions } var question = tokenProps["question"] @@ -225,8 +237,8 @@ func SendAnswer(roomId string, text string) bool { var room = getRoom(ctx, roomId) var wallet = getSender() - if !roomContainsPlayer(room.Players, wallet) || room.Status != RoomStatusAnswering { - return false // Only player can send content, room status must be answering + if !roomContainsPlayer(room.Players, wallet) || isPlayerDeactivate(room, wallet) || room.Status != RoomStatusAnswering { + return false // Only player can send content, player must be active, room status must be answering } // todo: Добавить списание токенов за добавление ответа @@ -251,6 +263,28 @@ func SendAnswer(roomId string, text string) bool { return true } +func deactivatingPlayers(rounds []Round, players []Player) []Player { + var previous, current = rounds[len(rounds)-2], rounds[len(rounds)-1] + for _, player := range players { + var isActive = false + for _, answer := range previous.Answers { + if answer.Wallet.Equals(player.Wallet) { + isActive = true + break + } + } + + for _, answer := range current.Answers { + if answer.Wallet.Equals(player.Wallet) { + isActive = true + } + } + player.isActive = isActive + } + + return players +} + func EndQuestion(roomId string) bool { var ctx = storage.GetContext() var room = getRoom(ctx, roomId) @@ -261,14 +295,18 @@ func EndQuestion(roomId string) bool { room.Status = RoomStatusVoting - var round = room.Rounds[len(room.Rounds)-1] + var rounds = room.Rounds + var round = rounds[len(rounds)-1] var result string for i, answer := range round.Answers { result += fmt.Sprintf("index:%d, player:%s, answer:%s\n", i, answer.Wallet, answer.Content) } - sendMessageToPlayers("RoundAnswers", result) + if len(rounds) > 1 { + room.Players = deactivatingPlayers(rounds, room.Players) + } + setRoom(ctx, &room) return true } @@ -278,8 +316,8 @@ func VoteAnswer(roomId string, answerIdx int) bool { var room = getRoom(ctx, roomId) var wallet = getSender() - if room.Host.Equals(wallet) || room.Status != RoomStatusVoting { - return false // Only player can choose answer, room status must be voting + if room.Host.Equals(wallet) || isPlayerDeactivate(room, wallet) || room.Status != RoomStatusVoting { + return false // Only player can choose answer, player must be active, room status must be voting } var round = room.Rounds[len(room.Rounds)-1] @@ -456,7 +494,7 @@ func chooseWonPlayers(room *Room, GameWinnersCount int) []Player { func finishGame(ctx storage.Context, room *Room) bool { var winners = chooseWonPlayers(room, room.GameWinnersCount) - var result = fmt.Sprintf("count winners:%d\n", len(winners)) + var result = fmt.Sprintf("finish the game! count winners:%d\n", len(winners)) for i, player := range winners { result += fmt.Sprintf("place:%d, player:%s, score:%d\n", i, player.Wallet, player.RoundsWon) } @@ -468,7 +506,3 @@ func finishGame(ctx storage.Context, room *Room) bool { setRoom(ctx, room) return true } - -/* todo: Логика выкидывания игрока из игры, если пропускает несколько вопросов подряд - - * делаем булевый флаг по которому определяем, активен игрок или нет. - */