From 12ecbae59d0f317cb471c0a2ec49e19bde5de3ec Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 8 Nov 2014 13:18:27 -0500 Subject: [PATCH] can now set up mining colonies --- commands.go | 32 ++++++++++++++++++++++++++++++++ session.go | 1 + system.go | 16 +++++++++------- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/commands.go b/commands.go index 807ab10..905c676 100644 --- a/commands.go +++ b/commands.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "math/rand" "sort" "strconv" "strings" @@ -189,6 +190,36 @@ var mineCommand = &Command{ }, } +var colonizeCommand = &Command{ + name: "colonize", + help: "establishes a mining colony on the current system", + handler: func(conn *Connection, arg ...string) { + system := conn.System() + var fn func() + fn = func() { + reward := int64(rand.NormFloat64()*5.0 + 100.0*system.miningRate) + system.colonizedBy.money += reward + fmt.Fprintf(system.colonizedBy, "mining colony on %s pays you %d space duckets. total: %d space duckets.\n", system.name, reward, system.colonizedBy.money) + After(5*time.Second, fn) + } + + if system.colonizedBy != nil { + system.colonizedBy = conn + After(5*time.Second, fn) + return + } + + if conn.money > 2000 { + conn.money -= 2000 + system.colonizedBy = conn + fmt.Fprintf(conn, "set up a mining colony on %s\n", conn.System().name) + After(5*time.Second, fn) + } else { + fmt.Fprintf(conn, "not enough money! it costs 2000 duckets to start a mining colony\n") + } + }, +} + func move(conn *Connection, to *System) { start := conn.System() start.Leave(conn) @@ -271,6 +302,7 @@ func init() { commandRegistry = make(map[string]*Command, 16) registerCommand(bombCommand) registerCommand(broadcastCommand) + registerCommand(colonizeCommand) registerCommand(commandsCommand) registerCommand(gotoCommand) registerCommand(mineCommand) diff --git a/session.go b/session.go index 47a190d..0216e21 100644 --- a/session.go +++ b/session.go @@ -22,6 +22,7 @@ type Connection struct { dead bool money int64 mining bool + colonies []*System } func NewConnection(conn net.Conn) *Connection { diff --git a/system.go b/system.go index 17800f1..445ee1a 100644 --- a/system.go +++ b/system.go @@ -14,12 +14,13 @@ var ( ) type System struct { - id int - x, y, z float64 - planets int - name string - players map[*Connection]bool - miningRate float64 + id int + x, y, z float64 + planets int + name string + players map[*Connection]bool + miningRate float64 + colonizedBy *Connection } func (s *System) Arrive(p *Connection) { @@ -182,7 +183,8 @@ func randomSystem() (*System, error) { } type scanResults struct { - life bool + life bool + miningRate float64 } func (r *scanResults) negative() bool {