Files
ruby-sea-battle/main-server/app/serializers/match_serializer.rb
T
2026-11-15 18:27:05 +03:00

41 lines
1.4 KiB
Ruby

# Serializes a Match as the JSON representation shared by the internal
# (game-server facing) and the player-facing endpoints. When `viewer` is
# given, the viewer's own rating delta is included under `your_rating_before`
# and `your_rating_change`.
class MatchSerializer
def self.as_json(match, viewer: nil)
match_json = {
id: match.uuid,
status: match.status,
game_server: match.game_server,
started_at: match.started_at,
finished_at: match.finished_at,
duration_seconds: match.duration_seconds,
player1: player_json(match.player1, match.player1_rating_before, match.player1_rating_change),
player2: player_json(match.player2, match.player2_rating_before, match.player2_rating_change)
}
if viewer
if viewer.id == match.player1_id
match_json[:your_rating_before] = match.player1_rating_before
match_json[:your_rating_change] = match.player1_rating_change
elsif viewer.id == match.player2_id
match_json[:your_rating_before] = match.player2_rating_before
match_json[:your_rating_change] = match.player2_rating_change
end
end
match_json
end
def self.player_json(user, rating_before, rating_change)
{
id: user.id,
nickname: user.nickname,
avatar_url: user.avatar_url,
rating_before: rating_before,
rating_change: rating_change
}
end
end