JOJ3/internal/scoreboard/scoreboard.go
张佳澈520370910044 625f123e18
All checks were successful
continuous-integration/drone/push Build is passing
feat(internal/scoreboard/scoreboard.go): skeleton of scoreboard
2024-06-03 00:50:31 +08:00

53 lines
1.2 KiB
Go

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)
}