@pixels-online/pixels-client-js-sdk 1.18.0 → 1.19.0

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.
@@ -634,25 +634,33 @@
634
634
  }
635
635
 
636
636
  class OfferStore {
637
- constructor(config) {
637
+ constructor(config, client) {
638
+ this.client = client;
638
639
  this.offers = new Map();
639
- this.player = null;
640
+ this.players = new Map();
640
641
  this.logger = createLogger(config, 'OfferStore');
641
642
  }
642
- getPlayer() {
643
- return this.player || null;
643
+ getPlayer(targetId = this.client.getSelfId()) {
644
+ if (!targetId)
645
+ return null;
646
+ return this.players.get(targetId) || null;
644
647
  }
645
648
  setPlayer(player) {
646
- this.player = player;
649
+ this.players.set(player.snapshot.playerId, player);
647
650
  this.logger.log('Updated player:', player);
648
651
  }
649
652
  /**
650
653
  * Set all offers (replaces existing)
651
654
  */
652
- setOffers(offers) {
653
- this.offers.clear();
655
+ setOffers(offers, target) {
656
+ const targetPlayer = target || this.getPlayer();
657
+ if (!targetPlayer) {
658
+ this.logger.warn('No target player to set offers for');
659
+ return;
660
+ }
661
+ this.offers.set(targetPlayer.snapshot.playerId, new Map());
654
662
  offers.forEach((offer) => {
655
- this.offers.set(offer.instanceId, offer);
663
+ this.offers.get(targetPlayer.snapshot.playerId).set(offer.instanceId, offer);
656
664
  });
657
665
  this.logger.log(`Set ${offers.length} offers`);
658
666
  }
@@ -660,16 +668,23 @@
660
668
  * Add or update a single offer
661
669
  */
662
670
  upsertOffer(offer) {
663
- const previousOffer = this.offers.get(offer.instanceId);
664
- this.offers.set(offer.instanceId, offer);
671
+ let playerOffers = this.offers.get(offer.playerId);
672
+ if (!playerOffers) {
673
+ playerOffers = new Map();
674
+ this.offers.set(offer.playerId, playerOffers);
675
+ }
676
+ const previousOffer = playerOffers.get(offer.instanceId);
677
+ playerOffers.set(offer.instanceId, offer);
665
678
  this.logger.log(`${previousOffer ? 'Updated' : 'Added'} offer:`, offer.instanceId);
666
679
  return previousOffer;
667
680
  }
668
681
  /**
669
682
  * Remove an offer
670
683
  */
671
- removeOffer(offerId) {
672
- const removed = this.offers.delete(offerId);
684
+ removeOffer(offerId, targetId = this.client.getSelfId()) {
685
+ if (!targetId)
686
+ return false;
687
+ const removed = this.offers.get(targetId)?.delete(offerId) || false;
673
688
  if (removed) {
674
689
  this.logger.log(`Removed offer:`, offerId);
675
690
  }
@@ -678,26 +693,30 @@
678
693
  /**
679
694
  * Get a single offer
680
695
  */
681
- getOffer(offerId) {
682
- return this.offers.get(offerId);
696
+ getOffer(offerId, targetId = this.client.getSelfId()) {
697
+ if (!targetId)
698
+ return undefined;
699
+ return this.offers.get(targetId)?.get(offerId);
683
700
  }
684
701
  /**
685
702
  * Get all offers
686
703
  */
687
- getAllOffers() {
688
- return Array.from(this.offers.values());
704
+ getAllOffers(targetId = this.client.getSelfId()) {
705
+ if (!targetId)
706
+ return [];
707
+ return Array.from(this.offers.get(targetId)?.values() || []);
689
708
  }
690
709
  /**
691
710
  * Get offers filtered by status
692
711
  */
693
- getOffersByStatus(status) {
694
- return this.getAllOffers().filter((offer) => offer.status === status);
712
+ getOffersByStatus(status, targetId = this.client.getSelfId()) {
713
+ return this.getAllOffers(targetId).filter((offer) => offer.status === status);
695
714
  }
696
715
  /**
697
716
  * Get active offers (not expired, not claimed)
698
717
  */
699
- getActiveOffers() {
700
- return this.getAllOffers().filter((offer) => {
718
+ getActiveOffers(targetId = this.client.getSelfId()) {
719
+ return this.getAllOffers(targetId).filter((offer) => {
701
720
  if (!offer.status)
702
721
  return false; // Must have a status
703
722
  return (offer.status === 'surfaced' ||
@@ -708,14 +727,14 @@
708
727
  /**
709
728
  * Get claimable offers
710
729
  */
711
- getClaimableOffers() {
712
- return this.getOffersByStatus('claimable');
730
+ getClaimableOffers(targetId = this.client.getSelfId()) {
731
+ return this.getOffersByStatus('claimable', targetId);
713
732
  }
714
733
  /**
715
734
  * Check if an offer has expired
716
735
  */
717
- isOfferExpired(offerId) {
718
- const offer = this.getOffer(offerId);
736
+ isOfferExpired(offerId, targetId = this.client.getSelfId()) {
737
+ const offer = this.getOffer(offerId, targetId);
719
738
  if (!offer)
720
739
  return true;
721
740
  // Check status
@@ -728,17 +747,29 @@
728
747
  return false;
729
748
  }
730
749
  /**
731
- * Clear all offers
750
+ * Clear all offers for a specific player
732
751
  */
733
- clear() {
752
+ clear(targetId = this.client.getSelfId()) {
753
+ if (!targetId)
754
+ return;
755
+ this.offers.set(targetId, new Map());
756
+ this.logger.log('Cleared all offers for player:', targetId);
757
+ }
758
+ /**
759
+ * Clear all offers for all players
760
+ */
761
+ clearAll() {
734
762
  this.offers.clear();
735
- this.logger.log('Cleared all offers');
763
+ this.logger.log('Cleared all offers for all players');
736
764
  }
737
765
  /**
738
- * Get offer count
766
+ * Get offer count (for self player)
739
767
  */
740
768
  get size() {
741
- return this.offers.size;
769
+ const selfId = this.client.getSelfId();
770
+ if (!selfId)
771
+ return 0;
772
+ return this.offers.get(selfId)?.size || 0;
742
773
  }
743
774
  }
744
775
 
@@ -841,7 +872,9 @@
841
872
  return null;
842
873
  }
843
874
  setCurrencyAssetContents(currencies) {
844
- this.currencies = currencies;
875
+ Object.keys(currencies).forEach((key) => {
876
+ this.currencies[key] = currencies[key];
877
+ });
845
878
  }
846
879
  resolveReward(reward) {
847
880
  if (reward.kind === 'loyalty_currency' && reward.rewardId) {
@@ -897,6 +930,7 @@
897
930
  constructor(config) {
898
931
  this.isInitializing = false;
899
932
  this.pendingStackedToken = null;
933
+ this.selfId = null;
900
934
  this.config = {
901
935
  autoConnect: config.autoConnect ?? false,
902
936
  reconnect: config.reconnect ?? true,
@@ -909,7 +943,7 @@
909
943
  this.hooks = this.config.hooks || {};
910
944
  this.logger = createLogger(this.config, 'OfferwallClient');
911
945
  this.eventEmitter = new EventEmitter(this.config);
912
- this.offerStore = new OfferStore(this.config);
946
+ this.offerStore = new OfferStore(this.config, this);
913
947
  this.tokenManager = new TokenManager(this.config);
914
948
  this.assetHelper = new AssetHelper(this.config);
915
949
  this.sseConnection = new SSEConnection(this.config, this.eventEmitter, this.tokenManager);
@@ -950,6 +984,9 @@
950
984
  get assets() {
951
985
  return this.assetHelper;
952
986
  }
987
+ getSelfId() {
988
+ return this.selfId;
989
+ }
953
990
  /**
954
991
  * Initialize the offerwall client and connect
955
992
  */
@@ -1012,8 +1049,9 @@
1012
1049
  if (this.sseConnection) {
1013
1050
  this.sseConnection.disconnect();
1014
1051
  }
1015
- this.offerStore.clear();
1052
+ this.offerStore.clearAll();
1016
1053
  this.tokenManager.clearToken();
1054
+ this.selfId = null;
1017
1055
  if (this.hooks.afterDisconnect) {
1018
1056
  await this.hooks.afterDisconnect();
1019
1057
  }
@@ -1021,8 +1059,8 @@
1021
1059
  /**
1022
1060
  * Claim rewards for an offer
1023
1061
  */
1024
- async claimReward(instanceId) {
1025
- const offer = this.offerStore.getOffer(instanceId);
1062
+ async claimReward(instanceId, targetId = this.getSelfId()) {
1063
+ const offer = this.offerStore.getOffer(instanceId, targetId);
1026
1064
  if (!offer) {
1027
1065
  throw new Error(`Offer ${instanceId} not found`);
1028
1066
  }
@@ -1037,7 +1075,7 @@
1037
1075
  }
1038
1076
  }
1039
1077
  try {
1040
- const response = await this.claimOfferAPI(instanceId);
1078
+ const response = await this.claimOfferAPI(instanceId, targetId);
1041
1079
  const updatedOffer = { ...offer, status: 'claimed' };
1042
1080
  this.offerStore.upsertOffer(updatedOffer);
1043
1081
  this.eventEmitter.emit(exports.OfferEvent.OFFER_CLAIMED, {
@@ -1093,7 +1131,7 @@
1093
1131
  });
1094
1132
  */
1095
1133
  this.eventEmitter.on(exports.OfferEvent.OFFER_SURFACED, ({ offer }) => {
1096
- this.offerStore.upsertOffer(offer);
1134
+ this.offerStore.upsertOffer(offer); // should always be selfId
1097
1135
  this.logger.log(`Surfaced offer: ${offer.instanceId}`);
1098
1136
  });
1099
1137
  }
@@ -1123,23 +1161,27 @@
1123
1161
  }
1124
1162
  return response.json();
1125
1163
  }
1126
- async claimOfferAPI(instanceId) {
1164
+ async claimOfferAPI(instanceId, targetId = null) {
1127
1165
  return this.postWithAuth('/v1/client/reward/claim', {
1128
1166
  instanceId,
1129
1167
  kind: 'offer',
1168
+ targetId: targetId || undefined,
1130
1169
  });
1131
1170
  }
1132
- getPlayer() {
1133
- return this.offerStore.getPlayer();
1171
+ getPlayer(targetId = this.getSelfId()) {
1172
+ return this.offerStore.getPlayer(targetId);
1134
1173
  }
1135
- getOffers() {
1136
- return this.offerStore.getAllOffers();
1174
+ getOffers(targetId = this.getSelfId()) {
1175
+ return this.offerStore.getAllOffers(targetId);
1137
1176
  }
1138
- async refreshOffersAndPlayer() {
1177
+ async refreshOffersAndPlayer(targetId = null) {
1139
1178
  try {
1140
- const { offers, player } = await this.getOffersAndPlayer();
1141
- this.offerStore.setOffers(offers);
1179
+ const { offers, player } = await this.getOffersAndPlayer(targetId);
1180
+ if (targetId == null) {
1181
+ this.selfId = player.snapshot.playerId;
1182
+ }
1142
1183
  this.offerStore.setPlayer(player);
1184
+ this.offerStore.setOffers(offers, player);
1143
1185
  this.eventEmitter.emit(exports.OfferEvent.REFRESH, { offers, player: player });
1144
1186
  this.logger.log('Refreshed offers and player snapshot');
1145
1187
  }
@@ -1148,9 +1190,10 @@
1148
1190
  throw error;
1149
1191
  }
1150
1192
  }
1151
- async getOffersAndPlayer() {
1193
+ async getOffersAndPlayer(targetId = null) {
1152
1194
  const data = await this.postWithAuth('/v1/client/player/campaigns', {
1153
1195
  viewingCampaigns: true,
1196
+ targetId: targetId || undefined,
1154
1197
  });
1155
1198
  if (!data.offers || !Array.isArray(data.offers)) {
1156
1199
  throw new Error('No offers returned from offers endpoint');