From 5b7bf82edb672d5d5dbd8b1de64532aeec695661 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 2 Mar 2018 19:47:19 -0500 Subject: Handled games with no images For games where no images could be downloaded from Steam, which happens if a game has been removed from the store, the bot will now use a default background. This image is copyright Nintendo. --- database.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ database.h | 2 ++ lunatic.cpp | 26 +++++++++++++++++--------- res/README.md | 2 +- res/default.png | Bin 0 -> 1080583 bytes 5 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 res/default.png diff --git a/database.cpp b/database.cpp index 953c217..19ba0e0 100644 --- a/database.cpp +++ b/database.cpp @@ -82,6 +82,47 @@ achievement database::getRandomAchievement() const return result; } +bool database::doesGameHaveImages(int gameId) const +{ + std::string queryString = "SELECT COUNT(*) FROM images WHERE game_id = ? ORDER BY RANDOM() LIMIT 1"; + + sqlite3_stmt* ppstmt; + if (sqlite3_prepare_v2( + ppdb_, + queryString.c_str(), + queryString.length(), + &ppstmt, + NULL) != SQLITE_OK) + { + std::string errorMsg = sqlite3_errmsg(ppdb_); + sqlite3_finalize(ppstmt); + + throw std::logic_error(errorMsg); + } + + if (sqlite3_bind_int(ppstmt, 1, gameId) != SQLITE_OK) + { + std::string errorMsg = sqlite3_errmsg(ppdb_); + sqlite3_finalize(ppstmt); + + throw std::logic_error(errorMsg); + } + + if (sqlite3_step(ppstmt) != SQLITE_ROW) + { + std::string errorMsg = sqlite3_errmsg(ppdb_); + sqlite3_finalize(ppstmt); + + throw std::logic_error(errorMsg); + } + + int result = sqlite3_column_int(ppstmt, 0); + + sqlite3_finalize(ppstmt); + + return (result > 0); +} + std::string database::getRandomImageForGame(int gameId) const { std::string queryString = "SELECT filename FROM images WHERE game_id = ? ORDER BY RANDOM() LIMIT 1"; diff --git a/database.h b/database.h index 9dcb118..50f5b55 100644 --- a/database.h +++ b/database.h @@ -46,6 +46,8 @@ public: achievement getRandomAchievement() const; + bool doesGameHaveImages(int gameId) const; + std::string getRandomImageForGame(int gameId) const; did getRandomDidForAchievement(int achievementId) const; diff --git a/lunatic.cpp b/lunatic.cpp index 291bd09..58a0854 100644 --- a/lunatic.cpp +++ b/lunatic.cpp @@ -65,9 +65,6 @@ int main(int argc, char** argv) std::cout << "Generating tweet" << std::endl; achievement ach = db.getRandomAchievement(); - std::string imageName = db.getRandomImageForGame(ach.gameId); - std::string imagePath = config["images"].as() - + "/" + imageName; Magick::Image moonColor; moonColor.read("res/" + ach.color + ".png"); @@ -150,14 +147,25 @@ int main(int argc, char** argv) shadow.negate(); shadow.blur(0, 12); - // Read the game image + // Read the game image, using a default if the game has no images Magick::Image image; - image.read(imagePath); - // Stretch and pixelate it - image.transform("1600x900!"); - image.scale("80x45"); - image.scale("1600x900"); + if (db.doesGameHaveImages(ach.gameId)) + { + std::string imageName = db.getRandomImageForGame(ach.gameId); + std::string imagePath = config["images"].as() + + "/" + imageName; + + image.read(imagePath); + + // Stretch and pixelate it + image.transform("1600x900!"); + image.scale("80x45"); + image.scale("1600x900"); + } else { + image.read("res/default.png"); + image.transform("1600x900!"); + } // Add the generated overlay to it image.composite(shadow, 0, 0, Magick::OverCompositeOp); diff --git a/res/README.md b/res/README.md index 876f0db..814c2b2 100644 --- a/res/README.md +++ b/res/README.md @@ -1 +1 @@ -The images in this directory are originally from the game Super Mario Odyssey, which is copyrighted by Nintendo. \ No newline at end of file +The images in this directory, apart from overlay.png and overlay.psd, are originally from the game Super Mario Odyssey, which is copyrighted by Nintendo. \ No newline at end of file diff --git a/res/default.png b/res/default.png new file mode 100644 index 0000000..6323270 Binary files /dev/null and b/res/default.png differ -- cgit 1.4.1