remember people

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

59
db.go

@ -9,19 +9,20 @@ import (
"os"
)
var planetIndex map[int]exoSystem
func dbconnect() (*sql.DB, error) {
return sql.Open("sqlite3", "./exo.db")
}
var (
planetIndex map[int]exoSystem
db *sql.DB
)
func setupDb() {
db, err := dbconnect()
func dbconnect() {
var err error
db, err = sql.Open("sqlite3", "./exo.db")
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 (
id integer not null primary key autoincrement,
name text,
@ -30,13 +31,17 @@ func setupDb() {
z 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 {
log_error("couldn't create table: %v", err)
log_error("couldn't count planets: %v", err)
return
}
n, err := countPlanets(db)
if n == 0 {
fi, err := os.Open(dataPath)
if err != nil {
@ -48,32 +53,26 @@ func setupDb() {
planet.Store(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_2 integer,
distance real
);`
_, err = db.Exec(stmnt)
if err != nil {
if _, err := db.Exec(stmnt); err != nil {
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 {

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

@ -1,6 +1,7 @@
package main
import (
"fmt"
"regexp"
)
@ -11,9 +12,38 @@ func ValidName(name string) bool {
}
type Player struct {
id int
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 {
net.Conn
*bufio.Reader
player Player
player *Player
}
func NewConnection(conn net.Conn) *Connection {
@ -35,16 +35,31 @@ func (c *Connection) Login() {
continue
}
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
}
}
func (c *Connection) Close() error {
log_info("player disconnecting: %s", c.player.name)
log_info("player disconnecting: %s", c.PlayerName())
return c.Conn.Close()
}
func (c *Connection) PlayerName() string {
if c.player == nil {
return ""
}
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`)
var n int

Loading…
Cancel
Save