@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.
- package/dist/core/OfferStore.d.ts +20 -14
- package/dist/core/OfferwallClient.d.ts +6 -4
- package/dist/index.esm.js +88 -45
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +88 -45
- package/dist/index.js.map +1 -1
- package/dist/offerwall-sdk.umd.js +88 -45
- package/dist/offerwall-sdk.umd.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { OfferwallConfig } from '../types';
|
|
2
2
|
import { IClientOffer, PlayerOfferStatus } from '../types/offer';
|
|
3
3
|
import { IClientPlayer } from '../types/player';
|
|
4
|
+
import { OfferwallClient } from './OfferwallClient';
|
|
4
5
|
export declare class OfferStore {
|
|
6
|
+
private client;
|
|
5
7
|
private offers;
|
|
6
|
-
private
|
|
8
|
+
private players;
|
|
7
9
|
private logger;
|
|
8
|
-
constructor(config: OfferwallConfig);
|
|
9
|
-
getPlayer(): IClientPlayer | null;
|
|
10
|
+
constructor(config: OfferwallConfig, client: OfferwallClient);
|
|
11
|
+
getPlayer(targetId?: string | null): IClientPlayer | null;
|
|
10
12
|
setPlayer(player: IClientPlayer): void;
|
|
11
13
|
/**
|
|
12
14
|
* Set all offers (replaces existing)
|
|
13
15
|
*/
|
|
14
|
-
setOffers(offers: IClientOffer[]): void;
|
|
16
|
+
setOffers(offers: IClientOffer[], target?: IClientPlayer): void;
|
|
15
17
|
/**
|
|
16
18
|
* Add or update a single offer
|
|
17
19
|
*/
|
|
@@ -19,37 +21,41 @@ export declare class OfferStore {
|
|
|
19
21
|
/**
|
|
20
22
|
* Remove an offer
|
|
21
23
|
*/
|
|
22
|
-
removeOffer(offerId: string): boolean;
|
|
24
|
+
removeOffer(offerId: string, targetId?: string | null): boolean;
|
|
23
25
|
/**
|
|
24
26
|
* Get a single offer
|
|
25
27
|
*/
|
|
26
|
-
getOffer(offerId: string): IClientOffer | undefined;
|
|
28
|
+
getOffer(offerId: string, targetId?: string | null): IClientOffer | undefined;
|
|
27
29
|
/**
|
|
28
30
|
* Get all offers
|
|
29
31
|
*/
|
|
30
|
-
getAllOffers(): IClientOffer[];
|
|
32
|
+
getAllOffers(targetId?: string | null): IClientOffer[];
|
|
31
33
|
/**
|
|
32
34
|
* Get offers filtered by status
|
|
33
35
|
*/
|
|
34
|
-
getOffersByStatus(status: PlayerOfferStatus): IClientOffer[];
|
|
36
|
+
getOffersByStatus(status: PlayerOfferStatus, targetId?: string | null): IClientOffer[];
|
|
35
37
|
/**
|
|
36
38
|
* Get active offers (not expired, not claimed)
|
|
37
39
|
*/
|
|
38
|
-
getActiveOffers(): IClientOffer[];
|
|
40
|
+
getActiveOffers(targetId?: string | null): IClientOffer[];
|
|
39
41
|
/**
|
|
40
42
|
* Get claimable offers
|
|
41
43
|
*/
|
|
42
|
-
getClaimableOffers(): IClientOffer[];
|
|
44
|
+
getClaimableOffers(targetId?: string | null): IClientOffer[];
|
|
43
45
|
/**
|
|
44
46
|
* Check if an offer has expired
|
|
45
47
|
*/
|
|
46
|
-
isOfferExpired(offerId: string): boolean;
|
|
48
|
+
isOfferExpired(offerId: string, targetId?: string | null): boolean;
|
|
47
49
|
/**
|
|
48
|
-
* Clear all offers
|
|
50
|
+
* Clear all offers for a specific player
|
|
49
51
|
*/
|
|
50
|
-
clear(): void;
|
|
52
|
+
clear(targetId?: string | null): void;
|
|
51
53
|
/**
|
|
52
|
-
*
|
|
54
|
+
* Clear all offers for all players
|
|
55
|
+
*/
|
|
56
|
+
clearAll(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Get offer count (for self player)
|
|
53
59
|
*/
|
|
54
60
|
get size(): number;
|
|
55
61
|
}
|
|
@@ -19,6 +19,7 @@ export declare class OfferwallClient {
|
|
|
19
19
|
private logger;
|
|
20
20
|
private isInitializing;
|
|
21
21
|
private pendingStackedToken;
|
|
22
|
+
private selfId;
|
|
22
23
|
constructor(config: OfferwallConfig);
|
|
23
24
|
private setupStackedLinkDetection;
|
|
24
25
|
/**
|
|
@@ -33,6 +34,7 @@ export declare class OfferwallClient {
|
|
|
33
34
|
* Get asset helper for resolving offer/reward content
|
|
34
35
|
*/
|
|
35
36
|
get assets(): AssetHelper;
|
|
37
|
+
getSelfId(): string | null;
|
|
36
38
|
/**
|
|
37
39
|
* Initialize the offerwall client and connect
|
|
38
40
|
*/
|
|
@@ -48,7 +50,7 @@ export declare class OfferwallClient {
|
|
|
48
50
|
/**
|
|
49
51
|
* Claim rewards for an offer
|
|
50
52
|
*/
|
|
51
|
-
claimReward(instanceId: string): Promise<void>;
|
|
53
|
+
claimReward(instanceId: string, targetId?: string | null): Promise<void>;
|
|
52
54
|
/**
|
|
53
55
|
* Check if connected
|
|
54
56
|
*/
|
|
@@ -63,9 +65,9 @@ export declare class OfferwallClient {
|
|
|
63
65
|
*/
|
|
64
66
|
private postWithAuth;
|
|
65
67
|
private claimOfferAPI;
|
|
66
|
-
getPlayer(): IClientPlayer | null;
|
|
67
|
-
getOffers(): IClientOffer[];
|
|
68
|
-
refreshOffersAndPlayer(): Promise<void>;
|
|
68
|
+
getPlayer(targetId?: string | null): IClientPlayer | null;
|
|
69
|
+
getOffers(targetId?: string | null): IClientOffer[];
|
|
70
|
+
refreshOffersAndPlayer(targetId?: string | null): Promise<void>;
|
|
69
71
|
private getOffersAndPlayer;
|
|
70
72
|
getAuthLinkToken(): Promise<string>;
|
|
71
73
|
getGameId(): string | null;
|
package/dist/index.esm.js
CHANGED
|
@@ -628,25 +628,33 @@ class SSEConnection {
|
|
|
628
628
|
}
|
|
629
629
|
|
|
630
630
|
class OfferStore {
|
|
631
|
-
constructor(config) {
|
|
631
|
+
constructor(config, client) {
|
|
632
|
+
this.client = client;
|
|
632
633
|
this.offers = new Map();
|
|
633
|
-
this.
|
|
634
|
+
this.players = new Map();
|
|
634
635
|
this.logger = createLogger(config, 'OfferStore');
|
|
635
636
|
}
|
|
636
|
-
getPlayer() {
|
|
637
|
-
|
|
637
|
+
getPlayer(targetId = this.client.getSelfId()) {
|
|
638
|
+
if (!targetId)
|
|
639
|
+
return null;
|
|
640
|
+
return this.players.get(targetId) || null;
|
|
638
641
|
}
|
|
639
642
|
setPlayer(player) {
|
|
640
|
-
this.player
|
|
643
|
+
this.players.set(player.snapshot.playerId, player);
|
|
641
644
|
this.logger.log('Updated player:', player);
|
|
642
645
|
}
|
|
643
646
|
/**
|
|
644
647
|
* Set all offers (replaces existing)
|
|
645
648
|
*/
|
|
646
|
-
setOffers(offers) {
|
|
647
|
-
this.
|
|
649
|
+
setOffers(offers, target) {
|
|
650
|
+
const targetPlayer = target || this.getPlayer();
|
|
651
|
+
if (!targetPlayer) {
|
|
652
|
+
this.logger.warn('No target player to set offers for');
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
this.offers.set(targetPlayer.snapshot.playerId, new Map());
|
|
648
656
|
offers.forEach((offer) => {
|
|
649
|
-
this.offers.set(offer.instanceId, offer);
|
|
657
|
+
this.offers.get(targetPlayer.snapshot.playerId).set(offer.instanceId, offer);
|
|
650
658
|
});
|
|
651
659
|
this.logger.log(`Set ${offers.length} offers`);
|
|
652
660
|
}
|
|
@@ -654,16 +662,23 @@ class OfferStore {
|
|
|
654
662
|
* Add or update a single offer
|
|
655
663
|
*/
|
|
656
664
|
upsertOffer(offer) {
|
|
657
|
-
|
|
658
|
-
|
|
665
|
+
let playerOffers = this.offers.get(offer.playerId);
|
|
666
|
+
if (!playerOffers) {
|
|
667
|
+
playerOffers = new Map();
|
|
668
|
+
this.offers.set(offer.playerId, playerOffers);
|
|
669
|
+
}
|
|
670
|
+
const previousOffer = playerOffers.get(offer.instanceId);
|
|
671
|
+
playerOffers.set(offer.instanceId, offer);
|
|
659
672
|
this.logger.log(`${previousOffer ? 'Updated' : 'Added'} offer:`, offer.instanceId);
|
|
660
673
|
return previousOffer;
|
|
661
674
|
}
|
|
662
675
|
/**
|
|
663
676
|
* Remove an offer
|
|
664
677
|
*/
|
|
665
|
-
removeOffer(offerId) {
|
|
666
|
-
|
|
678
|
+
removeOffer(offerId, targetId = this.client.getSelfId()) {
|
|
679
|
+
if (!targetId)
|
|
680
|
+
return false;
|
|
681
|
+
const removed = this.offers.get(targetId)?.delete(offerId) || false;
|
|
667
682
|
if (removed) {
|
|
668
683
|
this.logger.log(`Removed offer:`, offerId);
|
|
669
684
|
}
|
|
@@ -672,26 +687,30 @@ class OfferStore {
|
|
|
672
687
|
/**
|
|
673
688
|
* Get a single offer
|
|
674
689
|
*/
|
|
675
|
-
getOffer(offerId) {
|
|
676
|
-
|
|
690
|
+
getOffer(offerId, targetId = this.client.getSelfId()) {
|
|
691
|
+
if (!targetId)
|
|
692
|
+
return undefined;
|
|
693
|
+
return this.offers.get(targetId)?.get(offerId);
|
|
677
694
|
}
|
|
678
695
|
/**
|
|
679
696
|
* Get all offers
|
|
680
697
|
*/
|
|
681
|
-
getAllOffers() {
|
|
682
|
-
|
|
698
|
+
getAllOffers(targetId = this.client.getSelfId()) {
|
|
699
|
+
if (!targetId)
|
|
700
|
+
return [];
|
|
701
|
+
return Array.from(this.offers.get(targetId)?.values() || []);
|
|
683
702
|
}
|
|
684
703
|
/**
|
|
685
704
|
* Get offers filtered by status
|
|
686
705
|
*/
|
|
687
|
-
getOffersByStatus(status) {
|
|
688
|
-
return this.getAllOffers().filter((offer) => offer.status === status);
|
|
706
|
+
getOffersByStatus(status, targetId = this.client.getSelfId()) {
|
|
707
|
+
return this.getAllOffers(targetId).filter((offer) => offer.status === status);
|
|
689
708
|
}
|
|
690
709
|
/**
|
|
691
710
|
* Get active offers (not expired, not claimed)
|
|
692
711
|
*/
|
|
693
|
-
getActiveOffers() {
|
|
694
|
-
return this.getAllOffers().filter((offer) => {
|
|
712
|
+
getActiveOffers(targetId = this.client.getSelfId()) {
|
|
713
|
+
return this.getAllOffers(targetId).filter((offer) => {
|
|
695
714
|
if (!offer.status)
|
|
696
715
|
return false; // Must have a status
|
|
697
716
|
return (offer.status === 'surfaced' ||
|
|
@@ -702,14 +721,14 @@ class OfferStore {
|
|
|
702
721
|
/**
|
|
703
722
|
* Get claimable offers
|
|
704
723
|
*/
|
|
705
|
-
getClaimableOffers() {
|
|
706
|
-
return this.getOffersByStatus('claimable');
|
|
724
|
+
getClaimableOffers(targetId = this.client.getSelfId()) {
|
|
725
|
+
return this.getOffersByStatus('claimable', targetId);
|
|
707
726
|
}
|
|
708
727
|
/**
|
|
709
728
|
* Check if an offer has expired
|
|
710
729
|
*/
|
|
711
|
-
isOfferExpired(offerId) {
|
|
712
|
-
const offer = this.getOffer(offerId);
|
|
730
|
+
isOfferExpired(offerId, targetId = this.client.getSelfId()) {
|
|
731
|
+
const offer = this.getOffer(offerId, targetId);
|
|
713
732
|
if (!offer)
|
|
714
733
|
return true;
|
|
715
734
|
// Check status
|
|
@@ -722,17 +741,29 @@ class OfferStore {
|
|
|
722
741
|
return false;
|
|
723
742
|
}
|
|
724
743
|
/**
|
|
725
|
-
* Clear all offers
|
|
744
|
+
* Clear all offers for a specific player
|
|
726
745
|
*/
|
|
727
|
-
clear() {
|
|
746
|
+
clear(targetId = this.client.getSelfId()) {
|
|
747
|
+
if (!targetId)
|
|
748
|
+
return;
|
|
749
|
+
this.offers.set(targetId, new Map());
|
|
750
|
+
this.logger.log('Cleared all offers for player:', targetId);
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Clear all offers for all players
|
|
754
|
+
*/
|
|
755
|
+
clearAll() {
|
|
728
756
|
this.offers.clear();
|
|
729
|
-
this.logger.log('Cleared all offers');
|
|
757
|
+
this.logger.log('Cleared all offers for all players');
|
|
730
758
|
}
|
|
731
759
|
/**
|
|
732
|
-
* Get offer count
|
|
760
|
+
* Get offer count (for self player)
|
|
733
761
|
*/
|
|
734
762
|
get size() {
|
|
735
|
-
|
|
763
|
+
const selfId = this.client.getSelfId();
|
|
764
|
+
if (!selfId)
|
|
765
|
+
return 0;
|
|
766
|
+
return this.offers.get(selfId)?.size || 0;
|
|
736
767
|
}
|
|
737
768
|
}
|
|
738
769
|
|
|
@@ -835,7 +866,9 @@ class AssetHelper {
|
|
|
835
866
|
return null;
|
|
836
867
|
}
|
|
837
868
|
setCurrencyAssetContents(currencies) {
|
|
838
|
-
|
|
869
|
+
Object.keys(currencies).forEach((key) => {
|
|
870
|
+
this.currencies[key] = currencies[key];
|
|
871
|
+
});
|
|
839
872
|
}
|
|
840
873
|
resolveReward(reward) {
|
|
841
874
|
if (reward.kind === 'loyalty_currency' && reward.rewardId) {
|
|
@@ -891,6 +924,7 @@ class OfferwallClient {
|
|
|
891
924
|
constructor(config) {
|
|
892
925
|
this.isInitializing = false;
|
|
893
926
|
this.pendingStackedToken = null;
|
|
927
|
+
this.selfId = null;
|
|
894
928
|
this.config = {
|
|
895
929
|
autoConnect: config.autoConnect ?? false,
|
|
896
930
|
reconnect: config.reconnect ?? true,
|
|
@@ -903,7 +937,7 @@ class OfferwallClient {
|
|
|
903
937
|
this.hooks = this.config.hooks || {};
|
|
904
938
|
this.logger = createLogger(this.config, 'OfferwallClient');
|
|
905
939
|
this.eventEmitter = new EventEmitter(this.config);
|
|
906
|
-
this.offerStore = new OfferStore(this.config);
|
|
940
|
+
this.offerStore = new OfferStore(this.config, this);
|
|
907
941
|
this.tokenManager = new TokenManager(this.config);
|
|
908
942
|
this.assetHelper = new AssetHelper(this.config);
|
|
909
943
|
this.sseConnection = new SSEConnection(this.config, this.eventEmitter, this.tokenManager);
|
|
@@ -944,6 +978,9 @@ class OfferwallClient {
|
|
|
944
978
|
get assets() {
|
|
945
979
|
return this.assetHelper;
|
|
946
980
|
}
|
|
981
|
+
getSelfId() {
|
|
982
|
+
return this.selfId;
|
|
983
|
+
}
|
|
947
984
|
/**
|
|
948
985
|
* Initialize the offerwall client and connect
|
|
949
986
|
*/
|
|
@@ -1006,8 +1043,9 @@ class OfferwallClient {
|
|
|
1006
1043
|
if (this.sseConnection) {
|
|
1007
1044
|
this.sseConnection.disconnect();
|
|
1008
1045
|
}
|
|
1009
|
-
this.offerStore.
|
|
1046
|
+
this.offerStore.clearAll();
|
|
1010
1047
|
this.tokenManager.clearToken();
|
|
1048
|
+
this.selfId = null;
|
|
1011
1049
|
if (this.hooks.afterDisconnect) {
|
|
1012
1050
|
await this.hooks.afterDisconnect();
|
|
1013
1051
|
}
|
|
@@ -1015,8 +1053,8 @@ class OfferwallClient {
|
|
|
1015
1053
|
/**
|
|
1016
1054
|
* Claim rewards for an offer
|
|
1017
1055
|
*/
|
|
1018
|
-
async claimReward(instanceId) {
|
|
1019
|
-
const offer = this.offerStore.getOffer(instanceId);
|
|
1056
|
+
async claimReward(instanceId, targetId = this.getSelfId()) {
|
|
1057
|
+
const offer = this.offerStore.getOffer(instanceId, targetId);
|
|
1020
1058
|
if (!offer) {
|
|
1021
1059
|
throw new Error(`Offer ${instanceId} not found`);
|
|
1022
1060
|
}
|
|
@@ -1031,7 +1069,7 @@ class OfferwallClient {
|
|
|
1031
1069
|
}
|
|
1032
1070
|
}
|
|
1033
1071
|
try {
|
|
1034
|
-
const response = await this.claimOfferAPI(instanceId);
|
|
1072
|
+
const response = await this.claimOfferAPI(instanceId, targetId);
|
|
1035
1073
|
const updatedOffer = { ...offer, status: 'claimed' };
|
|
1036
1074
|
this.offerStore.upsertOffer(updatedOffer);
|
|
1037
1075
|
this.eventEmitter.emit(OfferEvent.OFFER_CLAIMED, {
|
|
@@ -1087,7 +1125,7 @@ class OfferwallClient {
|
|
|
1087
1125
|
});
|
|
1088
1126
|
*/
|
|
1089
1127
|
this.eventEmitter.on(OfferEvent.OFFER_SURFACED, ({ offer }) => {
|
|
1090
|
-
this.offerStore.upsertOffer(offer);
|
|
1128
|
+
this.offerStore.upsertOffer(offer); // should always be selfId
|
|
1091
1129
|
this.logger.log(`Surfaced offer: ${offer.instanceId}`);
|
|
1092
1130
|
});
|
|
1093
1131
|
}
|
|
@@ -1117,23 +1155,27 @@ class OfferwallClient {
|
|
|
1117
1155
|
}
|
|
1118
1156
|
return response.json();
|
|
1119
1157
|
}
|
|
1120
|
-
async claimOfferAPI(instanceId) {
|
|
1158
|
+
async claimOfferAPI(instanceId, targetId = null) {
|
|
1121
1159
|
return this.postWithAuth('/v1/client/reward/claim', {
|
|
1122
1160
|
instanceId,
|
|
1123
1161
|
kind: 'offer',
|
|
1162
|
+
targetId: targetId || undefined,
|
|
1124
1163
|
});
|
|
1125
1164
|
}
|
|
1126
|
-
getPlayer() {
|
|
1127
|
-
return this.offerStore.getPlayer();
|
|
1165
|
+
getPlayer(targetId = this.getSelfId()) {
|
|
1166
|
+
return this.offerStore.getPlayer(targetId);
|
|
1128
1167
|
}
|
|
1129
|
-
getOffers() {
|
|
1130
|
-
return this.offerStore.getAllOffers();
|
|
1168
|
+
getOffers(targetId = this.getSelfId()) {
|
|
1169
|
+
return this.offerStore.getAllOffers(targetId);
|
|
1131
1170
|
}
|
|
1132
|
-
async refreshOffersAndPlayer() {
|
|
1171
|
+
async refreshOffersAndPlayer(targetId = null) {
|
|
1133
1172
|
try {
|
|
1134
|
-
const { offers, player } = await this.getOffersAndPlayer();
|
|
1135
|
-
|
|
1173
|
+
const { offers, player } = await this.getOffersAndPlayer(targetId);
|
|
1174
|
+
if (targetId == null) {
|
|
1175
|
+
this.selfId = player.snapshot.playerId;
|
|
1176
|
+
}
|
|
1136
1177
|
this.offerStore.setPlayer(player);
|
|
1178
|
+
this.offerStore.setOffers(offers, player);
|
|
1137
1179
|
this.eventEmitter.emit(OfferEvent.REFRESH, { offers, player: player });
|
|
1138
1180
|
this.logger.log('Refreshed offers and player snapshot');
|
|
1139
1181
|
}
|
|
@@ -1142,9 +1184,10 @@ class OfferwallClient {
|
|
|
1142
1184
|
throw error;
|
|
1143
1185
|
}
|
|
1144
1186
|
}
|
|
1145
|
-
async getOffersAndPlayer() {
|
|
1187
|
+
async getOffersAndPlayer(targetId = null) {
|
|
1146
1188
|
const data = await this.postWithAuth('/v1/client/player/campaigns', {
|
|
1147
1189
|
viewingCampaigns: true,
|
|
1190
|
+
targetId: targetId || undefined,
|
|
1148
1191
|
});
|
|
1149
1192
|
if (!data.offers || !Array.isArray(data.offers)) {
|
|
1150
1193
|
throw new Error('No offers returned from offers endpoint');
|