Skip to content

Commit

Permalink
Add 20second delay before sending Loss to Warp.World to account for a…
Browse files Browse the repository at this point in the history
…ccidental exits.
  • Loading branch information
underscore-zi committed Aug 14, 2019
1 parent a7a9f37 commit 59b7bf2
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
72 changes: 71 additions & 1 deletion MarioMaker2OCR/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Reflection;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;



Expand All @@ -39,6 +40,7 @@ public string CurrentVersion

private FormPreview previewer = new FormPreview();
private FormWarpWorld warpworldForm = new FormWarpWorld();
private StatsTracker tracker = new StatsTracker();
private WarpWorldAPI WarpWorld;
private VideoProcessor processor;

Expand Down Expand Up @@ -142,6 +144,7 @@ private void startButton_Click(object sender, EventArgs e)
WarpWorld = null;
}
processor = new VideoProcessor(deviceComboBox.SelectedIndex, SelectedResolution);
//processor = new VideoProcessor(new VideoCapture("D:/2019-08-11 04-23-08.flv"));

processor.TemplateMatch += broadcastTemplateMatch;

Expand All @@ -159,7 +162,12 @@ private void startButton_Click(object sender, EventArgs e)
processor.ClearScreen += warpWorldCallback;
processor.Exit += warpWorldCallback;

processor.TemplateMatch += statsCallback;
processor.LevelScreen += statsCallback;
processor.ClearScreen += statsCallback;

processor.Start();
//processor.Start(true);
lockForm();
}
catch (Exception ex)
Expand Down Expand Up @@ -442,13 +450,75 @@ private void Processor_LevelScreen(object sender, VideoProcessor.LevelScreenEven
private void warpWorldCallback(object sender, VideoProcessor.TemplateMatchEventArgs e)
{
if (!Properties.Settings.Default.WarpWorldEnabled) return;
if (e.template.eventType == "exit") WarpWorld?.lose();
if (e.template.eventType != "exit") return;

var latestLevel = tracker.latest("level");

if (latestLevel == null)
{
log.Debug("[WarpWorld] Latest was null, no update");
return;
}

if (tracker.latest("clear") != null)
{
log.Debug("[WarpWorld] Level was already cleared, not sending loss.");
return;
}

Thread.Sleep(20000);

var currentLevel = tracker.latest("level");
if (currentLevel == null || currentLevel.time.Ticks == latestLevel.time.Ticks || currentLevel.data != latestLevel.data)
{
log.Debug("[WarpWorld] Loss");
WarpWorld?.lose();
} else
{
log.Debug("[WarpWorld] Rejoined level, not sending loss.");
}
}

private void warpWorldCallback(object sender, VideoProcessor.ClearScreenEventArgs e)
{
if (!Properties.Settings.Default.WarpWorldEnabled) return;
log.Debug("[WarpWorld] win");
WarpWorld?.win();
}

private void statsCallback(object sender, VideoProcessor.TemplateMatchEventArgs e)
{
tracker?.addEvent(e.template.eventType);

if(e.template.eventType == "exit")
{
// TODO: Send to endpoint or append to file?
}
}

private void statsCallback(object sender, VideoProcessor.ClearScreenEventArgs e)
{
var data = JsonConvert.SerializeObject( new
{
clearTime = e.clearTime,
firstClear = e.firstClear,
worldRecord = e.worldRecord,
});
tracker?.addEvent("clear", data);
}

private void statsCallback(object sender, VideoProcessor.LevelScreenEventArgs e)
{
var data = JsonConvert.SerializeObject(e.levelInfo);

var level = tracker.latest("level");
if (level == null || level.data != data)
{
log.Debug("New level, clearing stats history.");
tracker.clearHistory();
}

tracker?.addEvent("level", data);
}
}
}
2 changes: 2 additions & 0 deletions MarioMaker2OCR/MarioMaker2OCR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
</Compile>
<Compile Include="Objects\ClearEventWrapper.cs" />
<Compile Include="Objects\JsonSettings.cs" />
<Compile Include="StatsTracker.cs" />
<Compile Include="Test\BlackScreenTests.cs" />
<Compile Include="DirectShowLibrary.cs" />
<Compile Include="FormPreview.cs">
Expand All @@ -144,6 +145,7 @@
<Compile Include="SMMServer.cs" />
<Compile Include="Test\ClearScreenTests.cs" />
<Compile Include="Test\LevelScreenTests.cs" />
<Compile Include="Test\StatsTrackerTests.cs" />
<Compile Include="Test\TemplateTests.cs" />
<Compile Include="Test\WarpWorldAPITests.cs" />
<Compile Include="VideoProcessor.cs" />
Expand Down
2 changes: 1 addition & 1 deletion MarioMaker2OCR/Properties/Settings.Designer.cs

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

68 changes: 68 additions & 0 deletions MarioMaker2OCR/StatsTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MarioMaker2OCR.Objects;
using Newtonsoft.Json;

namespace MarioMaker2OCR
{
class StatsTracker
{
private List<StatsEvent> history = new List<StatsEvent>();

public void addEvent(string eventType)
{
addEvent(eventType, "");
}

public void addEvent(string eventType, string data)
{
var e = new StatsEvent
{
type = eventType,
time = DateTime.Now,
data = data,
};
history.Add(e);
}

public void clearHistory()
{
history.Clear();
}

public StatsEvent latest(string eventType)
{
return history.FindLast((StatsEvent e) =>
{
return e.type == eventType;
});
}

public int count(string eventType)
{
var results = history.FindAll((StatsEvent e) =>
{
return e.type == eventType;
});
return results.Count;
}
public int count()
{
return history.Count;
}

public string serialize()
{
return JsonConvert.SerializeObject(history);
}
}

class StatsEvent : DataEventWrapper
{
public DateTime time;

}
}
46 changes: 46 additions & 0 deletions MarioMaker2OCR/Test/StatsTrackerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarioMaker2OCR.Test
{
[TestClass]
public class StatsTrackerTests
{
[TestMethod]
public void CanAddEvents()
{
var tracker = new StatsTracker();

Assert.AreEqual(tracker.count(), 0);

tracker.addEvent("testEvent");
Assert.AreEqual(tracker.count(), 1);

tracker.addEvent("testEventWithData", "[test data]");
Assert.AreEqual(tracker.count(), 2);

for(int i=0;i<20;i++)
{
if(i%2==0)
{
tracker.addEvent("testEventWithData", String.Format("test{0}", i));
} else
{
tracker.addEvent("testEvent");
}
}

Assert.AreEqual(tracker.count(), 22);
Assert.AreEqual(tracker.count("testEvent"), 11);
Assert.AreEqual(tracker.count("testEventWithData"), 11);

var result = tracker.latest("testEventWithData");
Assert.IsNotNull(result);
Assert.AreEqual(result.data, "test18");
}
}
}
2 changes: 1 addition & 1 deletion MarioMaker2OCR/VideoProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private VideoCapture createCaptureDevice(int device, Size captureResolution)
private (Size resolution, int channels) getCaptureInfo(VideoCapture video)
{
Mat tmp = new Mat();
video.Retrieve(tmp);
video.Read(tmp);

if (tmp.Bitmap == null)
{
Expand Down

0 comments on commit 59b7bf2

Please sign in to comment.