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..30a96fa --- /dev/null +++ b/game.go @@ -0,0 +1,45 @@ +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 not null, + end text, + winner text, + win_method text + );` + if _, err := db.Exec(stmnt); err != nil { + 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 94508b2..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{}) { @@ -49,7 +50,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 +59,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) @@ -99,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 {