Jelajahi Sumber

Latest changes before refactoring.

Steve Thielemann 3 tahun lalu
induk
melakukan
8e93da8cf1
3 mengubah file dengan 47 tambahan dan 0 penghapusan
  1. 41 0
      db.cpp
  2. 4 0
      db.h
  3. 2 0
      main.cpp

+ 41 - 0
db.cpp

@@ -1,7 +1,9 @@
 #include "db.h"
 
 #include <SQLiteCpp/VariadicBind.h>
+#include <iomanip>
 #include <iostream>
+#include <sstream>
 
 DBData::DBData(void)
     : db("space-data.db", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE) {
@@ -16,6 +18,10 @@ void DBData::init(void) {
   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\" TEXT, \"hand\" INTEGER, \"score\" INTEGER, \
+PRIMARY KEY(\"username\", \"date\", \"hand\"));");
 }
 
 void DBData::setUser(std::string currentUser) { user = currentUser; }
@@ -42,4 +48,39 @@ void DBData::setSetting(const std::string &setting, const std::string &value) {
   stmt.bind(2, setting);
   stmt.bind(3, value);
   stmt.exec();
+}
+
+void DBData::save_score(time_t when, std::string date, int hand, int score) {
+  SQLite::Statement stmt(db, "INSERT INTO scores( \"username\", \"when\", "
+                             "\"date\", \"hand\", \"score\") VALUES(?,?,?,?);");
+  stmt.bind(1, user);
+  stmt.bind(2, when);
+  stmt.bind(3, date);
+  stmt.bind(4, hand);
+  stmt.bind(5, score);
+  stmt.exec();
+}
+
+bool DBData::has_played_day(time_t day) {
+  // get date from this
+
+  // std::stringstream ss;
+  // ss << std::put_time(std::localtime(&day), "%Y/%0m/%0d");
+  std::string today = make_date(day);
+  SQLite::Statement stmt(
+      db, "SELECT COUNT(*) FROM scores WHERE \"username\"=? AND \"DATE\"=?;");
+  stmt.bind(1, user);
+  stmt.bind(2, today);
+  int count = -1;
+  if (stmt.executeStep()) {
+    count = stmt.getColumn(0);
+  };
+  return (count > 0);
+}
+
+std::string make_date(time_t tt) {
+  std::stringstream ss;
+  ss << std::put_time(std::localtime(&tt), "%Y/%0m/%0d");
+  std::string date = ss.str();
+  return date;
 }

+ 4 - 0
db.h

@@ -20,6 +20,10 @@ public:
                     const std::string &value);*/
   std::string getSetting(const std::string &setting, std::string ifMissing);
   void setSetting(const std::string &setting, const std::string &value);
+  void save_score(time_t when, std::string date, int hand, int score);
+  bool has_played_day(time_t day);
 };
 
+std::string make_date(time_t tt);
+
 #endif

+ 2 - 0
main.cpp

@@ -1058,6 +1058,7 @@ next_hand:
           now_what = false;
           break;
         case ' ':
+        case '5':
           // can we play this card?
           /*
           get_logger() << "can_play( " << active_card << ":"
@@ -1248,6 +1249,7 @@ next_hand:
     }
   }
   if (r == 'Q') {
+    // continue with hand or quit?
     if (hand < total_hands) {
       press_a_key(door);
       hand++;