Skip to content

Commit 99deb10

Browse files
authored
Merge pull request #2 from cake4everyone/category-grouping
Category Groups
2 parents f96db01 + 689fac9 commit 99deb10

File tree

5 files changed

+57
-30
lines changed

5 files changed

+57
-30
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

+46-14
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,41 @@ type GameSummary struct {
2929
ChatWon int `json:"chat_won"`
3030
}
3131

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

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

4569
type Question struct {
@@ -61,12 +85,12 @@ const (
6185
)
6286

6387
type Round struct {
64-
Question string `json:"question"`
65-
Answers []string `json:"answers"`
66-
Correct int `json:"correct,omitempty"`
67-
Current int `json:"current_round"`
68-
Max int `json:"max_round"`
69-
Category string `json:"category"`
88+
Question string `json:"question"`
89+
Answers []string `json:"answers"`
90+
Correct int `json:"correct,omitempty"`
91+
Current int `json:"current_round"`
92+
Max int `json:"max_round"`
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)