package pages
import (
"fmt"
"github.com/ryanhamamura/games/chat"
chatcomponents "github.com/ryanhamamura/games/chat/components"
"github.com/ryanhamamura/games/features/common/components"
"github.com/ryanhamamura/games/features/common/layouts"
snakecomponents "github.com/ryanhamamura/games/features/snakegame/components"
"github.com/ryanhamamura/games/snake"
"github.com/starfederation/datastar-go/datastar"
)
// keydownScript builds the inline JS for a single data-on:keydown handler
// that dispatches WASD/arrow keys to direction POST endpoints.
func keydownScript(gameID string) string {
return fmt.Sprintf(
"const k=evt.key;"+
"if(k==='w'||k==='ArrowUp'){evt.preventDefault();%s}"+
"else if(k==='s'||k==='ArrowDown'){evt.preventDefault();%s}"+
"else if(k==='a'||k==='ArrowLeft'){evt.preventDefault();%s}"+
"else if(k==='d'||k==='ArrowRight'){evt.preventDefault();%s}",
datastar.PostSSE("/snake/%s/dir?d=0", gameID),
datastar.PostSSE("/snake/%s/dir?d=1", gameID),
datastar.PostSSE("/snake/%s/dir?d=2", gameID),
datastar.PostSSE("/snake/%s/dir?d=3", gameID),
)
}
templ GamePage(sg *snake.SnakeGame, mySlot int, messages []chat.Message, chatCfg chatcomponents.Config, gameID string) {
@layouts.Base("Snake") {