assign random planet on game start

pull/5/head
Jordan Orelli 10 years ago
parent 37dd4afc74
commit 45cb593785

44
db.go

@ -2,13 +2,21 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"math"
"math/rand"
"os"
)
var planetIndex map[int]exoSystem
func dbconnect() (*sql.DB, error) {
return sql.Open("sqlite3", "./exo.db")
}
func setupDb() {
db, err := sql.Open("sqlite3", "./exo.db")
db, err := dbconnect()
if err != nil {
bail(E_No_DB, "unable to open database: %v", err)
}
@ -29,7 +37,6 @@ func setupDb() {
}
n, err := countPlanets(db)
log_info("num planets: %v error: %v", n, err)
if n == 0 {
fi, err := os.Open(dataPath)
if err != nil {
@ -42,9 +49,9 @@ func setupDb() {
}
}
idx := indexPlanets(db)
log_info("%v", idx)
fillEdges(db, idx)
indexPlanets(db)
// log_info("%v", idx)
// fillEdges(db, idx)
stmnt = `create table if not exists edges (
id_1 integer,
@ -57,6 +64,16 @@ func setupDb() {
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 {
@ -78,7 +95,7 @@ func indexPlanets(db *sql.DB) map[int]exoSystem {
return nil
}
defer rows.Close()
planets := make(map[int]exoSystem, 551)
planetIndex = make(map[int]exoSystem, 551)
for rows.Next() {
var id int
p := exoSystem{}
@ -86,9 +103,9 @@ func indexPlanets(db *sql.DB) map[int]exoSystem {
log_info("unable to scan planet row: %v", err)
continue
}
planets[id] = p
planetIndex[id] = p
}
return planets
return planetIndex
}
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
}

@ -8,6 +8,7 @@ const (
E_Ok int = iota
E_No_Data
E_No_DB
E_No_Port
)
type errorGroup []error

@ -0,0 +1,6 @@
package main
import (
)

@ -1,8 +1,12 @@
package main
import (
"bufio"
"fmt"
"net"
"os"
"regexp"
"strings"
)
var dataPath = "/projects/exo/expl.speck"
@ -26,6 +30,41 @@ func bail(status int, template string, args ...interface{}) {
os.Exit(status)
}
func handleConnection(conn net.Conn) {
namePattern := regexp.MustCompile(`^[[:alpha:]][[:alnum:]-_]{0,19}$`)
r := bufio.NewReader(conn)
fmt.Fprintf(conn, "what is your name, adventurer?\n")
name, err := r.ReadString('\n')
if err == nil {
name = strings.TrimSpace(name)
log_info("player connected: %v", name)
} else {
log_error("player failed to connect: %v", err)
}
if !namePattern.MatchString(name) {
fmt.Fprintf(conn, "that name is illegal.\n")
}
planet, err := randomPlanet()
if err != nil {
log_error("player %s failed to get random planet: %v", name, err)
return
}
fmt.Fprintf(conn, "you are on the planet %s\n", planet.name)
}
func main() {
setupDb()
listener, err := net.Listen("tcp", ":9220")
if err != nil {
bail(E_No_Port, "unable to start server: %v", err)
}
for {
conn, err := listener.Accept()
if err != nil {
log_error("error accepting connection: %v", err)
continue
}
go handleConnection(conn)
}
}

Loading…
Cancel
Save