diff --git a/quiz/connection.go b/quiz/connection.go index 61cd8dc..a699077 100644 --- a/quiz/connection.go +++ b/quiz/connection.go @@ -145,7 +145,7 @@ func (c *Connection) NewGame(data []byte) error { var rounds []*Round for name, n := range gameData.Categories { cat := Categories.GetCategoryByName(name) - if cat.Title == "" { + if cat.ID == "" { return fmt.Errorf("create game: unknown category '%s'", name) } rounds = append(rounds, cat.GetRounds(n)...) diff --git a/quiz/global.go b/quiz/global.go index 795e126..4fd467a 100644 --- a/quiz/global.go +++ b/quiz/global.go @@ -45,19 +45,19 @@ func FetchQuestions() (err error) { var data []byte data, err = json.MarshalIndent(cat, "", " ") if err != nil { - log.Printf("Error marshaling category '%s': %v", cat.Title, err) + log.Printf("Error marshaling category '%s': %v", cat.ID, err) continue } err = os.Mkdir("sheets/"+group.Title, os.ModeDir) if err != nil { if !errors.Is(err, os.ErrExist) { - log.Printf("Error creting json file of category '%s/%s': %v", group.Title, cat.Title, err) + log.Printf("Error creting json file of category '%s/%s': %v", group.Title, cat.ID, err) continue } } - err = os.WriteFile("sheets/"+group.Title+"/"+cat.Title+".json", data, 0644) + err = os.WriteFile("sheets/"+group.Title+"/"+cat.ID+".json", data, 0644) if err != nil { - log.Printf("Error writing json file of category '%s/%s': %v", group.Title, cat.Title, err) + log.Printf("Error writing json file of category '%s/%s': %v", group.Title, cat.ID, err) } } } diff --git a/quiz/sheets.go b/quiz/sheets.go index 4653fb8..08d66a3 100644 --- a/quiz/sheets.go +++ b/quiz/sheets.go @@ -71,6 +71,7 @@ func parseCategoryGroups(s *sheets.Sheet, categories map[int]CategoryGroup) { hexColor := intFromColor(color) categoryGroup := categories[hexColor] + categoryGroup.ID = row.Values[0].FormattedValue categoryGroup.Title = row.Values[1].FormattedValue categoryGroup.IsDev = row.Values[2].FormattedValue == "TRUE" categoryGroup.IsRelease = row.Values[3].FormattedValue == "TRUE" @@ -79,13 +80,14 @@ func parseCategoryGroups(s *sheets.Sheet, categories map[int]CategoryGroup) { } func parseSheet(s *sheets.Sheet) Category { - category := Category{Title: s.Properties.Title} + var category Category + category.ID = s.Properties.Title for rowNum, row := range s.Data[0].RowData { if rowNum <= 1 { if rowNum == 0 && len(row.Values) > 0 { - // get description from first cell in first row - category.Description = row.Values[0].FormattedValue + // get title from first cell in first row + category.Title = row.Values[0].FormattedValue } // ignore rest of 1st + 2nd row continue diff --git a/quiz/types.go b/quiz/types.go index 7334478..7492e3b 100644 --- a/quiz/types.go +++ b/quiz/types.go @@ -28,17 +28,41 @@ type GameSummary struct { ChatWon int `json:"chat_won"` } +type CategoryGroupDefinition struct { + ID string `json:"id"` + Title string `json:"title"` + IsDev bool `json:"-"` + IsRelease bool `json:"-"` + Categories []CategoryDefinition `json:"categories"` +} + type CategoryGroup struct { - Title string `json:"title"` - IsDev bool `json:"-"` - IsRelease bool `json:"-"` + CategoryGroupDefinition Categories []Category `json:"categories"` } +func (cg CategoryGroup) GetDefinition() CategoryGroupDefinition { + cg.CategoryGroupDefinition.Categories = make([]CategoryDefinition, len(cg.Categories)) + for i, c := range cg.Categories { + cg.CategoryGroupDefinition.Categories[i] = c.GetDefinition() + } + return cg.CategoryGroupDefinition +} + +type CategoryDefinition struct { + ID string `json:"id"` + Title string `json:"title"` + Count int `json:"count,omitempty"` +} + type Category struct { - Title string `json:"-"` - Description string `json:"description"` - Pool []*Question `json:"pool"` + CategoryDefinition + Pool []*Question `json:"pool"` +} + +func (c Category) GetDefinition() CategoryDefinition { + c.CategoryDefinition.Count = len(c.Pool) + return c.CategoryDefinition } type Question struct { @@ -60,12 +84,12 @@ const ( ) type Round struct { - Question string `json:"question"` - Answers []string `json:"answers"` - Correct int `json:"correct,omitempty"` - Current int `json:"current_round"` - Max int `json:"max_round"` - Category string `json:"category"` + Question string `json:"question"` + Answers []string `json:"answers"` + Correct int `json:"correct,omitempty"` + Current int `json:"current_round"` + Max int `json:"max_round"` + Category CategoryDefinition `json:"category"` } type RoundSummary struct { @@ -87,7 +111,7 @@ var log = logger.New(logger.Writer(), "[WEB] ", logger.LstdFlags|logger.Lmsgpref func (cg categoryGroups) GetCategoryByName(name string) Category { for _, group := range cg { for _, c := range group.Categories { - if c.Title == name { + if c.ID == name { return c } } @@ -95,6 +119,14 @@ func (cg categoryGroups) GetCategoryByName(name string) Category { return Category{} } +func (cg categoryGroups) GetDefinition() (definitions map[int]CategoryGroupDefinition) { + definitions = make(map[int]CategoryGroupDefinition, len(cg)) + for color, group := range cg { + definitions[color] = group.GetDefinition() + } + return definitions +} + func (g Game) GetRoundSummary() RoundSummary { sum := RoundSummary{ StreamerPoints: g.Summary.StreamerPoints, @@ -197,7 +229,7 @@ func (c Category) GetRounds(n int) []*Round { continue } round := q.ToRound() - round.Category = c.Title + round.Category = c.GetDefinition() rounds = append(rounds, &round) } return rounds diff --git a/webserver/handle.go b/webserver/handle.go index 80bb96c..fd70ffc 100644 --- a/webserver/handle.go +++ b/webserver/handle.go @@ -178,14 +178,7 @@ func handleChat(w http.ResponseWriter, r *http.Request) { } func handleCategory(w http.ResponseWriter, r *http.Request) { - categories := make(map[string]int) - for _, group := range quiz.Categories { - for _, cat := range group.Categories { - categories[cat.Title] = len(cat.Pool) - } - } - - b, err := json.Marshal(categories) + b, err := json.Marshal(quiz.Categories.GetDefinition()) if err != nil { log.Printf("Failed to marshal categories: %v", err) w.WriteHeader(http.StatusInternalServerError)