Skip to content

Commit

Permalink
preklopi na lobby namesto http pollinga - linked to #44
Browse files Browse the repository at this point in the history
rejoin igralcev - resolves #47
končanje igre v primeru disconnectanih igralcev - resolves #48
  • Loading branch information
mytja committed Oct 30, 2023
1 parent 1bab5c7 commit b63b7c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
13 changes: 10 additions & 3 deletions backend/internal/ws/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ func (s *serverImpl) Results(gameId string) {
s.logger.Debugw("radelci dodani vsem udeležencem igre")

go func() {
for i := 0; i <= 10; i++ {
for i := 0; i <= 15; i++ {
s.Broadcast(
"",
gameId,
&messages.Message{
Data: &messages.Message_GameStartCountdown{GameStartCountdown: &messages.GameStartCountdown{Countdown: int32(10 - i)}},
Data: &messages.Message_GameStartCountdown{GameStartCountdown: &messages.GameStartCountdown{Countdown: int32(15 - i)}},
},
)
time.Sleep(time.Second)
Expand All @@ -113,7 +113,14 @@ func (s *serverImpl) Results(gameId string) {
}
}
if (game.GameCount == game.GamesRequired || game.GamesRequired == -1) && !game.Replay {
if game.VotedAdditionOfGames <= 0 && game.GamesRequired != -1 {
noClients := 0
for _, v := range game.Players {
if !v.GetBotStatus() && len(v.GetClients()) == 0 {
noClients++
}
}

if noClients > 0 || (game.VotedAdditionOfGames <= 0 && game.GamesRequired != -1) {
s.EndGame(gameId)
return
}
Expand Down
27 changes: 18 additions & 9 deletions backend/internal/ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ type GameDescriptor struct {
type SimpleUser struct {
ID string
Name string
Bot bool
}

func (s *serverImpl) GetGames(userId string) ([]GameDescriptor, []GameDescriptor) {
Expand All @@ -548,12 +549,10 @@ func (s *serverImpl) GetGames(userId string) ([]GameDescriptor, []GameDescriptor

players := make([]SimpleUser, 0)
for _, p := range v.Players {
if !p.GetBotStatus() && len(p.GetClients()) == 0 {
continue
}
players = append(players, SimpleUser{
ID: p.GetUser().ID,
Name: p.GetUser().Name,
Bot: p.GetBotStatus(),
})
}
desc := GameDescriptor{
Expand All @@ -570,8 +569,8 @@ func (s *serverImpl) GetGames(userId string) ([]GameDescriptor, []GameDescriptor
NapovedanMondfang: v.NapovedanMondfang,
KontraKazen: v.KazenZaKontro,
}
if v.Private {
if !helpers.Contains(v.InvitedPlayers, userId) {
if v.Private || v.Started {
if v.Private && !(helpers.Contains(v.InvitedPlayers, userId) || v.Owner == userId) {
continue
}
priorityGames = append(priorityGames, desc)
Expand Down Expand Up @@ -644,10 +643,6 @@ func (s *serverImpl) sendPlayers(client Client) {
sent := make([]string, 0)

for _, user := range game.Players {
if len(user.GetClients()) == 0 {
continue
}

if client.GetUserID() == user.GetUser().ID {
continue
}
Expand All @@ -671,5 +666,19 @@ func (s *serverImpl) sendPlayers(client Client) {
}

client.Send(msg)

if len(user.GetClients()) == 0 {
client.Send(&messages.Message{
PlayerId: user.GetUser().ID,
Data: &messages.Message_Connection{
Connection: &messages.Connection{
Rating: int32(user.GetUser().Rating),
Type: &messages.Connection_Disconnect{
Disconnect: &messages.Disconnect{},
},
},
},
})
}
}
}

0 comments on commit b63b7c5

Please sign in to comment.