From 625f123e18d3bf68c23485e809b3b1d22801af0e Mon Sep 17 00:00:00 2001 From: zjc_he Date: Mon, 3 Jun 2024 00:50:31 +0800 Subject: [PATCH] feat(internal/scoreboard/scoreboard.go): skeleton of scoreboard --- internal/scoreboard/scoreboard.go | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 internal/scoreboard/scoreboard.go diff --git a/internal/scoreboard/scoreboard.go b/internal/scoreboard/scoreboard.go new file mode 100644 index 0000000..d306ee0 --- /dev/null +++ b/internal/scoreboard/scoreboard.go @@ -0,0 +1,52 @@ +package scoreboard + +import ( + "encoding/json" + "os" +) + +type Question struct { + Name string `json:"name"` + Score int `json:"score"` + Comment string `json:"comment"` +} + +type ScoreboardData struct { + StudentName string `json:"studentname"` + StudentId string `json:"studentid"` + Questions []Question `json:"questions"` +} + +type Scoreboard struct { + scoreboard ScoreboardData +} + +func (b *Scoreboard) Init(studentName string, studentId string) { + b.scoreboard.StudentName = studentName + b.scoreboard.StudentId = studentId + b.scoreboard.Questions = make([]Question, 0) +} + +func (b *Scoreboard) AddScore(questionName string, score int) { + flag := false + for i, question := range b.scoreboard.Questions { + if question.Name == questionName { + b.scoreboard.Questions[i].Score = score + flag = true + break + } + } + if flag { + b.scoreboard.Questions = append(b.scoreboard.Questions, Question{Name: questionName, Score: score}) + } +} + +func (b *Scoreboard) SaveFile(filePath string) { + json_file, _ := os.Create(filePath) + defer json_file.Close() + + encoder := json.NewEncoder(json_file) + encoder.SetEscapeHTML(false) + encoder.SetIndent("", " ") + _ = encoder.Encode(b.scoreboard) +}