@smartico/public-api 0.0.4 → 0.0.6
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/README.md +23 -44
- package/dist/Base/ClassId.d.ts +53 -4
- package/dist/Core/GetLabelInfoRequest.d.ts +3 -0
- package/dist/Core/GetLabelInfoResponse.d.ts +7 -0
- package/dist/Core/GetTranslationsRequest.d.ts +7 -0
- package/dist/Core/GetTranslationsResponse.d.ts +7 -0
- package/dist/Core/PublicLabelSettings.d.ts +28 -0
- package/dist/Core/PublicProperties.d.ts +11 -0
- package/dist/Core/ResponseIdentify.d.ts +12 -0
- package/dist/Core/TranslationArea.d.ts +9 -0
- package/dist/Core/index.d.ts +8 -0
- package/dist/Inbox/GetInboxMessagesRequest.d.ts +5 -0
- package/dist/Inbox/GetInboxMessagesResponse.d.ts +5 -0
- package/dist/Inbox/InboxMessage.d.ts +12 -0
- package/dist/Inbox/InboxMessageType.d.ts +11 -0
- package/dist/Inbox/index.d.ts +5 -0
- package/dist/MiniGames/SAWAskForUsername.d.ts +4 -0
- package/dist/MiniGames/SAWDoSpinResponse.d.ts +1 -7
- package/dist/MiniGames/SAWSpinErrorCode.d.ts +7 -0
- package/dist/MiniGames/SAWTemplateUI.d.ts +2 -5
- package/dist/MiniGames/SAWUtils.d.ts +5 -0
- package/dist/MiniGames/index.d.ts +5 -2
- package/dist/NodeCache.d.ts +9 -0
- package/dist/OCache.d.ts +11 -0
- package/dist/SmarticoAPI.d.ts +16 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +431 -40
- package/dist/index.js.map +1 -1
- package/dist/index.modern.mjs +324 -23
- package/dist/index.modern.mjs.map +1 -1
- package/package.json +11 -3
- package/src/Base/ClassId.ts +64 -6
- package/src/Base/ProtocolMessage.ts +6 -1
- package/src/Core/GetLabelInfoRequest.ts +5 -0
- package/src/Core/GetLabelInfoResponse.ts +6 -0
- package/src/Core/GetTranslationsRequest.ts +10 -0
- package/src/Core/GetTranslationsResponse.ts +8 -0
- package/src/Core/PublicLabelSettings.ts +34 -0
- package/src/Core/PublicProperties.ts +11 -0
- package/src/Core/ResponseIdentify.ts +14 -0
- package/src/Core/TranslationArea.ts +11 -0
- package/src/Core/index.ts +18 -0
- package/src/Inbox/GetInboxMessagesRequest.ts +7 -0
- package/src/Inbox/GetInboxMessagesResponse.ts +7 -0
- package/src/Inbox/InboxMessage.ts +14 -0
- package/src/Inbox/InboxMessageType.ts +13 -0
- package/src/Inbox/index.ts +11 -0
- package/src/MiniGames/SAWAskForUsername.ts +5 -0
- package/src/MiniGames/SAWDoSpinResponse.ts +1 -8
- package/src/MiniGames/SAWSpinErrorCode.ts +8 -0
- package/src/MiniGames/SAWTemplateUI.ts +3 -5
- package/src/MiniGames/SAWUtils.ts +31 -0
- package/src/MiniGames/index.ts +8 -3
- package/src/NodeCache.ts +45 -0
- package/src/OCache.ts +55 -0
- package/src/SmarticoAPI.ts +142 -9
- package/src/index.ts +2 -0
package/src/NodeCache.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
class NodeCache {
|
|
3
|
+
|
|
4
|
+
private static ttlChecker: NodeJS.Timeout;
|
|
5
|
+
|
|
6
|
+
private static cache: { [key: string] : any } = {};
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
if (NodeCache.ttlChecker === undefined) {
|
|
10
|
+
NodeCache.ttlChecker = setInterval(() => {
|
|
11
|
+
const now = new Date().getTime();
|
|
12
|
+
for (const key in NodeCache.cache) {
|
|
13
|
+
if (NodeCache.cache.hasOwnProperty(key)) {
|
|
14
|
+
const o = NodeCache.cache[key];
|
|
15
|
+
if (o.ttl < now) {
|
|
16
|
+
delete NodeCache.cache[key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}, 1000);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public get(key: string): any {
|
|
25
|
+
const o = NodeCache.cache[key];
|
|
26
|
+
if (o !== undefined && o.ttl > new Date().getTime()) {
|
|
27
|
+
return o.value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public set(key: string, value: any, ttlSeconds: number = 60) {
|
|
32
|
+
NodeCache.cache[key] = {
|
|
33
|
+
value,
|
|
34
|
+
ttl: new Date().getTime() + ttlSeconds * 1000
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public flushAll() {
|
|
39
|
+
NodeCache.cache = {};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export { NodeCache }
|
package/src/OCache.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { NodeCache } from "./NodeCache";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export enum ECacheContext {
|
|
5
|
+
Translations,
|
|
6
|
+
LabelInfo
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const WITH_REF_CACHE = [
|
|
10
|
+
ECacheContext.Translations
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
export class OCache {
|
|
14
|
+
|
|
15
|
+
private static cache: { [key: string] : NodeCache } = {}
|
|
16
|
+
|
|
17
|
+
public static get<T>(oKey: any, cacheContext: ECacheContext): T | undefined {
|
|
18
|
+
|
|
19
|
+
const key = cacheContext.toString() + '_' + JSON.stringify(oKey);
|
|
20
|
+
|
|
21
|
+
if (this.cache[cacheContext] === undefined) {
|
|
22
|
+
this.cache[cacheContext] = new NodeCache( );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return this.cache[cacheContext].get(key);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public static set(oKey: any, o: any, cacheContext: ECacheContext, ttlSeconds: number = 60) {
|
|
29
|
+
|
|
30
|
+
const key = cacheContext.toString() + '_' + JSON.stringify(oKey);
|
|
31
|
+
|
|
32
|
+
this.cache[cacheContext].set(key, o, ttlSeconds);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public static async use<T>(oKey: any, cacheContext: ECacheContext, f: () => Promise<T>, ttlSeconds: number = 60) {
|
|
36
|
+
if (ttlSeconds <= 0) {
|
|
37
|
+
return await f();
|
|
38
|
+
} else {
|
|
39
|
+
let o: T = OCache.get(oKey, cacheContext);
|
|
40
|
+
|
|
41
|
+
if (o === undefined) {
|
|
42
|
+
o = await f();
|
|
43
|
+
OCache.set(oKey, o, cacheContext, ttlSeconds);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return o;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public static async clear(cacheContext: ECacheContext) {
|
|
51
|
+
if (this.cache[cacheContext]) {
|
|
52
|
+
this.cache[cacheContext].flushAll();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/SmarticoAPI.ts
CHANGED
|
@@ -7,9 +7,15 @@ import { SAWGetTemplatesRequest } from './MiniGames/SAWGetTemplatesRequest';
|
|
|
7
7
|
import { SAWTemplate } from './MiniGames/SAWTemplate';
|
|
8
8
|
import { IntUtils } from './IntUtils';
|
|
9
9
|
import { ILogger } from './ILogger';
|
|
10
|
-
import { SAWBuyInType, SAWBuyInTypeName, SAWGameType, SAWGameTypeName } from './MiniGames';
|
|
10
|
+
import { SAWBuyInType, SAWBuyInTypeName, SAWDoSpinRequest, SAWDoSpinResponse, SAWGameType, SAWGameTypeName, SAWSpinErrorCode, SAWUtils } from './MiniGames';
|
|
11
|
+
import { ECacheContext, OCache } from './OCache';
|
|
12
|
+
import { GetTranslationsRequest, GetTranslationsResponse, ResponseIdentify, TranslationArea } from './Core';
|
|
13
|
+
import { GetLabelInfoResponse } from './Core/GetLabelInfoResponse';
|
|
14
|
+
import { GetLabelInfoRequest } from './Core/GetLabelInfoRequest';
|
|
15
|
+
import { GetInboxMessagesRequest, GetInboxMessagesResponse } from './Inbox';
|
|
11
16
|
|
|
12
17
|
const PUBLIC_API_URL = 'https://papi{ENV_ID}.smartico.ai/services/public';
|
|
18
|
+
const AVATAR_DOMAIN = 'https://img{ENV_ID}.smr.vc';
|
|
13
19
|
|
|
14
20
|
interface IOptions {
|
|
15
21
|
logger?: ILogger;
|
|
@@ -21,6 +27,9 @@ interface IOptions {
|
|
|
21
27
|
class SmarticoAPI {
|
|
22
28
|
|
|
23
29
|
private publicUrl: string;
|
|
30
|
+
private avatarDomain: string;
|
|
31
|
+
|
|
32
|
+
|
|
24
33
|
private logger: ILogger;
|
|
25
34
|
private logCIDs: ClassId[];
|
|
26
35
|
private logHTTPTiming: boolean;
|
|
@@ -43,7 +52,8 @@ class SmarticoAPI {
|
|
|
43
52
|
}
|
|
44
53
|
label_api_key = label_api_key.substring(0, 36);
|
|
45
54
|
|
|
46
|
-
this.publicUrl = PUBLIC_API_URL.replace('{ENV_ID}', ENV_ID);
|
|
55
|
+
this.publicUrl = PUBLIC_API_URL.replace('{ENV_ID}', ENV_ID);
|
|
56
|
+
this.avatarDomain = AVATAR_DOMAIN.replace('{ENV_ID}', ENV_ID);
|
|
47
57
|
}
|
|
48
58
|
|
|
49
59
|
private async send<T>(message: any, expectCID?: ClassId): Promise<T> {
|
|
@@ -59,6 +69,7 @@ class SmarticoAPI {
|
|
|
59
69
|
try {
|
|
60
70
|
const timeStart = new Date().getTime();
|
|
61
71
|
const res = await superagent.post(this.publicUrl).send(message);
|
|
72
|
+
// const res = await superagent.post('http://channel01.int.smartico.ai:81/services/public').send(message);
|
|
62
73
|
const timeEnd = new Date().getTime();
|
|
63
74
|
|
|
64
75
|
if (this.logHTTPTiming) {
|
|
@@ -118,10 +129,94 @@ class SmarticoAPI {
|
|
|
118
129
|
...payload
|
|
119
130
|
};
|
|
120
131
|
|
|
132
|
+
if (message.ext_user_id === undefined || message.ext_user_id === null) {
|
|
133
|
+
delete message.ext_user_id;
|
|
134
|
+
}
|
|
135
|
+
|
|
121
136
|
return message as any
|
|
122
137
|
}
|
|
123
138
|
|
|
124
|
-
public async
|
|
139
|
+
public async coreReportCustomEvent(user_ext_id: string, eventType: string, payload: any = {}): Promise<any> {
|
|
140
|
+
const eventMessage = this.buildMessage<any, any>(user_ext_id, ClassId.EVENT, {
|
|
141
|
+
eventType,
|
|
142
|
+
payload
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const eventResponse = await this.send<any>(eventMessage, ClassId.EVENT_RESPONSE);
|
|
146
|
+
return eventResponse;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public async coreGetTranslations(user_ext_id: string, lang_code: string, areas: TranslationArea[], cacheSec: number = 60): Promise<GetTranslationsResponse> {
|
|
150
|
+
|
|
151
|
+
const response = await OCache.use<GetTranslationsResponse>(`${lang_code}-${this.label_api_key}-${this.brand_api_key}`, ECacheContext.Translations, async () => {
|
|
152
|
+
|
|
153
|
+
const tsBaseRQ = this.buildMessage<GetTranslationsRequest, GetTranslationsResponse>(user_ext_id, ClassId.GET_TRANSLATIONS_REQUEST, {
|
|
154
|
+
lang_code: "EN",
|
|
155
|
+
hash_code: 0,
|
|
156
|
+
areas
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const trBase = await this.send<GetTranslationsResponse>(tsBaseRQ);
|
|
160
|
+
|
|
161
|
+
if (lang_code !== "EN") {
|
|
162
|
+
const trUserRQ = this.buildMessage<GetTranslationsRequest, GetTranslationsResponse>(user_ext_id, ClassId.GET_TRANSLATIONS_REQUEST, {
|
|
163
|
+
lang_code,
|
|
164
|
+
hash_code: 0,
|
|
165
|
+
areas
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const trUser = await this.send<GetTranslationsResponse>(trUserRQ);
|
|
169
|
+
|
|
170
|
+
Object.keys(trUser.translations).forEach( k => {
|
|
171
|
+
trBase.translations[k] = trUser.translations[k];
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return trBase;
|
|
176
|
+
|
|
177
|
+
}, cacheSec );
|
|
178
|
+
|
|
179
|
+
return response;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public async coreIdentifyLabel(user_ext_id: string, cacheSec: number = 60): Promise<GetLabelInfoResponse> {
|
|
183
|
+
|
|
184
|
+
return OCache.use<GetLabelInfoResponse>(`${this.label_api_key} - ${this.brand_api_key}`, ECacheContext.LabelInfo, async () => {
|
|
185
|
+
|
|
186
|
+
const message = this.buildMessage<GetLabelInfoResponse, GetLabelInfoRequest>(user_ext_id, ClassId.INIT);
|
|
187
|
+
|
|
188
|
+
return this.send<GetLabelInfoResponse>(message, ClassId.INIT_RESPONSE)
|
|
189
|
+
|
|
190
|
+
}, cacheSec);
|
|
191
|
+
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public async coreIdentifyUser(user_ext_id: string): Promise<ResponseIdentify> {
|
|
195
|
+
|
|
196
|
+
const message = this.buildMessage<any, ResponseIdentify>(user_ext_id, ClassId.IDENTIFY, {
|
|
197
|
+
request_id: IntUtils.uuid() // AA: do we need request_id?
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
const r = await this.send<ResponseIdentify>(message, ClassId.IDENTIFY_RESPONSE);
|
|
201
|
+
|
|
202
|
+
if (!(r.avatar_id && r.avatar_id.startsWith('http'))) {
|
|
203
|
+
r.avatar_id = AVATAR_DOMAIN + '/avatar/' + r.avatar_id
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return r;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public async coreChangeUsername(user_ext_id: string, public_username_custom: string): Promise<{ public_username_custom: string }> {
|
|
210
|
+
|
|
211
|
+
const message = this.buildMessage<any, any>(user_ext_id, ClassId.CLIENT_SET_CUSTOM_USERNAME_REQUEST, {
|
|
212
|
+
public_username_custom
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
return this.send(message, ClassId.CLIENT_SET_CUSTOM_USERNAME_RESPONSE);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
public async sawGetTemplates(user_ext_id: string): Promise<SAWGetTemplatesResponse> {
|
|
125
220
|
|
|
126
221
|
const message = this.buildMessage<SAWGetTemplatesResponse, SAWGetTemplatesRequest>(user_ext_id, ClassId.SAW_GET_SPINS_REQUEST);
|
|
127
222
|
|
|
@@ -146,7 +241,7 @@ class SmarticoAPI {
|
|
|
146
241
|
|
|
147
242
|
}
|
|
148
243
|
|
|
149
|
-
public
|
|
244
|
+
public async sawFormatTemplatesForWidget(templates: SAWTemplate[], pointsBalance: number): Promise<any[]> {
|
|
150
245
|
|
|
151
246
|
return templates.filter( r => r.saw_template_id >= 1).map( r => (
|
|
152
247
|
{
|
|
@@ -158,11 +253,7 @@ class SmarticoAPI {
|
|
|
158
253
|
jackpot: r.jackpot_current,
|
|
159
254
|
spin_count: r.spin_count,
|
|
160
255
|
buyin_cost_points: r.buyin_cost_points,
|
|
161
|
-
can_play: (
|
|
162
|
-
r.saw_buyin_type_id === SAWBuyInType.Free
|
|
163
|
-
|| (r.saw_buyin_type_id === SAWBuyInType.Points && r.buyin_cost_points <= pointsBalance)
|
|
164
|
-
|| (r.saw_buyin_type_id === SAWBuyInType.Spins && r.spin_count > 0)
|
|
165
|
-
),
|
|
256
|
+
can_play: SAWUtils.canPlay(r, pointsBalance),
|
|
166
257
|
icon:
|
|
167
258
|
r.saw_skin_ui_definition?.skin_folder
|
|
168
259
|
? r.saw_skin_ui_definition?.skin_folder + '/ico.png'
|
|
@@ -172,6 +263,48 @@ class SmarticoAPI {
|
|
|
172
263
|
|
|
173
264
|
}
|
|
174
265
|
|
|
266
|
+
public async sawSpinRequest(user_ext_id: string, saw_template_id: number, round_id: number): Promise<SAWDoSpinResponse> {
|
|
267
|
+
|
|
268
|
+
const message = this.buildMessage<SAWDoSpinRequest, SAWDoSpinResponse>(user_ext_id, ClassId.SAW_DO_SPIN_REQUEST, {
|
|
269
|
+
saw_template_id,
|
|
270
|
+
request_id: IntUtils.uuid()
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const spinAttemptResponse = await this.send<SAWDoSpinResponse>(message, ClassId.SAW_DO_SPIN_RESPONSE);
|
|
274
|
+
|
|
275
|
+
// to simulate fail
|
|
276
|
+
// response.errCode = SAWSpinErrorCode.SAW_NO_SPINS;
|
|
277
|
+
|
|
278
|
+
const status: string = {
|
|
279
|
+
[SAWSpinErrorCode.SAW_OK]: 'OK',
|
|
280
|
+
[SAWSpinErrorCode.SAW_NO_SPINS]: 'NO SPINS AVAILABLE',
|
|
281
|
+
[SAWSpinErrorCode.SAW_PRIZE_POOL_EMPTY]: 'PRIZE POOL IS EMPTY',
|
|
282
|
+
[SAWSpinErrorCode.SAW_NOT_ENOUGH_POINTS]: 'NOT ENOUGH POINTS',
|
|
283
|
+
[SAWSpinErrorCode.SAW_FAILED_MAX_SPINS_REACHED]: 'MAX SPIN ATTEMPTS REACHED',
|
|
284
|
+
}[spinAttemptResponse.errCode] || 'OTHER';
|
|
285
|
+
|
|
286
|
+
await this.coreReportCustomEvent(user_ext_id, 'minigame_attempt', {
|
|
287
|
+
saw_template_id,
|
|
288
|
+
status,
|
|
289
|
+
round_id,
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
return spinAttemptResponse;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
public async inboxGetMessages(user_ext_id: string, limit: number = 10, offset: number = 0): Promise<GetInboxMessagesResponse> {
|
|
296
|
+
|
|
297
|
+
const message = this.buildMessage<GetInboxMessagesRequest, GetInboxMessagesResponse>(user_ext_id, ClassId.GET_ACTIVITY_LOG_REQUEST, {
|
|
298
|
+
limit,
|
|
299
|
+
offset
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
const response = await this.send<GetInboxMessagesResponse>(message);
|
|
303
|
+
|
|
304
|
+
return response;
|
|
305
|
+
|
|
306
|
+
}
|
|
307
|
+
|
|
175
308
|
}
|
|
176
309
|
|
|
177
310
|
export { SmarticoAPI }
|
package/src/index.ts
CHANGED