From 71be4f25829b677a1c4f4241d19421db6ce9e916 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 8 Nov 2014 06:03:43 -0500 Subject: [PATCH] cleaning up some naming inconsistencies --- db.go | 4 ++-- main.go | 32 ++++++++++++++++---------------- system.go | 10 +++------- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/db.go b/db.go index c245411..f27f85a 100644 --- a/db.go +++ b/db.go @@ -33,7 +33,7 @@ func planetsTable() { } func planetsData() { - n, err := countPlanets() + n, err := countSystems() if err != nil { log_error("couldn't count planets: %v", err) return @@ -49,7 +49,7 @@ func planetsData() { planet.Store(db) } } - indexPlanets(db) + indexSystems() } func edgesTable() { diff --git a/main.go b/main.go index 7ad22a2..508686f 100644 --- a/main.go +++ b/main.go @@ -40,16 +40,16 @@ func handleConnection(conn *Connection) { defer conn.Close() conn.Login() - planet, err := randomPlanet() + system, err := randomSystem() if err != nil { - log_error("player %s failed to get random planet: %v", conn.PlayerName(), err) + log_error("player %s failed to get random system: %v", conn.PlayerName(), err) return } - planet.Arrive(conn) - if planet.planets == 1 { - fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", planet.name, planet.planets) + system.Arrive(conn) + if system.planets == 1 { + fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", system.name, system.planets) } else { - fmt.Fprintf(conn, "you are in the system %s. There are %d planets here.\n", planet.name, planet.planets) + fmt.Fprintf(conn, "you are in the system %s. There are %d planets here.\n", system.name, system.planets) } for { line, err := conn.ReadString('\n') @@ -70,30 +70,30 @@ func handleConnection(conn *Connection) { switch parts[0] { case "scan": - for _, otherPlanet := range index { - if otherPlanet.name == planet.name { + for _, otherSystem := range index { + if otherSystem.name == system.name { continue } go func(p *System) { - dist := planetDistance(*planet, *p) + dist := system.DistanceTo(p) delay := time.Duration(int64(dist * 100000000)) time.Sleep(delay) mu.Lock() - fmt.Fprintf(conn, "PONG from planet %s (%v)\n", p.name, delay) + fmt.Fprintf(conn, "PONG from system %s (%v)\n", p.name, delay) mu.Unlock() - }(otherPlanet) + }(otherSystem) } case "broadcast": msg := strings.Join(parts[1:], " ") log_info("player %s is broadcasting message %s", conn.PlayerName(), msg) for _, otherSystem := range index { - if otherSystem.name == planet.name { - log_info("skpping duplicate system %s", planet.name) + if otherSystem.name == system.name { + log_info("skpping duplicate system %s", system.name) continue } go func(s *System) { - log_info("message reached planet %s with %d inhabitants", s.name, s.NumInhabitants()) - dist := planetDistance(*planet, *s) * 0.5 + log_info("message reached system %s with %d inhabitants", s.name, s.NumInhabitants()) + dist := system.DistanceTo(s) * 0.5 delay := time.Duration(int64(dist * 100000000)) time.Sleep(delay) s.EachConn(func(conn *Connection) { @@ -102,7 +102,7 @@ func handleConnection(conn *Connection) { }(otherSystem) } case "nearby": - neighbors, err := planet.Nearby(25) + neighbors, err := system.Nearby(25) fmt.Fprintf(conn, "fetching nearby star systems\n") if err != nil { log_error("%v", err) diff --git a/system.go b/system.go index a435eb3..6253c2d 100644 --- a/system.go +++ b/system.go @@ -99,7 +99,7 @@ func (e *System) Nearby(n int) ([]Neighbor, error) { return neighbors, nil } -func countPlanets() (int, error) { +func countSystems() (int, error) { row := db.QueryRow(`select count(*) from planets`) var n int @@ -115,11 +115,7 @@ func dist3d(x1, y1, z1, x2, y2, z2 float64) float64 { return math.Sqrt(sq(x1-x2) + sq(y1-y2) + sq(z1-z2)) } -func planetDistance(p1, p2 System) float64 { - return dist3d(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z) -} - -func indexPlanets(db *sql.DB) map[int]*System { +func indexSystems() map[int]*System { rows, err := db.Query(`select * from planets`) if err != nil { log_error("unable to select all planets: %v", err) @@ -138,7 +134,7 @@ func indexPlanets(db *sql.DB) map[int]*System { return index } -func randomPlanet() (*System, error) { +func randomSystem() (*System, error) { n := len(index) if n == 0 { return nil, fmt.Errorf("no planets are known to exist")