From 0ccc2f91b5e55632c1bdab939b094fb6c7c1ea0a Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 15 Nov 2014 20:13:44 -0500 Subject: [PATCH] mining stuff --- commands.go | 8 -------- connection.go | 17 +++++++++++++++-- idle.go | 30 ++++++++++++++++++++++-------- mining.go | 50 +++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 76 insertions(+), 29 deletions(-) diff --git a/commands.go b/commands.go index d997168..0eb4b6b 100644 --- a/commands.go +++ b/commands.go @@ -208,14 +208,6 @@ var commandsCommand = Command{ // }, // } -// var mineCommand = &Command{ -// name: "mine", -// help: "mines the current system for resources", -// handler: func(conn *Connection, args ...string) { -// conn.Mine() -// }, -// } - // var colonizeCommand = &Command{ // name: "colonize", // help: "establishes a mining colony on the current system", diff --git a/connection.go b/connection.go index c2b148a..8b128ae 100644 --- a/connection.go +++ b/connection.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net" + "sort" "strings" "time" ) @@ -90,10 +91,18 @@ func (c *Connection) RunCommand(name string, args ...string) { }() switch name { case "commands": + c.Line() commands := c.Commands() - for _, command := range commands { - c.Printf("%s\n", command.name) + names := make([]string, len(commands)) + for i := range commands { + names[i] = commands[i].name } + sort.Strings(names) + for _, name := range names { + cmd := c.GetCommand(name) + c.Printf("%-20s%s\n", name, cmd.help) + } + c.Line() return } @@ -157,6 +166,10 @@ func (c *Connection) ReadLines(out chan []string) { } } +func (c *Connection) Line() { + c.Printf("--------------------------------------------------------------------------------\n") +} + func (c *Connection) Printf(template string, args ...interface{}) (int, error) { return fmt.Fprintf(c, template, args...) } diff --git a/idle.go b/idle.go index 61caa4b..9999dc4 100644 --- a/idle.go +++ b/idle.go @@ -7,6 +7,8 @@ import ( type IdleState struct { CommandSuite + NopEnter + NopExit *System } @@ -35,6 +37,18 @@ func Idle(sys *System) ConnectionState { arity: 1, handler: i.bomb, }, + Command{ + name: "mine", + help: "mine the current system for resources", + arity: 0, + handler: i.mine, + }, + Command{ + name: "info", + help: "gives you information about the current star system", + arity: 0, + handler: i.info, + }, } return i } @@ -43,18 +57,10 @@ func (i *IdleState) String() string { return fmt.Sprintf("idle on %v", i.System) } -func (i *IdleState) Enter(c *Connection) { - c.Printf("You have landed on %v.\n", i.System) -} - func (i *IdleState) Tick(c *Connection, frame int64) ConnectionState { return i } -func (i *IdleState) Exit(c *Connection) { - c.Printf("Now leaving %v.\n", i.System) -} - func (i *IdleState) travelTo(c *Connection, args ...string) { dest, err := GetSystem(args[0]) if err != nil { @@ -101,3 +107,11 @@ func (i *IdleState) bomb(c *Connection, args ...string) { bomb := NewBomb(c, i.System, target) currentGame.Register(bomb) } + +func (i *IdleState) mine(c *Connection, args ...string) { + c.SetState(Mine(i.System)) +} + +func (i *IdleState) info(c *Connection, args ...string) { + c.Printf("Currently idle on system %v\n", i.System) +} diff --git a/mining.go b/mining.go index 500c18b..87a419f 100644 --- a/mining.go +++ b/mining.go @@ -6,38 +6,66 @@ import ( type MiningState struct { CommandSuite - sys *System + *System mined int } func Mine(sys *System) ConnectionState { - return &MiningState{sys: sys} + m := &MiningState{System: sys} + m.CommandSuite = CommandSet{ + balCommand, + helpCommand, + playersCommand, + Command{ + name: "stop", + help: "stops mining", + arity: 0, + handler: m.stop, + }, + Command{ + name: "info", + help: "gives you information about the current mining operation", + arity: 0, + handler: m.info, + }, + } + return m } func (m *MiningState) Enter(c *Connection) { - c.Printf("Mining %v. %v space duckets remaining.\n", m.sys, m.sys.money) + c.Printf("Mining %v. %v space duckets remaining.\n", m.System, m.money) } func (m *MiningState) Tick(c *Connection, frame int64) ConnectionState { - if m.sys.money <= 0 { - c.Printf("system %s is all out of space duckets.\n", m.sys) - return Idle(m.sys) + if m.money <= 0 { + c.Printf("system %s is all out of space duckets.\n", m.System) + return Idle(m.System) } else { c.Deposit(1) m.mined += 1 - m.sys.money -= 1 + m.money -= 1 return m } } func (m *MiningState) Exit(c *Connection) { - if m.sys.money == 0 { - c.Printf("Done mining %v. Mined %v space duckets total. %v space duckets remain on %v, and it can be mined again.", m.sys, m.mined, m.sys.money, m.sys) + if m.money == 0 { + c.Printf("Done mining %v.\nMined %v space duckets total.\nNo space duckets remain on %v, and it can't be mined again.\n", m.System, m.mined, m.System) } else { - c.Printf("Done mining %v. Mined %v space duckets total. No space duckets remain on %v, and it can't be mined again.", m.sys, m.mined, m.sys) + c.Printf("Done mining %v.\nMined %v space duckets total.\n%v space duckets remain on %v, and it can be mined again.\n", m.System, m.mined, m.money, m.System) } } func (m *MiningState) String() string { - return fmt.Sprintf("mining %v", m.sys) + return fmt.Sprintf("mining %v", m.System) +} + +func (m *MiningState) stop(c *Connection, args ...string) { + c.SetState(Idle(m.System)) +} + +func (m *MiningState) info(c *Connection, args ...string) { + c.Printf("Currently mining system %v\n", m.System) + c.Printf("Mined so far: %v\n", m.mined) + c.Printf("Remaining space duckets on %v: %v\n", m.System, m.money) }