Skip to content

Commit 6cc6685

Browse files
committed
enable configuring multiple goals
1 parent cbbc590 commit 6cc6685

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

pkg/config/config.go

+11
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ func UpdateTime(starth, startm, duration, pause int) {
3535
ConfigData.GameDuration = time.Duration(duration) * time.Minute
3636
ConfigData.KOPause = time.Duration(pause) * time.Minute
3737
}
38+
39+
func UpdatePlaces(nr int) {
40+
fmt.Printf("updating places to %d\n", nr)
41+
places := []*string{}
42+
for i := 1; i <= nr; i++ {
43+
s := fmt.Sprintf("Tor %d", i)
44+
fmt.Printf("adding places %s\n", s)
45+
places = append(places, &s)
46+
}
47+
ConfigData.Places = places
48+
}

pkg/frontend/handlers.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/chrischdi/gotournament/pkg/matchday"
1818
"github.com/chrischdi/gotournament/pkg/schedule"
1919
"github.com/chrischdi/gotournament/pkg/team"
20+
"github.com/chrischdi/gotournament/tpl"
2021
)
2122

2223
func registerHandlers() {
@@ -62,7 +63,7 @@ func setup(w http.ResponseWriter, r *http.Request) {
6263

6364
fmt.Fprintf(w, "<h2>Time</h2>\n")
6465
t = template.New("timeSetup")
65-
t, err = template.ParseFiles(filepath.Join("tpl", "timeSetup.tmpl"))
66+
t, err = template.ParseFS(tpl.Content, "timeSetup.tmpl")
6667
if err != nil {
6768
fmt.Fprintf(os.Stderr, "error: %v\n", err)
6869
}
@@ -71,13 +72,14 @@ func setup(w http.ResponseWriter, r *http.Request) {
7172
"Start": fmt.Sprintf("%02d:%02d", config.ConfigData.Start.Hour(), config.ConfigData.Start.Minute()),
7273
"Duration": config.ConfigData.GameDuration.Minutes(),
7374
"Pause": config.ConfigData.KOPause.Minutes(),
75+
"NrPlaces": len(config.ConfigData.Places),
7476
})
7577

7678
writeTemplate(w, "modifyGroups.tmpl", data.GetData())
7779

7880
fmt.Fprintf(w, "<h2>Add Groups</h2>\n")
7981
t = template.New("addGroup")
80-
t, err = template.ParseFiles(filepath.Join("tpl", "addGroup.tmpl"))
82+
t, err = template.ParseFS(tpl.Content, "addGroup.tmpl")
8183
if err != nil {
8284
fmt.Fprintf(os.Stderr, "error: %v\n", err)
8385
}
@@ -253,7 +255,13 @@ func setupUpdate(w http.ResponseWriter, r *http.Request) error {
253255
data.SaveFile()
254256
// Tournament().UpdateTime(starth, startm, duration, pause)
255257
// Tournament().UpdateKOTime()
256-
258+
case "places":
259+
nrPlaces, err := strconv.Atoi(r.FormValue("nrplaces"))
260+
if err != nil {
261+
return fmt.Errorf("error updating time: %v", err)
262+
}
263+
config.UpdatePlaces(nrPlaces)
264+
schedule.CalculateGroupSchedule()
257265
case "group":
258266
for _, v := range strings.Split(r.FormValue("uuids"), ",") {
259267
err := team.UpdateTeamName(v, r.FormValue(v))

pkg/frontend/template.go

+4-14
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package frontend
33
import (
44
"fmt"
55
"net/http"
6-
"os"
7-
"path/filepath"
86
"text/template"
97

108
"github.com/chrischdi/gotournament/pkg/matchday"
9+
"github.com/chrischdi/gotournament/tpl"
1110

1211
"github.com/chrischdi/gotournament/pkg/config"
1312
"github.com/chrischdi/gotournament/pkg/game"
@@ -50,20 +49,11 @@ func allGroupGamesDone() bool {
5049
return true
5150
}
5251

53-
func writeTemplate(w http.ResponseWriter, tpl string, vars interface{}) error {
54-
s, err := os.Executable()
55-
if err != nil {
56-
fmt.Printf("error: %v", err)
57-
return err
58-
}
59-
dir, _ := filepath.Split(s)
60-
61-
path := filepath.Join(dir, "tpl", tpl)
62-
63-
t, err := template.New(tpl).Funcs(funcMap).ParseFiles(path)
52+
func writeTemplate(w http.ResponseWriter, tplName string, vars interface{}) error {
53+
t, err := template.New(tplName).Funcs(funcMap).ParseFS(tpl.Content, tplName)
6454
if err != nil {
6555
fmt.Printf("error: %v\n", err)
66-
return fmt.Errorf("error opening template %s: %v", tpl, err)
56+
return fmt.Errorf("error opening template %s: %v", tplName, err)
6757
}
6858

6959
err = t.Execute(w, vars)

tpl/embed.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tpl
2+
3+
import "embed"
4+
5+
// content holds our static web server content.
6+
//
7+
//go:embed *.tmpl
8+
var Content embed.FS

tpl/timeSetup.tmpl

+5
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44
<p>Pause to KO: <input type="number" name="pause" value="{{ .Pause }}" style="text-align: center; width: 3em;"/>minutes</p>
55
<p><input type="submit" value="Update"/></p>
66
</form>
7+
8+
<form action="/setup/update/places">
9+
<p>Nr Places/Goals: <input type="number" name="nrplaces" value="{{ .NrPlaces }}" style="text-align: center; width: 3em;"/>Goals</p>
10+
<p><input type="submit" value="Update"/></p>
11+
</form>

0 commit comments

Comments
 (0)