From 731e9be13cb06cf42473b590d651a31c62ca8770 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 8 Nov 2014 09:58:26 -0500 Subject: [PATCH] can now move between systems --- commands.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 3 +++ session.go | 4 ++++ system.go | 6 +++++- 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 3bff937..ed1ddd1 100644 --- a/commands.go +++ b/commands.go @@ -3,7 +3,9 @@ package main import ( "fmt" "sort" + "strconv" "strings" + "time" ) var commandRegistry map[string]*Command @@ -12,6 +14,7 @@ type Command struct { name string help string handler func(*Connection, ...string) + mobile bool } 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 { _, ok := commandRegistry[name] return ok @@ -124,6 +173,11 @@ func runCommand(conn *Connection, name string, args ...string) { fmt.Fprintf(conn, "no such command: %s\n", name) return } + + if conn.InTransit() && !cmd.mobile { + fmt.Fprintf(conn, "command %s can not be used while in transit", name) + return + } cmd.handler(conn, args...) } @@ -138,4 +192,6 @@ func init() { registerCommand(infoCommand) registerCommand(nearbyCommand) registerCommand(scanCommand) + registerCommand(gotoCommand) + // registerCommand(bombCommand) } diff --git a/main.go b/main.go index 438d1ee..348e01c 100644 --- a/main.go +++ b/main.go @@ -60,6 +60,9 @@ func handleConnection(conn *Connection) { log_error("failed to read line from player %s: %v", conn.PlayerName(), err) } line = strings.TrimSpace(line) + if line == "" { + continue + } parts := strings.Split(line, " ") if isCommand(parts[0]) { runCommand(conn, parts[0], parts[1:]...) diff --git a/session.go b/session.go index 7478c5c..756d8d9 100644 --- a/session.go +++ b/session.go @@ -72,3 +72,7 @@ func (c *Connection) PlayerName() string { } return c.player.name } + +func (c *Connection) InTransit() bool { + return c.location == nil +} diff --git a/system.go b/system.go index 8505e25..27c00dd 100644 --- a/system.go +++ b/system.go @@ -9,7 +9,8 @@ import ( ) var ( - index map[int]*System + index map[int]*System + nameIndex map[string]*System ) type System struct { @@ -32,6 +33,7 @@ func (s *System) Arrive(p *Connection) { func (s *System) Leave(p *Connection) { delete(s.players, p) + p.location = nil } func (s *System) EachConn(fn func(*Connection)) { @@ -128,6 +130,7 @@ func indexSystems() map[int]*System { } defer rows.Close() index = make(map[int]*System, 551) + nameIndex = make(map[string]*System, 551) for rows.Next() { p := System{} 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 } index[p.id] = &p + nameIndex[p.name] = &p } return index }