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. systems. Star systems that are farther away take longer to communicate with.
` `
msg = strings.TrimSpace(msg) msg = strings.TrimSpace(msg)
fmt.Fprintln(conn, msg)
if len(args) == 0 { if len(args) == 0 {
fmt.Fprintln(conn, `use the "commands" command for a list of commands.`) 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) { func move(conn *Connection, to *System) {
start := conn.System() start := conn.System()
start.Leave(conn) start.Leave(conn)
@ -255,6 +273,7 @@ func init() {
registerCommand(broadcastCommand) registerCommand(broadcastCommand)
registerCommand(commandsCommand) registerCommand(commandsCommand)
registerCommand(gotoCommand) registerCommand(gotoCommand)
registerCommand(mineCommand)
registerCommand(helpCommand) registerCommand(helpCommand)
registerCommand(infoCommand) registerCommand(infoCommand)
registerCommand(nearbyCommand) registerCommand(nearbyCommand)

@ -60,10 +60,16 @@ func handleConnection(conn *Connection) {
log_error("failed to read line from player %s: %v", conn.PlayerName(), err) log_error("failed to read line from player %s: %v", conn.PlayerName(), err)
} }
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if conn.IsMining() {
conn.StopMining()
}
if line == "" { if line == "" {
continue continue
} }
parts := strings.Split(line, " ") parts := strings.Split(line, " ")
if isCommand(parts[0]) { if isCommand(parts[0]) {
runCommand(conn, parts[0], parts[1:]...) runCommand(conn, parts[0], parts[1:]...)
continue continue

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"math/rand"
"net" "net"
"strings" "strings"
"time" "time"
@ -19,6 +20,8 @@ type Connection struct {
lastBomb time.Time lastBomb time.Time
kills int kills int
dead bool dead bool
money int64
mining bool
} }
func NewConnection(conn net.Conn) *Connection { 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() { func (c *Connection) Win() {
for conn, _ := range connected { for conn, _ := range connected {
fmt.Fprintf(conn, "player %s has won.\n", c.PlayerName()) fmt.Fprintf(conn, "player %s has won.\n", c.PlayerName())

@ -18,8 +18,8 @@ type System struct {
x, y, z float64 x, y, z float64
planets int planets int
name string name string
players map[*Connection]bool players map[*Connection]bool
miningRate float64
} }
func (s *System) Arrive(p *Connection) { func (s *System) Arrive(p *Connection) {
@ -165,6 +165,7 @@ func indexSystems() map[int]*System {
} }
index[p.id] = &p index[p.id] = &p
nameIndex[p.name] = &p nameIndex[p.name] = &p
p.miningRate = rand.Float64()
} }
return index return index
} }

Loading…
Cancel
Save