remember people

pull/5/head
Jordan Orelli 10 years ago
parent 7ade356836
commit 4bcbadf92f

59
db.go

@ -9,19 +9,20 @@ import (
"os" "os"
) )
var planetIndex map[int]exoSystem var (
planetIndex map[int]exoSystem
func dbconnect() (*sql.DB, error) { db *sql.DB
return sql.Open("sqlite3", "./exo.db") )
}
func setupDb() { func dbconnect() {
db, err := dbconnect() var err error
db, err = sql.Open("sqlite3", "./exo.db")
if err != nil { if err != nil {
bail(E_No_DB, "unable to open database: %v", err) bail(E_No_DB, "couldn't connect to db: %v", err)
} }
defer db.Close() }
func planetsTable() {
stmnt := `create table if not exists planets ( stmnt := `create table if not exists planets (
id integer not null primary key autoincrement, id integer not null primary key autoincrement,
name text, name text,
@ -30,13 +31,17 @@ func setupDb() {
z integer, z integer,
planets integer planets integer
);` );`
_, err = db.Exec(stmnt) if _, err := db.Exec(stmnt); err != nil {
log_error("couldn't create planets table: %v", err)
}
}
func planetsData() {
n, err := countPlanets()
if err != nil { if err != nil {
log_error("couldn't create table: %v", err) log_error("couldn't count planets: %v", err)
return return
} }
n, err := countPlanets(db)
if n == 0 { if n == 0 {
fi, err := os.Open(dataPath) fi, err := os.Open(dataPath)
if err != nil { if err != nil {
@ -48,32 +53,26 @@ func setupDb() {
planet.Store(db) planet.Store(db)
} }
} }
indexPlanets(db) indexPlanets(db)
// log_info("%v", idx) }
// fillEdges(db, idx)
stmnt = `create table if not exists edges ( func edgesTable() {
stmnt := `create table if not exists edges (
id_1 integer, id_1 integer,
id_2 integer, id_2 integer,
distance real distance real
);` );`
_, err = db.Exec(stmnt) if _, err := db.Exec(stmnt); err != nil {
if err != nil {
log_error("couldn't create distance table: %v", err) log_error("couldn't create distance table: %v", err)
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 setupDb() {
planetsTable()
planetsData()
edgesTable()
playersTable()
// fillEdges(db, idx)
} }
func sq(x float64) float64 { func sq(x float64) float64 {

@ -11,7 +11,9 @@ import (
"time" "time"
) )
var dataPath = "/projects/exo/expl.speck" var (
dataPath = "/projects/exo/expl.speck"
)
func log_error(template string, args ...interface{}) { func log_error(template string, args ...interface{}) {
fmt.Fprint(os.Stderr, "ERROR ") fmt.Fprint(os.Stderr, "ERROR ")
@ -82,6 +84,7 @@ func handleConnection(conn *Connection) {
} }
func main() { func main() {
dbconnect()
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
setupDb() setupDb()

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"regexp" "regexp"
) )
@ -11,9 +12,38 @@ func ValidName(name string) bool {
} }
type Player struct { type Player struct {
id int
name string name string
} }
func (p *Player) Load() { func (p *Player) Create() error {
_, err := db.Exec(`
insert into players
(name)
values
(?)
;`, p.name)
if err != nil {
return fmt.Errorf("unable to create player: %v", err)
}
return nil
}
func playersTable() {
stmnt := `create table if not exists players (
id integer not null primary key autoincrement,
name text unique
);`
if _, err := db.Exec(stmnt); err != nil {
log_error("couldn't create player table: %v", err)
}
}
func loadPlayer(name string) (*Player, error) {
row := db.QueryRow(`select * from players where name = ?`, name)
var p Player
if err := row.Scan(&p.id, &p.name); err != nil {
return nil, fmt.Errorf("unable to fetch player from database: %v", err)
}
return &p, nil
} }

@ -10,7 +10,7 @@ import (
type Connection struct { type Connection struct {
net.Conn net.Conn
*bufio.Reader *bufio.Reader
player Player player *Player
} }
func NewConnection(conn net.Conn) *Connection { func NewConnection(conn net.Conn) *Connection {
@ -35,16 +35,31 @@ func (c *Connection) Login() {
continue continue
} }
log_info("player connected: %v", name) log_info("player connected: %v", name)
c.player = Player{name: name} player, err := loadPlayer(name)
if err != nil {
log_error("could not read player: %v", err)
player = &Player{name: name}
if err := player.Create(); err != nil {
}
fmt.Fprintf(c, "godspeed, %s.\n", player.name)
} else {
c.player = player
fmt.Fprintf(c, "welcome back, %s.\n", player.name)
}
break break
} }
} }
func (c *Connection) Close() error { func (c *Connection) Close() error {
log_info("player disconnecting: %s", c.player.name) log_info("player disconnecting: %s", c.PlayerName())
return c.Conn.Close() return c.Conn.Close()
} }
func (c *Connection) PlayerName() string { func (c *Connection) PlayerName() string {
if c.player == nil {
return ""
}
return c.player.name return c.player.name
} }

@ -54,7 +54,7 @@ func (e exoSystem) Store(db *sql.DB) {
} }
} }
func countPlanets(db *sql.DB) (int, error) { func countPlanets() (int, error) {
row := db.QueryRow(`select count(*) from planets`) row := db.QueryRow(`select count(*) from planets`)
var n int var n int

Loading…
Cancel
Save