Simplify codebase and fix Enter key on home page

- Enter key now triggers createGame action on home page
- Remove redundant setNickname action from home page
- Remove unused code: join channel, Leave(), Stop() methods
- Consolidate ID generation into game.GenerateID()
- Remove unused CreatedAt field from Game struct
This commit is contained in:
Ryan Hamamura
2026-01-14 14:10:18 -10:00
parent 389fc12bf2
commit 63d0773ab5
6 changed files with 34 additions and 61 deletions

61
main.go
View File

@@ -1,9 +1,6 @@
package main
import (
"crypto/rand"
"encoding/hex"
"github.com/ryanhamamura/c4/game"
"github.com/ryanhamamura/c4/ui"
"github.com/ryanhamamura/via"
@@ -29,14 +26,6 @@ func main() {
v.Page("/", func(c *via.Context) {
nickname := c.Signal("")
setNickname := c.Action(func() {
name := nickname.String()
if name != "" {
c.Session().Set("nickname", name)
c.Sync()
}
})
createGame := c.Action(func() {
name := nickname.String()
if name == "" {
@@ -51,7 +40,7 @@ func main() {
c.View(func() h.H {
return ui.LobbyView(
nickname.Bind(),
setNickname.OnKeyDown("Enter"),
createGame.OnKeyDown("Enter"),
createGame.OnClick(),
)
})
@@ -66,8 +55,6 @@ func main() {
colSignal := c.Signal(0)
var gi *game.GameInstance
var player *game.Player
var playerJoined bool
var gameExists bool
// Look up game (may not exist during warmup or invalid ID)
@@ -75,6 +62,13 @@ func main() {
gi, gameExists = store.Get(gameID)
}
// Generate a stable player ID for this session
playerID := game.PlayerID(c.Session().GetString("player_id"))
if playerID == "" {
playerID = game.PlayerID(game.GenerateID(8))
c.Session().Set("player_id", string(playerID))
}
setNickname := c.Action(func() {
if gi == nil {
return
@@ -85,12 +79,13 @@ func main() {
}
c.Session().Set("nickname", name)
if !playerJoined {
player = &game.Player{
ID: game.PlayerID(generatePlayerID()),
// Try to join if not already in game
if gi.GetPlayerColor(playerID) == 0 {
player := &game.Player{
ID: playerID,
Nickname: name,
}
playerJoined = gi.Join(&game.PlayerSession{
gi.Join(&game.PlayerSession{
Player: player,
Sync: c,
})
@@ -99,20 +94,25 @@ func main() {
})
dropPiece := c.Action(func() {
if gi == nil || player == nil {
if gi == nil {
return
}
myColor := gi.GetPlayerColor(playerID)
if myColor == 0 {
return
}
col := colSignal.Int()
gi.DropPiece(col, player.Color)
gi.DropPiece(col, myColor)
c.Sync()
})
// If nickname exists in session and game exists, join immediately
if gameExists && sessionNickname != "" {
player = &game.Player{
ID: game.PlayerID(generatePlayerID()),
if gameExists && sessionNickname != "" && gi.GetPlayerColor(playerID) == 0 {
player := &game.Player{
ID: playerID,
Nickname: sessionNickname,
}
playerJoined = gi.Join(&game.PlayerSession{
gi.Join(&game.PlayerSession{
Player: player,
Sync: c,
})
@@ -125,8 +125,10 @@ func main() {
return h.Div()
}
// Need nickname first
if !playerJoined {
myColor := gi.GetPlayerColor(playerID)
// Need nickname first / not joined yet
if myColor == 0 {
return ui.NicknamePrompt(
nickname.Bind(),
setNickname.OnKeyDown("Enter"),
@@ -135,7 +137,6 @@ func main() {
}
g := gi.GetGame()
myColor := player.Color
// Create column click function
columnClick := func(col int) h.H {
@@ -164,12 +165,6 @@ func main() {
v.Start()
}
func generatePlayerID() string {
b := make([]byte, 8)
rand.Read(b)
return hex.EncodeToString(b)
}
const gameCSS = `
body { margin: 0; }