Skip to content

Commit 79413ef

Browse files
authored
Merge pull request #8 from yetanalytics/statement-hook
Statement hook
2 parents 5095afd + ff0358b commit 79413ef

66 files changed

Lines changed: 448 additions & 857 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
1.24 KB
Binary file not shown.
8.23 KB
Binary file not shown.

Assemblies/System.Text.Json.dll

154 KB
Binary file not shown.

README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,83 @@ Please note that SendStartedStatement and SendCompletedStatement are both firing
178178

179179
In the OnApplicationQuit call, I showed an example of how to call the publisher with a custom verb. OnApplicationQuit is a hook that will fire when the user quits the application. If you go ahead and attempt to close out of the player you'll see it fire off stuff again.
180180

181-
There's also a way to make fully custom statements but I'm still trying to figure out the best way to make it clear on how to construct the statements from scratch and what gets included in them.
181+
182+
### Statement Hooks
183+
184+
Sometimes in your unity application you need to have access to a given statement for one reason or another. For this reason there's a way to hook into the statement sendoff from within the application. The following example shows how this is done.
185+
186+
```csharp
187+
using System;
188+
using UnityEngine;
189+
using LRS.Domain;
190+
using System.Text.Json.Nodes;
191+
192+
class Hook : MonoBehaviour
193+
{
194+
195+
// function that fires every time a statement is sent off.
196+
private void OnStatementSent(JsonObject statement)
197+
{
198+
Debug.Log(statement["verb"]["id"]);
199+
}
200+
201+
// Unity boilerplate for adding hooks to handlers
202+
private void OnEnable()
203+
{
204+
Publisher.OnStatementSent += OnStatementSent;
205+
}
206+
207+
// cleanup
208+
private void OnDisable()
209+
{
210+
Publisher.OnStatementSent -= OnStatementSent;
211+
}
212+
213+
}
214+
```
215+
216+
### Customize Statements
217+
218+
If you need to customize the shape of a statement at sendoff, this can be accomplished by overloading the `SendStatement` function with an argument that contains a higher order function that describes the changes you want to make. The following code shows how this is done.
219+
220+
```csharp
221+
using System;
222+
using UnityEngine;
223+
using LRS.Domain;
224+
using System.Text.Json.Nodes;
225+
226+
class CustomizeStatements : MonoBehaviour
227+
{
228+
// LRS Credentials
229+
public string lrsUrl;
230+
public string lrsKey;
231+
public string lrsSecret;
232+
233+
// define the publisher
234+
private Publisher publisher{get {return new Publisher(lrsUrl,lrsKey,lrsSecret);}}
235+
236+
void Start() {
237+
// define function that modifies statement
238+
Func <JsonObject, JsonObject> modifyFn = (statement) =>
239+
{
240+
// modify the statement
241+
statement["object"]["definition"]["extensions"]["https://video.games/publisher"] = new JsonObject{ ["name"] = "ACME Games Corp." };
242+
243+
// make sure to return for the callback
244+
return statement;
245+
};
246+
247+
// pass the function to the sendoff
248+
publisher.SendStatement("http://video.games/verbs/start", "Start",
249+
"http://video.games/clicker/level/1", "Level 1 of clicking game",
250+
modifyFn);
251+
}
252+
}
253+
```
254+
255+
### For anyone transitioning from the 1.0.x build to 1.1.x
256+
257+
Remove any references to the XAPI namespace by deleting the following line: `using XAPI`. this namespace no longer exists.
182258

183259

184260
### Technical mumbo jumbo

Runtime/LRS/Domain/Publisher.cs

Lines changed: 168 additions & 83 deletions
Large diffs are not rendered by default.

Runtime/XAPI/Activity.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

Runtime/XAPI/Activity.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Runtime/XAPI/ActivityDefinition.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

Runtime/XAPI/ActivityDefinition.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Runtime/XAPI/Agent.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)