added simple game stats
This commit is contained in:
@@ -40,6 +40,11 @@ func (ml *MapLock) add(g *game) {
|
||||
ml.Unlock()
|
||||
}
|
||||
|
||||
type WinnerMap struct {
|
||||
m map[string]int
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type game struct {
|
||||
id string
|
||||
players map[*player]bool
|
||||
@@ -58,27 +63,30 @@ type game struct {
|
||||
repair_hp int
|
||||
repair_rate float32
|
||||
tick_duration int
|
||||
winners WinnerMap
|
||||
}
|
||||
|
||||
func NewGame(id string, width, height float32, tick int) *game {
|
||||
g := &game{
|
||||
id: id,
|
||||
register: make(chan *player),
|
||||
unregister: make(chan *player, maxPlayer),
|
||||
projectiles: make(map[*Projectile]bool),
|
||||
splosions: make(map[*Splosion]bool),
|
||||
obstacles: GenerateObstacles(conf.Obstacles, width, height),
|
||||
players: make(map[*player]bool),
|
||||
turn: 0,
|
||||
width: width,
|
||||
height: height,
|
||||
spectators: make(map[*Spectator]bool),
|
||||
sregister: make(chan *Spectator),
|
||||
sunregister: make(chan *Spectator),
|
||||
kill: make(chan bool, maxPlayer),
|
||||
repair_hp: 5,
|
||||
repair_rate: 3.0,
|
||||
tick_duration: tick,
|
||||
id: id,
|
||||
register: make(chan *player),
|
||||
unregister: make(chan *player, maxPlayer),
|
||||
projectiles: make(map[*Projectile]bool),
|
||||
splosions: make(map[*Splosion]bool),
|
||||
obstacles: GenerateObstacles(conf.Obstacles, width, height),
|
||||
players: make(map[*player]bool),
|
||||
turn: 0,
|
||||
width: width,
|
||||
height: height,
|
||||
spectators: make(map[*Spectator]bool),
|
||||
sregister: make(chan *Spectator),
|
||||
sunregister: make(chan *Spectator),
|
||||
kill: make(chan bool, maxPlayer),
|
||||
repair_hp: 5,
|
||||
repair_rate: 3.0,
|
||||
tick_duration: tick,
|
||||
players_remaining: 2,
|
||||
winners: WinnerMap{m: make(map[string]int)},
|
||||
}
|
||||
return g
|
||||
}
|
||||
@@ -284,16 +292,21 @@ func (g *game) run() {
|
||||
|
||||
func (g *game) gameOver() (bool, *GameOver) {
|
||||
over := false
|
||||
stats := NewGameOver()
|
||||
var stats *GameOver
|
||||
|
||||
if g.players_remaining <= 1 && len(g.players) > 1 {
|
||||
g.obstacles = GenerateObstacles(conf.Obstacles, g.width, g.height)
|
||||
log.Printf("game %s: game over", g.id)
|
||||
stats = NewGameOver()
|
||||
|
||||
for p := range g.players {
|
||||
for _, r := range p.Robots {
|
||||
if r.Health > 0 {
|
||||
log.Printf("Robot %v Survived", r.Id)
|
||||
g.winners.Lock()
|
||||
g.winners.m[r.Id] += 1
|
||||
g.winners.Unlock()
|
||||
stats.Winners = append(stats.Winners, r.Id)
|
||||
}
|
||||
r.reset(g)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user