Пример #1
0
 // Record key presses during the actual game.
 // Call player to dig when the dig key is pressed.
 void KeyEvent(Event e)
 {
     // When a keyboard key is initially pressed down, add it to list
     // We don't want to record when a key is HELD down
     if (e.type == EventType.KeyDown && !keysDown.Contains(e.keyCode))
     {
         keysDown.Add(e.keyCode);
         bpMetric.recordEvent(new ButtonPressingEvent(DateTime.Now, e.keyCode, true));
         if (e.keyCode == digKey)
         {
             player.DigDown();
             DigGround();
         }
         // Remove key from list
     }
     else if (e.type == EventType.KeyUp)
     {
         keysDown.Remove(e.keyCode);
         bpMetric.recordEvent(new ButtonPressingEvent(DateTime.Now, e.keyCode, false));
         if (e.keyCode == digKey)
         {
             player.DigUp();
         }
     }
 }
    public void TestRecordEvent()
    {
        // Test adding null event (state of bpm1 shouldn't change)
        ButtonPressingMetric bpm1 = new ButtonPressingMetric();

        bpm1.startRecording();
        Assert.IsEmpty(bpm1.eventList);
        Assert.IsTrue(bpm1.isRecording);

        bpm1.recordEvent(null);
        Assert.IsEmpty(bpm1.eventList);

        // Test adding events to a ButtonPressingMetric which has not started recording
        ButtonPressingMetric bpm2 = new ButtonPressingMetric();

        Assert.IsFalse(bpm2.isRecording);
        Assert.IsEmpty(bpm2.eventList);

        bpm2.recordEvent(new ButtonPressingEvent(new System.DateTime(2021, 2, 1), KeyCode.Joystick8Button4, true));
        Assert.IsEmpty(bpm2.eventList);

        // Test adding valid events to a ButtonPressingMetric which has started recording
        ButtonPressingMetric bpm3 = new ButtonPressingMetric();

        bpm3.startRecording();
        Assert.IsTrue(bpm3.isRecording);
        Assert.IsEmpty(bpm3.eventList);

        bpm3.recordEvent(new ButtonPressingEvent(new System.DateTime(2021, 2, 1), KeyCode.Joystick8Button4, true));
        bpm3.recordEvent(new ButtonPressingEvent(new System.DateTime(2021, 2, 2), KeyCode.Delete, false));
        bpm3.recordEvent(new ButtonPressingEvent(new System.DateTime(2021, 2, 3), KeyCode.E, false));

        Assert.AreEqual(3, bpm3.eventList.Count);
        Assert.AreEqual(new System.DateTime(2021, 2, 1), bpm3.eventList[0].eventTime);
        Assert.AreEqual(KeyCode.Joystick8Button4, bpm3.eventList[0].keyCode);
        Assert.IsTrue(bpm3.eventList[0].keyDown);

        Assert.AreEqual(new System.DateTime(2021, 2, 2), bpm3.eventList[1].eventTime);
        Assert.AreEqual(KeyCode.Delete, bpm3.eventList[1].keyCode);
        Assert.IsFalse(bpm3.eventList[1].keyDown);

        Assert.AreEqual(new System.DateTime(2021, 2, 3), bpm3.eventList[2].eventTime);
        Assert.AreEqual(KeyCode.E, bpm3.eventList[2].keyCode);
        Assert.IsFalse(bpm3.eventList[2].keyDown);
    }
    public void TestGetJSON()
    {
        // Test getJSON() when metric has no records
        ButtonPressingMetric bpm1 = new ButtonPressingMetric();

        Assert.IsEmpty(bpm1.eventList);

        JObject json1 = bpm1.getJSON();

        Assert.IsNotNull(json1);
        Assert.IsNotNull(json1["metricName"]);      // JSON entry "buttonPressing" should be empty, as nothing has been recorded
        Assert.IsEmpty(json1["eventList"]);

        // Test getJSON() with one record
        ButtonPressingMetric bpm2 = new ButtonPressingMetric();

        bpm2.startRecording();
        Assert.IsTrue(bpm2.isRecording);
        Assert.IsEmpty(bpm2.eventList);

        bpm2.recordEvent(new ButtonPressingEvent(new System.DateTime(2021, 2, 1), KeyCode.B, true));
        bpm2.finishRecording();

        JObject json2 = bpm2.getJSON();

        Assert.IsNotNull(json2);
        Assert.IsNotNull(json2["metricName"]);

        JArray eventList2 = (JArray)json2["eventList"];

        Assert.AreEqual("buttonPressing", json2["metricName"].ToString());
        Assert.AreEqual(1, eventList2.Count);
        Assert.AreEqual("2021-02-01 12:00:00 AM", eventList2[0]["eventTime"].ToString());
        Assert.AreEqual(KeyCode.B, eventList2[0]["keyCode"].ToObject <KeyCode>());
        Assert.AreEqual(true, eventList2[0]["keyDown"].ToObject <bool>());

        // Test getJSON() with multiple records
        ButtonPressingMetric bpm3 = new ButtonPressingMetric();

        bpm3.startRecording();
        Assert.IsTrue(bpm3.isRecording);
        Assert.IsEmpty(bpm3.eventList);

        bpm3.recordEvent(new ButtonPressingEvent(new System.DateTime(2021, 2, 7, 13, 12, 15), KeyCode.A, true));
        bpm3.recordEvent(new ButtonPressingEvent(new System.DateTime(2021, 2, 7, 13, 12, 16), KeyCode.A, false));
        bpm3.recordEvent(new ButtonPressingEvent(new System.DateTime(2022, 4, 1), KeyCode.LeftAlt, true));
        bpm3.finishRecording();
        bpm3.recordEvent(new ButtonPressingEvent(new System.DateTime(2023, 1, 3), KeyCode.M, false));    // record an event after finishing recording
        Assert.IsFalse(bpm3.isRecording);

        JObject json3 = bpm3.getJSON();

        Assert.IsNotNull(json3);
        Assert.IsNotNull(json3["metricName"]);

        JArray eventList3 = (JArray)json3["eventList"];

        Assert.AreEqual(3, eventList3.Count);

        Assert.AreEqual("2021-02-07 1:12:15 PM", eventList3[0]["eventTime"].ToString());
        Assert.AreEqual(KeyCode.A, eventList3[0]["keyCode"].ToObject <KeyCode>());
        Assert.AreEqual(true, eventList3[0]["keyDown"].ToObject <bool>());

        Assert.AreEqual("2021-02-07 1:12:16 PM", eventList3[1]["eventTime"].ToString());
        Assert.AreEqual(KeyCode.A, eventList3[1]["keyCode"].ToObject <KeyCode>());
        Assert.AreEqual(false, eventList3[1]["keyDown"].ToObject <bool>());

        Assert.AreEqual("2022-04-01 12:00:00 AM", eventList3[2]["eventTime"].ToString());
        Assert.AreEqual(KeyCode.LeftAlt, eventList3[2]["keyCode"].ToObject <KeyCode>());
        Assert.AreEqual(true, eventList3[2]["keyDown"].ToObject <bool>());
    }