diff --git a/contracts/room_contract.go b/contracts/room_contract.go index 3150a36..4225a11 100644 --- a/contracts/room_contract.go +++ b/contracts/room_contract.go @@ -1,6 +1,7 @@ package contracts import ( + "bytes" "fmt" "github.com/google/uuid" "github.com/nspcc-dev/neo-go/pkg/interop" @@ -46,6 +47,7 @@ type Room struct { } type Round struct { + TokenId []byte // NFT token id, checking for uniqueness of questions Question string Answers []Answer } @@ -182,15 +184,33 @@ func StartGame(roomId string) bool { return true } -func AskQuestion(roomId string, question string) bool { +func checkingForUniqueness(rounds []Round, tokenId []byte) bool { + for _, round := range rounds { + if bytes.Equal(round.TokenId, tokenId) { + return false + } + } + + return true +} + +func AskQuestion(roomId string, tokenId []byte) bool { var ctx = storage.GetContext() var room = getRoom(ctx, roomId) + var wallet = getSender() - if !room.Host.Equals(getSender()) || room.Status != RoomStatusGaming { + if !room.Host.Equals(wallet) || room.Status != RoomStatusGaming { return false // Only host can ask question, room status must be gaming } + 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 + } + + var question = tokenProps["question"] var round = Round{ + TokenId: tokenId, Question: question, Answers: []Answer{}, } @@ -199,9 +219,6 @@ func AskQuestion(roomId string, question string) bool { sendMessageToPlayers("RoundQuestion", question) - // todo: Не забыть про nns - // todo: Добавить списание токенов за создание вопроса - setRoom(ctx, &room) return true } @@ -260,7 +277,7 @@ func EndQuestion(roomId string) bool { var result string for i, answer := range round.Answers { result += fmt.Sprintf("index:%d, player:%s, answer:%s\n", i, answer.Wallet, answer.Content) - } // todo: нужно проверить нормально ли всё с \n в логах + } sendMessageToPlayers("RoundAnswers", result) @@ -325,7 +342,6 @@ func chooseWonAnswers(round Round, RoundWinnersCount int) []Answer { return wonAnswers } -// GetRoundWinner todo(-): Нам сказали, что ссылку на другой контракт нельзя чисто, используем nns // GetRoundWinner todo: Разные реализации распределения наград можно реализовать в контракте с деньгами func GetRoundWinner(roomId string) bool { var ctx = storage.GetContext()