|
|
@ -2,13 +2,21 @@ package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"math"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var planetIndex map[int]exoSystem
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func dbconnect() (*sql.DB, error) {
|
|
|
|
|
|
|
|
return sql.Open("sqlite3", "./exo.db")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func setupDb() {
|
|
|
|
func setupDb() {
|
|
|
|
db, err := sql.Open("sqlite3", "./exo.db")
|
|
|
|
db, err := dbconnect()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
bail(E_No_DB, "unable to open database: %v", err)
|
|
|
|
bail(E_No_DB, "unable to open database: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -29,7 +37,6 @@ func setupDb() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
n, err := countPlanets(db)
|
|
|
|
n, err := countPlanets(db)
|
|
|
|
log_info("num planets: %v error: %v", n, err)
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
if n == 0 {
|
|
|
|
fi, err := os.Open(dataPath)
|
|
|
|
fi, err := os.Open(dataPath)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -42,9 +49,9 @@ func setupDb() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
idx := indexPlanets(db)
|
|
|
|
indexPlanets(db)
|
|
|
|
log_info("%v", idx)
|
|
|
|
// log_info("%v", idx)
|
|
|
|
fillEdges(db, idx)
|
|
|
|
// fillEdges(db, idx)
|
|
|
|
|
|
|
|
|
|
|
|
stmnt = `create table if not exists edges (
|
|
|
|
stmnt = `create table if not exists edges (
|
|
|
|
id_1 integer,
|
|
|
|
id_1 integer,
|
|
|
@ -57,6 +64,16 @@ func setupDb() {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stmnt = `create table if not exists players (
|
|
|
|
|
|
|
|
id integer not null primary key autoincrement,
|
|
|
|
|
|
|
|
name text
|
|
|
|
|
|
|
|
);`
|
|
|
|
|
|
|
|
_, err = db.Exec(stmnt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log_error("couldn't create player table: %v", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func sq(x float64) float64 {
|
|
|
|
func sq(x float64) float64 {
|
|
|
@ -78,7 +95,7 @@ func indexPlanets(db *sql.DB) map[int]exoSystem {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
defer rows.Close()
|
|
|
|
planets := make(map[int]exoSystem, 551)
|
|
|
|
planetIndex = make(map[int]exoSystem, 551)
|
|
|
|
for rows.Next() {
|
|
|
|
for rows.Next() {
|
|
|
|
var id int
|
|
|
|
var id int
|
|
|
|
p := exoSystem{}
|
|
|
|
p := exoSystem{}
|
|
|
@ -86,9 +103,9 @@ func indexPlanets(db *sql.DB) map[int]exoSystem {
|
|
|
|
log_info("unable to scan planet row: %v", err)
|
|
|
|
log_info("unable to scan planet row: %v", err)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
planets[id] = p
|
|
|
|
planetIndex[id] = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return planets
|
|
|
|
return planetIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func fillEdges(db *sql.DB, planets map[int]exoSystem) {
|
|
|
|
func fillEdges(db *sql.DB, planets map[int]exoSystem) {
|
|
|
@ -98,3 +115,14 @@ func fillEdges(db *sql.DB, planets map[int]exoSystem) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func randomPlanet() (*exoSystem, error) {
|
|
|
|
|
|
|
|
n := len(planetIndex)
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("no planets are known to exist")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pick := rand.Intn(n)
|
|
|
|
|
|
|
|
planet := planetIndex[pick]
|
|
|
|
|
|
|
|
return &planet, nil
|
|
|
|
|
|
|
|
}
|
|
|
|