This repository has been archived on 2019-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
scoreboard/features/game/page.go

80 lines
1.4 KiB
Go
Raw Normal View History

2019-05-27 21:46:09 +00:00
package game
import (
"html/template"
"io"
"git.cliffbreak.de/haveachin/scoreboard/data"
)
type previewGame struct {
ID string
Name string
}
type master struct {
Template template.Template
Games []previewGame
}
type previewGroup struct {
ID string
Name string
Points int
}
type detail struct {
Template template.Template
Game data.Game
Groups []previewGroup
}
func newMasterPage(t template.Template, games []data.Game) *master {
previewGames := []previewGame{}
for _, game := range games {
previewGames = append(previewGames, previewGame{
ID: game.ID.Hex(),
Name: game.Name,
})
}
return &master{
Template: *t.Lookup("gameMaster.html"),
Games: previewGames,
}
}
func newDetailPage(t template.Template, game data.Game, groups []data.Group) *detail {
previewGroups := []previewGroup{}
for _, group := range groups {
points := 0
for _, score := range group.Scores {
if score.GameID == game.ID {
points = score.Points
break
}
}
previewGroups = append(previewGroups, previewGroup{
ID: group.ID.Hex(),
Name: group.Name,
Points: points,
})
}
return &detail{
Template: *t.Lookup("gameDetail.html"),
Game: game,
Groups: previewGroups,
}
}
func (page master) Send(w io.Writer) {
page.Template.Execute(w, page)
}
func (page detail) Send(w io.Writer) {
page.Template.Execute(w, page)
}