Browse Source

Space Ace updates. Initial database. Config.

Steve Thielemann 3 years ago
parent
commit
0e5bf8c21e
3 changed files with 191 additions and 7 deletions
  1. 78 0
      config.go
  2. 67 0
      db.go
  3. 46 7
      space-ace.go

+ 78 - 0
config.go

@@ -0,0 +1,78 @@
+package main
+
+import (
+	"bufio"
+	"errors"
+	"fmt"
+	"log"
+	"os"
+	"strings"
+)
+
+func FileExists(path string) bool {
+	_, err := os.Stat(path)
+	return !errors.Is(err, os.ErrNotExist)
+}
+
+func LoadConfig(filename string) map[string]string {
+	file, err := os.Open(filename)
+	if err != nil {
+		log.Panicf("Open(%s): %s\n", filename, err)
+	}
+
+	defer file.Close()
+
+	var results map[string]string = make(map[string]string)
+
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		line := scanner.Text()
+		if len(line) == 0 {
+			continue
+		}
+		if line[0] == '#' {
+			continue
+		}
+		// Ok, I have a line, now what?
+		parts := strings.SplitN(line, ": ", 2)
+		if len(parts) == 2 {
+			parts[1] = strings.Trim(parts[1], " ")
+			// Handle quoted values
+			if parts[1][0] == '"' && parts[1][len(parts[1])-1] == '"' {
+				parts[1] = parts[1][1 : len(parts[1])-1]
+			}
+			if parts[1][0] == '\'' && parts[1][len(parts[1])-1] == '\'' {
+				parts[1] = parts[1][1 : len(parts[1])-1]
+			}
+
+			results[parts[0]] = parts[1]
+			// } else {
+			// 	fmt.Printf("I'm not sure about [%s]\n", line)
+		}
+	}
+	return results
+
+}
+
+func SaveConfig(filename string, config map[string]string) {
+	file, err := os.Create(filename)
+	if err != nil {
+		log.Panicf("Open(%s): %s\n", filename, err)
+	}
+
+	defer file.Close()
+
+	for key, value := range config {
+		fmt.Fprintf(file, "%s: %s\n", key, value)
+	}
+}
+
+// SetConfigDefault Set default value (if not present)
+func SetConfigDefault(config *map[string]string, key string, value string) bool {
+	_, ok := (*config)[key]
+	if !ok {
+		(*config)[key] = value
+		return true
+	}
+	return false
+}

+ 67 - 0
db.go

@@ -0,0 +1,67 @@
+package main
+
+import (
+	"database/sql"
+	"log"
+)
+
+type DBData struct {
+	DB   *sql.DB
+	User string
+}
+
+func (db *DBData) Open(filename string) {
+	var err error
+
+	db.DB, err = sql.Open("sqlite3", filename)
+	if err != nil {
+		log.Panicln("Error Opening Sqlite3:", err)
+	}
+}
+
+func (db *DBData) Close() {
+	db.DB.Close()
+}
+
+func (db *DBData) Create() {
+	_, err := db.DB.Exec(
+		"CREATE TABLE IF NOT EXISTS settings(username TEXT, setting TEXT, value TEXT, PRIMARY KEY(username, setting));")
+	if err != nil {
+		log.Panicln(err)
+	}
+	_, err = db.DB.Exec(
+		"CREATE TABLE IF NOT EXISTS scores ( \"username\" TEXT, \"when\" INTEGER, \"date\" INTEGER, \"hand\" INTEGER, \"won\" INTEGER, \"score\" INTEGER, PRIMARY KEY(\"username\", \"date\", \"hand\"));")
+	if err != nil {
+		log.Panicln(err)
+	}
+	_, err = db.DB.Exec(
+		"CREATE TABLE IF NOT EXISTS \"monthly\" ( \"month\"	INTEGER, \"username\" TEXT, \"days\" INTEGER, \"hands_won\" INTEGER, \"score\" INTEGER, PRIMARY KEY(\"month\",\"username\") );")
+	if err != nil {
+		log.Panicln(err)
+	}
+
+}
+
+func (db *DBData) GetSetting(setting string, ifMissing string) string {
+	rows, err := db.DB.Query("SELECT value FROM settings WHERE username=? AND setting=?", db.User, setting)
+	if err != nil {
+		log.Panicln("Query GetSetting", err)
+	}
+	defer rows.Close()
+	for rows.Next() {
+		var value string
+		err = rows.Scan(&value)
+		if err != nil {
+			log.Panicln("Scan GetSetting", err)
+		}
+		return value
+	}
+	return ifMissing
+}
+
+func (db *DBData) SetSetting(setting string, value string) {
+	_, err := db.DB.Query("REPLACE INTO settings(username, setting, value) VALUES(?,?,?)", db.User, setting, value)
+	if err != nil {
+		log.Panicln("Query SetSetting", err)
+	}
+}

+ 46 - 7
space-ace.go

@@ -1,7 +1,6 @@
 package main
 
 import (
-	"database/sql"
 	"fmt"
 	"math/rand"
 	"os"
@@ -302,10 +301,12 @@ func panel_demo(d *door.Door) {
 	d.Write(p.Output() + door.CRNL)
 }
 
+/*
 func createTable(db *sql.DB) {
 	db.Exec("CREATE TABLE IF NOT EXISTS settings(username TEXT, setting TEXT, value TEXT, PRIMARY KEY(username, setting));")
 	db.Exec("CREATE TABLE IF NOT EXISTS scores ( \"username\" TEXT, \"when\" INTEGER, \"date\" INTEGER, \"hand\" INTEGER, \"won\" INTEGER, \"score\" INTEGER, PRIMARY KEY(\"username\", \"date\", \"hand\"));")
 }
+*/
 
 func main() {
 	var message string
@@ -320,16 +321,54 @@ func main() {
 	rng := rand.New(mt19937.New())
 	rng.Seed(time.Now().UnixNano())
 
-	sqliteDatabase, err := sql.Open("sqlite3", "./space-database.db")
-	if err != nil {
-		fmt.Printf("%#v\n", err)
-	}
-	defer sqliteDatabase.Close()
-	createTable(sqliteDatabase)
+	/*
+		sqliteDatabase, err := sql.Open("sqlite3", "./space-database.db")
+		if err != nil {
+			fmt.Printf("%#v\n", err)
+		}
+		defer sqliteDatabase.Close()
+		createTable(sqliteDatabase)
+	*/
 	d := door.Door{}
 
 	d.Init("space-ace")
 
+	db := DBData{}
+	db.Open("space-database.db")
+	defer db.Close()
+	db.User = d.Config.Real_name // or d.Config.Handle
+
+	var Config map[string]string
+	const config_filename = "space-ace.yaml"
+
+	if FileExists(config_filename) {
+		Config = LoadConfig(config_filename)
+	} else {
+		Config = make(map[string]string)
+
+	}
+
+	var update_config bool = false
+
+	config_defaults := map[string]string{"hands_per_day": "3",
+		"date_format":     "%B %d",
+		"date_score":      "%m/%d/%Y",
+		"makeup_per_day":  "5",
+		"play_days_ahead": "2",
+		"date_monthly":    "%B %Y"}
+
+	// _seed
+
+	for key, value := range config_defaults {
+		if SetConfigDefault(&Config, key, value) {
+			update_config = true
+		}
+	}
+
+	if update_config {
+		SaveConfig(config_filename, Config)
+	}
+
 	s := StarField{}
 	s.Regenerate()