@media-quest/engine 0.0.35 → 0.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,91 +1,92 @@
1
- import { DPlayer, DPlayerData } from "./dplayer";
2
- import { PageSequenceDto } from "./SchemaDto";
3
- import { Rule } from "../rules/rule";
4
- import { DTimestamp } from "../common/DTimestamp";
5
- import { RuleActionPageQue } from "./page-que-ruleengine-action";
6
- import { PageDto } from "../page/Page";
7
- import { PageResult } from "../page/page-result";
8
-
9
- const page = (id: number): PageDto => {
10
- return PageDto.createDummy(id);
11
- // return { id: PageID.create(), elements: [] };
12
- };
13
- const p1 = page(1);
14
- const p2 = page(2);
15
- const p3 = page(3);
16
- const p4 = page(4);
17
- const p5 = page(5);
18
- const p6 = page(6);
19
- const all = [p1, p2, p3, p4, p5, p6];
20
- const Seq1 = {
21
- p10: page(10),
22
- p11: page(11),
23
- p12: page(12),
24
- };
25
-
26
- const seq1: PageSequenceDto = {
27
- id: "s1",
28
- pages: [Seq1.p10, Seq1.p11, Seq1.p12],
29
- rules: [],
30
- };
31
- const data = (
32
- pages: PageDto[],
33
- pageSequences: PageSequenceDto[] = [],
34
- rules: Rule<RuleActionPageQue, never>[] = [],
35
- ): DPlayerData => ({ pages: pages, pageSequences, rules });
36
- // const seq = (pages: PageDto[], rules: Rule[]) => {};
37
- beforeEach(() => {
38
- // data = { pages: [], rules: [], pageSequences: [] };
39
- });
40
-
41
- describe("DPlayer", () => {
42
- it("should create an instance", () => {
43
- const player = new DPlayer(data([p1, p1]));
44
- expect(player).toBeTruthy();
45
- });
46
-
47
- it("can app pages, and get next", () => {
48
- const player = new DPlayer(data([p1, p2]));
49
-
50
- expect(player.getNextPage()).toBe(p1);
51
- expect(player.getNextPage()).toBe(p2);
52
- expect(player.getNextPage()).toBe(false);
53
- });
54
-
55
- it("Can insert a sequence", () => {
56
- const player = new DPlayer(data(all, [seq1]));
57
-
58
- expect(player.getNextPage()).toBe(p1);
59
- expect(player.getNextPage()).toBe(p2);
60
- player.insertSequence(seq1.id);
61
- expect(player.getNextPage()).toBe(Seq1.p10);
62
- expect(player.getNextPage()).toBe(Seq1.p11);
63
- expect(player.getNextPage()).toBe(Seq1.p12);
64
- expect(player.getNextPage()).toBe(p3);
65
- });
66
-
67
- it("Save history", () => {
68
- const player = new DPlayer(data(all));
69
-
70
- const curr = player.getNextPage() as PageDto;
71
-
72
- const result: PageResult = {
73
- pageId: curr.id,
74
- pagePrefix: curr.id,
75
- eventLog: [],
76
- pageTime: DTimestamp.diff(DTimestamp.now(), DTimestamp.now()),
77
- collectedFacts: [
78
- {
79
- referenceId: "as",
80
- referenceLabel: "as-label",
81
- kind: "numeric-fact",
82
- label: "litt",
83
- value: 2,
84
- },
85
- ],
86
- };
87
- player.saveHistory(result);
88
- expect(player.getNextPage()).toBe(p2);
89
- expect(player.getResults().answerFacts.length).toBe(1);
90
- });
91
- });
1
+ import { DPlayer, DPlayerData } from "./dplayer";
2
+ import { PageSequenceDto } from "./SchemaDto";
3
+ import { Rule } from "../rules/rule";
4
+ import { DTimestamp } from "../common/DTimestamp";
5
+ import { RuleActionPageQue } from "./page-que-ruleengine-action";
6
+ import { PageDto } from "../page/Page";
7
+ import { PageResult } from "../page/page-result";
8
+
9
+ const page = (id: number): PageDto => {
10
+ return PageDto.createDummy(id);
11
+ // return { id: PageID.create(), elements: [] };
12
+ };
13
+ const p1 = page(1);
14
+ const p2 = page(2);
15
+ const p3 = page(3);
16
+ const p4 = page(4);
17
+ const p5 = page(5);
18
+ const p6 = page(6);
19
+ const all = [p1, p2, p3, p4, p5, p6];
20
+ const Seq1 = {
21
+ p10: page(10),
22
+ p11: page(11),
23
+ p12: page(12),
24
+ };
25
+
26
+ const seq1: PageSequenceDto = {
27
+ id: "s1",
28
+ pages: [Seq1.p10, Seq1.p11, Seq1.p12],
29
+ rules: [],
30
+ };
31
+ const data = (
32
+ pages: PageDto[],
33
+ pageSequences: PageSequenceDto[] = [],
34
+ rules: Rule<RuleActionPageQue, never>[] = [],
35
+ ): DPlayerData => ({ pages: pages, pageSequences, rules });
36
+ // const seq = (pages: PageDto[], rules: Rule[]) => {};
37
+ beforeEach(() => {
38
+ // data = { pages: [], rules: [], pageSequences: [] };
39
+ });
40
+
41
+ describe("DPlayer", () => {
42
+ it("should create an instance", () => {
43
+ const player = new DPlayer(data([p1, p1]));
44
+ expect(player).toBeTruthy();
45
+ });
46
+
47
+ it("can app pages, and get next", () => {
48
+ const player = new DPlayer(data([p1, p2]));
49
+
50
+ expect(player.getNextPage()).toBe(p1);
51
+ expect(player.getNextPage()).toBe(p2);
52
+ expect(player.getNextPage()).toBe(false);
53
+ });
54
+
55
+ it("Can insert a sequence", () => {
56
+ const player = new DPlayer(data(all, [seq1]));
57
+
58
+ expect(player.getNextPage()).toBe(p1);
59
+ expect(player.getNextPage()).toBe(p2);
60
+ player.insertSequence(seq1.id);
61
+ expect(player.getNextPage()).toBe(Seq1.p10);
62
+ expect(player.getNextPage()).toBe(Seq1.p11);
63
+ expect(player.getNextPage()).toBe(Seq1.p12);
64
+ expect(player.getNextPage()).toBe(p3);
65
+ });
66
+
67
+ it("Save history", () => {
68
+ const player = new DPlayer(data(all));
69
+
70
+ const curr = player.getNextPage() as PageDto;
71
+
72
+ const result: PageResult = {
73
+ pageId: curr.id,
74
+ pagePrefix: curr.id,
75
+ pageNumber: 1,
76
+ eventLog: [],
77
+ pageTime: DTimestamp.diff(DTimestamp.now(), DTimestamp.now()),
78
+ collectedFacts: [
79
+ {
80
+ referenceId: "as",
81
+ referenceLabel: "as-label",
82
+ kind: "numeric-fact",
83
+ label: "litt",
84
+ value: 2,
85
+ },
86
+ ],
87
+ };
88
+ player.saveHistory(result);
89
+ expect(player.getNextPage()).toBe(p2);
90
+ expect(player.getResults().answerFacts.length).toBe(1);
91
+ });
92
+ });
@@ -28,10 +28,10 @@ export class DPlayer {
28
28
  this.eventLog.push(event);
29
29
  }
