@looplay/sdk 0.2.1 → 0.5.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.
@@ -1,6 +1,6 @@
1
- import { ApiClient } from '../apps/api-client';
2
- import type { EmitGameEventResponse, GameEventKey } from '../apps/LooplaySDK.types';
3
- import type { EmitGameEventOptions, LooplayIframeAuthListener, LooplayIframeAuthOptions, LooplayIframeAuthState, TrackMatchOptions } from './types';
1
+ import { ApiClient } from "../apps/api-client";
2
+ import type { EmitGameEventResponse, GameEventKey } from "../apps/LooplaySDK.types";
3
+ import type { EmitGameEventOptions, LooplayIframeAuthListener, LooplayIframeAuthOptions, LooplayIframeAuthState, RequestAdsOptions, TrackMatchOptions } from "./types";
4
4
  /**
5
5
  * Handles auth for games that may run in two contexts:
6
6
  *
@@ -27,6 +27,12 @@ export declare class LooplayIframeAuth {
27
27
  private readonly options;
28
28
  private messageListener;
29
29
  private anonymousFallbackTimer;
30
+ private pendingAdsRequests;
31
+ private requestAdsSequence;
32
+ private adsRequestInFlight;
33
+ private adsRequestsSent;
34
+ private lastAdsRequestAt;
35
+ private autoAdsActionCount;
30
36
  constructor(options?: LooplayIframeAuthOptions);
31
37
  getState(): LooplayIframeAuthState;
32
38
  getJwt(): string | null;
@@ -38,12 +44,23 @@ export declare class LooplayIframeAuth {
38
44
  canTrack(): boolean;
39
45
  subscribe(listener: LooplayIframeAuthListener): () => void;
40
46
  requestAuth(): void;
47
+ /**
48
+ * Requests the parent host to show a rewarded ad.
49
+ * In a Telegram Mini App setup, the game sends this request from the iframe
50
+ * and the parent page (the Mini App shell) bridges it to Adsgram, then
51
+ * responds with `LOOPLAY_ADS_WATCHED` after the ad finishes.
52
+ */
53
+ requestAds(options?: RequestAdsOptions): Promise<boolean>;
41
54
  /** Idempotent; attaches the postMessage listener (if embedded) and arms the anonymous fallback. */
42
55
  init(): void;
43
56
  /** Removes the postMessage listener, cancels timers, and clears subscribers; safe to call multiple times. */
44
57
  dispose(): void;
45
58
  /** Lazily builds (and rebuilds on auth change) the `ApiClient` bound to the current auth mode. */
46
59
  getClient(): ApiClient;
60
+ /** Call once when the game view opens, before any play/match tracking. */
61
+ trackView(): Promise<boolean>;
62
+ /** Starts a new play attempt; call when the user presses Play. Returns the attempt id. */
63
+ startAttempt(): string;
47
64
  trackPlay(playTimeSeconds?: number): Promise<boolean>;
48
65
  trackMatch(matchId: string, opts: TrackMatchOptions): Promise<boolean>;
49
66
  emitGameEvent(actionCode: GameEventKey | string, opts?: EmitGameEventOptions): Promise<EmitGameEventResponse | null>;
@@ -53,7 +70,11 @@ export declare class LooplayIframeAuth {
53
70
  /** Generates (or restores) a persisted anonymous device id and activates anonymous tracking. */
54
71
  private activateAnonymousMode;
55
72
  private handleAuthMessage;
73
+ private handleRequestAdsResult;
74
+ private handleRequestAdsUnavailable;
75
+ private handleIncomingMessage;
56
76
  private setState;
57
77
  private resolveParentOrigin;
58
78
  private debug;
79
+ private recordAutoRequestAdsActivity;
59
80
  }
@@ -35,6 +35,18 @@ export interface LooplayIframeAuthOptions {
35
35
  anonymousFallbackTimeoutMs?: number;
36
36
  /** localStorage key for the persisted anonymous device id. Default `'looplay:anon-id'`. */
37
37
  anonymousIdStorageKey?: string;
38
+ /**
39
+ * Optional auto-trigger for rewarded ads after play/match activity.
40
+ * When enabled, the SDK may call `requestAds()` after the configured
41
+ * total play/match threshold is reached and the random chance passes.
42
+ */
43
+ autoRequestAds?: {
44
+ enabled?: boolean;
45
+ afterActionCount?: number;
46
+ chance?: number;
47
+ cooldownMs?: number;
48
+ maxRequestsPerSession?: number;
49
+ };
38
50
  }
39
51
  export interface TrackMatchOptions {
40
52
  durationSeconds: number;
@@ -46,3 +58,11 @@ export interface EmitGameEventOptions {
46
58
  refId?: string;
47
59
  payload?: Record<string, unknown>;
48
60
  }
61
+ export interface RequestAdsOptions {
62
+ /** How long to wait for the parent to answer before resolving `false`. Default 5s. */
63
+ timeoutMs?: number;
64
+ }
65
+ export interface RequestAdsResult {
66
+ /** `true` when the parent handled the request and the ad flow completed successfully. */
67
+ success: boolean;
68
+ }
@@ -12,6 +12,10 @@ export declare class LooplaySDK {
12
12
  getAccessToken(): Promise<string | undefined>;
13
13
  getGameId(): string | undefined;
14
14
  getMyProfile(): Promise<any>;
15
+ /** Call once when the game view opens, before any play/match tracking. */
16
+ trackView(): Promise<boolean>;
17
+ /** Starts a new play attempt; call when the user presses Play. Returns the attempt id. */
18
+ startAttempt(): string;
15
19
  trackPlay(playTimeSeconds?: number): Promise<boolean>;
16
20
  /**
17
21
  * Report that a match/round ended.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@looplay/sdk",
3
- "version": "0.2.1",
3
+ "version": "0.5.0",
4
4
  "description": "LooplaySDK (auth + points) for game integrations",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",