serverstatuswidget.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "serverstatuswidget.h"
  2. #include "ui_serverstatuswidget.h"
  3. #include <QDebug>
  4. #include "constants.h"
  5. #include "models/settings.h"
  6. ServerStatusWidget::ServerStatusWidget(QWidget *parent) :
  7. QWidget(parent),
  8. ui(new Ui::ServerStatusWidget)
  9. {
  10. servers_disabled = false;
  11. setMouseTracking(true);
  12. ui->setupUi(this);
  13. connect(&update_timer, &QTimer::timeout, &status_downloader, &Downloader::start);
  14. connect(&status_downloader, &Downloader::downloadFinished, this, &ServerStatusWidget::updateStatus);
  15. // updateStatus will start tech_work_message_downloader
  16. connect(&tech_work_message_downloader, &Downloader::downloadFinished, this, &ServerStatusWidget::updateTechWorksMessage);
  17. status_downloader.targetBytearray = &status_data;
  18. status_downloader.setUrl(Settings::getValue("Network/game_servers_status").toUrl());
  19. tech_work_message_downloader.targetBytearray = &tech_work_message_data;
  20. tech_work_message_downloader.setUrl(Settings::getValue("Network/game_servers_message").toUrl());
  21. update_timer.setInterval(1000 * 60); // 1 minute;
  22. update_timer.start();
  23. // Perform update on startup
  24. status_downloader.start();
  25. }
  26. ServerStatusWidget::~ServerStatusWidget()
  27. {
  28. delete ui;
  29. }
  30. void ServerStatusWidget::updateFontsSizes()
  31. {
  32. foreach (QString server_name, servers_list) {
  33. QLabel* label = ui->servers_list->findChild<QLabel*>(server_name.toLower());
  34. if (!label) {
  35. qDebug() << "Error! Cannot find server widget " << server_name;
  36. continue;
  37. }
  38. label->setFont(trajan_9pt);
  39. }
  40. }
  41. void ServerStatusWidget::resizeEvent(QResizeEvent*)
  42. {
  43. updateFontsSizes();
  44. }
  45. void ServerStatusWidget::leaveEvent(QEvent *event)
  46. {
  47. event->ignore();
  48. emit showNoTooltip();
  49. }
  50. void ServerStatusWidget::enterEvent(QEvent *event)
  51. {
  52. event->ignore();
  53. emit showServersTooltip(server_status_tooltip_);
  54. }
  55. void ServerStatusWidget::updateServerStatus()
  56. {
  57. status_downloader.start();
  58. }
  59. void ServerStatusWidget::updateStatus()
  60. {
  61. if (QString(status_data).contains("profilaktika")) {
  62. servers_disabled = true;
  63. qDebug() << "All servers are closed!";
  64. foreach (QString server_name, servers_list) {
  65. QLabel* label = ui->servers_list->findChild<QLabel*>(server_name.toLower());
  66. if (!label) {
  67. qDebug() << "Error! Cannot find server widget " << server_name;
  68. continue;
  69. }
  70. label->setStyleSheet(closed_color);
  71. }
  72. status_data = "";
  73. tech_work_message_downloader.start();
  74. return;
  75. }
  76. servers_disabled = false;
  77. QStringList servers = QString(status_data).split("|||");
  78. foreach (QString server, servers) {
  79. QStringList data = server.split(":::");
  80. QWidget* widget = findChild<QWidget*>(QString(data[0]).toLower());
  81. if (!widget)
  82. continue;
  83. if (data.size() > 1 && data[1] == "on")
  84. widget->setStyleSheet(open_color);
  85. else
  86. widget->setStyleSheet(closed_color);
  87. }
  88. status_data = "";
  89. tech_work_message_downloader.start();
  90. }
  91. void ServerStatusWidget::updateTechWorksMessage()
  92. {
  93. if (tech_work_message_data.isEmpty()) {
  94. if (!servers_disabled) {
  95. server_status_tooltip_ = "Серверы игры работают в штатном режиме.";
  96. } else {
  97. server_status_tooltip_ = "<p style =\"color:#ff0000\">Серверы игры отключены.</p>";
  98. }
  99. tech_work_message_data = "";
  100. return;
  101. }
  102. QStringList list = QString(tech_work_message_data).split(":::");
  103. if (list.size() < 4) {
  104. tech_work_message_data = "";
  105. return;
  106. }
  107. QString time_beginning = "<span style =\"font-family:Trajan Pro 3\">" + list[0] + "</span>";
  108. QString time_end = "<span style =\"font-family:Trajan Pro 3\">" + list[1] + "</span>";
  109. QString day = "<span style =\"font-family:Trajan Pro 3\">" + list[2] + "</span>";
  110. QString month = QString(" " + list[3]).toUpper();
  111. if (!servers_disabled) {
  112. server_status_tooltip_ = "<p style=\"color:#ffff7f\">Планируется отключение серверов игры " + day + month + " с " + time_beginning + " до " + time_end+ " (мск).</p>";
  113. } else {
  114. server_status_tooltip_ = "<p style=\"color:#ff0000\">Серверы игры отключены на профилактику<br>Время проф. работ: " + day + month + " с " + time_beginning + " до " + time_end + " (мск).</p>";
  115. }
  116. tech_work_message_data = "";
  117. }