No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stats.lua 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. UUID = nil
  2. HOST = 'otclient.herokuapp.com'
  3. PORT = 80
  4. FIRST_REPORT_DELAY = 15
  5. REPORT_DELAY = 60
  6. sendReportEvent = nil
  7. firstReportEvent = nil
  8. function initUUID()
  9. UUID = g_settings.getString('report-uuid')
  10. if not UUID or #UUID ~= 36 then
  11. UUID = g_crypt.genUUID()
  12. g_settings.set('report-uuid', UUID)
  13. end
  14. end
  15. function init()
  16. connect(g_game, { onGameStart = onGameStart,
  17. onGameEnd = onGameEnd })
  18. initUUID()
  19. end
  20. function terminate()
  21. disconnect(g_game, { onGameStart = onGameStart,
  22. onGameEnd = onGameEnd })
  23. removeEvent(firstReportEvent)
  24. removeEvent(sendReportEvent)
  25. end
  26. function configure(host, port, delay)
  27. if not host then return end
  28. HOST = host
  29. PORT = port or PORT
  30. REPORT_DELAY = delay or REPORT_DELAY
  31. end
  32. function sendReport()
  33. if not HOST then return end
  34. local protocolHttp = ProtocolHttp.create()
  35. protocolHttp.onConnect = onConnect
  36. protocolHttp.onRecv = onRecv
  37. protocolHttp.onError = onError
  38. protocolHttp:connect(HOST, PORT)
  39. end
  40. function onGameStart()
  41. if not HOST then return end
  42. removeEvent(firstReportEvent)
  43. removeEvent(sendReportEvent)
  44. firstReportEvent = addEvent(sendReport, FIRST_REPORT_DELAY*1000)
  45. sendReportEvent = cycleEvent(sendReport, REPORT_DELAY*1000)
  46. end
  47. function onGameEnd()
  48. removeEvent(firstReportEvent)
  49. removeEvent(sendReportEvent)
  50. end
  51. function onConnect(protocol)
  52. if not g_game.isOnline() then
  53. protocol:disconnect()
  54. return
  55. end
  56. local post = ''
  57. post = post .. 'uid=' .. urlencode(UUID)
  58. post = post .. '&report_delay=' .. REPORT_DELAY
  59. post = post .. '&os=' .. urlencode(g_app.getOs())
  60. post = post .. '&graphics_vendor=' .. urlencode(g_graphics.getVendor())
  61. post = post .. '&graphics_renderer=' .. urlencode(g_graphics.getRenderer())
  62. post = post .. '&graphics_version=' .. urlencode(g_graphics.getVersion())
  63. post = post .. '&painter_engine=' .. urlencode(g_graphics.getPainterEngine())
  64. post = post .. '&fps=' .. urlencode(g_app.getBackgroundPaneFps())
  65. post = post .. '&max_fps=' .. urlencode(g_app.getBackgroundPaneMaxFps())
  66. post = post .. '&fullscreen=' .. urlencode(tostring(g_window.isFullscreen()))
  67. post = post .. '&window_width=' .. urlencode(g_window.getWidth())
  68. post = post .. '&window_height=' .. urlencode(g_window.getHeight())
  69. post = post .. '&player_name=' .. urlencode(g_game.getCharacterName())
  70. post = post .. '&world_name=' .. urlencode(g_game.getWorldName())
  71. post = post .. '&otserv_host=' .. urlencode(G.host)
  72. post = post .. '&otserv_port=' .. G.port
  73. post = post .. '&otserv_protocol=' .. urlencode(g_game.getProtocolVersion())
  74. post = post .. '&otserv_client=' .. urlencode(g_game.getClientVersion())
  75. post = post .. '&build_version=' .. urlencode(g_app.getVersion())
  76. post = post .. '&build_revision=' .. urlencode(g_app.getBuildRevision())
  77. post = post .. '&build_commit=' .. urlencode(g_app.getBuildCommit())
  78. post = post .. '&build_date=' .. urlencode(g_app.getBuildDate())
  79. post = post .. '&display_width=' .. g_window.getDisplayWidth()
  80. post = post .. '&display_height=' .. g_window.getDisplayHeight()
  81. post = post .. '&cpu=' .. urlencode(g_platform.getCPUName())
  82. post = post .. '&mem=' .. g_platform.getTotalSystemMemory()
  83. post = post .. '&os_name=' .. urlencode(g_platform.getOSName())
  84. post = post .. getAdditionalData()
  85. local message = ''
  86. message = message .. "POST /report HTTP/1.1\r\n"
  87. message = message .. "Host: " .. HOST .. "\r\n"
  88. message = message .. "Accept: */*\r\n"
  89. message = message .. "Connection: close\r\n"
  90. message = message .. "Content-Type: application/x-www-form-urlencoded\r\n"
  91. message = message .. "Content-Length: " .. post:len() .. "\r\n\r\n"
  92. message = message .. post
  93. protocol:send(message)
  94. protocol:recv()
  95. end
  96. function getAdditionalData()
  97. return ''
  98. end
  99. function onRecv(protocol, message)
  100. if string.find(message, 'HTTP/1.1 200 OK') then
  101. --pinfo('Stats sent to server successfully!')
  102. end
  103. protocol:disconnect()
  104. end
  105. function onError(protocol, message, code)
  106. pdebug('Could not send statistics: ' .. message)
  107. end
  108. function urlencode(str)
  109. local encodeChar=function(chr)
  110. return string.format("%%%X",string.byte(chr))
  111. end
  112. local output, t = string.gsub(str,"[^%w]",encodeChar)
  113. return output
  114. end