cleaning up some naming inconsistencies

pull/5/head
Jordan Orelli 10 years ago
parent fd40042ed3
commit 71be4f2582

@ -33,7 +33,7 @@ func planetsTable() {
} }
func planetsData() { func planetsData() {
n, err := countPlanets() n, err := countSystems()
if err != nil { if err != nil {
log_error("couldn't count planets: %v", err) log_error("couldn't count planets: %v", err)
return return
@ -49,7 +49,7 @@ func planetsData() {
planet.Store(db) planet.Store(db)
} }
} }
indexPlanets(db) indexSystems()
} }
func edgesTable() { func edgesTable() {

@ -40,16 +40,16 @@ func handleConnection(conn *Connection) {
defer conn.Close() defer conn.Close()
conn.Login() conn.Login()
planet, err := randomPlanet() system, err := randomSystem()
if err != nil { 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 return
} }
planet.Arrive(conn) system.Arrive(conn)
if planet.planets == 1 { if system.planets == 1 {
fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", planet.name, planet.planets) fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", system.name, system.planets)
} else { } 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 { for {
line, err := conn.ReadString('\n') line, err := conn.ReadString('\n')
@ -70,30 +70,30 @@ func handleConnection(conn *Connection) {
switch parts[0] { switch parts[0] {
case "scan": case "scan":
for _, otherPlanet := range index { for _, otherSystem := range index {
if otherPlanet.name == planet.name { if otherSystem.name == system.name {
continue continue
} }
go func(p *System) { go func(p *System) {
dist := planetDistance(*planet, *p) dist := system.DistanceTo(p)
delay := time.Duration(int64(dist * 100000000)) delay := time.Duration(int64(dist * 100000000))
time.Sleep(delay) time.Sleep(delay)
mu.Lock() 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() mu.Unlock()
}(otherPlanet) }(otherSystem)
} }
case "broadcast": case "broadcast":
msg := strings.Join(parts[1:], " ") msg := strings.Join(parts[1:], " ")
log_info("player %s is broadcasting message %s", conn.PlayerName(), msg) log_info("player %s is broadcasting message %s", conn.PlayerName(), msg)
for _, otherSystem := range index { for _, otherSystem := range index {
if otherSystem.name == planet.name { if otherSystem.name == system.name {
log_info("skpping duplicate system %s", planet.name) log_info("skpping duplicate system %s", system.name)
continue continue
} }
go func(s *System) { go func(s *System) {
log_info("message reached planet %s with %d inhabitants", s.name, s.NumInhabitants()) log_info("message reached system %s with %d inhabitants", s.name, s.NumInhabitants())
dist := planetDistance(*planet, *s) * 0.5 dist := system.DistanceTo(s) * 0.5
delay := time.Duration(int64(dist * 100000000)) delay := time.Duration(int64(dist * 100000000))
time.Sleep(delay) time.Sleep(delay)
s.EachConn(func(conn *Connection) { s.EachConn(func(conn *Connection) {
@ -102,7 +102,7 @@ func handleConnection(conn *Connection) {
}(otherSystem) }(otherSystem)
} }
case "nearby": case "nearby":
neighbors, err := planet.Nearby(25) neighbors, err := system.Nearby(25)
fmt.Fprintf(conn, "fetching nearby star systems\n") fmt.Fprintf(conn, "fetching nearby star systems\n")
if err != nil { if err != nil {
log_error("%v", err) log_error("%v", err)

@ -99,7 +99,7 @@ func (e *System) Nearby(n int) ([]Neighbor, error) {
return neighbors, nil return neighbors, nil
} }
func countPlanets() (int, error) { func countSystems() (int, error) {
row := db.QueryRow(`select count(*) from planets`) row := db.QueryRow(`select count(*) from planets`)
var n int 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)) return math.Sqrt(sq(x1-x2) + sq(y1-y2) + sq(z1-z2))
} }
func planetDistance(p1, p2 System) float64 { func indexSystems() map[int]*System {
return dist3d(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z)
}
func indexPlanets(db *sql.DB) map[int]*System {
rows, err := db.Query(`select * from planets`) rows, err := db.Query(`select * from planets`)
if err != nil { if err != nil {
log_error("unable to select all planets: %v", err) log_error("unable to select all planets: %v", err)
@ -138,7 +134,7 @@ func indexPlanets(db *sql.DB) map[int]*System {
return index return index
} }
func randomPlanet() (*System, error) { func randomSystem() (*System, error) {
n := len(index) n := len(index)
if n == 0 { if n == 0 {
return nil, fmt.Errorf("no planets are known to exist") return nil, fmt.Errorf("no planets are known to exist")

Loading…
Cancel
Save