diff options
-rw-r--r-- | pkg/ui/views/api.go | 18 | ||||
-rw-r--r-- | pkg/ui/views/play.go | 2 | ||||
-rw-r--r-- | pkg/ui/views/play_api.go | 1 | ||||
-rw-r--r-- | pkg/ui/views/play_content.go | 2 | ||||
-rw-r--r-- | pkg/ui/views/play_util.go | 31 |
5 files changed, 52 insertions, 2 deletions
diff --git a/pkg/ui/views/api.go b/pkg/ui/views/api.go index 3788f91..ba202c8 100644 --- a/pkg/ui/views/api.go +++ b/pkg/ui/views/api.go @@ -6,6 +6,8 @@ import ( "fmt" "net/http" "os" + + "github.com/boozec/rahanna/internal/api/auth" ) // getAuthorizationToken reads the authentication token from the .rahannarc file @@ -29,6 +31,22 @@ func getAuthorizationToken() (string, error) { return authorization, nil } +// From a JWT token it returns the associated user ID +func getUserID() (int, error) { + token, err := getAuthorizationToken() + if err != nil { + return -1, err + } + + claims, err := auth.ValidateJWT("Bearer " + token) + if err != nil { + return -1, err + } + + return claims.UserID, nil + +} + // sendAPIRequest sends an HTTP request to the API with the given parameters func sendAPIRequest(method, url string, payload []byte, authorization string) (*http.Response, error) { req, err := http.NewRequest(method, url, bytes.NewReader(payload)) diff --git a/pkg/ui/views/play.go b/pkg/ui/views/play.go index a205c25..ed5f1e1 100644 --- a/pkg/ui/views/play.go +++ b/pkg/ui/views/play.go @@ -40,6 +40,7 @@ type PlayModel struct { paginator paginator.Model // Game state + userID int playName string currentGameId int game *database.Game @@ -85,6 +86,7 @@ func (m PlayModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case database.Game: return m.handleGameResponse(msg) case []database.Game: + m.userID, m.err = getUserID() return m.handleGamesResponse(msg) case StartGameMsg: return m, SwitchModelCmd(NewGameModel(m.width, m.height+1, "peer-2", m.currentGameId, m.network)) diff --git a/pkg/ui/views/play_api.go b/pkg/ui/views/play_api.go index 1fb02e5..7119ffa 100644 --- a/pkg/ui/views/play_api.go +++ b/pkg/ui/views/play_api.go @@ -216,6 +216,7 @@ func (m *PlayModel) fetchGames() tea.Cmd { if err := json.NewDecoder(resp.Body).Decode(&games); err != nil { return []database.Game{} } + return games } } diff --git a/pkg/ui/views/play_content.go b/pkg/ui/views/play_content.go index a128800..d6a327f 100644 --- a/pkg/ui/views/play_content.go +++ b/pkg/ui/views/play_content.go @@ -29,7 +29,7 @@ func (m PlayModel) renderPageContent(base lipgloss.Style) string { return base.Render(chessBoard) } else { start, end := m.paginator.GetSliceBounds(len(m.games)) - gamesStrings := formatGamesForPage(m.games[start:end], altCodeStyle) + gamesStrings := formatGamesForPage(m.userID, m.games[start:end], altCodeStyle) pageInfo := m.paginator.View() return base.Render(lipgloss.JoinVertical(lipgloss.Center, strings.Join(gamesStrings, "\n"), pageInfo)) } diff --git a/pkg/ui/views/play_util.go b/pkg/ui/views/play_util.go index c57f331..b2f70b5 100644 --- a/pkg/ui/views/play_util.go +++ b/pkg/ui/views/play_util.go @@ -21,7 +21,13 @@ func (m *PlayModel) handleError(msg error) (tea.Model, tea.Cmd) { return m, nil } -func formatGamesForPage(games []database.Game, altCodeStyle lipgloss.Style) []string { +var ( + winIcon = lipgloss.NewStyle().Foreground(lipgloss.Color("#f1c40f")).Render("🏆") + loseIcon = lipgloss.NewStyle().Foreground(lipgloss.Color("#895129")).Render("💩") + drawIcon = lipgloss.NewStyle().Foreground(lipgloss.Color("#bdc3c7")).Render("🤝") +) + +func formatGamesForPage(userID int, games []database.Game, altCodeStyle lipgloss.Style) []string { var gamesStrings []string gamesStrings = append(gamesStrings, "Games list") @@ -34,6 +40,28 @@ func formatGamesForPage(games []database.Game, altCodeStyle lipgloss.Style) []st for i, game := range games { indexStr := altCodeStyle.Render(fmt.Sprintf("[%d] ", i)) + icon := " " + + if game.Outcome != "*" { + if len(game.Outcome) >= 2 { + if game.Outcome[0:2] == "1-" { + if game.Player1.ID == userID { + icon = winIcon + } else { + icon = loseIcon + } + } else if game.Outcome[0:2] == "0-" { + if game.Player2 != nil && game.Player2.ID == userID { + icon = winIcon + } else { + icon = loseIcon + } + } else { + icon = drawIcon + } + } + } + nameStr := game.Name dateStr := game.UpdatedAt.Format("2006-01-02 15:04") @@ -44,6 +72,7 @@ func formatGamesForPage(games []database.Game, altCodeStyle lipgloss.Style) []st indexStr, nameStr, paddingStr, + icon, lipgloss.NewStyle().Foreground(lipgloss.Color("#d35400")).Render(dateStr), ) gamesStrings = append(gamesStrings, line) |