@smartico/public-api 0.0.69 → 0.0.71

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.
@@ -263,8 +263,8 @@ const SAWTemplatesTransform = items => {
263
263
  over_limit_message: r.saw_template_ui_definition.over_limit_message,
264
264
  no_attempts_message: r.saw_template_ui_definition.no_attempts_message,
265
265
  jackpot_symbol: r.saw_template_ui_definition.jackpot_symbol,
266
- saw_game_type: SAWGameTypeNamed[r.saw_game_type_id],
267
- saw_buyin_type: SAWBuyInTypeNamed[r.saw_buyin_type_id],
266
+ saw_game_type: SAWGameTypeNamed(r.saw_game_type_id),
267
+ saw_buyin_type: SAWBuyInTypeNamed(r.saw_buyin_type_id),
268
268
  buyin_cost_points: r.buyin_cost_points,
269
269
  jackpot_add_on_attempt: r.jackpot_add_on_attempt,
270
270
  jackpot_current: r.jackpot_current,
@@ -358,17 +358,22 @@ var ECacheContext;
358
358
  (function (ECacheContext) {
359
359
  ECacheContext[ECacheContext["Translations"] = 0] = "Translations";
360
360
  ECacheContext[ECacheContext["LabelInfo"] = 1] = "LabelInfo";
361
+ ECacheContext[ECacheContext["WSAPI"] = 2] = "WSAPI";
361
362
  })(ECacheContext || (ECacheContext = {}));
362
363
  class OCache {
363
- static get(oKey, cacheContext) {
364
- const key = cacheContext.toString() + '_' + JSON.stringify(oKey);
364
+ static init(cacheContext) {
365
365
  if (this.cache[cacheContext] === undefined) {
366
366
  this.cache[cacheContext] = new NodeCache();
367
367
  }
368
+ }
369
+ static get(oKey, cacheContext) {
370
+ const key = cacheContext.toString() + '_' + JSON.stringify(oKey);
371
+ this.init(cacheContext);
368
372
  return this.cache[cacheContext].get(key);
369
373
  }
370
374
  static set(oKey, o, cacheContext, ttlSeconds = 60) {
371
375
  const key = cacheContext.toString() + '_' + JSON.stringify(oKey);
376
+ this.init(cacheContext);
372
377
  this.cache[cacheContext].set(key, o, ttlSeconds);
373
378
  }
374
379
  static async use(oKey, cacheContext, f, ttlSeconds = 60) {
@@ -774,12 +779,27 @@ const GetLevelMapResponseTransform = levels => {
774
779
  }));
775
780
  };
776
781
 
782
+ /** @hidden */
783
+ const CACHE_DATA_SEC = 30;
784
+ /** @hidden */
785
+ var onUpdateContextKey;
786
+ (function (onUpdateContextKey) {
787
+ onUpdateContextKey["Saw"] = "saw";
788
+ onUpdateContextKey["Missions"] = "missions";
789
+ onUpdateContextKey["TournamentList"] = "tournamentList";
790
+ })(onUpdateContextKey || (onUpdateContextKey = {}));
777
791
  /** @group General API */
778
792
  class WSAPI {
779
793
  /** @private */
780
794
  constructor(api) {
781
795
  this.api = void 0;
796
+ this.onUpdateCallback = new Map();
782
797
  this.api = api;
798
+ const on = this.api.tracker.on;
799
+ on(ClassId.SAW_SPINS_COUNT_PUSH, data => this.updateOnSpin(data));
800
+ on(ClassId.SAW_DO_SPIN_RESPONSE, data => on(ClassId.SAW_AKNOWLEDGE_RESPONSE, () => this.updateOnPrizeWin(data)));
801
+ on(ClassId.MISSION_OPTIN_RESPONSE, () => this.updateMissionsOnOptIn());
802
+ on(ClassId.TOURNAMENT_REGISTER_RESPONSE, () => this.updateTournamentsOnRegistration());
783
803
  }
784
804
  /** Returns information about current user */
785
805
  getUserProfile() {
@@ -795,9 +815,16 @@ class WSAPI {
795
815
  async getLevels() {
796
816
  return this.api.levelsGetT(null);
797
817
  }
798
- /** Returns all the missions available the current user */
799
- async getMissions() {
800
- return this.api.missionsGetItemsT(null);
818
+ /** Returns all the missions available the current user.
819
+ * The returned missions is cached for 30 seconds. But you can pass the onUpdate callback as a parameter. Note that each time you call getMissions with a new onUpdate callback, the old one will be overwritten by the new one.
820
+ * The onUpdate callback will be called on mission OptIn and the updated missions will be passed to it. */
821
+ async getMissions({
822
+ onUpdate
823
+ } = {}) {
824
+ if (onUpdate) {
825
+ this.onUpdateCallback.set(onUpdateContextKey.Missions, onUpdate);
826
+ }
827
+ return OCache.use(onUpdateContextKey.Missions, ECacheContext.WSAPI, () => this.api.missionsGetItemsT(null), CACHE_DATA_SEC);
801
828
  }
802
829
  /** Returns all the badges available the current user */
803
830
  async getBadges() {
@@ -811,13 +838,21 @@ class WSAPI {
811
838
  async getStoreCategories() {
812
839
  return this.api.storeGetItemsT(null);
813
840
  }
814
- /** Returns the list of mini-games available for user */
815
- async getMiniGames() {
816
- return this.api.sawGetTemplatesT(null);
841
+ /** Returns the list of mini-games available for user
842
+ * The returned list of mini-games is cached for 30 seconds. But you can pass the onUpdate callback as a parameter. Note that each time you call getMiniGames with a new onUpdate callback, the old one will be overwritten by the new one.
843
+ * The onUpdate callback will be called on available spin count change, if mini-game has increasing jackpot per spin or wined prize is spin/jackpot and if max count of the available user spin equal one. Updated templates will be passed to onUpdate callback. */
844
+ async getMiniGames({
845
+ onUpdate
846
+ } = {}) {
847
+ if (onUpdate) {
848
+ this.onUpdateCallback.set(onUpdateContextKey.Saw, onUpdate);
849
+ }
850
+ return OCache.use(onUpdateContextKey.Saw, ECacheContext.WSAPI, () => this.api.sawGetTemplatesT(null), CACHE_DATA_SEC);
817
851
  }
818
852
  /** Plays the specified by template_id mini-game on behalf of user and returns prize_id or err_code */
819
853
  async playMiniGame(template_id) {
820
854
  const r = await this.api.sawSpinRequest(null, template_id);
855
+ this.api.doAcknowledgeRequest(null, r.request_id);
821
856
  const o = {
822
857
  err_code: r.errCode,
823
858
  err_message: r.errMsg,
@@ -825,14 +860,54 @@ class WSAPI {
825
860
  };
826
861
  return o;
827
862
  }
828
- /** Returns all the active instances of tournaments */
829
- async getTournamentsList() {
830
- return this.api.tournamentsGetLobbyT(null);
863
+ /** Returns all the active instances of tournaments
864
+ * The returned list is cached for 30 seconds. But you can pass the onUpdate callback as a parameter. Note that each time you call getTournamentsList with a new onUpdate callback, the old one will be overwritten by the new one.
865
+ * The onUpdate callback will be called when the user has registered in a tournament. Updated list will be passed to onUpdate callback.*/
866
+ async getTournamentsList({
867
+ onUpdate
868
+ } = {}) {
869
+ if (onUpdate) {
870
+ this.onUpdateCallback.set(onUpdateContextKey.TournamentList, onUpdate);
871
+ }
872
+ return OCache.use(onUpdateContextKey.TournamentList, ECacheContext.WSAPI, () => this.api.tournamentsGetLobbyT(null), CACHE_DATA_SEC);
831
873
  }
832
874
  /** Returns details information of specific tournament instance, the response will includ tournamnet info and the leaderboard of players */
833
875
  async getTournamentInstanceInfo(tournamentInstanceId) {
834
876
  return this.api.tournamentsGetInfoT(null, tournamentInstanceId);
835
877
  }
878
+ async updateOnSpin(data) {
879
+ const templates = await OCache.use(onUpdateContextKey.Saw, ECacheContext.WSAPI, () => this.api.sawGetTemplatesT(null), CACHE_DATA_SEC);
880
+ const index = templates.findIndex(t => t.id === data.saw_template_id);
881
+ templates[index].spin_count = data.spin_count;
882
+ this.updateEntity(onUpdateContextKey.Saw, templates);
883
+ }
884
+ async updateOnPrizeWin(data) {
885
+ if (data.errCode === SAWSpinErrorCode.SAW_OK) {
886
+ var _template$prizes$find;
887
+ const templates = await OCache.use(onUpdateContextKey.Saw, ECacheContext.WSAPI, () => this.api.sawGetTemplatesT(null), CACHE_DATA_SEC);
888
+ const template = templates.find(t => t.prizes.find(p => p.id === data.saw_prize_id));
889
+ const prizeType = (_template$prizes$find = template.prizes.find(p => p.id === data.saw_prize_id)) == null ? void 0 : _template$prizes$find.prize_type;
890
+ if (template.jackpot_add_on_attempt || template.spin_count === 1 || prizeType === MiniGamePrizeTypeName.JACKPOT || prizeType === MiniGamePrizeTypeName.SPIN) {
891
+ const updatedTemplates = await this.api.sawGetTemplatesT(null);
892
+ this.updateEntity(onUpdateContextKey.Saw, updatedTemplates);
893
+ }
894
+ }
895
+ }
896
+ async updateMissionsOnOptIn() {
897
+ const payload = await this.api.missionsGetItemsT(null);
898
+ this.updateEntity(onUpdateContextKey.Missions, payload);
899
+ }
900
+ async updateTournamentsOnRegistration() {
901
+ const payload = await this.api.tournamentsGetLobbyT(null);
902
+ this.updateEntity(onUpdateContextKey.TournamentList, payload);
903
+ }
904
+ async updateEntity(contextKey, payload) {
905
+ OCache.set(contextKey, payload, ECacheContext.WSAPI);
906
+ const onUpdate = this.onUpdateCallback.get(contextKey);
907
+ if (onUpdate) {
908
+ onUpdate(payload);
909
+ }
910
+ }
836
911
  }
837
912
 
838
913
  const PUBLIC_API_URL = 'https://papi{ENV_ID}.smartico.ai/services/public';
@@ -1052,10 +1127,17 @@ class SmarticoAPI {
1052
1127
  async sawGetTemplatesT(user_ext_id) {
1053
1128
  return SAWTemplatesTransform((await this.sawGetTemplates(user_ext_id)).templates);
1054
1129
  }
1130
+ async doAcknowledgeRequest(user_ext_id, request_id) {
1131
+ const message = this.buildMessage(user_ext_id, ClassId.SAW_AKNOWLEDGE_REQUEST, {
1132
+ request_id
1133
+ });
1134
+ return await this.send(message, ClassId.SAW_AKNOWLEDGE_RESPONSE);
1135
+ }
1055
1136
  async sawSpinRequest(user_ext_id, saw_template_id, round_id) {
1137
+ const request_id = IntUtils.uuid();
1056
1138
  const message = this.buildMessage(user_ext_id, ClassId.SAW_DO_SPIN_REQUEST, {
1057
1139
  saw_template_id,
1058
- request_id: IntUtils.uuid()
1140
+ request_id
1059
1141
  });
1060
1142
  const spinAttemptResponse = await this.send(message, ClassId.SAW_DO_SPIN_RESPONSE);
1061
1143
  // to simulate fail
@@ -1072,7 +1154,9 @@ class SmarticoAPI {
1072
1154
  status,
1073
1155
  round_id
1074
1156
  });
1075
- return spinAttemptResponse;
1157
+ return _extends({}, spinAttemptResponse, {
1158
+ request_id
1159
+ });
1076
1160
  }
1077
1161
  async inboxGetMessages(user_ext_id, limit = 10, offset = 0) {
1078
1162
  const message = this.buildMessage(user_ext_id, ClassId.GET_INBOX_MESSAGES_REQUEST, {