iz system

pull/5/head
Jordan Orelli 10 years ago
parent 50461b87b3
commit a92320cdbc

@ -7,8 +7,7 @@ import (
)
var (
planetIndex map[int]Planet
db *sql.DB
db *sql.DB
)
func dbconnect() {
@ -44,7 +43,7 @@ func planetsData() {
if err != nil {
bail(E_No_Data, "unable to open data path: %v", err)
}
c := make(chan Planet)
c := make(chan System)
go speckStream(fi, c)
for planet := range c {
planet.Store(db)
@ -72,7 +71,7 @@ func setupDb() {
// fillEdges(db, idx)
}
func fillEdges(db *sql.DB, planets map[int]Planet) {
func fillEdges(db *sql.DB, planets map[int]System) {
for i := 0; i < len(planets); i++ {
for j := i + 1; j < len(planets); j++ {
log_info("distance from %s to %s: %v", planets[i].name, planets[j].name, planetDistance(planets[i], planets[j]))

@ -45,7 +45,11 @@ func handleConnection(conn *Connection) {
log_error("player %s failed to get random planet: %v", conn.PlayerName(), err)
return
}
fmt.Fprintf(conn, "you are on the planet %s\n", planet.name)
if planet.planets == 1 {
fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", planet.name, planet.planets)
} else {
fmt.Fprintf(conn, "you are in the system %s. There are %d planets here.\n", planet.name, planet.planets)
}
for {
line, err := conn.ReadString('\n')
switch err {
@ -60,11 +64,11 @@ func handleConnection(conn *Connection) {
parts := strings.Split(line, " ")
switch parts[0] {
case "scan":
for _, otherPlanet := range planetIndex {
for _, otherPlanet := range index {
if otherPlanet.name == planet.name {
continue
}
go func(p Planet) {
go func(p System) {
dist := planetDistance(*planet, p)
delay := time.Duration(int64(dist * 100000000))
time.Sleep(delay)

@ -8,7 +8,7 @@ import (
"strings"
)
func speckStream(r io.ReadCloser, c chan Planet) {
func speckStream(r io.ReadCloser, c chan System) {
defer close(c)
defer r.Close()
keep := regexp.MustCompile(`^\s*[\d-]`)
@ -34,11 +34,11 @@ func speckStream(r io.ReadCloser, c chan Planet) {
}
}
func parseSpeckLine(line []byte) *Planet {
func parseSpeckLine(line []byte) *System {
parts := strings.Split(string(line), " ")
var err error
var g errorGroup
s := new(Planet)
s := new(System)
s.x, err = strconv.ParseFloat(parts[0], 64)
g.AddError(err)

@ -7,13 +7,17 @@ import (
"math/rand"
)
type Planet struct {
var (
index map[int]System
)
type System struct {
x, y, z float64
planets int
name string
}
func (e Planet) Store(db *sql.DB) {
func (e System) Store(db *sql.DB) {
_, err := db.Exec(`
insert into planets
(name, x, y, z, planets)
@ -25,7 +29,7 @@ func (e Planet) Store(db *sql.DB) {
}
}
func (e Planet) String() string {
func (e System) String() string {
return fmt.Sprintf("<name: %s x: %v y: %v z: %v planets: %v>", e.name, e.x, e.y, e.z, e.planets)
}
@ -45,37 +49,37 @@ 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 Planet) float64 {
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]Planet {
func indexPlanets(db *sql.DB) map[int]System {
rows, err := db.Query(`select * from planets`)
if err != nil {
log_error("unable to select all planets: %v", err)
return nil
}
defer rows.Close()
planetIndex = make(map[int]Planet, 551)
index = make(map[int]System, 551)
for rows.Next() {
var id int
p := Planet{}
p := System{}
if err := rows.Scan(&id, &p.name, &p.x, &p.y, &p.z, &p.planets); err != nil {
log_info("unable to scan planet row: %v", err)
continue
}
planetIndex[id] = p
index[id] = p
}
return planetIndex
return index
}
func randomPlanet() (*Planet, error) {
n := len(planetIndex)
func randomPlanet() (*System, error) {
n := len(index)
if n == 0 {
return nil, fmt.Errorf("no planets are known to exist")
}
pick := rand.Intn(n)
planet := planetIndex[pick]
planet := index[pick]
return &planet, nil
}
Loading…
Cancel
Save