30
30
 
31
- saveHistory(pageHistory: PageResult) {
31
+ saveHistory(pageResult: PageResult) {
32
32
  // console.log("SAVE HISTORY", pageHistory);
33
- this.history.addToHistory(pageHistory);
34
- this.eventLog.push(...pageHistory.eventLog);
33
+ this.history.addToHistory(pageResult);
34
+ this.eventLog.push(...pageResult.eventLog);
35
35
  // Evaluate rules
36
36
  const userGeneratedFact = this.history.getFacts();
37
37
  const predefinedFacts = this.predefinedFacts;
@@ -65,8 +65,10 @@ export class DPlayer {
65
65
  const answerFacts = this.history.getFacts();
66
66
  const predefinedFacts = this.predefinedFacts;
67
67
  const eventLog = [...this.eventLog];
68
+ const pageResults = this.history.getPageResults();
69
+ // const pageResults = this.history.;
68
70
  // console.log(answerFacts);
69
- return { answerFacts, predefinedFacts, eventLog, pagesLeft };
71
+ return { answerFacts, predefinedFacts, eventLog, pagesLeft, pageResults };
70
72
  }
71
73
 
72
74
  insertSequence(sequenceId: string) {
@@ -8,6 +8,7 @@ const p = (id: number): PageDto => {
8
8
  return {
9
9
  id: DUtil.randomObjectId(),
10
10
  prefix: "prefix" + id,
11
+ pageNumber: id,
11
12
  elements: [],
12
13
  initialTasks: [],
13
14
  // staticElements: [],
@@ -16,7 +17,7 @@ const p = (id: number): PageDto => {
16
17
  };
17
18
  };
18
19
 
19
- const pageResult = (id: number, value: number): PageResult => {
20
+ const pageResult = (id: number, pageNumber: number, value: number): PageResult => {
20
21
  const pageEntered = DTimestamp.now();
21
22
  const pageExited = DTimestamp.addMills(pageEntered, 1000);
22
23
  const pageTime = DTimestamp.diff(pageEntered, pageExited);
@@ -24,6 +25,7 @@ const pageResult = (id: number, value: number): PageResult => {
24
25
  const result: PageResult = {
25
26
  pageId: "_dummyId" + id,
26
27
  pagePrefix: "prefix" + id,
28
+ pageNumber,
27
29
  eventLog: [],
28
30
  pageTime,
29
31
  collectedFacts: [
@@ -61,7 +63,7 @@ describe("HistoryQue", () => {
61
63
  });
62
64
 
63
65
  it("Can add history, and get facts back", () => {
64
- history.addToHistory(pageResult(1, 2));
66
+ history.addToHistory(pageResult(1, 0, 2));
65
67
  expect(history.getFacts().length).toBe(1);
66
68
  });
67
69
  });
@@ -1,17 +1,21 @@
1
- import { Fact } from "../rules/fact";
2
- import { PageResult } from "../page/page-result";
3
-
4
- export class HistoryQue {
5
- private history: PageResult[] = [];
6
-
7
- getFacts(): Array<Fact> {
8
- const answers = this.history.map((h) => h.collectedFacts).flat(1);
9
- // const facts = answers.map((a) => a.fact);
10
- // TODO FIND LATEST FACT (answer) if have multiple.
11
- return answers;
12
- }
13
-
14
- addToHistory(result: PageResult) {
15
- this.history.push(result);
16
- }
17
- }
1
+ import { Fact } from "../rules/fact";
2
+ import { PageResult } from "../page/page-result";
3
+
4
+ export class HistoryQue {
5
+ private history: PageResult[] = [];
6
+
7
+ getFacts(): Array<Fact> {
8
+ const answers = this.history.map((h) => h.collectedFacts).flat(1);
9
+ // const facts = answers.map((a) => a.fact);
10
+ // TODO FIND LATEST FACT (answer) if have multiple.
11
+ return answers;
12
+ }
13
+
14
+ getPageResults(): PageResult[] {
15
+ return [...this.history];
16
+ }
17
+
18
+ addToHistory(result: PageResult) {
19
+ this.history.push(result);
20
+ }
21
+ }
@@ -10,6 +10,7 @@ let que = new NextQue();
10
10
  const createPage = (tags: string[]): PageDto => {
11
11
  const dto: PageDto = {
12
12
  id: DUtil.randomObjectId(),
13
+ pageNumber: 1,
13
14
  prefix: "prefix",
14
15
  elements: [],
15
16
  background: "white",
@@ -1,101 +1,101 @@
1
- import { PageDto } from "../page/Page";
2
-
3
- export class NextQue {
4
- private originalOrder: ReadonlyArray<string> = [];
5
- private allPages: PageDto[] = [];
6
- private excludedTags = new Set<string>();
7
- private excludedByPageId = new Set<string>();
8
- private remaining: PageDto[] = [];
9
- constructor(pages: PageDto[] = []) {
10
- this.resetQue(pages);
11
- }
12
-
13
- /**
14
- * Will reset que with the new pages.
15
- * @param pages
16
- */
17
- resetQue(pages: PageDto[]) {
18
- this.allPages = [...pages];
19
- this.remaining = [...pages];
20
- this.excludedTags = new Set();
21
- this.excludedByPageId = new Set();
22
- this.originalOrder = this.allPages.map((p) => p.id);
23
- }
24
-
25
- pop(): PageDto | false {
26
- const next = this.remaining.shift();
27
- // TODO CLONE??
28
- return next ?? false;
29
- }
30
-
31
- peek(): PageDto | false {
32
- const next = this.remaining[0];
33
- return next ?? false;
34
- }
35
-
36
- jumpToPageById(pageId: string): boolean {
37
- const index = this.remaining.findIndex((p) => p.id === pageId);
38
- if (index < 0) {
39
- return false;
40
- }
41
- this.remaining = this.remaining.slice(index);
42
- return true;
43
- }
44
-
45
- removeByTag(tag: string | string[]) {
46
- if (Array.isArray(tag)) {
47
- tag.forEach((tag) => {
48
- this.excludedTags.add(tag);
49
- });
50
- } else {
51
- this.excludedTags.add(tag);
52
- }
53
- this.filterRemaining();
54
- }
55
-
56
- /**
57
- * Will not be included
58
- * @param pages
59
- */
60
- insertAsNextByForce(pages: PageDto[]) {
61
- this.remaining.unshift(...pages);
62
- }
63
-
64
- removeByPageId(pageId: string | string[]) {
65
- if (Array.isArray(pageId)) {
66
- pageId.forEach((id) => {
67
- this.excludedByPageId.add(id);
68
- });
69
- } else {
70
- this.excludedByPageId.add(pageId);
71
- }
72
- this.filterRemaining();
73
- // this.excludedByPageId.add(pageId);
74
- }
75
-
76
- private filterRemaining() {
77
- this.remaining = this.remaining.filter((p) => {
78
- const tags = p.tags ?? [];
79
- const isIncluededByTag = !tags.some((tag) => this.excludedTags.has(tag));
80
- const isIncludedByPageId = !this.excludedByPageId.has(p.id);
81
- return isIncludedByPageId && isIncluededByTag;
82
- });
83
- }
84
- get isEmpty(): boolean {
85
- return this.remaining.length === 0;
86
- }
87
-
88
- /**
89
- * Total number of pages left in que
90
- */
91
- get size(): number {
92
- return this.remaining.length;
93
- }
94
-
95
- /**
96
- * Total number of pages in test
97
- */
98
- get pageCount(): number {
99
- return this.originalOrder.length;
100
- }
101
- }
1
+ import { PageDto } from "../page/Page";
2
+
3
+ export class NextQue {
4
+ private originalOrder: ReadonlyArray<string> = [];
5
+ private allPages: PageDto[] = [];
6
+ private excludedTags = new Set<string>();
7
+ private excludedByPageId = new Set<string>();
8
+ private remaining: PageDto[] = [];
9
+ constructor(pages: PageDto[] = []) {
10
+ this.resetQue(pages);
11
+ }
12
+
13
+ /**
14
+ * Will reset que with the new pages.
15
+ * @param pages
16
+ */
17
+ resetQue(pages: PageDto[]) {
18
+ this.allPages = [...pages];
19
+ this.remaining = [...pages];
20
+ this.excludedTags = new Set();
21
+ this.excludedByPageId = new Set();
22
+ this.originalOrder = this.allPages.map((p) => p.id);
23
+ }
24
+
25
+ pop(): PageDto | false {
26
+ const next = this.remaining.shift();
27
+ // TODO CLONE??
28
+ return next ?? false;
29
+ }
30
+
31
+ peek(): PageDto | false {
32
+ const next = this.remaining[0];
33
+ return next ?? false;
34
+ }
35
+
36
+ jumpToPageById(pageId: string): boolean {
37
+ const index = this.remaining.findIndex((p) => p.id === pageId);
38
+ if (index < 0) {
39
+ return false;
40
+ }
41
+ this.remaining = this.remaining.slice(index);
42
+ return true;
43
+ }
44
+
45
+ removeByTag(tag: string | string[]) {
46
+ if (Array.isArray(tag)) {
47
+ tag.forEach((tag) => {
48
+ this.excludedTags.add(tag);
49
+ });
50
+ } else {
51
+ this.excludedTags.add(tag);
52
+ }
53
+ this.filterRemaining();
54
+ }
55
+
56
+ /**
57
+ * Will not be included
58
+ * @param pages
59
+ */
60
+ insertAsNextByForce(pages: PageDto[]) {
61
+ this.remaining.unshift(...pages);
62
+ }
63
+
64
+ removeByPageId(pageId: string | string[]) {
65
+ if (Array.isArray(pageId)) {
66
+ pageId.forEach((id) => {
67
+ this.excludedByPageId.add(id);
68
+ });
69
+ } else {
70
+ this.excludedByPageId.add(pageId);
71
+ }
72
+ this.filterRemaining();
73
+ // this.excludedByPageId.add(pageId);
74
+ }
75
+
76
+ private filterRemaining() {
77
+ this.remaining = this.remaining.filter((p) => {
78
+ const tags = p.tags ?? [];
79
+ const isIncluededByTag = !tags.some((tag) => this.excludedTags.has(tag));
80
+ const isIncludedByPageId = !this.excludedByPageId.has(p.id);
81
+ return isIncludedByPageId && isIncluededByTag;
82
+ });
83
+ }
84
+ get isEmpty(): boolean {
85
+ return this.remaining.length === 0;
86
+ }
87
+
88
+ /**
89
+ * Total number of pages left in que
90
+ */
91
+ get size(): number {
92
+ return this.remaining.length;
93
+ }
94
+
95
+ /**
96
+ * Total number of pages in test
97
+ */
98
+ get pageCount(): number {
99
+ return this.originalOrder.length;
100
+ }
101
+ }
@@ -1,63 +1,76 @@
1
- import { DTimestamp } from "../common/DTimestamp";
2
-
3
- interface _MqEvent<K extends string, P extends object> {
4
- readonly kind: K;
5
- readonly timestamp: DTimestamp;
6
- readonly payload: P;
7
- }
8
-
9
- export type MqEventEngineStart = _MqEvent<
10
- "engine-start",
11
- {
12
- readonly schemaId: string;
13
- readonly schemaPrefix: string;
14
- }
15
- >;
16
- export interface MqEventPageEnter
17
- extends _MqEvent<"page-enter", { readonly pageId: string; readonly pagePrefix: string }> {}
18
- export interface MqEventPageLeave
19
- extends _MqEvent<"page-leave", { readonly pageId: string; readonly pagePrefix: string }> {}
20
-
21
- export type MqEventUserClicked = _MqEvent<
22
- "user-clicked",
23
- { readonly pageId: string; pagePrefix: string; action: string; descriptions: string }
24
- >;
25
-
26
- export type MqEvent = MqEventPageEnter | MqEventPageLeave | MqEventEngineStart | MqEventUserClicked;
27
-
28
- export const MqEvent = {
29
- engineStart(schemaId: string, schemaPrefix: string): MqEventEngineStart {
30
- return {
31
- kind: "engine-start",
32
- timestamp: DTimestamp.now(),
33
- payload: { schemaId, schemaPrefix },
34
- };
35
- },
36
- pageEnter(pageId: string, pagePrefix: string): MqEventPageEnter {
37
- return {
38
- kind: "page-enter",
39
- timestamp: DTimestamp.now(),
40
- payload: { pageId, pagePrefix },
41
- };
42
- },
43
- pageLeave(pageId: string, pagePrefix: string): MqEventPageLeave {
44
- return {
45
- kind: "page-leave",
46
- timestamp: DTimestamp.now(),
47
- payload: { pageId, pagePrefix },
48
- };
49
- },
50
- userClicked(data: {
51
- pageId: string;
52
- pagePrefix: string;
53
- action: string;
54
- descriptions: string;
55
- }): MqEventUserClicked {
56
- const { pageId, pagePrefix, action, descriptions } = data;
57
- return {
58
- kind: "user-clicked",
59
- timestamp: DTimestamp.now(),
60
- payload: { pageId, pagePrefix, action, descriptions },
61
- };
62
- },
63
- } as const;
1
+ import { DTimestamp } from "../common/DTimestamp";
2
+ interface _MqEvent<K extends string, P extends object> {
3
+ readonly kind: K;
4
+ readonly timestamp: DTimestamp;
5
+ readonly payload: P;
6
+ }
7
+
8
+ export type MqEventEngineStart = _MqEvent<
9
+ "engine-start",
10
+ {
11
+ readonly schemaId: string;
12
+ readonly schemaPrefix: string;
13
+ }
14
+ >;
15
+ export interface MqEventPageEnter
16
+ extends _MqEvent<
17
+ "page-enter",
18
+ { readonly pageId: string; readonly pageNumber: number; readonly pagePrefix: string }
19
+ > {}
20
+
21
+ export interface MqEventPageLeave
22
+ extends _MqEvent<
23
+ "page-leave",
24
+ { readonly pageId: string; readonly pageNumber: number; readonly pagePrefix: string }
25
+ > {}
26
+
27
+ export type MqEventUserClicked = _MqEvent<
28
+ "user-clicked",
29
+ {
30
+ readonly pageId: string;
31
+ readonly pagePrefix: string;
32
+ readonly pageNumber: number;
33
+ action: string;
34
+ descriptions: string;
35
+ }
36
+ >;
37
+
38
+ export type MqEvent = MqEventPageEnter | MqEventPageLeave | MqEventEngineStart | MqEventUserClicked;
39
+
40
+ export const MqEvent = {
41
+ engineStart(schemaId: string, schemaPrefix: string): MqEventEngineStart {
42
+ return {
43
+ kind: "engine-start",
44
+ timestamp: DTimestamp.now(),
45
+ payload: { schemaId, schemaPrefix },
46
+ };
47
+ },
48
+ pageEnter(pageId: string, pagePrefix: string, pageNumber: number): MqEventPageEnter {
49
+ return {
50
+ kind: "page-enter",
51
+ timestamp: DTimestamp.now(),
52
+ payload: { pageId, pagePrefix, pageNumber },
53
+ };
54
+ },
55
+ pageLeave(pageId: string, pagePrefix: string, pageNumber: number): MqEventPageLeave {
56
+ return {
57
+ kind: "page-leave",
58
+ timestamp: DTimestamp.now(),
59
+ payload: { pageId, pagePrefix, pageNumber },
60
+ };
61
+ },
62
+ userClicked(data: {
63
+ pageId: string;
64
+ pagePrefix: string;
65
+ pageNumber: number;
66
+ action: string;
67
+ descriptions: string;
68
+ }): MqEventUserClicked {
69
+ const { pageId, pageNumber, pagePrefix, action, descriptions } = data;
70
+ return {
71
+ kind: "user-clicked",
72
+ timestamp: DTimestamp.now(),
73
+ payload: { pageId, pagePrefix, action, descriptions, pageNumber },
74
+ };
75
+ },
76
+ } as const;