Jelajahi Sumber

Saving scores. Normalizing datetime (to 2AM).

Steve Thielemann 3 tahun lalu
induk
melakukan
888c374ac0
4 mengubah file dengan 72 tambahan dan 20 penghapusan
  1. 31 8
      db.cpp
  2. 3 2
      db.h
  3. 2 0
      main.cpp
  4. 36 10
      play.cpp

+ 31 - 8
db.cpp

@@ -9,6 +9,14 @@
 #include "yaml-cpp/yaml.h"
 extern YAML::Node config;
 
+/*
+The database access is slow.
+
+So, make sure you set it up so that you do your writes right
+before you collect user input.  That way, the user won't see
+the lags.
+*/
+
 DBData::DBData(void)
     : db("space-data.db", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE) {
 
@@ -29,7 +37,7 @@ 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, \
+\"date\" INTEGER, \"hand\" INTEGER, \"score\" INTEGER, \
 PRIMARY KEY(\"username\", \"date\", \"hand\"));");
 }
 
@@ -55,9 +63,10 @@ void DBData::setSetting(const std::string &setting, const std::string &value) {
   stmt_setSet->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(?,?,?,?);");
+void DBData::save_score(time_t when, time_t 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);
@@ -66,21 +75,21 @@ void DBData::save_score(time_t when, std::string date, int hand, int score) {
   stmt.exec();
 }
 
-bool DBData::has_played_day(time_t day) {
+int 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);
+  stmt.bind(2, day);
   int count = -1;
   if (stmt.executeStep()) {
     count = stmt.getColumn(0);
   };
-  return (count > 0);
+  return count;
 }
 
 std::string make_date(time_t tt) {
@@ -94,4 +103,18 @@ std::string make_date(time_t tt) {
 
   std::string date = ss.str();
   return date;
+}
+
+void standard_date(time_t &tt, int hour) {
+  std::tm *local_tm = localtime(&tt);
+  // adjust date to 2:00:00 AM
+
+  tt -= (local_tm->tm_min * 60) + local_tm->tm_sec;
+  while (local_tm->tm_hour < hour) {
+    ++local_tm->tm_hour;
+    tt += 60 * 60;
+  }
+  if (local_tm->tm_hour > hour) {
+    tt -= (60 * 60) * (local_tm->tm_hour - hour);
+  }
 }

+ 3 - 2
db.h

@@ -22,10 +22,11 @@ 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);
+  void save_score(time_t when, time_t date, int hand, int score);
+  int has_played_day(time_t day);
 };
 
+void standard_date(time_t &tt, int hour = 2);
 std::string make_date(time_t tt);
 
 #endif

+ 2 - 0
main.cpp

@@ -439,7 +439,9 @@ int configure(door::Door &door, DBData &db) {
   std::string newColor;
 
   while (r >= 0) {
+
     if (save_deckcolor) {
+      door << menu;
       db.setSetting(deckcolor, newColor);
       save_deckcolor = false;
     }

+ 36 - 10
play.cpp

@@ -30,11 +30,24 @@ For now, it will play today.
 
 PlayCards::PlayCards(door::Door &d, DBData &dbd, std::mt19937 &r)
     : door{d}, db{dbd}, rng{r} {
-
+  get_logger = [this]() -> ofstream & { return door.log(); };
   init_values();
 
   play_day = std::chrono::system_clock::now();
-
+  // adjustment
+  time_t time_play_day = std::chrono::system_clock::to_time_t(play_day);
+  if (get_logger) {
+    get_logger() << "before: "
+                 << std::put_time(std::localtime(&time_play_day), "%F %R")
+                 << std::endl;
+  };
+  standard_date(time_play_day);
+  play_day = std::chrono::system_clock::from_time_t(time_play_day);
+  if (get_logger) {
+    get_logger() << "after: "
+                 << std::put_time(std::localtime(&time_play_day), "%F %R")
+                 << std::endl;
+  };
   spaceAceTriPeaks = make_tripeaks();
   score_panel = make_score_panel();
   streak_panel = make_streak_panel();
@@ -44,8 +57,6 @@ PlayCards::PlayCards(door::Door &d, DBData &dbd, std::mt19937 &r)
     int mx = door.width;
     int my = door.height;
   */
-
-  get_logger = [this]() -> ofstream & { return door.log(); };
 }
 
 PlayCards::~PlayCards() { get_logger = nullptr; }
@@ -72,7 +83,6 @@ void PlayCards::bonus(void) {
 }
 
 int PlayCards::play_cards(void) {
-  play_day = std::chrono::system_clock::now();
   init_values();
   std::string currentDefault = db.getSetting("DeckColor", "ALL");
   get_logger() << "DeckColor shows as " << currentDefault << std::endl;
@@ -235,8 +245,10 @@ next_hand:
           if (current_streak > 1)
             score += current_streak * 5;
           score_panel->update(door);
+          /*
           if (get_logger)
             get_logger() << "score_panel update : " << score << std::endl;
+            */
 
           // play card!
           state.at(active_card) = 2;
@@ -307,9 +319,8 @@ next_hand:
                 }
               }
             } else {
-              // this would be a "top" card.  Should set status = 4 and
-              // display something here?
-              get_logger() << "top card cleared?" << std::endl;
+              // top card cleared
+              // get_logger() << "top card cleared?" << std::endl;
               // display something at active_card position
               int cx, cy, level;
               cardgo(active_card, cx, cy, level);
@@ -331,10 +342,25 @@ next_hand:
               if (new_active != -1) {
                 active_card = new_active;
               } else {
-                get_logger() << "This looks like END OF GAME." << std::endl;
-                get_logger() << "SCORE: " << score << std::endl;
+                get_logger() << "Winner!" << std::endl;
+
                 // bonus for cards left
+                int bonus = 15 * (51 - card_number);
+
+                score += 15 * (51 - card_number);
+                score_panel->update(door);
+                door << " BONUS: " << bonus << door::nl;
+                get_logger() << "SCORE: " << score << std::endl;
+
+                // if (!config[CHEATER]) {
+                time_t right_now = std::chrono::system_clock::to_time_t(
+                    std::chrono::system_clock::now());
+                db.save_score(right_now,
+                              std::chrono::system_clock::to_time_t(play_day),
+                              hand, score);
+                //}
                 press_a_key(door);
+
                 if (hand < total_hands) {
                   hand++;
                   // current_streak = 0;