@m2c2kit/cli 0.1.8 → 0.1.9

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.
package/dist/.env CHANGED
@@ -1 +1 @@
1
- CLI_VERSION=0.1.8
1
+ CLI_VERSION=0.1.9
@@ -7,8 +7,8 @@
7
7
  },
8
8
  "private": true,
9
9
  "dependencies": {
10
- "@m2c2kit/core": "0.1.6",
11
- "@m2c2kit/addons": "0.1.6"
10
+ "@m2c2kit/core": "0.1.7",
11
+ "@m2c2kit/addons": "0.1.7"
12
12
  },
13
13
  "devDependencies": {
14
14
  "rollup": "2.63.0",
@@ -15,12 +15,15 @@ import {
15
15
  Session,
16
16
  GameTrialEvent,
17
17
  GameLifecycleEvent,
18
+ SessionLifecycleEvent,
19
+ EventBase,
20
+ EventType
18
21
  } from "@m2c2kit/core";
19
22
  import { Button, Instructions } from "@m2c2kit/addons";
20
23
 
21
24
  class {{className}} extends Game {
22
25
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
- constructor(specifiedParameters?: any) {
26
+ constructor() {
24
27
  const defaultParameters: GameParameters = {
25
28
  ReadyTime: {
26
29
  value: 1000,
@@ -76,7 +79,7 @@ class {{className}} extends Game {
76
79
  ],
77
80
  };
78
81
 
79
- super(options, specifiedParameters);
82
+ super(options);
80
83
  // just for convenience, alias the variable game to "this"
81
84
  // (even though eslint doesn't like it)
82
85
  // eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -339,61 +342,121 @@ class {{className}} extends Game {
339
342
  }
340
343
  }
341
344
 
342
- // ============================================================================
345
+ // ===========================================================================
343
346
 
344
- // When running within an Android webview, the below defines how the session
345
- // can communicate events to the Android app. Note: the names of this Android
346
- // namespace and its functions must match the corresponding Android code
347
- // in addJavascriptInterface() and @JavascriptInterface
347
+ //#region to support m2c2kit in Android WebView
348
+ /** When running within an Android WebView, the below defines how the session
349
+ * can communicate events back to the Android app. Note: names of this Android
350
+ * namespace and its functions must match the corresponding Android code
351
+ * in addJavascriptInterface() and @JavascriptInterface */
348
352
  // eslint-disable-next-line @typescript-eslint/no-namespace
349
353
  declare namespace Android {
350
354
  function onGameTrialComplete(gameTrialEventAsString: string): void;
351
355
  function onGameLifecycleChange(gameLifecycleEventAsString: string): void;
356
+ function onSessionLifecycleChange(
357
+ sessionLifecycleEventAsString: string
358
+ ): void;
359
+ /** if the Android native app will control the session execution and be
360
+ * able to set custom game paraemters (which is probably what you want),
361
+ * be sure that sessionManualStart() in the native code returns true */
362
+ function sessionManualStart(): boolean;
352
363
  }
353
364
 
365
+ function contextIsAndroidWebView(): boolean {
366
+ return typeof Android !== "undefined";
367
+ }
368
+
369
+ function sendEventToAndroid(event: EventBase) {
370
+ switch (event.eventType) {
371
+ case EventType.sessionLifecycle: {
372
+ Android.onSessionLifecycleChange(JSON.stringify(event));
373
+ break;
374
+ }
375
+ case EventType.gameTrial: {
376
+ Android.onGameTrialComplete(JSON.stringify(event));
377
+ break;
378
+ }
379
+ case EventType.gameLifecycle: {
380
+ Android.onGameLifecycleChange(JSON.stringify(event));
381
+ break;
382
+ }
383
+ default:
384
+ throw new Error(
385
+ `attempt to send unknown event ${event.eventType} to Android`
386
+ );
387
+ }
388
+ }
389
+ //#endregion
390
+
391
+ const game1 = new {{className}}();
354
392
  // default was 3 trials; this is how we can specify a different value
355
- const game1 = new {{className}}({ TrialNum: 2 });
393
+ game1.setParameters({ TrialNum: 2 });
356
394
 
357
395
  const session = new Session({
358
396
  activities: [game1],
397
+ sessionCallbacks: {
398
+ // onSessionLifecycleChange() will be called on events such
399
+ // as when the session initialization is complete. Once initialized,
400
+ // the session will automatically start, unless we're running
401
+ // in an Android WebView and a manual start is desired.
402
+ onSessionLifecycleChange: (event: SessionLifecycleEvent) => {
403
+ if (event.initialized) {
404
+ //#region to support m2c2kit in Android WebView
405
+ if (contextIsAndroidWebView()) {
406
+ sendEventToAndroid(event);
407
+ }
408
+ if (contextIsAndroidWebView() && Android.sessionManualStart()) {
409
+ return;
410
+ }
411
+ //#endregion
412
+ session.start();
413
+ }
414
+ if (event.ended) {
415
+ console.log("session ended");
416
+ //#region to support m2c2kit in Android WebView
417
+ if (contextIsAndroidWebView()) {
418
+ sendEventToAndroid(event);
419
+ }
420
+ //#endregion
421
+ }
422
+ },
423
+ },
359
424
  gameCallbacks: {
360
425
  // onGameTrialComplete() is where you insert code to post data to an API
361
- // or interop with a native function in the host app, if applicable
362
- onGameTrialComplete: (e: GameTrialEvent) => {
363
- console.log(`********** trial ${e.trialIndex} complete`);
364
- console.log("data: " + JSON.stringify(e.gameData));
365
- console.log("trial schema: " + JSON.stringify(e.trialSchema));
366
- console.log("game parameters: " + JSON.stringify(e.gameParameters));
367
-
368
- // callback to native Android app, if running in that context
369
- if (typeof Android !== "undefined") {
370
- Android.onGameTrialComplete(JSON.stringify(e));
426
+ // or interop with a native function in the host app, if applicable
427
+ onGameTrialComplete: (event: GameTrialEvent) => {
428
+ console.log(`********** trial (index ${event.trialIndex}) complete`);
429
+ console.log("data: " + JSON.stringify(event.gameData));
430
+ console.log("trial schema: " + JSON.stringify(event.trialSchema));
431
+ console.log("game parameters: " + JSON.stringify(event.gameParameters));
432
+
433
+ //#region to support m2c2kit in Android WebView
434
+ if (contextIsAndroidWebView()) {
435
+ sendEventToAndroid(event);
371
436
  }
437
+ //#endregion
372
438
  },
373
- // onGameLifecycleChange() is called when the game lifecycles changes
374
- onGameLifecycleChange: (e: GameLifecycleEvent) => {
375
- if (e.ended) {
376
- console.log(`user requested exit in game ${e.gameName}`);
377
- // this session has only one activity, but this is how it would go to
378
- // the next activity
439
+ onGameLifecycleChange: (event: GameLifecycleEvent) => {
440
+ if (event.ended) {
441
+ console.log(`ended game ${event.gameName}`);
379
442
  if (session.nextActivity) {
380
443
  session.advanceToNextActivity();
444
+ } else {
445
+ session.end();
381
446
  }
382
-
383
- // callback to native Android app, if running in that context
384
- if (typeof Android !== "undefined") {
385
- Android.onGameLifecycleChange(JSON.stringify(e));
447
+ //#region to support m2c2kit in Android WebView
448
+ if (contextIsAndroidWebView()) {
449
+ sendEventToAndroid(event);
386
450
  }
451
+ //#endregion
387
452
  }
388
453
  },
389
454
  },
390
455
  });
391
456
 
392
- // make session also available on window in case we want to control
393
- // the session through another means
457
+ /** make session also available on window in case we want to control
458
+ * the session through another means, such as other javascript or
459
+ * browser code, or the Android WebView loadUrl() method */
394
460
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
395
461
  (window as unknown as any).session = session;
396
-
397
- session.init().then(() => {
398
- session.start();
399
- });
462
+ session.init();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@m2c2kit/cli",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "m2c2kit command line interface",
5
5
  "module": "dist/cli.js",
6
6
  "files": [