package server

import (
	"log"
)

// deathmatch is a game type that resets when there is one bot standing. There
// is an obvious winner each round.
type deathmatch struct {
}

func (g *deathmatch) setup(gg *Game) {
}

func (g *deathmatch) gameOver(gg *Game) (bool, *GameOver) {
	over := false
	var stats *GameOver

	if gg.players_remaining <= 1 && len(gg.players) > 1 {
		if len(gg.defaultObstacles) == 0 {
			gg.obstacles = GenerateObstacles(
				gg.obstacleCount,
				gg.width,
				gg.height,
			)
		}
		log.Printf("game %s: game over", gg.id)
		stats = NewGameOver()

		for p := range gg.players {
			gg.stats.Lock()
			playerWin := false
			for _, r := range p.Robots {
				if r.Health > 0 {
					log.Printf("Robot %v Survived", r.Id)
					gg.stats.PlayerStats[p.Id].BotStats[r.Name].Wins += 1
					stats.Winners = append(stats.Winners, r.Id)
					playerWin = true
				}
				r.reset(gg)
			}
			if playerWin {
				gg.stats.PlayerStats[p.Id].Wins += 1
			}
			gg.stats.Unlock()
		}
		over = true
	}
	return over, stats
}

func (g *deathmatch) tick(gg *Game, payload *Boardstate) {
}
