From 68a7fc7a9091b8f1e465c76fe90dcc245faf2c27 Mon Sep 17 00:00:00 2001 From: Evan Todd Date: Sat, 12 Jan 2013 20:55:34 -0500 Subject: [PATCH] Added analytics upload functionality. --- .gitmodules | 3 ++ Lemma/AnalyticsForm.cs | 70 +++++++++++++++++++++++++++++++-- Lemma/AnalyticsForm.designer.cs | 10 +++++ Lemma/AnalyticsForm.resx | 3 ++ Lemma/Components/Analytics.cs | 9 +++++ Lemma/GameMain.cs | 8 ++++ Lemma/Lemma.XNA.csproj | 2 +- Lemma/Program.cs | 2 +- LemmaAnalytics | 1 + 9 files changed, 102 insertions(+), 6 deletions(-) create mode 160000 LemmaAnalytics diff --git a/.gitmodules b/.gitmodules index 3783a6ed..0d06796a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "Lemma/Content/Maps"] path = Lemma/Content/Maps url = https://et1337@bitbucket.org/et1337/lemma-game-assets.git +[submodule "LemmaAnalytics"] + path = LemmaAnalytics + url = https://et1337@bitbucket.org/et1337/lemmaanalytics.git diff --git a/Lemma/AnalyticsForm.cs b/Lemma/AnalyticsForm.cs index a5a56ff4..0553e2f4 100644 --- a/Lemma/AnalyticsForm.cs +++ b/Lemma/AnalyticsForm.cs @@ -7,15 +7,20 @@ using System.Text; using System.Windows.Forms; using System.Diagnostics; +using Lemma.Components; namespace Lemma { public partial class AnalyticsForm : Form { private string error; - public AnalyticsForm(string error) + private GameMain main; + private bool success; + private bool cancelled; + public AnalyticsForm(GameMain main, string error) { - InitializeComponent(); + this.InitializeComponent(); + this.main = main; this.error = error; } @@ -28,12 +33,69 @@ private void AnalyticsForm_Load(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e) { - // TODO: Analytics data upload + this.button1.Enabled = false; + this.button1.Text = "Uploading"; + this.button2.Text = "Cancel"; + this.label2.Text = "Uploading (0%)"; + this.backgroundWorker1.RunWorkerAsync(); } private void button2_Click(object sender, EventArgs e) { - this.Close(); + if (this.backgroundWorker1.IsBusy) + { + this.cancelled = true; + this.backgroundWorker1.CancelAsync(); + this.button2.Enabled = false; + this.button2.Text = "Canceling"; + } + else + this.Close(); + } + + private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) + { + BackgroundWorker worker = sender as BackgroundWorker; + + string[] sessionFiles = this.main.AnalyticsSessionFiles; + int i = 0; + try + { + foreach (string file in sessionFiles) + { + if (worker.CancellationPending == true) + { + e.Cancel = true; + break; + } + else + { +#if ANALYTICS // Just to prevent compile errors + Session.Recorder.UploadSession(file); +#endif + i++; + worker.ReportProgress((int)(((float)i / (float)sessionFiles.Length) * 100.0f)); + } + } + this.success = true; + } + catch (Exception) + { + + } + } + + private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) + { + this.label2.Text = "Uploading (" + e.ProgressPercentage.ToString() + "%"; + } + + private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + { + this.button2.Enabled = true; + this.label2.Text = this.cancelled ? "Upload cancelled." : (this.success ? "Upload complete. Thanks!" : "Upload failed. Please try again later."); + this.button1.Text = "Done"; + this.button2.Text = "Close"; } } } diff --git a/Lemma/AnalyticsForm.designer.cs b/Lemma/AnalyticsForm.designer.cs index 1d65456d..93c68578 100644 --- a/Lemma/AnalyticsForm.designer.cs +++ b/Lemma/AnalyticsForm.designer.cs @@ -37,6 +37,7 @@ private void InitializeComponent() this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); + this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); this.SuspendLayout(); // // label1 @@ -130,6 +131,14 @@ private void InitializeComponent() this.label7.TabIndex = 13; this.label7.Text = "- Crash logs"; // + // backgroundWorker1 + // + this.backgroundWorker1.WorkerReportsProgress = true; + this.backgroundWorker1.WorkerSupportsCancellation = true; + this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork); + this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged); + this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted); + // // AnalyticsForm // this.AcceptButton = this.button1; @@ -167,5 +176,6 @@ private void InitializeComponent() private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label7; + private System.ComponentModel.BackgroundWorker backgroundWorker1; } } \ No newline at end of file diff --git a/Lemma/AnalyticsForm.resx b/Lemma/AnalyticsForm.resx index 61bc649b..405801b3 100644 --- a/Lemma/AnalyticsForm.resx +++ b/Lemma/AnalyticsForm.resx @@ -117,6 +117,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + True diff --git a/Lemma/Components/Analytics.cs b/Lemma/Components/Analytics.cs index a444ce82..14c7b5a7 100644 --- a/Lemma/Components/Analytics.cs +++ b/Lemma/Components/Analytics.cs @@ -5,6 +5,8 @@ using Microsoft.Xna.Framework; using System.Xml.Serialization; using System.IO; +using System.Collections.Specialized; +using System.Net; namespace Lemma.Components { @@ -275,6 +277,13 @@ public static void Event(Main main, string name, string data = null) #endif } + public static void UploadSession(string file) + { + string url = "http://powerful-dusk-6047.herokuapp.com/" + Path.GetFileName(file); + new WebClient().UploadData(url, "PUT", File.ReadAllBytes(file)); + File.Delete(file); + } + public const float Interval = 0.25f; private float intervalTime = 0.0f; diff --git a/Lemma/GameMain.cs b/Lemma/GameMain.cs index 4916016c..b214643e 100644 --- a/Lemma/GameMain.cs +++ b/Lemma/GameMain.cs @@ -202,6 +202,14 @@ public void SaveAnalytics() { this.SessionRecorder.Save(Path.Combine(this.analyticsDirectory, this.MapFile.Value + "-" + Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 32) + ".xml")); } + + public string[] AnalyticsSessionFiles + { + get + { + return Directory.GetFiles(this.analyticsDirectory, "*", SearchOption.TopDirectoryOnly); + } + } #endif public List LoadAnalytics(string map) diff --git a/Lemma/Lemma.XNA.csproj b/Lemma/Lemma.XNA.csproj index a64fd6c5..13107eb4 100644 --- a/Lemma/Lemma.XNA.csproj +++ b/Lemma/Lemma.XNA.csproj @@ -49,7 +49,7 @@ full false bin\x86\Debug - TRACE;DEBUG;WINDOWS;PERFORMANCE_MONITOR;DEVELOPMENT + TRACE;DEBUG;WINDOWS;PERFORMANCE_MONITOR;DEVELOPMENT;ANALYTICS prompt 4 true diff --git a/Lemma/Program.cs b/Lemma/Program.cs index 05eda882..50035b18 100644 --- a/Lemma/Program.cs +++ b/Lemma/Program.cs @@ -54,7 +54,7 @@ public static void Main(string[] args) // TODO: MonoGame analytics form #else System.Windows.Forms.Application.EnableVisualStyles(); - AnalyticsForm analyticsForm = new AnalyticsForm(error); + AnalyticsForm analyticsForm = new AnalyticsForm(main, error); System.Windows.Forms.Application.Run(analyticsForm); #endif #else diff --git a/LemmaAnalytics b/LemmaAnalytics new file mode 160000 index 00000000..e2a81cb5 --- /dev/null +++ b/LemmaAnalytics @@ -0,0 +1 @@ +Subproject commit e2a81cb56952a49589e39261acccc02e77c96b42