From 42fb45fac821b465d8d2a706290e247031d3c3c7 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 10 Nov 2014 09:04:30 -0500 Subject: [PATCH 1/2] add a games table --- commands.go | 9 +++++++++ db.go | 1 + game.go | 25 +++++++++++++++++++++++++ main.go | 4 +--- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 game.go diff --git a/commands.go b/commands.go index 785f229..5e96912 100644 --- a/commands.go +++ b/commands.go @@ -231,6 +231,14 @@ var colonizeCommand = &Command{ }, } +var winCommand = &Command{ + name: "win", + help: "win the game.", + handler: func(conn *Connection, args ...string) { + conn.Win() + }, +} + func move(conn *Connection, to *System) { start := conn.System() start.Leave(conn) @@ -344,4 +352,5 @@ func init() { registerCommand(nearbyCommand) registerCommand(scanCommand) registerCommand(mkBombCommand) + registerCommand(winCommand) } diff --git a/db.go b/db.go index f27f85a..19496d3 100644 --- a/db.go +++ b/db.go @@ -68,6 +68,7 @@ func setupDb() { planetsData() edgesTable() playersTable() + gamesTable() fillEdges() } diff --git a/game.go b/game.go new file mode 100644 index 0000000..8e3bd52 --- /dev/null +++ b/game.go @@ -0,0 +1,25 @@ +package main + +import ( + "time" +) + +type Game struct { + start time.Time + end time.Time + winner string + winMethod string +} + +func gamesTable() { + stmnt := `create table if not exists games ( + id integer not null primary key autoincrement, + start text, + end text, + winner text, + win_method text + );` + if _, err := db.Exec(stmnt); err != nil { + log_error("couldn't create games table: %v", err) + } +} diff --git a/main.go b/main.go index 94508b2..92096c5 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,6 @@ func handleConnection(conn *Connection) { } else { fmt.Fprintf(conn, "you are in the system %s. There are %d planets here.\n", system.name, system.planets) } -READING: for { line, err := conn.ReadString('\n') switch err { @@ -59,8 +58,7 @@ READING: break default: log_error("failed to read line from player %s: %v", conn.PlayerName(), err) - time.Sleep(time.Second) - continue READING + return } line = strings.TrimSpace(line) From c944be15f93a0209f783eee80639c2e26e0fe8c4 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 10 Nov 2014 17:22:13 -0500 Subject: [PATCH 2/2] define a game object --- game.go | 22 +++++++++++++++++++++- main.go | 10 +++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/game.go b/game.go index 8e3bd52..30a96fa 100644 --- a/game.go +++ b/game.go @@ -14,7 +14,7 @@ type Game struct { func gamesTable() { stmnt := `create table if not exists games ( id integer not null primary key autoincrement, - start text, + start text not null, end text, winner text, win_method text @@ -23,3 +23,23 @@ func gamesTable() { log_error("couldn't create games table: %v", err) } } + +func NewGame() *Game { + game := &Game{ + start: time.Now(), + } + if err := game.Create(); err != nil { + log_error("%v", err) + } + return game +} + +func (g *Game) Create() error { + _, err := db.Exec(` + insert into games + (start) + values + (?) + ;`, g.start) + return err +} diff --git a/main.go b/main.go index 92096c5..6f26e4c 100644 --- a/main.go +++ b/main.go @@ -12,9 +12,10 @@ import ( ) var ( - dataPath = "/projects/exo/expl.speck" - info_log *log.Logger - error_log *log.Logger + dataPath = "/projects/exo/expl.speck" + info_log *log.Logger + error_log *log.Logger + currentGame *Game ) func log_error(template string, args ...interface{}) { @@ -97,6 +98,9 @@ func main() { bail(E_No_Port, "unable to start server: %v", err) } go RunQueue() + + currentGame = NewGame() + for { conn, err := listener.Accept() if err != nil {