Skip to content

Instantly share code, notes, and snippets.

@nkijak
Created November 4, 2015 02:40
Show Gist options
  • Save nkijak/fdcb4d8a75dff13ac58e to your computer and use it in GitHub Desktop.
Save nkijak/fdcb4d8a75dff13ac58e to your computer and use it in GitHub Desktop.
var events = [{"event": circuitStarted, value: { name: "A"}, when: <timestamp>}
{"event": exerciseComplete, value: {id: "exID 1", "count": 150}, when:<timestamp>}
{"event": circuitStarted, value: { name: "B"}, when: <timestamp>}
{"event": exerciseComplete, value: {id: "exID 2", "count": 20}, when:<timestamp>}
{"event": exerciseComplete, value: {id: "exID 3", "count": 6}, when:<timestamp>}
{"event": circuitComplete, value: { name: "B"}, when: <timestamp>}
{"event": circuitStarted, value: { name: "B"}, when: <timestamp>}
{"event": exerciseComplete, value: {id: "exID 2", "count": 18}, when:<timestamp>}
{"event": exerciseComplete, value: {id: "exID 3", "count": 5}, when:<timestamp>}
{"event": circuitComplete, value: { name: "B"}, when: <timestamp>}
{"event": circuitComplete, value: { name: "A"}, when: <timestamp>}
{"event": circuitStarted, value: { name: "A"}, when: <timestamp>}
{"event": exerciseComplete, value: {id: "exID 1", "count": 145}, when:<timestamp>}
{"event": circuitStarted, value: { name: "B"}, when: <timestamp>}
{"event": exerciseComplete, value: {id: "exID 2", "count": 18}, when:<timestamp>}
{"event": exerciseComplete, value: {id: "exID 3", "count": 5}, when:<timestamp>}
{"event": circuitComplete, value: { name: "B"}, when: <timestamp>}
{"event": circuitStarted, value: { name: "B"}, when: <timestamp>}
{"event": exerciseComplete, value: {id: "exID 2", "count": 17}, when:<timestamp>}
{"event": exerciseComplete, value: {id: "exID 3", "count": 4}, when:<timestamp>}
{"event": circuitComplete, value: { name: "B"}, when: <timestamp>}
{"event": circuitComplete, value: { name: "A"}, when: <timestamp>}]
function Report() {
}
Report.Replayer = function() {
var replayerSelf = this;
var _state = {totalscore: 0, curcuits: []};
this.circuitStarted = function(value) {
_state.curcuits.push({"name": value.name, "score":0, "curcuits":[]});
}
this.circuitComplete = function(value) {
var circuit = _state.curcuits.pop();
_state.curcuits.peek().curcuits.push(circuit);
}
this.exerciseComplete = function(value) {
_state.curcuits.peek().score += value.count;
_state.totalscore += value.count;
}
}
var replayer = new Report.Replayer()
for (var event in events) {
replayer[event.event](event.value);
}
logJson(replayer._state);
//{totalscore: 388, curcuits:[
// {name: A, score: 49, curcuits: [
// {name: B, score: 26, curcuits:[]}
// {name: B, score: 23, curcuits:[]}
// ]},
// {name: A, score: 44, curcuits: [
// {name: B, score: 23, curcuits:[]}
// {name: B, score: 21, curcuits:[]}
// ]}
//}
@nkijak
Copy link
Author

nkijak commented Nov 4, 2015

Then you can change this.exerciseComplete to record exercises too:

this.exerciseComplete = function(value) {
    _state.curcuits.peek().score += value.count;
    _state.curcuits.peek().exercises.push(value);
    _state.totalscore += value.count;
  }

Giving something like

//{totalscore: 388, curcuits:[
//  {name: A, score: 49, curcuits: [
//    {name: B, score: 26, curcuits:[], "exercises": [{id: "exID 2", "count": 20}, {id: "exId 3", "count": 6}]},
//    {name: B, score: 23, curcuits:[], "exercises": [{id: "exID 2", "count": 18}, {id: "exId 3", "count": 5}]}
//  ], "exercises": [{id: "exID 1", count: 150}]},
....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment