Generated _templ.go files are deterministic output from .templ sources, same as output.css from input.css. Remove them from version control to reduce diff noise and merge conflicts. Add build:templ and live:templ tasks to the Taskfile so generation happens as part of the build.
66 lines
1.2 KiB
Plaintext
66 lines
1.2 KiB
Plaintext
package components
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/ryanhamamura/c4/game"
|
|
"github.com/starfederation/datastar-go/datastar"
|
|
)
|
|
|
|
templ Board(g *game.Game, myColor int) {
|
|
<div id="c4-board" class="board">
|
|
for col := 0; col < 7; col++ {
|
|
@column(g, col, myColor)
|
|
}
|
|
</div>
|
|
}
|
|
|
|
templ column(g *game.Game, colIdx int, myColor int) {
|
|
if g.Status == game.StatusInProgress && myColor == g.CurrentTurn {
|
|
<div
|
|
class="column clickable"
|
|
data-on:click={ datastar.PostSSE("/games/%s/drop?col=%d", g.ID, colIdx) }
|
|
>
|
|
for row := 0; row < 6; row++ {
|
|
@cell(g, row, colIdx)
|
|
}
|
|
</div>
|
|
} else {
|
|
<div class="column">
|
|
for row := 0; row < 6; row++ {
|
|
@cell(g, row, colIdx)
|
|
}
|
|
</div>
|
|
}
|
|
}
|
|
|
|
templ cell(g *game.Game, row int, col int) {
|
|
<div class={ cellClass(g, row, col) }></div>
|
|
}
|
|
|
|
func cellClass(g *game.Game, row, col int) string {
|
|
color := g.Board[row][col]
|
|
activeTurn := 0
|
|
if g.Status == game.StatusInProgress {
|
|
activeTurn = g.CurrentTurn
|
|
}
|
|
|
|
class := "cell"
|
|
switch color {
|
|
case 1:
|
|
class += " red"
|
|
case 2:
|
|
class += " yellow"
|
|
}
|
|
if g.IsWinningCell(row, col) {
|
|
class += " winning"
|
|
}
|
|
if color != 0 && color == activeTurn {
|
|
class += " active-turn"
|
|
}
|
|
return class
|
|
}
|
|
|
|
// suppress unused import
|
|
var _ = fmt.Sprintf
|