WIP: feat: scoreboard (#15) #31
							
								
								
									
										52
									
								
								internal/scoreboard/scoreboard.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								internal/scoreboard/scoreboard.go
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user