can now move between systems

pull/5/head
Jordan Orelli 10 years ago
parent 590a4487a6
commit 731e9be13c

@ -3,7 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"sort" "sort"
"strconv"
"strings" "strings"
"time"
) )
var commandRegistry map[string]*Command var commandRegistry map[string]*Command
@ -12,6 +14,7 @@ type Command struct {
name string name string
help string help string
handler func(*Connection, ...string) handler func(*Connection, ...string)
mobile bool
} }
var infoCommand = &Command{ var infoCommand = &Command{
@ -113,6 +116,52 @@ var broadcastCommand = &Command{
}, },
} }
var gotoCommand = &Command{
name: "goto",
help: "moves to a different system, specified by either name or ID",
handler: func(conn *Connection, args ...string) {
dest_name := strings.Join(args, " ")
to, ok := nameIndex[dest_name]
if ok {
move(conn, to)
return
}
id_n, err := strconv.Atoi(dest_name)
if err != nil {
fmt.Fprintf(conn, `hmm, I don't know a system by the name "%s", try something else`, dest_name)
return
}
to, ok = index[id_n]
if !ok {
fmt.Fprintf(conn, `oh dear, there doesn't seem to be a system with id %d`, id_n)
return
}
move(conn, to)
},
}
func move(conn *Connection, to *System) {
start := conn.System()
start.Leave(conn)
delay := start.TimeTo(to)
delay = time.Duration(int64(float64(delay/time.Nanosecond) * 1.25))
After(delay, func() {
to.Arrive(conn)
fmt.Fprintf(conn, "You have arrived at the %s system.\n", to.name)
})
}
// var bombCommand = &Command{
// name: "bomb",
// help: "bombs a system, with a big space bomb",
// handler: func(conn *Connection, args ...string) {
//
// },
// }
func isCommand(name string) bool { func isCommand(name string) bool {
_, ok := commandRegistry[name] _, ok := commandRegistry[name]
return ok return ok
@ -124,6 +173,11 @@ func runCommand(conn *Connection, name string, args ...string) {
fmt.Fprintf(conn, "no such command: %s\n", name) fmt.Fprintf(conn, "no such command: %s\n", name)
return return
} }
if conn.InTransit() && !cmd.mobile {
fmt.Fprintf(conn, "command %s can not be used while in transit", name)
return
}
cmd.handler(conn, args...) cmd.handler(conn, args...)
} }
@ -138,4 +192,6 @@ func init() {
registerCommand(infoCommand) registerCommand(infoCommand)
registerCommand(nearbyCommand) registerCommand(nearbyCommand)
registerCommand(scanCommand) registerCommand(scanCommand)
registerCommand(gotoCommand)
// registerCommand(bombCommand)
} }

@ -60,6 +60,9 @@ 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 line == "" {
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:]...)

@ -72,3 +72,7 @@ func (c *Connection) PlayerName() string {
} }
return c.player.name return c.player.name
} }
func (c *Connection) InTransit() bool {
return c.location == nil
}

@ -10,6 +10,7 @@ import (
var ( var (
index map[int]*System index map[int]*System
nameIndex map[string]*System
) )
type System struct { type System struct {
@ -32,6 +33,7 @@ func (s *System) Arrive(p *Connection) {
func (s *System) Leave(p *Connection) { func (s *System) Leave(p *Connection) {
delete(s.players, p) delete(s.players, p)
p.location = nil
} }
func (s *System) EachConn(fn func(*Connection)) { func (s *System) EachConn(fn func(*Connection)) {
@ -128,6 +130,7 @@ func indexSystems() map[int]*System {
} }
defer rows.Close() defer rows.Close()
index = make(map[int]*System, 551) index = make(map[int]*System, 551)
nameIndex = make(map[string]*System, 551)
for rows.Next() { for rows.Next() {
p := System{} p := System{}
if err := rows.Scan(&p.id, &p.name, &p.x, &p.y, &p.z, &p.planets); err != nil { if err := rows.Scan(&p.id, &p.name, &p.x, &p.y, &p.z, &p.planets); err != nil {
@ -135,6 +138,7 @@ func indexSystems() map[int]*System {
continue continue
} }
index[p.id] = &p index[p.id] = &p
nameIndex[p.name] = &p
} }
return index return index
} }

Loading…
Cancel
Save