From 7959b99c54473505ee37e35e7a32133d143ee5a9 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 12 Jun 2016 21:59:17 -0400 Subject: Bot now logs stats in a file in case it restarts --- troublemaker.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/troublemaker.cpp b/troublemaker.cpp index 31381dc..d924894 100644 --- a/troublemaker.cpp +++ b/troublemaker.cpp @@ -10,6 +10,7 @@ #include #include #include +#include struct userstats { int total = 0; @@ -42,6 +43,33 @@ int main(int argc, char** argv) std::map stats; std::mutex stats_mutex; + // Read in old data + { + std::ifstream datafile("data.txt"); + + if (datafile.is_open()) + { + std::string line; + while (std::getline(datafile, line)) + { + std::istringstream iss(line); + + twitter::user_id uid; + iss >> uid; + + userstats& s = stats[uid]; + iss >> s.total; + iss >> s.days; + iss >> s.day2; + iss >> s.day3; + iss >> s.day4; + iss >> s.day5; + iss >> s.day6; + iss >> s.day7; + } + } + } + twitter::client client(auth); client.setUserStreamNotifyCallback([&] (twitter::notification n) { std::lock_guard friend_guard(friends_mutex); @@ -150,9 +178,12 @@ int main(int argc, char** argv) std::cout << "Twitter error while getting friends: " << resp << std::endl; } + std::cout << "stat rotation" << std::endl; // This is all just for stats rotation { std::lock_guard stats_guard(stats_mutex); + std::ofstream datafile("data.txt", std::ofstream::out | std::ofstream::trunc); + for (auto mapping : stats) { auto& s = mapping.second; @@ -167,9 +198,18 @@ int main(int argc, char** argv) s.day4 = s.day3; s.day3 = s.day2; s.day2 = s.day1; - s.day1 = 0; s.total = s.day2 + s.day3 + s.day4 + s.day5 + s.day6 + s.day7; + + datafile << mapping.first << " "; + datafile << s.total << " "; + datafile << s.days << " "; + datafile << s.day2 << " "; + datafile << s.day3 << " "; + datafile << s.day4 << " "; + datafile << s.day5 << " "; + datafile << s.day6 << " "; + datafile << s.day7 << std::endl; } } } -- cgit 1.4.1