@m2c2kit/core 0.3.29 → 0.3.31

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/index.d.ts CHANGED
@@ -937,6 +937,15 @@ interface LocalizationOptions {
937
937
  additionalTranslation?: Translation;
938
938
  }
939
939
 
940
+ /**
941
+ * ScoringSchema defines the data that are generated by the scoring code. They
942
+ * are key-value pairs in which the key is the variable name, and the value is
943
+ * JSON Schema that defines the type of the variable.
944
+ */
945
+ interface ScoringSchema {
946
+ [key: string]: JsonSchema;
947
+ }
948
+
940
949
  /**
941
950
  * Options to specify HTML canvas, set game canvas size, and load game assets.
942
951
  */
@@ -965,6 +974,8 @@ interface GameOptions extends LocalizationOptions {
965
974
  stretch?: boolean;
966
975
  /** Schema of trial data; JSON object where key is variable name, value is data type */
967
976
  trialSchema?: TrialSchema;
977
+ /** Schema of scoring data; JSON object where key is variable name, value is data type */
978
+ scoringSchema?: ScoringSchema;
968
979
  /** Default game parameters; JSON object where key is the game parameter, value is default value */
969
980
  parameters?: GameParameters;
970
981
  /** Font assets to use. The first element will be the default font */
@@ -997,6 +1008,7 @@ interface GameOptions extends LocalizationOptions {
997
1008
 
998
1009
  interface GameData extends ActivityKeyValueData {
999
1010
  trials: Array<TrialData>;
1011
+ scoring: ActivityKeyValueData;
1000
1012
  }
1001
1013
 
1002
1014
  /**
@@ -1593,6 +1605,22 @@ declare class Game implements Activity {
1593
1605
  */
1594
1606
  constructor(options: GameOptions);
1595
1607
  private createFreeNodesScene;
1608
+ /**
1609
+ * Returns the base URL of an imported module.
1610
+ *
1611
+ * @remarks Previously, a regex was used:
1612
+ * `const regex = new RegExp(`^.*${packageName}[^\\/]*`);`
1613
+ * but this triggered irrelevant warnings for ReDoS in some overly
1614
+ * sensitive package scanners, so now we use URL and pathname parsing.
1615
+ * Also: trailing slashes are removed from the returned base URL.
1616
+ *
1617
+ * @param packageName - the name of the imported package module, like
1618
+ * `@m2c2kit/assessment-symbol-search`
1619
+ * @param moduleUrl - the full URL of the module's entrypoint, possibly
1620
+ * including a version suffix, like `https://cdn.com/@m2c2kit/assessment-symbol-search@0.8.13/dist/index.js`
1621
+ * @returns - the base URL of the imported module, without the entrypoint,
1622
+ * like `https://cdn.com/@m2c2kit/assessment-symbol-search@0.8.13`
1623
+ */
1596
1624
  private getImportedModuleBaseUrl;
1597
1625
  private addLocalizationParametersToGameParameters;
1598
1626
  init(): Promise<void>;
@@ -1974,20 +2002,43 @@ declare class Game implements Activity {
1974
2002
  */
1975
2003
  dispose(): void;
1976
2004
  private initData;
2005
+ private validateSchema;
1977
2006
  private propertySchemaDataTypeIsValid;
1978
2007
  private getDeviceMetadata;
1979
2008
  /**
1980
2009
  * Adds data to the game's TrialData object.
1981
2010
  *
1982
- * @remarks The variableName must be previously defined in the trialSchema
1983
- * object passed in during game initialization through
1984
- * {@link GameInitOptions.trialSchema}. The type of the value must match
1985
- * what was defined in the trialSchema, otherwise an error is thrown.
2011
+ * @remarks `variableName` must be previously defined in the
2012
+ * {@link TrialSchema} object in {@link GameOptions}. The type of the value
2013
+ * must match what was defined in the trial schema, otherwise an error is
2014
+ * thrown.
1986
2015
  *
1987
2016
  * @param variableName - variable to be set
1988
2017
  * @param value - value of the variable to set
1989
2018
  */
1990
2019
  addTrialData(variableName: string, value: JsonSchemaDataTypeScriptTypes): void;
2020
+ /**
2021
+ * Adds data to the game's scoring data.
2022
+ *
2023
+ * @remarks The variable name (or object property names) must be previously
2024
+ * defined in the {@link ScoringSchema} object in {@link GameOptions}.
2025
+ * The type of the value must match what was defined in the scoring schema,
2026
+ * otherwise an error is thrown.
2027
+ *
2028
+ * @param variableNameOrObject - Either a variable name (string) or an object
2029
+ * containing multiple key-value pairs to add all at once.
2030
+ * @param value - Value of the variable to set (only used when
2031
+ * variableNameOrObject is a variable name string).
2032
+ */
2033
+ addScoringData(variableNameOrObject: string | Record<string, JsonSchemaDataTypeScriptTypes> | Array<Record<string, JsonSchemaDataTypeScriptTypes>>, value?: JsonSchemaDataTypeScriptTypes): void;
2034
+ /**
2035
+ * Helper method to validate and set a single scoring variable
2036
+ *
2037
+ * @param variableName - Name of the variable to set
2038
+ * @param value - Value to set
2039
+ * @private
2040
+ */
2041
+ private validateAndSetScoringVariable;
1991
2042
  /**
1992
2043
  * Adds custom trial schema to the game's trialSchema object.
1993
2044
  *
@@ -2036,6 +2087,22 @@ declare class Game implements Activity {
2036
2087
  * the appropriate time. It is not triggered automatically.
2037
2088
  */
2038
2089
  trialComplete(): void;
2090
+ /**
2091
+ * Marks scoring as complete.
2092
+ *
2093
+ * @remarks This method must be called after the game has finished adding
2094
+ * scores using addScoringData(). Calling will trigger the onActivityResults
2095
+ * callback function, if one was provided in SessionOptions. This is how the
2096
+ * game communicates scoring data to the parent session, which can then save
2097
+ * or process the data. It is the responsibility of the the game programmer
2098
+ * to call this at the appropriate time. It is not triggered automatically.
2099
+ */
2100
+ scoringComplete(): void;
2101
+ /**
2102
+ * The m2c2kit engine will automatically include these schema and their
2103
+ * values in the scoring data.
2104
+ */
2105
+ private readonly automaticScoringSchema;
2039
2106
  /**
2040
2107
  * The m2c2kit engine will automatically include these schema and their
2041
2108
  * values in the trial data.
@@ -2052,6 +2119,7 @@ declare class Game implements Activity {
2052
2119
  */
2053
2120
  private makeGameActivityConfiguration;
2054
2121
  private makeGameActivityConfigurationSchema;
2122
+ private makeScoringDataSchema;
2055
2123
  /**
2056
2124
  * Should be called when current game has ended successfully.
2057
2125
  *
@@ -2298,14 +2366,69 @@ declare class I18n {
2298
2366
  private localeTranslationAvailable;
2299
2367
  switchToLocale(locale: string): void;
2300
2368
  /**
2369
+ * Returns the localized text and font information for the given key in the
2370
+ * current locale.
2301
2371
  *
2302
2372
  * @param key - Translation key
2303
2373
  * @param interpolation - Interpolation keys and values to replace
2304
- * placeholders in the translated text
2305
- * @returns a `TextLocalizationResult` object with the localized text, font
2306
- * information, and whether the translation is a fallback.
2374
+ * string interpolation placeholders in the translated text
2375
+ * @returns object with the localized text, font information, and whether the
2376
+ * translation is a fallback.
2307
2377
  */
2308
2378
  getTextLocalization(key: string, interpolation?: StringInterpolationMap): TextLocalizationResult;
2379
+ /**
2380
+ *
2381
+ * @param key - Translation key to be translated
2382
+ * @param interpolation - String interpolation keys and values to replace
2383
+ * @returns result object with the localized text, font
2384
+ */
2385
+ private attemptTranslation;
2386
+ /**
2387
+ * Handles translation placeholders in a string.
2388
+ *
2389
+ * @remarks The value of a translation placeholder (text within `[[]]`)
2390
+ * may also contain string interpolation placeholders (`{{}}`), so we need
2391
+ * to handle that as well.
2392
+ *
2393
+ * @param key - Translation key for the string to be localized
2394
+ * @param initialResult - Initial translation result for the string
2395
+ * @param interpolation - Interpolation keys and values to replace
2396
+ * interpolation placeholders in the translated text
2397
+ * @returns result object with the localized text,
2398
+ */
2399
+ private handleTranslationPlaceholders;
2400
+ /**
2401
+ * Translates a translation placeholder key to its text and collects font properties.
2402
+ *
2403
+ * @param placeholderKey - Translation key for the placeholder
2404
+ * @param fontProps - Font properties sets to collect font information
2405
+ * @param interpolation - Interpolation keys and values to replace
2406
+ * string interpolation placeholders in the translated text
2407
+ * @returns result object with the translated text and whether it is a fallback translation
2408
+ */
2409
+ private translatePlaceholder;
2410
+ /**
2411
+ * Extracts translation key placeholders from a string.
2412
+ *
2413
+ * @remarks Translation key placeholders are denoted by double square brackets,
2414
+ * e.g., "The translated word is [[RED]]", and RED is a key for translation.
2415
+ *
2416
+ * @param s - string to search for placeholders
2417
+ * @returns an array of placeholders found in the string, without the square
2418
+ * brackets
2419
+ */
2420
+ private getTranslationPlaceholders;
2421
+ /**
2422
+ * Logs warnings if the string to be localized has conflicting font
2423
+ * properties.
2424
+ *
2425
+ * @remarks This can happen due to multiple placeholders in the string
2426
+ * that specify different font sizes, font names, or font names arrays.
2427
+ *
2428
+ * @param key - Translation key for which the string is being localized
2429
+ * @param fontProps - font properties sets collected from the placeholders
2430
+ */
2431
+ private warnConflictingFontProperties;
2309
2432
  /**
2310
2433
  * Returns the translation text for the given key in the current locale.
2311
2434
  *
@@ -3761,6 +3884,13 @@ declare enum Dimensions {
3761
3884
  MatchConstraint = 0
3762
3885
  }
3763
3886
 
3887
+ /**
3888
+ * Custom error class for m2c2kit errors.
3889
+ */
3890
+ declare class M2Error extends Error {
3891
+ constructor(...params: string[]);
3892
+ }
3893
+
3764
3894
  type M2NodeConstructor = new (options?: M2NodeOptions) => M2Node;
3765
3895
 
3766
3896
  /**
@@ -4197,7 +4327,7 @@ declare enum LabelHorizontalAlignmentMode {
4197
4327
  }
4198
4328
 
4199
4329
  interface LabelOptions extends M2NodeOptions, DrawableOptions, TextOptions {
4200
- /** Text to be displayed. Tags for bold, italic, and underline are supported, e.g., `<b><u>Bold and underline</u></b>`. */
4330
+ /** Text to be displayed. When operating with localization, text within double square brackets will be replaced with the value of the key in the translation. For example, if the text is `The translated word is [[RED]]`, `[[RED]]` will be replaced with the translation. If no localization is used, or a translation for the key is missing, then the text will be rendered as is. Tags for bold (`b`), italic (`i`), underline (`u`), overline(`o`), and strikethrough (`s`) are supported, e.g., `<b><u>Bold and underline</u></b>`. Note that while bold and italic and be combined, only one of underline, overline, and strikethrough can be used on a text segment. */
4201
4331
  text?: string;
4202
4332
  /** Horizontal alignment of label text. see {@link LabelHorizontalAlignmentMode}. Default is LabelHorizontalAlignmentMode.center */
4203
4333
  horizontalAlignmentMode?: LabelHorizontalAlignmentMode;
@@ -4230,14 +4360,12 @@ declare class Label extends M2Node implements IDrawable, IText, LabelOptions {
4230
4360
  private builder?;
4231
4361
  private _fontPaint?;
4232
4362
  private _backgroundPaint?;
4233
- private _underlinePaint?;
4234
4363
  private localizedFontSize;
4235
4364
  private localizedFontName;
4236
4365
  private localizedFontNames;
4366
+ private textAfterLocalization;
4237
4367
  private plainText;
4238
4368
  private styleSegments;
4239
- private underlinedRanges;
4240
- private currentBuilderPosition;
4241
4369
  /**
4242
4370
  * Single or multi-line text formatted and rendered on the screen.
4243
4371
  *
@@ -4284,7 +4412,23 @@ declare class Label extends M2Node implements IDrawable, IText, LabelOptions {
4284
4412
  * @throws Error if tags are improperly nested or unclosed
4285
4413
  */
4286
4414
  private parseFormattedText;
4287
- private addTextWithStylesToParagraphBuilder;
4415
+ /**
4416
+ * Adds text segments with consistent styling to the paragraph builder.
4417
+ *
4418
+ * @param builder - {@link ParagraphBuilder}
4419
+ * @param styleSegments - Segments of text with consistent styling
4420
+ * @param defaultStyle - Default style to apply
4421
+ */
4422
+ private addStyleSegmentsToParagraphBuilder;
4423
+ /**
4424
+ * Helper that adds text to the paragraph builder with the specified style.
4425
+ *
4426
+ * @param builder - {@link ParagraphBuilder}
4427
+ * @param text - The text to add
4428
+ * @param defaultStyle - The default style to apply
4429
+ * @param styleModifiers - Additional style modifications
4430
+ * @returns void
4431
+ */
4288
4432
  private addTextWithStyle;
4289
4433
  /**
4290
4434
  * Determines the M2Font objects that need to be ready in order to draw
@@ -4326,8 +4470,6 @@ declare class Label extends M2Node implements IDrawable, IText, LabelOptions {
4326
4470
  private set backgroundPaint(value);
4327
4471
  private get fontPaint();
4328
4472
  private set fontPaint(value);
4329
- private get underlinePaint();
4330
- private set underlinePaint(value);
4331
4473
  /**
4332
4474
  * Duplicates a node using deep copy.
4333
4475
  *
@@ -4341,7 +4483,6 @@ declare class Label extends M2Node implements IDrawable, IText, LabelOptions {
4341
4483
  duplicate(newName?: string): Label;
4342
4484
  update(): void;
4343
4485
  draw(canvas: Canvas): void;
4344
- private drawUnderlines;
4345
4486
  warmup(canvas: Canvas): void;
4346
4487
  }
4347
4488
 
@@ -4536,12 +4677,27 @@ declare class RandomDraws {
4536
4677
  }
4537
4678
 
4538
4679
  /**
4539
- * ScoringSchema defines the data that are generated by the scoring code. They
4540
- * are key-value pairs in which the key is the variable name, and the value is
4541
- * JSON Schema that defines the type of the variable.
4680
+ * Assessments that generate scoring data must implement the ScoringProvider
4681
+ * interface
4542
4682
  */
4543
- interface ScoringSchema {
4544
- [key: string]: JsonSchema;
4683
+ interface ScoringProvider {
4684
+ /**
4685
+ * Calculates scores based on the provided activity data and optional
4686
+ * additional parameters.
4687
+ *
4688
+ * @param data - Activity data from which to calculate scores.
4689
+ * @param extras - Optional additional parameters that are needed for an
4690
+ * activity's scoring calculations. This can include things like
4691
+ * game parameters, user context, or other metadata that is relevant
4692
+ * to the scoring logic. The structure of this object is not
4693
+ * defined by this interface, as it can vary widely between different
4694
+ * activities.
4695
+ * @returns The calculated scores object or an array of such objects. If an
4696
+ * array of is returned, it should have length 1.
4697
+ */
4698
+ calculateScores(data: ActivityKeyValueData[], extras?: {
4699
+ [key: string]: unknown;
4700
+ }): ActivityKeyValueData | Array<ActivityKeyValueData>;
4545
4701
  }
4546
4702
 
4547
4703
  declare enum ShapeType {
@@ -5367,5 +5523,5 @@ declare class WebGlInfo {
5367
5523
  static dispose(): void;
5368
5524
  }
5369
5525
 
5370
- export { Action, ActivityType, CanvasKitHelpers, ColorfulMutablePath, Composite, Constants, ConstraintType, CustomAction, Dimensions, Easings, Equal, Equals, EventStore, EventStoreMode, FadeAlphaAction, FontManager, Game, GroupAction, I18n, ImageManager, Label, LabelHorizontalAlignmentMode, LayoutConstraint, LegacyTimer, M2EventType, M2ImageStatus, M2Node, M2NodeFactory, M2NodeType, M2SoundStatus, M2c2KitHelpers, MoveAction, MutablePath, NoneTransition, PlayAction, RandomDraws, RepeatAction, RepeatForeverAction, RotateAction, ScaleAction, Scene, SceneTransition, SequenceAction, Shape, ShapeType, SlideTransition, SoundManager, SoundPlayer, SoundRecorder, Sprite, Story, TextLine, Timer, Transition, TransitionDirection, TransitionType, Uuid, WaitAction, WebColors, WebGlInfo, handleInterfaceOptions };
5371
- export type { Activity, ActivityCallbacks, ActivityEvent, ActivityEventListener, ActivityKeyValueData, ActivityLifecycleEvent, ActivityResultsEvent, BrowserImage, BrowserImageDataReadyEvent, CallbackOptions, CompositeEvent, CompositeOptions, Constraints, CustomActionOptions, DefaultParameter, DomPointerDownEvent, DrawableOptions, EasingFunction, FadeAlphaActionOptions, FontAsset, FontData, GameData, GameEvent, GameOptions, GameParameters, GlobalVariables, I18nDataReadyEvent, IDataStore, IDrawable, IText, LabelOptions, Layout, LocaleSvg, M2ColorfulPath, M2DragEvent, M2Event, M2EventListener, M2Image, M2KeyboardEvent, M2NodeAddChildEvent, M2NodeConstructor, M2NodeEvent, M2NodeEventListener, M2NodeNewEvent, M2NodeOptions, M2NodePropertyChangeEvent, M2NodeRemoveChildEvent, M2Path, M2PointerEvent, M2Sound, MoveActionOptions, PlayActionOptions, Plugin, PluginEvent, Point, RectOptions, RgbaColor, ScaleActionOptions, SceneOptions, ScenePresentEvent, ScoringSchema, ShapeOptions, Size, SlideTransitionOptions, SoundAsset, SoundPlayerOptions, SoundRecorderOptions, SoundRecorderResults, SpriteOptions, StoryOptions, StringInterpolationMap, TapEvent, TextAndFont, TextLineOptions, TextLocalizationResult, TextOptions, TextWithFontCustomization, Translation, TranslationConfiguration, TranslationOptions, TrialData, TrialSchema, WaitActionOptions };
5526
+ export { Action, ActivityType, CanvasKitHelpers, ColorfulMutablePath, Composite, Constants, ConstraintType, CustomAction, Dimensions, Easings, Equal, Equals, EventStore, EventStoreMode, FadeAlphaAction, FontManager, Game, GroupAction, I18n, ImageManager, Label, LabelHorizontalAlignmentMode, LayoutConstraint, LegacyTimer, M2Error, M2EventType, M2ImageStatus, M2Node, M2NodeFactory, M2NodeType, M2SoundStatus, M2c2KitHelpers, MoveAction, MutablePath, NoneTransition, PlayAction, RandomDraws, RepeatAction, RepeatForeverAction, RotateAction, ScaleAction, Scene, SceneTransition, SequenceAction, Shape, ShapeType, SlideTransition, SoundManager, SoundPlayer, SoundRecorder, Sprite, Story, TextLine, Timer, Transition, TransitionDirection, TransitionType, Uuid, WaitAction, WebColors, WebGlInfo, handleInterfaceOptions };
5527
+ export type { Activity, ActivityCallbacks, ActivityEvent, ActivityEventListener, ActivityKeyValueData, ActivityLifecycleEvent, ActivityResultsEvent, BrowserImage, BrowserImageDataReadyEvent, CallbackOptions, CompositeEvent, CompositeOptions, Constraints, CustomActionOptions, DefaultParameter, DomPointerDownEvent, DrawableOptions, EasingFunction, FadeAlphaActionOptions, FontAsset, FontData, GameData, GameEvent, GameOptions, GameParameters, GlobalVariables, I18nDataReadyEvent, IDataStore, IDrawable, IText, LabelOptions, Layout, LocaleSvg, M2ColorfulPath, M2DragEvent, M2Event, M2EventListener, M2Image, M2KeyboardEvent, M2NodeAddChildEvent, M2NodeConstructor, M2NodeEvent, M2NodeEventListener, M2NodeNewEvent, M2NodeOptions, M2NodePropertyChangeEvent, M2NodeRemoveChildEvent, M2Path, M2PointerEvent, M2Sound, MoveActionOptions, PlayActionOptions, Plugin, PluginEvent, Point, RectOptions, RgbaColor, ScaleActionOptions, SceneOptions, ScenePresentEvent, ScoringProvider, ScoringSchema, ShapeOptions, Size, SlideTransitionOptions, SoundAsset, SoundPlayerOptions, SoundRecorderOptions, SoundRecorderResults, SpriteOptions, StoryOptions, StringInterpolationMap, TapEvent, TextAndFont, TextLineOptions, TextLocalizationResult, TextOptions, TextWithFontCustomization, Translation, TranslationConfiguration, TranslationOptions, TrialData, TrialSchema, WaitActionOptions };