From 0b6f9e4af8f1ab695a193fabdb32b6219223c455 Mon Sep 17 00:00:00 2001
From: daniel-Jones <daniel@danieljon.es>
Date: Fri, 22 Jul 2016 11:43:37 +0930
Subject: Added parsing functions for both steam and csgo data, added testing
 displays on the UI etc

---
 csgo_stats.pro           |  6 ++++--
 parse_file.cpp           | 49 ++++++++++++++++++++++++++++++++++++++++++
 parse_file.h             | 15 +++++++++++++
 stats_display_window.cpp | 55 ++++++++++++++++++++++++++++++++----------------
 stats_display_window.h   | 12 +++++------
 stats_display_window.ui  | 28 ++++++++++++++++++------
 6 files changed, 132 insertions(+), 33 deletions(-)
 create mode 100644 parse_file.cpp
 create mode 100644 parse_file.h

diff --git a/csgo_stats.pro b/csgo_stats.pro
index ef1f61c..647e441 100644
--- a/csgo_stats.pro
+++ b/csgo_stats.pro
@@ -15,11 +15,13 @@ TEMPLATE = app
 SOURCES += main.cpp\
         user_select_dialog.cpp \
     file_download.cpp \
-    stats_display_window.cpp
+    stats_display_window.cpp \
+    parse_file.cpp
 
 HEADERS  += user_select_dialog.h \
     file_download.h \
-    stats_display_window.h
+    stats_display_window.h \
+    parse_file.h
 
 FORMS    += user_select_dialog.ui \
     stats_display_window.ui
diff --git a/parse_file.cpp b/parse_file.cpp
new file mode 100644
index 0000000..b939a7d
--- /dev/null
+++ b/parse_file.cpp
@@ -0,0 +1,49 @@
+#include "parse_file.h"
+
+parse_file::parse_file()
+{
+
+}
+
+QString parse_file::parse_user_data(QString option, QString split)
+{
+    /*
+     * this function parses the userdata.dat file for info on the users steam account
+     */
+    QFile file("userdata.dat");
+    if (file.open(QIODevice::ReadOnly))
+    {
+       QTextStream in(&file);
+       while (!in.atEnd())
+       {
+          QString line = in.readLine();
+          if (line.contains(option))
+              return line.split(split)[1].replace("\"", "").replace(",", "");
+       }
+       file.close();
+    }
+    return "null";
+}
+
+QString parse_file::parse_csgo_data(QString option)
+{
+    /*
+     * this function parses csgodata.dat for stats
+     */
+    QFile file("csgodata.dat");
+    if (file.open(QIODevice::ReadOnly))
+    {
+       QTextStream in(&file);
+       while (!in.atEnd())
+       {
+          QString line = in.readLine();
+          if (line.contains(option))
+          {
+              line = in.readLine();
+              return line.split(": ")[1];
+          }
+       }
+       file.close();
+    }
+    return "null";
+}
diff --git a/parse_file.h b/parse_file.h
new file mode 100644
index 0000000..3ee8b0d
--- /dev/null
+++ b/parse_file.h
@@ -0,0 +1,15 @@
+#ifndef PARSE_FILE_H
+#define PARSE_FILE_H
+#include <QFile>
+#include <QDebug>
+#include <QTextStream>
+
+class parse_file
+{
+public:
+    parse_file();
+    QString parse_user_data(QString option, QString split);
+    QString parse_csgo_data(QString option);
+};
+
+#endif // PARSE_FILE_H
diff --git a/stats_display_window.cpp b/stats_display_window.cpp
index 1dee284..24bbbec 100644
--- a/stats_display_window.cpp
+++ b/stats_display_window.cpp
@@ -6,7 +6,7 @@ stats_display_window::stats_display_window(QWidget *parent) :
     ui(new Ui::stats_display_window)
 {
     ui->setupUi(this);
-    connect(this, SIGNAL(window_loaded()), this, SLOT(test()), Qt::ConnectionType(Qt::QueuedConnection | Qt::UniqueConnection));
+    connect(this, SIGNAL(window_loaded()), this, SLOT(window_open()), Qt::ConnectionType(Qt::QueuedConnection | Qt::UniqueConnection));
 }
 
 stats_display_window::~stats_display_window()
@@ -19,25 +19,18 @@ void stats_display_window::showEvent(QShowEvent *ev)
     emit window_loaded();
 }
 
