Show user's active games on home page after login
This commit is contained in:
101
ui/gamelist.go
Normal file
101
ui/gamelist.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ryanhamamura/via/h"
|
||||
)
|
||||
|
||||
type GameListItem struct {
|
||||
ID string
|
||||
Status int
|
||||
OpponentName string
|
||||
IsMyTurn bool
|
||||
LastPlayed time.Time
|
||||
}
|
||||
|
||||
func GameList(games []GameListItem) h.H {
|
||||
if len(games) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var items []h.H
|
||||
for _, g := range games {
|
||||
items = append(items, gameListEntry(g))
|
||||
}
|
||||
|
||||
listItems := []h.H{h.Class("game-list-items")}
|
||||
listItems = append(listItems, items...)
|
||||
|
||||
return h.Div(h.Class("game-list"),
|
||||
h.H3(h.Text("Your Games")),
|
||||
h.Div(listItems...),
|
||||
)
|
||||
}
|
||||
|
||||
func gameListEntry(g GameListItem) h.H {
|
||||
statusText, statusClass := getStatusDisplay(g)
|
||||
|
||||
return h.A(
|
||||
h.Href("/game/"+g.ID),
|
||||
h.Class("game-entry"),
|
||||
h.Div(h.Class("game-entry-main"),
|
||||
h.Span(h.Class("opponent-name"), h.Text(getOpponentDisplay(g))),
|
||||
h.Span(h.Class("game-status "+statusClass), h.Text(statusText)),
|
||||
),
|
||||
h.Div(h.Class("game-entry-meta"),
|
||||
h.Span(h.Class("time-ago"), h.Text(formatTimeAgo(g.LastPlayed))),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func getStatusDisplay(g GameListItem) (string, string) {
|
||||
switch g.Status {
|
||||
case 0: // Waiting
|
||||
return "Waiting for opponent", "waiting"
|
||||
case 1: // In progress
|
||||
if g.IsMyTurn {
|
||||
return "Your turn!", "your-turn"
|
||||
}
|
||||
return "Opponent's turn", "opponent-turn"
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
||||
func getOpponentDisplay(g GameListItem) string {
|
||||
if g.OpponentName == "" {
|
||||
return "Waiting for opponent..."
|
||||
}
|
||||
return "vs " + g.OpponentName
|
||||
}
|
||||
|
||||
func formatTimeAgo(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return ""
|
||||
}
|
||||
duration := time.Since(t)
|
||||
|
||||
if duration < time.Minute {
|
||||
return "just now"
|
||||
}
|
||||
if duration < time.Hour {
|
||||
mins := int(duration.Minutes())
|
||||
if mins == 1 {
|
||||
return "1 minute ago"
|
||||
}
|
||||
return fmt.Sprintf("%d minutes ago", mins)
|
||||
}
|
||||
if duration < 24*time.Hour {
|
||||
hours := int(duration.Hours())
|
||||
if hours == 1 {
|
||||
return "1 hour ago"
|
||||
}
|
||||
return fmt.Sprintf("%d hours ago", hours)
|
||||
}
|
||||
days := int(duration.Hours() / 24)
|
||||
if days == 1 {
|
||||
return "yesterday"
|
||||
}
|
||||
return fmt.Sprintf("%d days ago", days)
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"github.com/ryanhamamura/via/h"
|
||||
)
|
||||
|
||||
func LobbyView(nicknameBind, createGameKeyDown, createGameClick h.H, isLoggedIn bool, username string, logoutClick h.H) h.H {
|
||||
func LobbyView(nicknameBind, createGameKeyDown, createGameClick h.H, isLoggedIn bool, username string, logoutClick h.H, userGames []GameListItem) h.H {
|
||||
var authSection h.H
|
||||
if isLoggedIn {
|
||||
authSection = AuthHeader(username, logoutClick)
|
||||
@@ -35,6 +35,7 @@ func LobbyView(nicknameBind, createGameKeyDown, createGameClick h.H, isLoggedIn
|
||||
createGameClick,
|
||||
),
|
||||
),
|
||||
GameList(userGames),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user