Skip to content

Commit

Permalink
fix null references
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias committed Jan 26, 2021
1 parent b9aa6eb commit 77dc2bb
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func (tg *Manager) NewGame() {
Question: questions[r.Intn(len(questions))],
StartTime: time.Now(),
}
log.Printf("Question: %s", tg.currentGame.Question.question)
log.Printf("Answer: %s", tg.currentGame.Question.answer)
log.Printf("[Question]: %s", tg.currentGame.Question.question)
log.Printf("[Answer]: %s", tg.currentGame.Question.answer)

tg.Send(fmt.Sprintf("Question: %s?", tg.currentGame.Question.question), nil)
tg.Send(fmt.Sprintf("[New Question]: %s?::..", tg.currentGame.Question.question), nil)
}

func (tg *Manager) CurrentGame() *Game {
Expand All @@ -57,9 +57,14 @@ func (tg *Manager) CurrentGame() *Game {

func (tg *Manager) TryAnswer(p *Player, a string) {
tg.mutex.Lock()
if tg.currentGame == nil {
tg.mutex.Unlock()
return
}

if !strings.EqualFold(tg.currentGame.Question.answer, a) {
tg.Send(fmt.Sprintf("..::Wrong Answer by: %s::..", p.Name), p)
tg.Send(fmt.Sprintf("[Wrong Answer]: %s", p.Name), p)
tg.Send(fmt.Sprintf("[Question]: %s?", tg.currentGame.Question.question), p)
tg.mutex.Unlock()
return
}
Expand All @@ -71,7 +76,7 @@ func (tg *Manager) TryAnswer(p *Player, a string) {
tg.Players[p.Id] = p
}

tg.Send(fmt.Sprintf("..::Correct Answer By: %s::..", p.Name), p)
tg.Send(fmt.Sprintf("[Correct Answer]: %s", p.Name), p)

tg.mutex.Unlock()
tg.NewGame()
Expand All @@ -80,16 +85,28 @@ func (tg *Manager) TryAnswer(p *Player, a string) {
func (tg *Manager) GetQuestion() {
tg.mutex.Lock()
defer tg.mutex.Unlock()
tg.Send(fmt.Sprintf("..::Current Question: %s? ::..", tg.currentGame.Question.question), nil)

if tg.currentGame == nil {
tg.Send("[NO GAME]", nil)
} else {
tg.Send(fmt.Sprintf("[Current Question]: %s? ", tg.CurrentGame().Question.question), nil)
}
}

func (tg *Manager) Skip() {
tg.mutex.Lock()
tg.Send(fmt.Sprintf("[Skipped] Answer: %s ", tg.currentGame.Question.answer), nil)
tg.mutex.Unlock()
tg.NewGame()
}

func (tg *Manager) GetScore(p *Player) {
tg.mutex.Lock()
defer tg.mutex.Unlock()
if player, found := tg.Players[p.Id]; found {
tg.Send(fmt.Sprintf("...::%s Score: %d", p.Name, player.Correct), nil)
tg.Send(fmt.Sprintf("[%s Score]: %d", p.Name, player.Correct), nil)
} else {
tg.Send(fmt.Sprintf("...::%s Score: 0", p.Name), nil)
tg.Send(fmt.Sprintf("[%s Score]: 0", p.Name), nil)
}
}
// communication
Expand Down

0 comments on commit 77dc2bb

Please sign in to comment.