diff options
author | Zach Latta <zach@zachlatta.com> | 2016-03-06 05:51:53 -0500 |
---|---|---|
committer | Zach Latta <zach@zachlatta.com> | 2016-03-06 05:51:53 -0500 |
commit | 127d885494fb2f9da76f5043dee54b6c0ac1e427 (patch) | |
tree | c31d341c938387515f020514d4b68f046fdd9ab6 | |
parent | 753d276b82a12b84fe7b3c0a6ea14fd8381978c4 (diff) | |
download | sshtron-127d885494fb2f9da76f5043dee54b6c0ac1e427.tar.gz |
Don't let people share colors
-rw-r--r-- | game.go | 26 | ||||
-rw-r--r-- | main.go | 3 |
2 files changed, 26 insertions, 3 deletions
@@ -493,6 +493,26 @@ func (g *Game) WorldHeight() int { return len(g.level[0]) } +func (g *Game) AvailableColors() []color.Attribute { + usedColors := map[color.Attribute]bool{} + for _, color := range playerColors { + usedColors[color] = false + } + + for player := range g.players() { + usedColors[player.Color] = true + } + + availableColors := []color.Attribute{} + for color, used := range usedColors { + if !used { + availableColors = append(availableColors, color) + } + } + + return availableColors +} + func (g *Game) Run() { // Proxy g.Redraw's channel to g.hub.Redraw go func() { @@ -589,9 +609,11 @@ type Session struct { Player *Player } -func NewSession(c ssh.Channel, worldWidth, worldHeight int) *Session { +func NewSession(c ssh.Channel, worldWidth, worldHeight int, + color color.Attribute) *Session { + s := Session{c: c} - s.newGame(worldWidth, worldHeight, -1) + s.newGame(worldWidth, worldHeight, color) return &s } @@ -59,7 +59,8 @@ func handler(conn net.Conn, game *Game, config *ssh.ServerConfig) { fmt.Println("Received new connection") - session := NewSession(channel, game.WorldWidth(), game.WorldHeight()) + session := NewSession(channel, game.WorldWidth(), game.WorldHeight(), + game.AvailableColors()[0]) game.AddSession(session) reader := bufio.NewReader(channel) |