-void stats_display_window::test()
+void stats_display_window::window_open()
 {
-    QFile file("userdata.dat");
-    if (file.open(QIODevice::ReadOnly))
-    {
-       QTextStream in(&file);
-       while (!in.atEnd())
-       {
-          QString line = in.readLine();
-          if (line.contains("\"personaname\":"))
-              user.username = line.split(": ")[1].replace("\"", "").replace(",", "");
-          if (line.contains("\"realname\":"))
-              user.realname = line.split(": ")[1].replace("\"", "").replace(",", "");
-          if (line.contains("\"avatarfull\":"))
-              user.avatar_url = line.split(": ")[1].replace("\"", "").replace(",", "");
-       }
-       file.close();
-    }
+    /*
+     * this slot is called when our stats window is opened
+     * we will parse our steam account file and edit the UI accordingly
+     */
+    user.username = parse.parse_user_data("\"personaname\":", ": ");
+    user.realname = parse.parse_user_data("\"realname\":", ": ");
+    user.avatar_url = parse.parse_user_data("\"avatarfull\":", ": ");
+    user.status = parse.parse_user_data("\"personastate\":", ": ");
     download.download_file(user.avatar_url, "avatar.jpg");
+    /* now we have to deal with our user account details */
     ui->username_label->setText(user.username);
     if (user.realname == NULL)
         ui->realname_label->setText("Realname not set");
@@ -46,4 +39,30 @@ void stats_display_window::test()
     QPixmap image("avatar.jpg");
     ui->avatar_label->setPixmap(image);
 
+    /* user online status */
+    switch(user.status.toInt())
+    {
+        case 0:
+            ui->status_label->setText("<font color=red>Offline</font>");
+        break;
+        case 1:
+            ui->status_label->setText("<font color=green>Online</font>");
+        break;
+        case 2:
+            ui->status_label->setText("<font color=yellow>Busy</font>");
+        break;
+        case 3:
+            ui->status_label->setText("<font color=orange>Away</font>");
+        break;
+        case 4:
+            ui->status_label->setText("<font color=black>Snooze</font>");
+        break;
+        default:
+            ui->status_label->setText("Unknown status");
+        break;
+    }
+    user.time_ingame = parse.parse_csgo_data("total_time_played");
+    QString time_in_game = "Hours in CS:GO: " + QString::number(user.time_ingame.toInt() / 3600);
+    ui->time_in_label->setText(time_in_game);
+    qDebug() << user.time_ingame.toInt();
 }
diff --git a/stats_display_window.h b/stats_display_window.h
index 887c550..d73f6ca 100644
--- a/stats_display_window.h
+++ b/stats_display_window.h
@@ -3,11 +3,8 @@
 
 #include <QWidget>
 #include <QDebug>
-#include <QByteArray>
-#include <QFile>
-#include <QJsonObject>
-#include <QJsonDocument>
 #include "file_download.h"
+#include "parse_file.h"
 
 namespace Ui {
 class stats_display_window;
@@ -28,18 +25,21 @@ public:
 private:
     Ui::stats_display_window *ui;
 
-    struct user_info {
+    struct user_info { /* contains our user data for both steam and csgo */
         QString username;
         QString realname;
         QString avatar_url;
+        QString status;
+        QString time_ingame;
     } user;
 
     file_download download;
+    parse_file parse;
 signals:
       void window_loaded();
 
 private slots:
-    void test();
+    void window_open();
 };
 
 #endif // STATS_DISPLAY_WINDOW_H
diff --git a/stats_display_window.ui b/stats_display_window.ui
index bf6d27e..ca16129 100644
--- a/stats_display_window.ui
+++ b/stats_display_window.ui
@@ -9,15 +9,22 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>740</width>
-    <height>479</height>
+    <width>104</width>
+    <height>112</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string>CS:GO Statistics</string>
   </property>
-  <layout class="QFormLayout" name="formLayout">
-   <item row="0" column="0" colspan="2">
+  <layout class="QGridLayout" name="gridLayout_2">
+   <item row="3" column="0">
+    <widget class="QLabel" name="status_label">
+     <property name="text">
+      <string>status</string>
+     </property>
+    </widget>
+   </item>
+   <item row="1" column="0">
     <widget class="QLabel" name="username_label">
      <property name="font">
       <font>
@@ -32,6 +39,13 @@
      </property>
     </widget>
    </item>
+   <item row="0" column="0">
+    <widget class="QLabel" name="avatar_label">
+     <property name="text">
+      <string>avatar</string>
+     </property>
+    </widget>
+   </item>
    <item row="2" column="0">
     <widget class="QLabel" name="realname_label">
      <property name="text">
@@ -39,10 +53,10 @@
      </property>
     </widget>
    </item>
-   <item row="3" column="0">
-    <widget class="QLabel" name="avatar_label">
+   <item row="4" column="0">
+    <widget class="QLabel" name="time_in_label">
      <property name="text">
-      <string>avatar</string>
+      <string>Time in CS:GO</string>
      </property>
     </widget>
    </item>
-- 
cgit v1.2.3