Skip to content

Commit 14859a4

Browse files
committed
Merge branch 'main' into images
2 parents 18cec7c + 99deb10 commit 14859a4

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

quiz/connection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (c *Connection) NewGame(data []byte) error {
145145
var rounds []*Round
146146
for name, n := range gameData.Categories {
147147
cat := Categories.GetCategoryByName(name)
148-
if cat.Title == "" {
148+
if cat.ID == "" {
149149
return fmt.Errorf("create game: unknown category '%s'", name)
150150
}
151151
rounds = append(rounds, cat.GetRounds(n)...)

quiz/global.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ func FetchQuestions() (err error) {
4545
var data []byte
4646
data, err = json.MarshalIndent(cat, "", " ")
4747
if err != nil {
48-
log.Printf("Error marshaling category '%s': %v", cat.Title, err)
48+
log.Printf("Error marshaling category '%s': %v", cat.ID, err)
4949
continue
5050
}
5151
err = os.Mkdir("sheets/"+group.Title, os.ModeDir)
5252
if err != nil {
5353
if !errors.Is(err, os.ErrExist) {
54-
log.Printf("Error creting json file of category '%s/%s': %v", group.Title, cat.Title, err)
54+
log.Printf("Error creting json file of category '%s/%s': %v", group.Title, cat.ID, err)
5555
continue
5656
}
5757
}
58-
err = os.WriteFile("sheets/"+group.Title+"/"+cat.Title+".json", data, 0644)
58+
err = os.WriteFile("sheets/"+group.Title+"/"+cat.ID+".json", data, 0644)
5959
if err != nil {
60-
log.Printf("Error writing json file of category '%s/%s': %v", group.Title, cat.Title, err)
60+
log.Printf("Error writing json file of category '%s/%s': %v", group.Title, cat.ID, err)
6161
}
6262
}
6363
}

quiz/sheets.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func parseCategoryGroups(s *sheets.Sheet, categories map[int]CategoryGroup) {
7171

7272
hexColor := intFromColor(color)
7373
categoryGroup := categories[hexColor]
74+
categoryGroup.ID = row.Values[0].FormattedValue
7475
categoryGroup.Title = row.Values[1].FormattedValue
7576
categoryGroup.IsDev = row.Values[2].FormattedValue == "TRUE"
7677
categoryGroup.IsRelease = row.Values[3].FormattedValue == "TRUE"
@@ -79,13 +80,14 @@ func parseCategoryGroups(s *sheets.Sheet, categories map[int]CategoryGroup) {
7980
}
8081

8182
func parseSheet(s *sheets.Sheet) Category {
82-
category := Category{Title: s.Properties.Title}
83+
var category Category
84+
category.ID = s.Properties.Title
8385

8486
for rowNum, row := range s.Data[0].RowData {
8587
if rowNum <= 1 {
8688
if rowNum == 0 && len(row.Values) > 0 {
87-
// get description from first cell in first row
88-
category.Description = row.Values[0].FormattedValue
89+
// get title from first cell in first row
90+
category.Title = row.Values[0].FormattedValue
8991
}
9092
// ignore rest of 1st + 2nd row
9193
continue

quiz/types.go

+41-9
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,41 @@ type GameSummary struct {
2828
ChatWon int `json:"chat_won"`
2929
}
3030

31+
type CategoryGroupDefinition struct {
32+
ID string `json:"id"`
33+
Title string `json:"title"`
34+
IsDev bool `json:"-"`
35+
IsRelease bool `json:"-"`
36+
Categories []CategoryDefinition `json:"categories"`
37+
}
38+
3139
type CategoryGroup struct {
32-
Title string `json:"title"`
33-
IsDev bool `json:"-"`
34-
IsRelease bool `json:"-"`
40+
CategoryGroupDefinition
3541
Categories []Category `json:"categories"`
3642
}
3743

44+
func (cg CategoryGroup) GetDefinition() CategoryGroupDefinition {
45+
cg.CategoryGroupDefinition.Categories = make([]CategoryDefinition, len(cg.Categories))
46+
for i, c := range cg.Categories {
47+
cg.CategoryGroupDefinition.Categories[i] = c.GetDefinition()
48+
}
49+
return cg.CategoryGroupDefinition
50+
}
51+
52+
type CategoryDefinition struct {
53+
ID string `json:"id"`
54+
Title string `json:"title"`
55+
Count int `json:"count,omitempty"`
56+
}
57+
3858
type Category struct {
39-
Title string `json:"-"`
40-
Description string `json:"description"`
41-
Pool []*Question `json:"pool"`
59+
CategoryDefinition
60+
Pool []*Question `json:"pool"`
61+
}
62+
63+
func (c Category) GetDefinition() CategoryDefinition {
64+
c.CategoryDefinition.Count = len(c.Pool)
65+
return c.CategoryDefinition
4266
}
4367

4468
type Question struct {
@@ -66,7 +90,7 @@ type Round struct {
6690
Current int `json:"current_round"`
6791
Max int `json:"max_round"`
6892
Group string `json:"group"`
69-
Category string `json:"category"`
93+
Category CategoryDefinition `json:"category"`
7094
}
7195

7296
type RoundSummary struct {
@@ -88,14 +112,22 @@ var log = logger.New(logger.Writer(), "[WEB] ", logger.LstdFlags|logger.Lmsgpref
88112
func (cg categoryGroups) GetCategoryByName(name string) Category {
89113
for _, group := range cg {
90114
for _, c := range group.Categories {
91-
if c.Title == name {
115+
if c.ID == name {
92116
return c
93117
}
94118
}
95119
}
96120
return Category{}
97121
}
98122

123+
func (cg categoryGroups) GetDefinition() (definitions map[int]CategoryGroupDefinition) {
124+
definitions = make(map[int]CategoryGroupDefinition, len(cg))
125+
for color, group := range cg {
126+
definitions[color] = group.GetDefinition()
127+
}
128+
return definitions
129+
}
130+
99131
func (g Game) GetRoundSummary() RoundSummary {
100132
sum := RoundSummary{
101133
StreamerPoints: g.Summary.StreamerPoints,
@@ -198,7 +230,7 @@ func (c Category) GetRounds(n int) []*Round {
198230
continue
199231
}
200232
round := q.ToRound()
201-
round.Category = c.Title
233+
round.Category = c.GetDefinition()
202234
rounds = append(rounds, &round)
203235
}
204236
return rounds

webserver/handle.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,7 @@ func handleChat(w http.ResponseWriter, r *http.Request) {
178178
}
179179

180180
func handleCategory(w http.ResponseWriter, r *http.Request) {
181-
categories := make(map[string]int)
182-
for _, group := range quiz.Categories {
183-
for _, cat := range group.Categories {
184-
categories[cat.Title] = len(cat.Pool)
185-
}
186-
}
187-
188-
b, err := json.Marshal(categories)
181+
b, err := json.Marshal(quiz.Categories.GetDefinition())
189182
if err != nil {
190183
log.Printf("Failed to marshal categories: %v", err)
191184
w.WriteHeader(http.StatusInternalServerError)

0 commit comments

Comments
 (0)