Skip to content

Commit

Permalink
seznam vseh replayev - resolves #49
Browse files Browse the repository at this point in the history
  • Loading branch information
mytja committed Oct 30, 2023
1 parent 5b1f855 commit 2971cad
Show file tree
Hide file tree
Showing 15 changed files with 1,218 additions and 937 deletions.
18 changes: 18 additions & 0 deletions backend/internal/lobby/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lobby

import (
"fmt"
"github.com/gorilla/websocket"
"github.com/mytja/Tarok/backend/internal/helpers"
"github.com/mytja/Tarok/backend/internal/lobby_messages"
Expand Down Expand Up @@ -399,6 +400,23 @@ func (s *serverImpl) Authenticated(client Client) {
},
})
}

replays, err := s.db.GetGamesByUserID(id)
if err != nil {
return
}

for _, v := range replays {
client.Send(&lobby_messages.LobbyMessage{
Data: &lobby_messages.LobbyMessage_Replay{
Replay: &lobby_messages.Replay{
Url: fmt.Sprintf("https://palcka.si/replay/%s?password=%s", v.GameID, v.Password),
GameId: v.GameID,
CreatedAt: v.CreatedAt,
},
},
})
}
}

func (s *serverImpl) GetDB() sql.SQL {
Expand Down
308 changes: 204 additions & 104 deletions backend/internal/lobby_messages/lobby_messages.pb.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions backend/internal/sql/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func (db *sqlImpl) GetGame(id string) (game Game, err error) {
return game, err
}

func (db *sqlImpl) GetGamesByUserID(id string) (games []Game, err error) {
err = db.db.Select(&games, "SELECT * FROM game_user WHERE user_id=$1 ORDER BY created_at DESC", id)
return games, err
}

func (db *sqlImpl) InsertGame(game Game) (err error) {
_, err = db.db.NamedExec(
"INSERT INTO game_user (user_id, game_id, messages, password) VALUES (:user_id, :game_id, :messages, :password)",
Expand Down
1 change: 1 addition & 0 deletions backend/internal/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type SQL interface {
DeleteFriends(ID string) error

GetGame(id string) (game Game, err error)
GetGamesByUserID(id string) (games []Game, err error)
InsertGame(game Game) (err error)
GetGames() (games []Game, err error)
}
Expand Down
5 changes: 5 additions & 0 deletions backend/internal/ws/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (s *serverImpl) NextReplayStep(gameId string) {
return
}

if len(game.ReplayMessages) <= game.ReplayGame {
s.logger.Debugw("ne morem pridobiti igre", "replayState", game.ReplayState, "g", game.ReplayGame, "msg", len(game.ReplayMessages))
return
}

g := game.ReplayMessages[game.ReplayGame]

game.ReplayState++
Expand Down
8 changes: 8 additions & 0 deletions messages/lobby_messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ message FriendRequestAcceptDecline {
message FriendRequestSend { string email = 1; }
message RemoveFriend { string relationshipId = 1; }

message Replay {
string url = 1;
string gameId = 2;
string createdAt = 3;
}

message LobbyMessage {
string player_id = 1;

Expand All @@ -105,5 +111,7 @@ message LobbyMessage {
FriendRequestAcceptDecline friend_request_accept_decline = 22;
FriendRequestSend friend_request_send = 23;
RemoveFriend remove_friend = 24;

Replay replay = 25;
}
}
Loading

0 comments on commit 2971cad

Please sign in to comment.