@pixels-online/pixels-client-js-sdk 1.16.0 → 1.17.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/index.esm.js CHANGED
@@ -1278,19 +1278,47 @@ class OfferwallClient {
1278
1278
  }
1279
1279
 
1280
1280
  const keyPattern = /\{([a-zA-Z_][a-zA-Z0-9_]*)\}/g;
1281
- // renders template by replacing {keyName} with values from dynamic
1281
+ /**
1282
+ * This replaces {keyName} keys from the template with corresponding values from the dynamic object.
1283
+ */
1282
1284
  function renderTemplate(template, dynamic) {
1283
1285
  if (!template)
1284
1286
  return '';
1285
- return template.replace(keyPattern, (match, key) => {
1287
+ return template.replace(keyPattern, (_match, key) => {
1288
+ if (dynamic && typeof dynamic[key] === 'boolean') {
1289
+ return dynamic[key] ? '✓' : '✗';
1290
+ }
1286
1291
  if (dynamic && dynamic[key] !== undefined) {
1287
1292
  return String(dynamic[key]);
1288
1293
  }
1289
1294
  return '{?}'; // indicate missing key
1290
1295
  });
1291
1296
  }
1297
+ /**
1298
+ * This replaces {{keyName}} in dynamic condition keys with corresponding values from
1299
+ * the PlayerOffer.trackers
1300
+ *
1301
+ * eg. a condition high_score_pet-{{surfacerPlayerId}} with high_score_pet-12345
1302
+ */
1303
+ function replaceDynamicConditionKey(key, trackers) {
1304
+ return key?.replace(/\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}/g, (match, p1) => {
1305
+ const value = trackers[p1];
1306
+ return value !== undefined ? String(value) : match;
1307
+ });
1308
+ }
1309
+ /** this replaces all of the dynamic conditions.keys by calling replaceDynamicConditionKey */
1310
+ function replaceDynamicConditionKeys(conditions, trackers) {
1311
+ return conditions.map((condition) => ({
1312
+ ...condition,
1313
+ key: replaceDynamicConditionKey(condition.key, trackers),
1314
+ }));
1315
+ }
1292
1316
 
1293
- const meetsBaseConditions = ({ conditions, playerSnap, addDetails, }) => {
1317
+ const meetsBaseConditions = ({ conditions, playerSnap, addDetails,
1318
+ /** this exists if calling meetsBaseConditions from meetsCompletionConditions. but surfacing
1319
+ * check doesn't use this since we don't have a playerOffer at surfacing time
1320
+ */
1321
+ playerOffer, }) => {
1294
1322
  const conditionData = [];
1295
1323
  let isValid = true;
1296
1324
  if (conditions?.minDaysInGame) {
@@ -1657,7 +1685,11 @@ const meetsBaseConditions = ({ conditions, playerSnap, addDetails, }) => {
1657
1685
  }
1658
1686
  // Evaluate dynamic conditions
1659
1687
  if (conditions?.dynamic?.conditions?.length) {
1660
- const dynamicResult = meetsDynamicConditions(playerSnap.dynamic || {}, conditions.dynamic);
1688
+ const resolvedConditions = replaceDynamicConditionKeys(conditions.dynamic.conditions, playerOffer?.trackers || {});
1689
+ const dynamicResult = meetsDynamicConditions(playerSnap.dynamic, {
1690
+ ...conditions.dynamic,
1691
+ conditions: resolvedConditions,
1692
+ });
1661
1693
  if (addDetails) {
1662
1694
  conditionData.push({
1663
1695
  isMet: dynamicResult,
@@ -1766,6 +1798,31 @@ const meetsSurfacingConditions = ({ surfacingConditions, playerSnap, context, pl
1766
1798
  return { isValid: false };
1767
1799
  }
1768
1800
  }
1801
+ if (conditions.allowedCountries?.length) {
1802
+ const playerCountry = playerSnap.ip?.countryCode;
1803
+ if (!playerCountry || !conditions.allowedCountries.includes(playerCountry)) {
1804
+ return { isValid: false };
1805
+ }
1806
+ }
1807
+ if (conditions.restrictedCountries?.length) {
1808
+ const playerCountry = playerSnap.ip?.countryCode;
1809
+ if (!playerCountry) {
1810
+ return { isValid: false };
1811
+ }
1812
+ if (conditions.restrictedCountries.includes(playerCountry)) {
1813
+ return { isValid: false };
1814
+ }
1815
+ }
1816
+ if (conditions.networkRestrictions?.length) {
1817
+ if (!playerSnap.ip) {
1818
+ return { isValid: false };
1819
+ }
1820
+ for (const restriction of conditions.networkRestrictions) {
1821
+ if (playerSnap.ip[restriction]) {
1822
+ return { isValid: false };
1823
+ }
1824
+ }
1825
+ }
1769
1826
  return meetsBaseConditions({ conditions, playerSnap });
1770
1827
  };
1771
1828
  const hasConditions = (conditions) => {
@@ -1818,6 +1875,12 @@ const hasConditions = (conditions) => {
1818
1875
  return true;
1819
1876
  if (surCond.links && Object.keys(surCond.links).length > 0)
1820
1877
  return true;
1878
+ if (surCond.allowedCountries?.length)
1879
+ return true;
1880
+ if (surCond.restrictedCountries?.length)
1881
+ return true;
1882
+ if (surCond.networkRestrictions?.length)
1883
+ return true;
1821
1884
  const compCond = conditions;
1822
1885
  if (compCond.context)
1823
1886
  return true;
@@ -1839,9 +1902,21 @@ const hasConditions = (conditions) => {
1839
1902
  return true;
1840
1903
  return false;
1841
1904
  };
1842
- const meetsCompletionConditions = ({ completionConditions, completionTrackers, playerSnap, addDetails = false, }) => {
1905
+ const offerMeetsCompletionConditions = (offer, snapshot) => {
1906
+ return meetsCompletionConditions({
1907
+ completionConditions: offer.completionConditions || {},
1908
+ completionTrackers: offer.completionTrackers,
1909
+ playerSnap: snapshot,
1910
+ playerOffer: offer,
1911
+ addDetails: true,
1912
+ });
1913
+ };
1914
+ const meetsCompletionConditions = ({ completionConditions, completionTrackers, playerSnap, playerOffer, addDetails = false, maxClaimCount, }) => {
1843
1915
  if (completionConditions) {
1844
1916
  const conditions = completionConditions;
1917
+ // For multi-claim offers, scale cumulative requirements by (claimedCount + 1)
1918
+ const shouldScale = maxClaimCount === -1 || (maxClaimCount && maxClaimCount > 1);
1919
+ const claimMultiplier = shouldScale ? (playerOffer.claimedCount || 0) + 1 : 1;
1845
1920
  const conditionData = [];
1846
1921
  let isValid = true;
1847
1922
  if (completionConditions?.context?.id) {
@@ -1864,13 +1939,14 @@ const meetsCompletionConditions = ({ completionConditions, completionTrackers, p
1864
1939
  }
1865
1940
  }
1866
1941
  if (conditions?.buyItem) {
1867
- const isDisqualify = (completionTrackers?.buyItem || 0) < (conditions.buyItem.amount || 1);
1942
+ const scaledAmount = (conditions.buyItem.amount || 1) * claimMultiplier;
1943
+ const isDisqualify = (completionTrackers?.buyItem || 0) < scaledAmount;
1868
1944
  if (addDetails) {
1869
1945
  conditionData.push({
1870
1946
  isMet: !isDisqualify,
1871
1947
  kind: 'buyItem',
1872
1948
  trackerAmount: completionTrackers?.buyItem || 0,
1873
- text: `Buy ${conditions.buyItem.amount || 1} ${conditions.buyItem.name}`,
1949
+ text: `Buy ${scaledAmount} ${conditions.buyItem.name}`,
1874
1950
  });
1875
1951
  if (isDisqualify)
1876
1952
  isValid = false;
@@ -1881,13 +1957,14 @@ const meetsCompletionConditions = ({ completionConditions, completionTrackers, p
1881
1957
  }
1882
1958
  }
1883
1959
  if (conditions?.spendCurrency) {
1884
- const isDisqualify = (completionTrackers?.spendCurrency || 0) < (conditions.spendCurrency.amount || 1);
1960
+ const scaledAmount = (conditions.spendCurrency.amount || 1) * claimMultiplier;
1961
+ const isDisqualify = (completionTrackers?.spendCurrency || 0) < scaledAmount;
1885
1962
  if (addDetails) {
1886
1963
  conditionData.push({
1887
1964
  isMet: !isDisqualify,
1888
1965
  kind: 'spendCurrency',
1889
1966
  trackerAmount: completionTrackers?.spendCurrency || 0,
1890
- text: `Spend ${conditions.spendCurrency.amount || 1} ${conditions.spendCurrency.name}`,
1967
+ text: `Spend ${scaledAmount} ${conditions.spendCurrency.name}`,
1891
1968
  });
1892
1969
  if (isDisqualify)
1893
1970
  isValid = false;
@@ -1898,15 +1975,17 @@ const meetsCompletionConditions = ({ completionConditions, completionTrackers, p
1898
1975
  }
1899
1976
  }
1900
1977
  if (conditions?.depositCurrency) {
1901
- const isDisqualify = (completionTrackers?.depositCurrency || 0) <
1902
- (conditions.depositCurrency.amount || 1);
1978
+ const scaledAmount = (conditions.depositCurrency.amount || 1) * claimMultiplier;
1979
+ const isDisqualify = (completionTrackers?.depositCurrency || 0) < scaledAmount;
1903
1980
  if (addDetails) {
1904
1981
  conditionData.push({
1905
1982
  isMet: !isDisqualify,
1906
1983
  kind: 'depositCurrency',
1907
1984
  trackerAmount: completionTrackers?.depositCurrency || 0,
1908
- text: `Deposit ${conditions.depositCurrency.amount || 1} ${conditions.depositCurrency.name}`,
1985
+ text: `Deposit ${scaledAmount} ${conditions.depositCurrency.name}`,
1909
1986
  });
1987
+ if (isDisqualify)
1988
+ isValid = false;
1910
1989
  }
1911
1990
  else {
1912
1991
  if (isDisqualify)
@@ -1914,12 +1993,13 @@ const meetsCompletionConditions = ({ completionConditions, completionTrackers, p
1914
1993
  }
1915
1994
  }
1916
1995
  if (conditions?.login) {
1917
- const isMet = completionTrackers?.login || false;
1996
+ const isMet = new Date(playerSnap.snapshotLastUpdated || 0).getTime() >
1997
+ new Date(playerOffer.createdAt || 0).getTime();
1918
1998
  if (addDetails) {
1919
1999
  conditionData.push({
1920
2000
  isMet,
1921
2001
  kind: 'login',
1922
- trackerAmount: completionTrackers?.login ? 1 : 0,
2002
+ trackerAmount: isMet ? 1 : 0,
1923
2003
  text: `Login to the game`,
1924
2004
  });
1925
2005
  isValid = isMet;
@@ -1956,9 +2036,11 @@ const meetsCompletionConditions = ({ completionConditions, completionTrackers, p
1956
2036
  const hasContent = Boolean(mode === 'accumulate'
1957
2037
  ? tSocial?.mode === 'accumulate'
1958
2038
  : tSocial && tSocial.mode !== 'accumulate' && !!tSocial.videoId);
1959
- const minLikes = cSocial?.minLikes || 0;
1960
- const minViews = cSocial?.minViews || 0;
1961
- const minComments = cSocial?.minComments || 0;
2039
+ // Only scale social metrics in accumulate mode (attach mode is single content)
2040
+ const socialMultiplier = mode === 'accumulate' ? claimMultiplier : 1;
2041
+ const minLikes = (cSocial?.minLikes || 0) * socialMultiplier;
2042
+ const minViews = (cSocial?.minViews || 0) * socialMultiplier;
2043
+ const minComments = (cSocial?.minComments || 0) * socialMultiplier;
1962
2044
  const likes = tSocial?.likes || 0;
1963
2045
  const views = tSocial?.views || 0;
1964
2046
  const comments = tSocial?.comments || 0;
@@ -2043,14 +2125,14 @@ const meetsCompletionConditions = ({ completionConditions, completionTrackers, p
2043
2125
  // Linked completions - wait for N linked entities to complete
2044
2126
  if (conditions?.linkedCompletions?.min) {
2045
2127
  const currentCount = completionTrackers?.linkedCompletions || 0;
2046
- const requiredCount = conditions.linkedCompletions.min;
2047
- const isDisqualify = currentCount < requiredCount;
2128
+ const scaledMin = conditions.linkedCompletions.min * claimMultiplier;
2129
+ const isDisqualify = currentCount < scaledMin;
2048
2130
  if (addDetails) {
2049
2131
  conditionData.push({
2050
2132
  isMet: !isDisqualify,
2051
2133
  kind: 'linkedCompletions',
2052
2134
  trackerAmount: currentCount,
2053
- text: `Wait for ${requiredCount} linked ${requiredCount === 1 ? 'entity' : 'entities'} to complete`,
2135
+ text: `Wait for ${scaledMin} linked ${scaledMin === 1 ? 'entity' : 'entities'} to complete`,
2054
2136
  });
2055
2137
  if (isDisqualify)
2056
2138
  isValid = false;
@@ -2061,7 +2143,12 @@ const meetsCompletionConditions = ({ completionConditions, completionTrackers, p
2061
2143
  }
2062
2144
  }
2063
2145
  if (conditions?.dynamicTracker?.conditions?.length) {
2064
- const dynamicResult = meetsDynamicConditions(completionTrackers?.dynamicTracker || {}, conditions.dynamicTracker);
2146
+ const resolvedConditions = replaceDynamicConditionKeys(conditions.dynamicTracker.conditions, playerOffer?.trackers || {});
2147
+ // now we have the game-defined conditions with {{}} keys populated. feed these conditions into evaluator
2148
+ const dynamicResult = meetsDynamicConditions(completionTrackers?.dynamicTracker, {
2149
+ ...conditions.dynamicTracker,
2150
+ conditions: resolvedConditions,
2151
+ }, claimMultiplier);
2065
2152
  if (addDetails) {
2066
2153
  conditionData.push({
2067
2154
  isMet: dynamicResult,
@@ -2080,6 +2167,7 @@ const meetsCompletionConditions = ({ completionConditions, completionTrackers, p
2080
2167
  conditions,
2081
2168
  playerSnap,
2082
2169
  addDetails: true,
2170
+ playerOffer,
2083
2171
  });
2084
2172
  isValid = isValid && r.isValid;
2085
2173
  conditionData.push(...(r.conditionData || []));
@@ -2094,10 +2182,9 @@ const meetsCompletionConditions = ({ completionConditions, completionTrackers, p
2094
2182
  * @param completionConditions - The completion conditions to check
2095
2183
  * @param completionTrackers - The completion trackers (for buyItem, spendCurrency, etc.)
2096
2184
  * @param playerSnap - The player snapshot with field timestamps
2097
- * @param expiryTime - The expiry timestamp in milliseconds
2098
2185
  * @returns true if all conditions were met before expiry, false otherwise
2099
2186
  */
2100
- const meetsCompletionConditionsBeforeExpiry = ({ completionConditions, completionTrackers, playerSnap, expiryTime, }) => {
2187
+ const meetsCompletionConditionsBeforeExpiry = ({ completionConditions, completionTrackers, playerSnap, playerOffer, maxClaimCount, }) => {
2101
2188
  if (!completionConditions)
2102
2189
  return false;
2103
2190
  // Check if there are actually any conditions to evaluate
@@ -2107,10 +2194,15 @@ const meetsCompletionConditionsBeforeExpiry = ({ completionConditions, completio
2107
2194
  const conditionsMet = meetsCompletionConditions({
2108
2195
  completionConditions,
2109
2196
  completionTrackers,
2197
+ playerOffer,
2110
2198
  playerSnap,
2199
+ maxClaimCount,
2111
2200
  });
2112
2201
  if (!conditionsMet.isValid)
2113
2202
  return false;
2203
+ if (!playerOffer.expiresAt)
2204
+ return true;
2205
+ const expiryTime = new Date(playerOffer.expiresAt).getTime();
2114
2206
  const lastSnapshotUpdate = new Date(playerSnap.snapshotLastUpdated).getTime();
2115
2207
  /**
2116
2208
  * Checks if a field was updated after the expiry time.
@@ -2221,16 +2313,30 @@ const meetsCompletionConditionsBeforeExpiry = ({ completionConditions, completio
2221
2313
  * Checks if a dynamic object meets a set of dynamic field conditions.
2222
2314
  * @param dynamicObj - The object with any key and string or number value.
2223
2315
  * @param conditions - Array of conditions to check.
2316
+ * @param claimMultiplier - Multiplier to scale conditions (used for numeric comparisons).
2224
2317
  * @returns true if all conditions are met, false otherwise.
2225
2318
  */
2226
2319
  /**
2227
2320
  * Evaluates a single dynamic condition against the dynamic object.
2228
2321
  */
2229
- function evaluateDynamicCondition(dynamicObj, cond) {
2322
+ function evaluateDynamicCondition(dynamicObj, cond, claimMultiplier = 1) {
2323
+ if (!dynamicObj)
2324
+ return false;
2230
2325
  const val = dynamicObj[cond.key];
2231
2326
  if (val === undefined)
2232
2327
  return false;
2233
2328
  const isNumber = typeof val === 'number';
2329
+ const isBoolean = typeof val === 'boolean';
2330
+ if (isBoolean) {
2331
+ switch (cond.operator) {
2332
+ case '==':
2333
+ return val === Boolean(cond.compareTo);
2334
+ case '!=':
2335
+ return val !== Boolean(cond.compareTo);
2336
+ default:
2337
+ return false;
2338
+ }
2339
+ }
2234
2340
  const compareTo = isNumber ? Number(cond.compareTo) : String(cond.compareTo);
2235
2341
  switch (cond.operator) {
2236
2342
  case '==':
@@ -2241,13 +2347,13 @@ function evaluateDynamicCondition(dynamicObj, cond) {
2241
2347
  if (isNumber && typeof compareTo === 'number') {
2242
2348
  switch (cond.operator) {
2243
2349
  case '>':
2244
- return val > compareTo;
2350
+ return val > compareTo * claimMultiplier;
2245
2351
  case '>=':
2246
- return val >= compareTo;
2352
+ return val >= compareTo * claimMultiplier;
2247
2353
  case '<':
2248
- return val < compareTo;
2354
+ return val < compareTo * claimMultiplier;
2249
2355
  case '<=':
2250
- return val <= compareTo;
2356
+ return val <= compareTo * claimMultiplier;
2251
2357
  }
2252
2358
  }
2253
2359
  else if (!isNumber && typeof compareTo === 'string') {
@@ -2264,20 +2370,23 @@ function evaluateDynamicCondition(dynamicObj, cond) {
2264
2370
  * Evaluates a group of dynamic conditions with logical links (AND, OR, AND NOT).
2265
2371
  * @param dynamicObj - The player's dynamic object with any key and string or number value.
2266
2372
  * @param dynamicGroup - The group of conditions and links to check.
2373
+ * @param claimMultiplier - Multiplier to scale conditions (used for numeric comparisons).
2267
2374
  * @returns true if the group evaluates to true, false otherwise.
2268
2375
  */
2269
- function meetsDynamicConditions(dynamicObj, dynamicGroup) {
2376
+ function meetsDynamicConditions(dynamicObj, dynamicGroup, claimMultiplier = 1) {
2270
2377
  const { conditions, links } = dynamicGroup;
2271
2378
  if (!conditions || conditions.length === 0)
2272
2379
  return true;
2380
+ if (!dynamicObj)
2381
+ return false;
2273
2382
  // If no links, treat as AND between all conditions
2274
2383
  if (!links || links.length === 0) {
2275
- return conditions.every((cond) => evaluateDynamicCondition(dynamicObj, cond));
2384
+ return conditions.every((cond) => evaluateDynamicCondition(dynamicObj, cond, claimMultiplier));
2276
2385
  }
2277
2386
  // Evaluate the first condition
2278
- let result = evaluateDynamicCondition(dynamicObj, conditions[0]);
2387
+ let result = evaluateDynamicCondition(dynamicObj, conditions[0], claimMultiplier);
2279
2388
  for (let i = 0; i < links.length; i++) {
2280
- const nextCond = evaluateDynamicCondition(dynamicObj, conditions[i + 1]);
2389
+ const nextCond = evaluateDynamicCondition(dynamicObj, conditions[i + 1], claimMultiplier);
2281
2390
  const link = links[i];
2282
2391
  if (link === 'AND') {
2283
2392
  result = result && nextCond;
@@ -2359,5 +2468,5 @@ const rewardSchema = {
2359
2468
  image: String,
2360
2469
  };
2361
2470
 
2362
- export { AssetHelper, ConnectionState, EventEmitter, OfferEvent, OfferStore, OfferwallClient, PlayerOfferStatuses, SSEConnection, hasConditions, meetsBaseConditions, meetsClaimableConditions, meetsCompletionConditions, meetsCompletionConditionsBeforeExpiry, meetsDynamicConditions, meetsSurfacingConditions, offerListenerEvents, rewardKinds, rewardSchema };
2471
+ export { AssetHelper, ConnectionState, EventEmitter, OfferEvent, OfferStore, OfferwallClient, PlayerOfferStatuses, SSEConnection, hasConditions, meetsBaseConditions, meetsClaimableConditions, meetsCompletionConditions, meetsCompletionConditionsBeforeExpiry, meetsDynamicConditions, meetsSurfacingConditions, offerListenerEvents, offerMeetsCompletionConditions, rewardKinds, rewardSchema };
2363
2472
  //# sourceMappingURL=index.esm.js.map