From bdbce01c970ea0d8bca3147210253f4520b92045 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 22 Aug 2012 22:50:03 -0300 Subject: [PATCH] Add uid param to stats module --- modules/client_stats/stats.lua | 30 +++++++++++++++++++++--------- src/framework/luafunctions.cpp | 1 + src/framework/util/crypt.cpp | 10 ++++++++++ src/framework/util/crypt.h | 3 ++- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/modules/client_stats/stats.lua b/modules/client_stats/stats.lua index 6d439a1b..8a7ec2fb 100644 --- a/modules/client_stats/stats.lua +++ b/modules/client_stats/stats.lua @@ -1,9 +1,23 @@ HOST = 'otclient.herokuapp.com' PORT = 80 +UUID = nil + +--HOST = 'localhost' +--PORT = 3000 + +function initUUID() + UUID = g_settings.getString('report-uuid') + if not UUID or #UUID ~= 36 then + UUID = g_crypt.genUUID() + g_settings.set('report-uuid', UUID) + end +end function init() connect(g_game, { onGameStart = onGameStart }) + initUUID() + protocolHttp = ProtocolHttp.create() connect(protocolHttp, { onConnect = onConnect, onRecv = onRecv, @@ -19,33 +33,31 @@ function terminate() protocolHttp = nil end -function sendInfo() +function sendReport() protocolHttp:connect(HOST, PORT) end -- events function onGameStart() - scheduleEvent(sendInfo, 5*1000) + scheduleEvent(sendReport, 5*1000) end function onConnect(protocol) - pinfo('Connected to stats server.') - if not g_game.isOnline() then - perror('Could not send stats. Game not online.') protocol:disconnect() return end local post = '' - post = post .. 'os=' .. g_app.getOs() + post = post .. 'uid=' .. UUID + post = post .. '&os' .. g_app.getOs() post = post .. '&graphics_vendor=' .. g_graphics.getVendor() post = post .. '&graphics_renderer=' .. g_graphics.getRenderer() post = post .. '&graphics_version=' .. g_graphics.getVersion() post = post .. '&painter_engine=' .. g_graphics.getPainterEngine() post = post .. '&fps=' .. g_app.getBackgroundPaneFps() post = post .. '&max_fps=' .. g_app.getBackgroundPaneMaxFps() - post = post .. '&fullscreen=' .. fromboolean(g_window.isFullscreen()) + post = post .. '&fullscreen=' .. tostring(g_window.isFullscreen()) post = post .. '&window_width=' .. g_window.getWidth() post = post .. '&window_height=' .. g_window.getHeight() post = post .. '&player_name=' .. g_game.getLocalPlayer():getName() @@ -74,11 +86,11 @@ end function onRecv(protocol, message) if string.find(message, 'HTTP/1.1 200 OK') then - pinfo('Stats sent to server successfully!') + --pinfo('Stats sent to server successfully!') end protocol:disconnect() end function onError(protocol, message, code) - perror('Could not send statistics. ' .. message .. 'Code: ' .. code) + perror('Could not send statistics: ' .. message) end diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 2eee7751..1db49ad4 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -88,6 +88,7 @@ void Application::registerLuaFunctions() // Crypt g_lua.registerSingletonClass("g_crypt"); + g_lua.bindSingletonFunction("g_crypt", "genUUID", &Crypt::genUUID, &g_crypt); g_lua.bindSingletonFunction("g_crypt", "encrypt", &Crypt::encrypt, &g_crypt); g_lua.bindSingletonFunction("g_crypt", "decrypt", &Crypt::decrypt, &g_crypt); g_lua.bindSingletonFunction("g_crypt", "sha1Encode", &Crypt::sha1Encode, &g_crypt); diff --git a/src/framework/util/crypt.cpp b/src/framework/util/crypt.cpp index cb0e7c2e..8bf4f9bb 100644 --- a/src/framework/util/crypt.cpp +++ b/src/framework/util/crypt.cpp @@ -25,6 +25,9 @@ #include #include +#include +#include + #include #include @@ -147,6 +150,13 @@ std::string Crypt::xorCrypt(const std::string& buffer, const std::string& key) return out; } +std::string Crypt::genUUID() +{ + boost::uuids::random_generator gen; + boost::uuids::uuid u = gen(); + return boost::uuids::to_string(u); +} + std::string Crypt::genUUIDKey() { boost::hash uuid_hasher; diff --git a/src/framework/util/crypt.h b/src/framework/util/crypt.h index 5571d68c..7a08b85a 100644 --- a/src/framework/util/crypt.h +++ b/src/framework/util/crypt.h @@ -37,7 +37,7 @@ public: std::string base64Encode(const std::string& decoded_string); std::string base64Decode(const std::string& encoded_string); std::string xorCrypt(const std::string& buffer, const std::string& key); - std::string genUUIDKey(); + std::string genUUID(); std::string encrypt(const std::string& decrypted_string); std::string decrypt(const std::string& encrypted_string); std::string md5Encode(const std::string& decoded_string, bool upperCase); @@ -52,6 +52,7 @@ public: bool rsaDecrypt(unsigned char *msg, int size); private: + std::string genUUIDKey(); RSA *m_rsa; };