list games command

master
Jordan Orelli 5 years ago
parent d603c74a18
commit ae409fd065

@ -85,6 +85,9 @@ func (g *Game) Store() error {
func (g *Game) Join(conn *Connection) {
log_info("Player %s has joined game %s", conn.Name(), g.id)
for there, _ := range g.connections {
there.Printf("Player %s has joined the game", conn.Name())
}
g.connections[conn] = true
g.Register(conn)
}
@ -105,6 +108,8 @@ func (g *Game) Win(winner *Connection, method string) {
for conn, _ := range g.connections {
conn.Printf("player %s has won by %s victory.\n", winner.Name(), method)
}
gm.Remove(g)
}
func (g *Game) Reset() {

@ -32,3 +32,10 @@ func (g *GameManager) Get(id string) *Game {
return g.games[id]
}
func (g *GameManager) Remove(game *Game) {
g.Lock()
defer g.Unlock()
delete(g.games, game.id)
}

@ -47,6 +47,7 @@ func EnterLobby() ConnectionState {
CommandSuite: CommandSet{
newGameCommand,
joinGameCommand,
listGamesCommand,
},
}
}
@ -127,6 +128,18 @@ var joinGameCommand = Command{
variadic: false,
handler: func(c *Connection, args ...string) {
if len(args) == 0 {
gm.Lock()
defer gm.Unlock()
if len(gm.games) == 1 {
for _, game := range gm.games {
c.game = game
log_info("%s Joining game: %s", c.profile.name, c.game.id)
c.Printf("You have joined game %s\n", game.id)
c.SetState(game.SpawnPlayer())
c.game.Join(c)
return
}
}
c.Printf(strings.TrimLeft(`
Missing game code! When a player starts a game, they will be given a code to
identify their game. Use this game to join the other player's game.
@ -138,8 +151,35 @@ Usage: join [game-code]`, " \n\t"))
game := gm.Get(id)
c.game = game
log_info("%s Joining game: %s", c.profile.name, c.game.id)
c.Printf("You have joined game %s\n", game.id)
c.SetState(game.SpawnPlayer())
c.game.Join(c)
},
debug: false,
}
var listGamesCommand = Command{
name: "list",
summary: "lists game lobbies that can be joined",
usage: "list",
arity: 0,
variadic: false,
handler: func(c *Connection, args ...string) {
gm.Lock()
defer gm.Unlock()
c.Line()
c.Printf("%-8s %-20s\n", "Game", "Player")
c.Line()
for id, game := range gm.games {
c.Printf("%-8s %-20s\n", id, "")
for conn, _ := range game.connections {
if conn.profile != nil {
c.Printf("%-8s %-20s\n", "", conn.profile.name)
}
}
c.Printf("--------------------\n")
}
},
}

Loading…
Cancel
Save