package components
import (
"fmt"
"math"
"time"
"github.com/ryanhamamura/games/config"
"github.com/ryanhamamura/games/snake"
"github.com/starfederation/datastar-go/datastar"
)
templ StatusBanner(sg *snake.SnakeGame, mySlot int, gameID string) {
switch sg.Status {
case snake.StatusWaitingForPlayers:
if sg.Mode == snake.ModeSinglePlayer {
Ready?
} else {
Waiting for players...
}
case snake.StatusCountdown:
{{ remaining := time.Until(sg.CountdownEnd) }}
{{ secs := int(math.Ceil(remaining.Seconds())) }}
if secs < 0 {
{{ secs = 0 }}
}
{ fmt.Sprintf("Starting in %d...", secs) }
case snake.StatusInProgress:
if sg.State != nil && mySlot >= 0 && mySlot < len(sg.State.Snakes) && sg.State.Snakes[mySlot] != nil && !sg.State.Snakes[mySlot].Alive {
You're out!
} else if sg.Mode == snake.ModeSinglePlayer {
{ fmt.Sprintf("Score: %d", sg.Score) }
} else {
Go!
}
case snake.StatusFinished:
@finishedBanner(sg, mySlot, gameID)
}
}
templ finishedBanner(sg *snake.SnakeGame, mySlot int, gameID string) {
if sg.Mode == snake.ModeSinglePlayer {
{ fmt.Sprintf("Game Over! Score: %d", sg.Score) }
@rematchOrJoin(sg, gameID)
} else if sg.Winner != nil {
if sg.Winner.Slot == mySlot {
You win!
@rematchOrJoin(sg, gameID)
} else {
{ sg.Winner.Nickname + " wins!" }
@rematchOrJoin(sg, gameID)
}
} else {
It's a draw!
@rematchOrJoin(sg, gameID)
}
}
templ rematchOrJoin(sg *snake.SnakeGame, gameID string) {
if sg.RematchGameID != nil {
Join Rematch
} else {
}
}
templ PlayerList(sg *snake.SnakeGame, mySlot int) {
for i, p := range sg.Players {
if p != nil {
{ p.Nickname }
if i == mySlot {
{ " (You)" }
}
if sg.Status == snake.StatusInProgress || sg.Status == snake.StatusFinished {
if sg.State != nil && i < len(sg.State.Snakes) && sg.State.Snakes[i] != nil {
if sg.State.Snakes[i].Alive {
{ fmt.Sprintf("(%d)", len(sg.State.Snakes[i].Body)) }
} else {
(dead)
}
}
}
}
}
}
templ InviteLink(gameID string) {
{{ fullURL := config.Global.AppURL + "/snake/" + gameID }}
Share this link to invite players:
{ fullURL }
}
script copyToClipboard(url string) {
navigator.clipboard.writeText(url)
}