Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Category Groups #2

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quiz/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
Expand Down
8 changes: 4 additions & 4 deletions quiz/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions quiz/sheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
60 changes: 46 additions & 14 deletions quiz/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -87,14 +111,22 @@ 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
}
}
}
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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions webserver/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down