refactor: make structures fields start name to Uppercase
This commit is contained in:
parent
e1b9be373d
commit
5ceadfbc9d
1 changed files with 38 additions and 38 deletions
|
@ -45,21 +45,21 @@ type Room struct {
|
|||
}
|
||||
|
||||
type Round struct {
|
||||
question string
|
||||
answers []Answer
|
||||
Question string
|
||||
Answers []Answer
|
||||
}
|
||||
|
||||
type Answer struct {
|
||||
wallet interop.Hash160
|
||||
content string
|
||||
votes []interop.Hash160 // Wallets who voted for answer
|
||||
Wallet interop.Hash160
|
||||
Content string
|
||||
Votes []interop.Hash160 // Wallets who voted for answer
|
||||
}
|
||||
|
||||
type Player struct {
|
||||
Wallet interop.Hash160
|
||||
RoundsWon int
|
||||
isReady bool
|
||||
isVotedToFinish bool
|
||||
IsReady bool
|
||||
IsVotedToFinish bool
|
||||
}
|
||||
|
||||
// GLOBAL PRIVATE METHODS FOR ROOM
|
||||
|
@ -132,8 +132,8 @@ func JoinRoom(roomId string) bool {
|
|||
var player = Player{
|
||||
Wallet: wallet,
|
||||
RoundsWon: 0,
|
||||
isReady: false,
|
||||
isVotedToFinish: false,
|
||||
IsReady: false,
|
||||
IsVotedToFinish: false,
|
||||
}
|
||||
|
||||
room.Players = append(room.Players, player)
|
||||
|
@ -148,10 +148,10 @@ func ConfirmReadiness(roomId string) bool {
|
|||
|
||||
for i, p := range room.Players {
|
||||
if p.Wallet.Equals(wallet) {
|
||||
if p.isReady {
|
||||
if p.IsReady {
|
||||
return false // Player is already ready
|
||||
}
|
||||
room.Players[i].isReady = true
|
||||
room.Players[i].IsReady = true
|
||||
setRoom(ctx, &room)
|
||||
return true
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ func StartGame(roomId string) bool {
|
|||
}
|
||||
|
||||
for _, player := range room.Players {
|
||||
if !player.isReady {
|
||||
if !player.IsReady {
|
||||
return false // If any player is not ready, the game can not be started
|
||||
}
|
||||
}
|
||||
|
@ -188,8 +188,8 @@ func AskQuestion(roomId string, question string) bool {
|
|||
}
|
||||
|
||||
var round = Round{
|
||||
question: question,
|
||||
answers: []Answer{},
|
||||
Question: question,
|
||||
Answers: []Answer{},
|
||||
}
|
||||
room.Rounds = append(room.Rounds, round)
|
||||
room.Status = RoomStatusAnswering
|
||||
|
@ -227,19 +227,19 @@ func SendAnswer(roomId string, text string) bool {
|
|||
|
||||
var round = room.Rounds[len(room.Rounds)-1]
|
||||
|
||||
for _, answer := range round.answers {
|
||||
if answer.wallet.Equals(wallet) {
|
||||
for _, answer := range round.Answers {
|
||||
if answer.Wallet.Equals(wallet) {
|
||||
return false // Player cannot send answer twice
|
||||
}
|
||||
}
|
||||
|
||||
answer := Answer{
|
||||
wallet: wallet,
|
||||
content: text,
|
||||
votes: []interop.Hash160{},
|
||||
var answer = Answer{
|
||||
Wallet: wallet,
|
||||
Content: text,
|
||||
Votes: []interop.Hash160{},
|
||||
}
|
||||
|
||||
round.answers = append(round.answers, answer)
|
||||
round.Answers = append(round.Answers, answer)
|
||||
room.Rounds[len(room.Rounds)-1] = round
|
||||
setRoom(ctx, &room)
|
||||
return true
|
||||
|
@ -257,8 +257,8 @@ func EndQuestion(roomId string) bool {
|
|||
|
||||
var round = room.Rounds[len(room.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)
|
||||
for i, answer := range round.Answers {
|
||||
result += fmt.Sprintf("index:%d, player:%s, answer:%s\n", i, answer.Wallet, answer.Content)
|
||||
}
|
||||
|
||||
for _, player := range room.Players {
|
||||
|
@ -279,30 +279,30 @@ func VoteAnswer(roomId string, answerIdx int) bool {
|
|||
}
|
||||
|
||||
var round = room.Rounds[len(room.Rounds)-1]
|
||||
if !(0 <= answerIdx && answerIdx < len(round.answers)) || round.answers[answerIdx].wallet.Equals(wallet) {
|
||||
if !(0 <= answerIdx && answerIdx < len(round.Answers)) || round.Answers[answerIdx].Wallet.Equals(wallet) {
|
||||
return false // answerIdx is incorrect and player cannot vote for himself
|
||||
}
|
||||
|
||||
for _, votedWallet := range round.answers[answerIdx].votes {
|
||||
for _, votedWallet := range round.Answers[answerIdx].Votes {
|
||||
if votedWallet.Equals(wallet) {
|
||||
return false // Player cannot vote twice for one answer
|
||||
}
|
||||
}
|
||||
|
||||
round.answers[answerIdx].votes = append(round.answers[answerIdx].votes, wallet)
|
||||
round.Answers[answerIdx].Votes = append(round.Answers[answerIdx].Votes, wallet)
|
||||
setRoom(ctx, &room)
|
||||
return true
|
||||
}
|
||||
|
||||
func chooseWonAnswers(round Round, countWinners int) []Answer {
|
||||
var wonAnswers []Answer
|
||||
var answers = round.answers
|
||||
var answers = round.Answers
|
||||
sort.Slice(answers, func(a, b int) bool {
|
||||
return len(answers[a].votes) > len(answers[b].votes)
|
||||
return len(answers[a].Votes) > len(answers[b].Votes)
|
||||
})
|
||||
|
||||
if countWinners > len(round.answers) {
|
||||
return round.answers
|
||||
if countWinners > len(round.Answers) {
|
||||
return round.Answers
|
||||
}
|
||||
|
||||
if countWinners == 1 {
|
||||
|
@ -312,10 +312,10 @@ func chooseWonAnswers(round Round, countWinners int) []Answer {
|
|||
// 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)
|
||||
var lastVote = len(answers[0].Votes)
|
||||
wonAnswers = append(wonAnswers, answers[0])
|
||||
for i := 1; i < len(answers) && len(wonAnswers) < countWinners; i++ {
|
||||
var currentVote = len(answers[i].votes)
|
||||
var currentVote = len(answers[i].Votes)
|
||||
if lastVote == currentVote {
|
||||
countWinners++ // todo: Могут возникнуть проблемы, надо протестировать
|
||||
}
|
||||
|
@ -336,7 +336,7 @@ func GetWinner(roomId string) bool {
|
|||
}
|
||||
|
||||
var round = room.Rounds[len(room.Rounds)-1]
|
||||
if len(round.answers) == 0 {
|
||||
if len(round.Answers) == 0 {
|
||||
return false // Zero winners, because no answer
|
||||
}
|
||||
|
||||
|
@ -344,7 +344,7 @@ func GetWinner(roomId string) bool {
|
|||
var players = room.Players
|
||||
for _, answer := range wonAnswers {
|
||||
for _, player := range players {
|
||||
if answer.wallet.Equals(player.Wallet) {
|
||||
if answer.Wallet.Equals(player.Wallet) {
|
||||
player.RoundsWon++
|
||||
break
|
||||
}
|
||||
|
@ -354,7 +354,7 @@ func GetWinner(roomId string) bool {
|
|||
|
||||
var result string
|
||||
for i, answer := range wonAnswers {
|
||||
result += fmt.Sprintf("place:%d, winner:%s, votes:%s", i, answer.wallet, answer.votes)
|
||||
result += fmt.Sprintf("place:%d, winner:%s, votes:%s", i, answer.Wallet, answer.Votes)
|
||||
}
|
||||
|
||||
for _, player := range room.Players {
|
||||
|
@ -389,14 +389,14 @@ func VoteToFinishGame(roomId string) bool {
|
|||
var isFound = false
|
||||
for i, p := range room.Players {
|
||||
if p.Wallet.Equals(wallet) {
|
||||
if p.isVotedToFinish {
|
||||
if p.IsVotedToFinish {
|
||||
return false // Player is already voted to finish the game
|
||||
}
|
||||
room.Players[i].isVotedToFinish = true
|
||||
room.Players[i].IsVotedToFinish = true
|
||||
isFound = true
|
||||
}
|
||||
|
||||
if p.isVotedToFinish {
|
||||
if p.IsVotedToFinish {
|
||||
voted++ // Count voted players for finish the game
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue