added mining

pull/5/head
Jordan Orelli 10 years ago
parent 0e84073d4a
commit b5f447aab6

@ -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)

@ -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

@ -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())

@ -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
}

Loading…
Cancel
Save