From d84419532c79c99b0d2e8b346fd0487b5b4ef388 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 16 Nov 2014 12:37:29 -0500 Subject: [PATCH] refining nearby command --- commands.go | 40 +++++++++++++++++++++++----------------- idle.go | 23 +---------------------- mining.go | 1 + 3 files changed, 25 insertions(+), 39 deletions(-) diff --git a/commands.go b/commands.go index 3c8e0f5..c76b576 100644 --- a/commands.go +++ b/commands.go @@ -125,23 +125,29 @@ func BroadcastCommand(sys *System) Command { } } -// var colonizeCommand = &Command{ -// name: "colonize", -// help: "establishes a mining colony on the current system", -// handler: func(conn *Connection, arg ...string) { -// system := conn.System() -// if conn.money > 2000 { -// conn.Withdraw(2000) -// if system.colonizedBy != nil { -// system.colonizedBy.Printf("your colony on %s has been stolen by %s\n", system.Label(), conn.Name()) -// } -// system.colonizedBy = conn -// conn.Printf("set up a mining colony on %s\n", conn.System().name) -// } else { -// conn.Printf("not enough money! it costs 2000 duckets to start a mining colony\n") -// } -// }, -// } +func NearbyCommand(sys *System) Command { + handler := func(c *Connection, args ...string) { + neighbors, err := sys.Nearby(25) + if err != nil { + log_error("unable to get neighbors: %v", err) + return + } + c.Printf("--------------------------------------------------------------------------------\n") + c.Printf("%-4s %-20s %s\n", "id", "name", "distance") + c.Printf("--------------------------------------------------------------------------------\n") + for _, neighbor := range neighbors { + other := index[neighbor.id] + c.Printf("%-4d %-20s %v\n", other.id, other.name, neighbor.distance) + } + c.Printf("--------------------------------------------------------------------------------\n") + } + return Command{ + name: "nearby", + help: "list nearby star systems", + arity: 0, + handler: handler, + } +} var winCommand = Command{ name: "win", diff --git a/idle.go b/idle.go index 7387b9c..a2d6993 100644 --- a/idle.go +++ b/idle.go @@ -18,18 +18,13 @@ func Idle(sys *System) ConnectionState { helpCommand, playersCommand, BroadcastCommand(sys), + NearbyCommand(sys), Command{ name: "goto", help: "travel between star systems", arity: 1, handler: i.travelTo, }, - Command{ - name: "nearby", - help: "list nearby star systems", - arity: 0, - handler: i.nearby, - }, Command{ name: "bomb", help: "bomb another star system", @@ -84,22 +79,6 @@ func (i *IdleState) travelTo(c *Connection, args ...string) { c.SetState(NewTravel(c, i.System, dest)) } -func (i *IdleState) nearby(c *Connection, args ...string) { - neighbors, err := i.Nearby(25) - if err != nil { - log_error("unable to get neighbors: %v", err) - return - } - c.Printf("--------------------------------------------------------------------------------\n") - c.Printf("%-4s %-20s %s\n", "id", "name", "distance") - c.Printf("--------------------------------------------------------------------------------\n") - for _, neighbor := range neighbors { - other := index[neighbor.id] - c.Printf("%-4d %-20s %v\n", other.id, other.name, neighbor.distance) - } - c.Printf("--------------------------------------------------------------------------------\n") -} - func (i *IdleState) bomb(c *Connection, args ...string) { if c.bombs <= 0 { c.Printf("Cannot send bomb: no bombs left! Build more bombs!\n") diff --git a/mining.go b/mining.go index 9df85e8..4f25201 100644 --- a/mining.go +++ b/mining.go @@ -17,6 +17,7 @@ func Mine(sys *System) ConnectionState { helpCommand, playersCommand, BroadcastCommand(sys), + NearbyCommand(sys), Command{ name: "stop", help: "stops mining",