Browse Source

Working database insert/replace.

Steve Thielemann 3 years ago
parent
commit
68b8392c3a
2 changed files with 25 additions and 3 deletions
  1. 5 2
      db.go
  2. 20 1
      space-ace.go

+ 5 - 2
db.go

@@ -43,7 +43,7 @@ func (db *DBData) Create() {
 }
 
 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)
+	rows, err := db.DB.Query("SELECT value FROM settings WHERE username=? AND setting=?;", db.User, setting)
 	if err != nil {
 		log.Panicln("Query GetSetting", err)
 	}
@@ -54,14 +54,17 @@ func (db *DBData) GetSetting(setting string, ifMissing string) string {
 		if err != nil {
 			log.Panicln("Scan GetSetting", err)
 		}
+		// log.Printf("GetSetting %s %s : %s\n", db.User, setting, value)
 		return value
 	}
+	// log.Printf("GetSetting %s %s :(", db.User, setting)
 	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)
+	_, err := db.DB.Exec("REPLACE INTO settings(username, setting, value) VALUES(?,?,?);", db.User, setting, value)
 	if err != nil {
 		log.Panicln("Query SetSetting", err)
 	}
+	// log.Printf("SetSetting %s %s = %s\n", db.User, setting, value)
 }

+ 20 - 1
space-ace.go

@@ -374,7 +374,26 @@ func main() {
 
 	s.Display(&d)
 	d.Write(door.Goto(1, 1) + door.Reset)
-	d.Key()
+
+	last_call, _ := strconv.ParseInt(db.GetSetting("LastCall", "0"), 10, 64)
+	now := time.Now()
+	db.SetSetting("LastCall", fmt.Sprintf("%d", now.Unix()))
+
+	if last_call != 0 {
+		d.Write("Welcome Back!" + door.CRNL)
+		delta := now.Sub(time.Unix(last_call, 0))
+		hours := delta.Hours()
+		if hours > 24 {
+			d.Write(fmt.Sprintf("It's been %0.1f days since you last played."+door.CRNL, hours/24))
+		} else {
+			if hours > 1 {
+				d.Write(fmt.Sprintf("It's been %0.1f hours since you last played."+door.CRNL, hours))
+			} else {
+				minutes := delta.Minutes()
+				d.Write(fmt.Sprintf("It's been %0.1f minutes since you last played."+door.CRNL, minutes))
+			}
+		}
+	}
 
 	bold := door.Color(1, 37, 40)
 	bolder := door.ColorText("BLI BOLD YEL ON BLUE")