From 62a7290de9ec645b3ff4b60c865b7371a19307b6 Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Sun, 1 Sep 2024 23:38:52 +0200 Subject: [PATCH 1/5] added category group sending --- quiz/types.go | 2 +- webserver/handle.go | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/quiz/types.go b/quiz/types.go index 7334478..9dc3bad 100644 --- a/quiz/types.go +++ b/quiz/types.go @@ -36,7 +36,7 @@ type CategoryGroup struct { } type Category struct { - Title string `json:"-"` + Title string `json:"title"` Description string `json:"description"` Pool []*Question `json:"pool"` } diff --git a/webserver/handle.go b/webserver/handle.go index 80bb96c..b524535 100644 --- a/webserver/handle.go +++ b/webserver/handle.go @@ -178,10 +178,20 @@ func handleChat(w http.ResponseWriter, r *http.Request) { } func handleCategory(w http.ResponseWriter, r *http.Request) { - categories := make(map[string]int) + type ResponseCategory struct { + Title string `json:"title"` + Description string `json:"description"` + Count int `json:"count"` + } + + categories := make(map[string][]ResponseCategory) for _, group := range quiz.Categories { for _, cat := range group.Categories { - categories[cat.Title] = len(cat.Pool) + responseCategory := ResponseCategory{ + Title: cat.Title, + Description: cat.Description, + Count: len(cat.Pool)} + categories[group.Title] = append(categories[group.Title], responseCategory) } } From 989c5d21f4cd5ecb49982dabea479da66d5a579a Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Sun, 8 Sep 2024 18:57:16 +0200 Subject: [PATCH 2/5] added group IDs --- quiz/sheets.go | 1 + quiz/types.go | 1 + webserver/handle.go | 19 +++++++++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/quiz/sheets.go b/quiz/sheets.go index 4653fb8..699060f 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" diff --git a/quiz/types.go b/quiz/types.go index 9dc3bad..dcc200a 100644 --- a/quiz/types.go +++ b/quiz/types.go @@ -29,6 +29,7 @@ type GameSummary struct { } type CategoryGroup struct { + ID string `json:"id"` Title string `json:"title"` IsDev bool `json:"-"` IsRelease bool `json:"-"` diff --git a/webserver/handle.go b/webserver/handle.go index b524535..23506f5 100644 --- a/webserver/handle.go +++ b/webserver/handle.go @@ -183,16 +183,27 @@ func handleCategory(w http.ResponseWriter, r *http.Request) { Description string `json:"description"` Count int `json:"count"` } - - categories := make(map[string][]ResponseCategory) - for _, group := range quiz.Categories { + type ResponseGroup struct { + Title string `json:"title"` + Color int `json:"color"` + Categories []ResponseCategory `json:"categories"` + } + + categories := make(map[string]ResponseGroup) + for color, group := range quiz.Categories { + responseGroup := ResponseGroup{ + Title: group.Title, + Color: color, + Categories: make([]ResponseCategory, 0, len(group.Categories)), + } for _, cat := range group.Categories { responseCategory := ResponseCategory{ Title: cat.Title, Description: cat.Description, Count: len(cat.Pool)} - categories[group.Title] = append(categories[group.Title], responseCategory) + responseGroup.Categories = append(responseGroup.Categories, responseCategory) } + categories[group.ID] = responseGroup } b, err := json.Marshal(categories) From 2c92ec119a204c6075c4709d984e2c360af2dd41 Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Sun, 8 Sep 2024 19:08:47 +0200 Subject: [PATCH 3/5] renamed category id fields --- quiz/connection.go | 2 +- quiz/global.go | 8 ++++---- quiz/sheets.go | 6 +++--- quiz/types.go | 10 +++++----- webserver/handle.go | 13 +++++++------ 5 files changed, 20 insertions(+), 19 deletions(-) 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 699060f..7baf06f 100644 --- a/quiz/sheets.go +++ b/quiz/sheets.go @@ -80,13 +80,13 @@ func parseCategoryGroups(s *sheets.Sheet, categories map[int]CategoryGroup) { } func parseSheet(s *sheets.Sheet) Category { - category := Category{Title: s.Properties.Title} + 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 dcc200a..5ccf858 100644 --- a/quiz/types.go +++ b/quiz/types.go @@ -37,9 +37,9 @@ type CategoryGroup struct { } type Category struct { - Title string `json:"title"` - Description string `json:"description"` - Pool []*Question `json:"pool"` + ID string `json:"id"` + Title string `json:"title"` + Pool []*Question `json:"pool"` } type Question struct { @@ -88,7 +88,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 } } @@ -198,7 +198,7 @@ func (c Category) GetRounds(n int) []*Round { continue } round := q.ToRound() - round.Category = c.Title + round.Category = c.ID rounds = append(rounds, &round) } return rounds diff --git a/webserver/handle.go b/webserver/handle.go index 23506f5..a195530 100644 --- a/webserver/handle.go +++ b/webserver/handle.go @@ -179,9 +179,9 @@ func handleChat(w http.ResponseWriter, r *http.Request) { func handleCategory(w http.ResponseWriter, r *http.Request) { type ResponseCategory struct { - Title string `json:"title"` - Description string `json:"description"` - Count int `json:"count"` + ID string `json:"id"` + Title string `json:"title"` + Count int `json:"count"` } type ResponseGroup struct { Title string `json:"title"` @@ -198,9 +198,10 @@ func handleCategory(w http.ResponseWriter, r *http.Request) { } for _, cat := range group.Categories { responseCategory := ResponseCategory{ - Title: cat.Title, - Description: cat.Description, - Count: len(cat.Pool)} + ID: cat.ID, + Title: cat.Title, + Count: len(cat.Pool), + } responseGroup.Categories = append(responseGroup.Categories, responseCategory) } categories[group.ID] = responseGroup From 5a60ee706759b3ef01baf0fe00fc4718d1184598 Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Sun, 8 Sep 2024 20:27:25 +0200 Subject: [PATCH 4/5] refactored category groups --- quiz/sheets.go | 3 ++- quiz/types.go | 45 ++++++++++++++++++++++++++++++++++++++------- webserver/handle.go | 31 +------------------------------ 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/quiz/sheets.go b/quiz/sheets.go index 7baf06f..08d66a3 100644 --- a/quiz/sheets.go +++ b/quiz/sheets.go @@ -80,7 +80,8 @@ func parseCategoryGroups(s *sheets.Sheet, categories map[int]CategoryGroup) { } func parseSheet(s *sheets.Sheet) Category { - category := Category{ID: s.Properties.Title} + var category Category + category.ID = s.Properties.Title for rowNum, row := range s.Data[0].RowData { if rowNum <= 1 { diff --git a/quiz/types.go b/quiz/types.go index 5ccf858..752dba2 100644 --- a/quiz/types.go +++ b/quiz/types.go @@ -28,18 +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 { - ID string `json:"id"` - 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 { - ID string `json:"id"` - Title string `json:"title"` - 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 { @@ -96,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, diff --git a/webserver/handle.go b/webserver/handle.go index a195530..fd70ffc 100644 --- a/webserver/handle.go +++ b/webserver/handle.go @@ -178,36 +178,7 @@ func handleChat(w http.ResponseWriter, r *http.Request) { } func handleCategory(w http.ResponseWriter, r *http.Request) { - type ResponseCategory struct { - ID string `json:"id"` - Title string `json:"title"` - Count int `json:"count"` - } - type ResponseGroup struct { - Title string `json:"title"` - Color int `json:"color"` - Categories []ResponseCategory `json:"categories"` - } - - categories := make(map[string]ResponseGroup) - for color, group := range quiz.Categories { - responseGroup := ResponseGroup{ - Title: group.Title, - Color: color, - Categories: make([]ResponseCategory, 0, len(group.Categories)), - } - for _, cat := range group.Categories { - responseCategory := ResponseCategory{ - ID: cat.ID, - Title: cat.Title, - Count: len(cat.Pool), - } - responseGroup.Categories = append(responseGroup.Categories, responseCategory) - } - categories[group.ID] = responseGroup - } - - 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) From 689fac9fb96acffd1a04d1ca5401a0e204fa86f7 Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Fri, 13 Sep 2024 21:00:24 +0200 Subject: [PATCH 5/5] including whole category in round data --- quiz/types.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/quiz/types.go b/quiz/types.go index 752dba2..7492e3b 100644 --- a/quiz/types.go +++ b/quiz/types.go @@ -84,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 { @@ -229,7 +229,7 @@ func (c Category) GetRounds(n int) []*Round { continue } round := q.ToRound() - round.Category = c.ID + round.Category = c.GetDefinition() rounds = append(rounds, &round) } return rounds