@woosh/meep-engine 2.44.6 → 2.44.8

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,199 +0,0 @@
1
- import levenshtein from "fast-levenshtein";
2
- import { GameAssetType } from "../engine/asset/GameAssetType.js";
3
- import { parseTooltipString } from "../view/tooltip/gml/parser/parseTooltipString.js";
4
- import { assert } from "./assert.js";
5
- import ObservedString from "./model/ObservedString.js";
6
- import { seedVariablesIntoTemplateString } from "./parser/seedVariablesIntoTemplateString.js";
7
- import { STRING_TEMPLATE_VARIABLE_REGEX } from "./parser/STRING_TEMPLATE_VARIABLE_REGEX.js";
8
-
9
- /**
10
- * Validation utility method
11
- * @param {string} template
12
- * @return {string}
13
- */
14
- function validationMockSeed(template) {
15
-
16
- const result = template.replace(STRING_TEMPLATE_VARIABLE_REGEX, function (match, varName) {
17
- return "0";
18
- });
19
-
20
- return result;
21
- }
22
-
23
- const FAILURE_MESSAGE_CACHE = {};
24
-
25
- export class Localization {
26
- constructor() {
27
- /**
28
- *
29
- * @type {AssetManager|null}
30
- */
31
- this.assetManager = null;
32
-
33
- this.json = {};
34
-
35
- /**
36
- * Measured in characters per second
37
- * @type {number}
38
- */
39
- this.reading_speed = 10;
40
-
41
- /**
42
- *
43
- * @type {ObservedString}
44
- */
45
- this.locale = new ObservedString('');
46
- }
47
-
48
- /**
49
- *
50
- * @param {string} text
51
- * @returns {number}
52
- */
53
- computeReadingTime(text) {
54
- assert.typeOf(text, 'string', 'text');
55
-
56
- return text.length / this.reading_speed;
57
- }
58
-
59
- /**
60
- *
61
- * @param {AssetManager} am
62
- */
63
- setAssetManager(am) {
64
- this.assetManager = am;
65
- }
66
-
67
- /**
68
- * @returns {boolean}
69
- * @param {function(key:string, error:*, original: string)} errorConsumer
70
- */
71
- validate(errorConsumer) {
72
- let result = true;
73
-
74
- for (let key in this.json) {
75
- const value = this.json[key];
76
-
77
- const seededValue = validationMockSeed(value);
78
-
79
- try {
80
- parseTooltipString(seededValue);
81
- } catch (e) {
82
- result = false;
83
- errorConsumer(key, e, value);
84
- }
85
- }
86
-
87
- return result;
88
- }
89
-
90
- /**
91
- *
92
- * @param {string} locale
93
- * @returns {Promise}
94
- */
95
- loadLocale(locale) {
96
- const am = this.assetManager;
97
-
98
- const pLoadData = am.promise(`data/database/text/${locale}.json`, GameAssetType.JSON)
99
- .then(asset => {
100
- const json = asset.create();
101
-
102
- this.json = json;
103
-
104
- this.locale.set(locale);
105
- });
106
-
107
- const pLoadMetadata = am.promise(`data/database/text/languages.json`, GameAssetType.JSON)
108
- .then(asset => {
109
- const languages_metadata = asset.create();
110
-
111
- const { reading_speed = 10 } = languages_metadata[locale];
112
-
113
- assert.isNumber(reading_speed, 'reading_speed');
114
- assert.greaterThan(reading_speed, 0, 'reading_speed');
115
-
116
- this.reading_speed = reading_speed;
117
- });
118
-
119
- return Promise.all([pLoadData, pLoadMetadata]);
120
- }
121
-
122
- /**
123
- *
124
- * @param {String} id
125
- * @return {boolean}
126
- */
127
- hasString(id) {
128
- return this.json[id] !== undefined;
129
- }
130
-
131
- /**
132
- *
133
- * @param {number} value
134
- */
135
- formatIntegerByThousands(value) {
136
- const formatter = new Intl.NumberFormat(this.locale.getValue(), { useGrouping: true });
137
-
138
- return formatter.format(value);
139
- }
140
-
141
- /**
142
- *
143
- * @param {string} id
144
- * @param {object} [seed]
145
- *
146
- * @returns {string}
147
- */
148
- getString(id, seed = {}) {
149
- assert.typeOf(id, 'string', 'id');
150
-
151
- const value = this.json[id];
152
-
153
- if (value === undefined) {
154
-
155
- if (!ENV_PRODUCTION) {
156
- const locale = this.locale.getValue();
157
-
158
- if (FAILURE_MESSAGE_CACHE[locale] === undefined) {
159
- FAILURE_MESSAGE_CACHE[locale] = {};
160
- }
161
-
162
- if (FAILURE_MESSAGE_CACHE[locale][id] === undefined) {
163
-
164
- //try to find similar keys
165
- const similarities = Object.keys(this.json).map(function (key) {
166
- const distance = levenshtein.get(key, id);
167
- return {
168
- key,
169
- distance
170
- };
171
- });
172
-
173
- similarities.sort(function (a, b) {
174
- return a.distance - b.distance;
175
- });
176
-
177
- const suggestions = similarities.slice(0, 3).map(p => p.key);
178
-
179
- const message = `No localization value for id='${id}', seed=${JSON.stringify(seed)}, approximate matches: ${suggestions.join(', ')}`;
180
-
181
- FAILURE_MESSAGE_CACHE[locale][id] = message;
182
-
183
- }
184
-
185
- console.warn(FAILURE_MESSAGE_CACHE[locale][id]);
186
-
187
- }
188
-
189
- //no value
190
- return `@${id}`;
191
-
192
- }
193
-
194
- //value needs to be seeded
195
- const seededValue = seedVariablesIntoTemplateString(value, seed);
196
-
197
- return seededValue;
198
- }
199
- }