55 * version Pre-Alpha 2019-09-11
66 *
77 * TODO:
8- * Threshold interval
9- * Threadding
108 * Daemonise
119 */
1210
1311#include < iostream>
14- #include < chrono>
1512#include < thread>
1613#include < deque>
1714#include < vector>
1815#include < functional>
1916
17+ #include " core/counterThread.h"
18+ #include " core/params.h"
19+ #include " tool/args.h"
20+ #include " tool/getPlayerCount.h"
2021#include " wrapper/curl.h"
22+ #include " wrapper/daemon.h"
2123#include " wrapper/notify.h"
22- #include " tool/getPlayerCount.h"
23- #include " tool/args.h"
24- #include " core/params.h"
2524
25+ /*
26+ * Main function of steamcountsnotifyd
27+ */
2628int main (int argc, char **argv)
2729{
2830 std::deque<std::string> args = tool::toArgs (argc, argv);
2931 args.pop_front (); // Program execution name not needed
3032 param::config config;
3133 bool running = config.setFromArgs (args);
3234
33- // Functional lambda
34- std::function<void (unsigned int , param::appidName_s, const param::config &)> appidRunningThread = [](unsigned int appid, param::appidName_s game, const param::config &config)
35- {
36- unsigned int currentCount = 0 ;
37- bool notify = false ;
38- bool running = true ;
39- std::string messageTitle;
40- std::string messageDetails;
41- wrapper::curl curlJob;
42- unsigned int currentThreadInterval = config.getIntervalMins ();
43-
44- curlJob.setTimeout (config.getConnectionTimeout ());
45-
46- while (running)
47- {
48- curlJob.setUrl (" https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?appid=" +std::to_string (appid));
49-
50- // Perform the job
51- curlJob.perform ();
52-
53- // Determine message
54- if (curlJob.getHttpResponseCode () == 200 )
55- {
56- currentCount = tool::getPlayerCount (curlJob.getHttpData ());
57- if (currentCount >= game.threshold )
58- {
59- notify = true ;
60- messageTitle = game.name ;
61- messageDetails = " Player counts: " +std::to_string (currentCount);
62- }
63- }
64- else
65- {
66- messageTitle = " ERROR" ;
67- messageDetails = " Cannot fetch player numbers - no internet connection or steam API is down." ;
68- }
69-
70- // Clear data
71- curlJob.clearHttpData ();
72-
73- // Notify
74- if (notify)
75- {
76- wrapper::notify::init ();
77- wrapper::notify notifyJob (messageTitle, messageDetails);
78- notifyJob.setTimeout (config.getNotificationTimeout ());
79- notifyJob.show ();
80-
81- notify = false ;
82- }
83-
84- // Delay
85- std::this_thread::sleep_for (std::chrono::minutes (currentThreadInterval));
86- }
87- };
88-
8935 // If help or version message not used (normal execution)
9036 if (running)
9137 {
38+ // std::function<void(unsigned int, param::appidName_s, const param::config &)> thread::appidRunning;
39+ wrapper::notify::init (" steamcountsnotifyd" );
40+
9241 // Initialise and run threads for each appid/game
9342 std::vector<std::thread> appidThreadVector;
9443
9544 for (const auto &[appid, game] : config.getAppidMap ())
9645 {
97- appidThreadVector.emplace_back (std::thread (appidRunningThread , appid, game, config));
46+ appidThreadVector.emplace_back (std::thread (std::function (cthread::appidRunning) , appid, game, config));
9847 }
9948
10049 // Join thread if joinable
@@ -105,6 +54,8 @@ int main(int argc, char **argv)
10554 thread.join ();
10655 }
10756 }
57+
58+ wrapper::notify::uninit ();
10859 }
10960
11061 return 0 ;
0 commit comments