Skip to content

Commit

Permalink
Added analytics upload functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
etodd committed Jan 13, 2013
1 parent c3b2505 commit 68a7fc7
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -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
70 changes: 66 additions & 4 deletions Lemma/AnalyticsForm.cs
Expand Up @@ -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;
}

Expand All @@ -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";
}
}
}
10 changes: 10 additions & 0 deletions Lemma/AnalyticsForm.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Lemma/AnalyticsForm.resx
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="backgroundWorker1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
Expand Down
9 changes: 9 additions & 0 deletions Lemma/Components/Analytics.cs
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions Lemma/GameMain.cs
Expand Up @@ -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<Session> LoadAnalytics(string map)
Expand Down
2 changes: 1 addition & 1 deletion Lemma/Lemma.XNA.csproj
Expand Up @@ -49,7 +49,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\x86\Debug</OutputPath>
<DefineConstants>TRACE;DEBUG;WINDOWS;PERFORMANCE_MONITOR;DEVELOPMENT</DefineConstants>
<DefineConstants>TRACE;DEBUG;WINDOWS;PERFORMANCE_MONITOR;DEVELOPMENT;ANALYTICS</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoStdLib>true</NoStdLib>
Expand Down
2 changes: 1 addition & 1 deletion Lemma/Program.cs
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions LemmaAnalytics
Submodule LemmaAnalytics added at e2a81c

0 comments on commit 68a7fc7

Please sign in to comment.