From b5f447aab6a23f922f21bb684180b31ed50dc1d5 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 8 Nov 2014 12:59:36 -0500 Subject: [PATCH] added mining --- commands.go | 19 +++++++++++++++++++ main.go | 6 ++++++ session.go | 24 ++++++++++++++++++++++++ system.go | 13 +++++++------ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/commands.go b/commands.go index 9c9445c..807ab10 100644 --- a/commands.go +++ b/commands.go @@ -69,6 +69,7 @@ another, it takes time for the light of your message to reach the other star systems. Star systems that are farther away take longer to communicate with. ` msg = strings.TrimSpace(msg) + fmt.Fprintln(conn, msg) if len(args) == 0 { fmt.Fprintln(conn, `use the "commands" command for a list of commands.`) @@ -171,6 +172,23 @@ var gotoCommand = &Command{ }, } +var mineCommand = &Command{ + name: "mine", + help: "mines the current system for resources", + handler: func(conn *Connection, args ...string) { + conn.StartMining() + var fn func() + fn = func() { + if !conn.IsMining() { + return + } + conn.Payout() + After(500*time.Millisecond, fn) + } + After(500*time.Millisecond, fn) + }, +} + func move(conn *Connection, to *System) { start := conn.System() start.Leave(conn) @@ -255,6 +273,7 @@ func init() { registerCommand(broadcastCommand) registerCommand(commandsCommand) registerCommand(gotoCommand) + registerCommand(mineCommand) registerCommand(helpCommand) registerCommand(infoCommand) registerCommand(nearbyCommand) diff --git a/main.go b/main.go index 47ceb83..aaf2cbb 100644 --- a/main.go +++ b/main.go @@ -60,10 +60,16 @@ func handleConnection(conn *Connection) { log_error("failed to read line from player %s: %v", conn.PlayerName(), err) } line = strings.TrimSpace(line) + + if conn.IsMining() { + conn.StopMining() + } + if line == "" { continue } parts := strings.Split(line, " ") + if isCommand(parts[0]) { runCommand(conn, parts[0], parts[1:]...) continue diff --git a/session.go b/session.go index 7bf084f..47a190d 100644 --- a/session.go +++ b/session.go @@ -3,6 +3,7 @@ package main import ( "bufio" "fmt" + "math/rand" "net" "strings" "time" @@ -19,6 +20,8 @@ type Connection struct { lastBomb time.Time kills int dead bool + money int64 + mining bool } func NewConnection(conn net.Conn) *Connection { @@ -119,6 +122,27 @@ func (c *Connection) MadeKill(victim *Connection) { } } +func (c *Connection) StartMining() { + fmt.Fprintf(c, "now mining %s with a payout rate of %v\n", c.System().name, c.System().miningRate) + fmt.Fprintln(c, "(press enter to stop mining)") + c.mining = true +} + +func (c *Connection) StopMining() { + fmt.Fprintf(c, "done mining\n") + c.mining = false +} + +func (c *Connection) IsMining() bool { + return c.mining +} + +func (c *Connection) Payout() { + reward := int64(rand.NormFloat64()*5.0 + 100.0*c.System().miningRate) + c.money += reward + fmt.Fprintf(c, "mined: %d space duckets. total: %d\n", reward, c.money) +} + func (c *Connection) Win() { for conn, _ := range connected { fmt.Fprintf(conn, "player %s has won.\n", c.PlayerName()) diff --git a/system.go b/system.go index a3f3aec..17800f1 100644 --- a/system.go +++ b/system.go @@ -14,12 +14,12 @@ var ( ) type System struct { - id int - x, y, z float64 - planets int - name string - - players map[*Connection]bool + id int + x, y, z float64 + planets int + name string + players map[*Connection]bool + miningRate float64 } func (s *System) Arrive(p *Connection) { @@ -165,6 +165,7 @@ func indexSystems() map[int]*System { } index[p.id] = &p nameIndex[p.name] = &p + p.miningRate = rand.Float64() } return index }