@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.
|
|
1
|
+
CLI_VERSION=0.1.9
|
|
@@ -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(
|
|
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
|
|
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
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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
|
-
|
|
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: (
|
|
363
|
-
console.log(`********** trial ${
|
|
364
|
-
console.log("data: " + JSON.stringify(
|
|
365
|
-
console.log("trial schema: " + JSON.stringify(
|
|
366
|
-
console.log("game parameters: " + JSON.stringify(
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
if (
|
|
370
|
-
|
|
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
|
-
|
|
374
|
-
|
|
375
|
-
|
|
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
|
-
|
|
384
|
-
|
|
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
|
-
|
|
393
|
-
|
|
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();
|