fbinstant.d.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. //Copyright (c) 2014-present, Egret Technology.
  2. //for fbinstant.6.1.js
  3. declare class FBInstant {
  4. /**
  5. * 获取用户信息.
  6. * Contains functions and properties related to the current player.
  7. */
  8. static player: FBInstant.FBPlayer;
  9. /**
  10. * 当前游戏的来源信息
  11. * Contains functions and properties related to the current game context.
  12. */
  13. static context: FBInstant.Context;
  14. /**
  15. * 获取用户的语言设置,例如:zh_CN en_US
  16. * The current locale
  17. */
  18. static getLocale(): string;
  19. /**
  20. * 获取运行的平台信息: IOS | ANDROID | WEB | MOBILE_WEB
  21. * The platform on which the game is currently running
  22. */
  23. static getPlatform(): FBPlatform;
  24. /**
  25. * SDK 的版本号,例如: '4.1'
  26. * The string representation of this SDK version.
  27. */
  28. static getSDKVersion(): string;
  29. /**
  30. * SDK 初始化结束会返回一个 Promise 方法,应当在其他 API 使用前调用
  31. * Initializes the SDK library.
  32. */
  33. static initializeAsync(): Promise<void>;
  34. /**
  35. * 通知平台资源加载的百分比
  36. * Report the game's initial loading progress.
  37. * @param percentage 0-100
  38. */
  39. static setLoadingProgress(percentage: number): void;
  40. /**
  41. * 获取平台支持的 api 列表
  42. * Provides a list of API functions that are supported by the client.
  43. */
  44. static getSupportedAPIs(): string[];
  45. /**
  46. * 获取入口点数据
  47. * Returns any data object associated with the entry point that the game was launched from.
  48. */
  49. static getEntryPointData(): Object;
  50. /**
  51. * 用户从哪个入口进入的游戏
  52. * Returns the entry point that the game was launched from
  53. */
  54. static getEntryPointAsync(): string;
  55. /**
  56. * 设置会话数据
  57. * Sets the data associated with the individual gameplay session for the current context.
  58. */
  59. static setSessionData(sessionData: Object): void;
  60. /**
  61. * 游戏已完成加载资源,用户点击了开始游戏的按钮
  62. * 返回一个 Promise 方法
  63. * This indicates that the game has finished initial loading and is ready to start. Context information will be up-to-date when the returned promise resolves.
  64. */
  65. static startGameAsync(): Promise<void>;
  66. /**
  67. * 分享游戏
  68. * This invokes a dialog to let the user share specified content, either as a message in Messenger or as a post on the user's timeline.
  69. */
  70. static shareAsync(payload: FBInstant.SharePayload): Promise<void>;
  71. /**
  72. * 通知 Facebook 在游戏中发生的更新
  73. * Informs Facebook of an update that occurred in the game.
  74. */
  75. static updateAsync(payload: FBInstant.CustomUpdatePayload | FBInstant.LeaderboardUpdatePayload): Promise<void>;
  76. /**
  77. * 请求客户端切换到另一个小游戏
  78. * Request that the client switch to a different Instant Game.
  79. */
  80. static switchGameAsync(appID: string, data?): Promise<void>;
  81. /**
  82. * 用户是否有资格创建快捷方式。
  83. * Returns whether or not the user is eligible to have shortcut creation requested.
  84. */
  85. static canCreateShortcutAsync(): Promise<Boolean>;
  86. /**
  87. * 如果用户有资格,提示用户创建游戏的快捷方式。
  88. * Prompts the user to create a shortcut to the game if they are eligible to Can only be called once per session.
  89. */
  90. static createShortcutAsync(): Promise<void>;
  91. /**
  92. * 退出游戏
  93. * Quits the game.
  94. */
  95. static quit(): void;
  96. /**
  97. * 使用 Facebook 的分析功能来分析应用。
  98. * Log an app event with FB Analytics
  99. * @param eventName 要分析的事件名称
  100. * @param valueToSum 可选,FB分析可以计算它。
  101. * @param parameters 可选,它可以包含多达25个 key-value,以记录事件。key 必须是2-40个字符,只能包含'_', '-', ' '和字母数字的字符。 Value 必须少于100个字符。
  102. */
  103. static logEvent(eventName: string, valueToSum?: number, parameters?: Object): FBInstant.APIError;
  104. /**
  105. * 设置一个暂停触发的方法
  106. * Set a callback to be fired when a pause event is triggered.
  107. */
  108. static onPause(func: Function): void;
  109. /**
  110. * 创建交互广告
  111. * Attempt to create an instance of interstitial ad. This instance can then be preloaded and presented.
  112. * @param placementID 在 Audience Network 设置的位置ID
  113. */
  114. static getInterstitialAdAsync(placementID: String): Promise<FBInstant.AdInstance>;
  115. /**
  116. * 创建激励视频广告
  117. * Attempt to create an instance of rewarded video. This instance can then be preloaded and presented.
  118. * @param placementID 在 Audience Network 设置的位置ID
  119. */
  120. static getRewardedVideoAsync(placementID: String): Promise<FBInstant.AdInstance>;
  121. /**
  122. * 尝试将当前玩家与等待他人加入游戏的其他玩家进行匹配
  123. * Attempts to match the current player with other users looking for people to play with
  124. */
  125. static matchPlayerAsync(matchTag: string, switchContextWhenMatched: boolean): Promise<void>;
  126. /**
  127. * 检查当前玩家是否符合 matchPlayerAsync API 的条件。
  128. * Checks if the current player is eligible for the matchPlayerAsync API.
  129. */
  130. static checkCanPlayerMatchAsync(): Promise<boolean>;
  131. /**
  132. * 获取这款小游戏中的特有排行榜。
  133. * An Instant Game leaderboard
  134. */
  135. static getLeaderboardAsync(name: string): Promise<FBInstant.Leaderboard>;
  136. }
  137. declare namespace FBInstant {
  138. interface AdInstance {
  139. /**
  140. * 获取广告位id
  141. */
  142. getPlacementID(): string;
  143. /**
  144. * 加载广告资源
  145. */
  146. loadAsync(): Promise<void>;
  147. /**
  148. * 播放广告
  149. */
  150. showAsync(): Promise<void>;
  151. }
  152. interface FBPlayer {
  153. /**
  154. * 玩家的唯一标识ID.
  155. * A unique identifier for the player.
  156. */
  157. getID(): string;
  158. /**
  159. * 获取玩家的唯一ID和一个签名,签名用来验证该 ID 来自 Facebook ,没有被篡改。
  160. * Fetch the player's unique identifier along with a signature that verifies that the identifier indeed comes from Facebook without being tampered with
  161. */
  162. getSignedPlayerInfoAsync(requestPayload?: string): Promise<SignedPlayerInfo>;
  163. /**
  164. * 返回一个 promise,表示玩家是否可以与游戏机器人对战。
  165. * Returns a promise that resolves with whether the player can subscribe to the game bot or not.
  166. */
  167. canSubscribeBotAsync(): Promise<Boolean>;
  168. /**
  169. * 请求玩家订阅游戏机器人。
  170. * Request that the player subscribe the bot associated to the game.
  171. */
  172. subscribeBotAsync(): Promise<void>;
  173. /**
  174. * 获取用户在Facebook上的的名字,使用用户的语言种类显示
  175. * The player's localized display name.
  176. */
  177. getName(): string;
  178. /**
  179. * 获取用户在Facebook上的头像的url,头像为正方形,最小尺寸为200x200.
  180. * A url to the player's public profile photo.
  181. */
  182. getPhoto(): string;
  183. /**
  184. * 取回在FB平台储存的当前用户的数据
  185. * Retrieve data from the designated cloud storage of the current player
  186. * @param keys 数据的 key 的数组
  187. */
  188. getDataAsync(keys: string[]): Promise<Object>;
  189. /**
  190. * 把当前用户的数据储存在FB平台上。
  191. * Set data to be saved to the designated cloud storage of the current player.
  192. * @param data 包含key-value的数据对象.
  193. */
  194. setDataAsync(data: Object): Promise<void>;
  195. /**
  196. * 立刻保存数据
  197. * Immediately flushes any changes to the player data to the designated cloud storage.
  198. */
  199. flushDataAsync(): Promise<void>;
  200. /**
  201. * 获取当前玩家数据
  202. * Retrieve stats from the designated cloud storage of the current player.
  203. * @param keys 数据的 key 的数组
  204. */
  205. getStatsAsync(keys: string[]): Promise<void>;
  206. /**
  207. * 把当前用户的数据储存在FB平台上。
  208. * Set stats to be saved to the designated cloud storage of the current player.
  209. * @param data 包含key-value的数据对象.
  210. */
  211. setStatsAsync(stats: Object): Promise<void>;
  212. /**
  213. * 把当前玩家数据增量更新储存到FB平台上。
  214. * Increment stats saved in the designated cloud storage of the current player.
  215. * @param data 包含key-value的数据对象.
  216. */
  217. incrementStatsAsync(increments: Object): Promise<void>;
  218. /**
  219. * 获取玩家好友的信息
  220. * Fetches an array of ConnectedPlayer objects containing information about players that are connected to the current player.
  221. */
  222. getConnectedPlayersAsync(): Promise<ConnectedPlayer[]>;
  223. }
  224. /**
  225. * 当前游戏的来源信息
  226. */
  227. interface Context {
  228. /**
  229. * 当前游戏来源的唯一id
  230. * A unique identifier for the current game context.
  231. */
  232. getID(): string;
  233. /**
  234. * 游戏的来源类型:"POST" | "THREAD" | "GROUP" | "SOLO"
  235. * The type of the current game context.
  236. */
  237. getType(): string;
  238. /**
  239. * 用这个方法来判断当前游戏环境中游戏参与者的数量是否介于指定的最小值和最大值之间。
  240. * This function determines whether the number of participants in the current game context is between a given minimum and maximum, inclusive.
  241. */
  242. isSizeBetween(minSize: number, maxSize: number): { answer: boolean, minSize: number, maxSize: number };
  243. /**
  244. * 切换游戏场景
  245. * Request a switch into a specific context.
  246. */
  247. switchAsync(id: string): Promise<void>;
  248. /**
  249. * 选择游戏场景
  250. * Opens a context selection dialog for the player.
  251. */
  252. chooseAsync(options?: { filter?: FBContextFilter[], maxSize?: number, minSize?: number }): Promise<void>;
  253. /**
  254. * 创建游戏场景
  255. * Attempts to create or switch into a context between a specified player and the current player.
  256. */
  257. createAsync(playerID: string): Promise<void>;
  258. /**
  259. * 获取当前环境中正在玩游戏的玩家列表,它可能包含当前玩家的信息。
  260. * Gets an array of #contextplayer objects containing information about active players
  261. */
  262. getPlayersAsync(): Promise<ContextPlayer[]>;
  263. }
  264. /**
  265. * 支付
  266. */
  267. interface payments {
  268. /**
  269. * 获取游戏的产品目录。
  270. * Fetches the game's product catalog.
  271. */
  272. getCatalogAsync(): Promise<Product[]>;
  273. /**
  274. * 开始特定产品的购买流程。
  275. * Begins the purchase flow for a specific product.
  276. */
  277. purchaseAsync(purchaseConfig: PurchaseConfig): Promise<Purchase>;
  278. /**
  279. * 获取玩家未消费的所有购买商品
  280. * Fetches all of the player's unconsumed purchases.
  281. */
  282. getPurchasesAsync(): Promise<Purchase[]>;
  283. /**
  284. * 消费当前玩家拥有的特定购买商品
  285. * Consumes a specific purchase belonging to the current player.
  286. */
  287. consumePurchaseAsync(purchaseToken: string): Promise<void>;
  288. /**
  289. * 设置一个回调,在支付操作可进行时触发。
  290. * Sets a callback to be triggered when Payments operations are available.
  291. */
  292. onReady(callBack: Function): void;
  293. }
  294. /**
  295. * 游戏好友的信息
  296. */
  297. interface ConnectedPlayer {
  298. /**
  299. * 关联用户的ID
  300. * Get the id of the connected player.
  301. */
  302. getID(): string;
  303. /**
  304. * 关联用户的名字
  305. * Get the player's full name.
  306. */
  307. getName(): string;
  308. /**
  309. * Get the player's public profile photo.
  310. * 关联用户的头像 ulr 地址
  311. */
  312. getPhoto(): string;
  313. }
  314. /**
  315. * 游戏环境中的玩家
  316. */
  317. interface ContextPlayer {
  318. /**
  319. * 关联用户的ID
  320. * Get the id of the context player.
  321. */
  322. getID(): string;
  323. /**
  324. * 关联用户的名字
  325. * Get the player's localized display name.
  326. */
  327. getName(): string;
  328. /**
  329. * 关联用户的头像 ulr 地址
  330. * Get the player's public profile photo.
  331. */
  332. getPhoto(): string;
  333. }
  334. /**
  335. * 玩家的签名信息
  336. */
  337. interface SignedPlayerInfo {
  338. /**
  339. * 玩家的id
  340. * Get the id of the player.
  341. */
  342. getPlayerID(): string;
  343. /**
  344. * 验证这个对象的签名确实来自Facebook。该字符串是base64url编码的,使用 HMAC 对您应用的 Sccret 进行签名,基于 OAuth 2.0 规范,
  345. * A signature to verify this object indeed comes from Facebook.
  346. */
  347. getSignature(): string;
  348. }
  349. /**
  350. * 要分享的内容
  351. */
  352. interface SharePayload {
  353. /**
  354. * 表示共享的目标
  355. * Indicates the intent of the share.
  356. * "INVITE" | "REQUEST" | "CHALLENGE" | "SHARE"
  357. */
  358. intent: string;
  359. /**
  360. * 要分享的图像,使用 base64 编码
  361. * A base64 encoded image to be shared.
  362. */
  363. image: string;
  364. /**
  365. * 要分享的文字
  366. * A text message to be shared.
  367. */
  368. text: string;
  369. /**
  370. * 一个附加到分享上的数据。
  371. * 所有从这个分享启动的游戏都可以通过 FBInstant.getEntryPointData() 方法获取到该数据。
  372. * A blob of data to attach to the share.
  373. */
  374. data?: Object;
  375. }
  376. /**
  377. * 自定义更新内容
  378. */
  379. interface CustomUpdatePayload {
  380. /**
  381. * 对于自定义更新来说,该值应该为 'CUSTOM'.
  382. * For custom updates, this should be 'CUSTOM'.
  383. */
  384. action: string;
  385. /**
  386. * 自定义更新使用的模板的ID,模板应该在 fbapp-config.json 中预定义。
  387. * 查看配置文件说明:https://developers.facebook.com/docs/games/instant-games/bundle-config
  388. * ID of the template this custom update is using.
  389. */
  390. template: string;
  391. /**
  392. * 可选,按钮文字。默认情况下,我们本地化的 'Play' 作为按钮文字。
  393. * Optional call-to-action button text.
  394. */
  395. cta?: string;
  396. /**
  397. * base64 编码的图像信息
  398. * Data URL of a base64 encoded image.
  399. */
  400. image: string;
  401. /**
  402. * 文本信息
  403. * A text message, or an object with the default text as the value of 'default' and another object mapping locale keys to translations as the value of 'localizations'.
  404. */
  405. text: string;
  406. /**
  407. * 附加到更新上的数据。当游戏通过分享启动时,可以通过 FBInstant.getEntryPointData() 方法获取。
  408. * 该数据必须少于1000个字符。
  409. * A blob of data to attach to the update.
  410. */
  411. data?: Object;
  412. /**
  413. * 指定更新的方式。
  414. * Specifies how the update should be delivered.
  415. * 'IMMEDIATE' - 默认值,立即发布更新
  416. * 'LAST' - 当游戏结束时,发布更新
  417. * 'IMMEDIATE_CLEAR' - 立即发布更新,并清除任何其他正在等待的更新
  418. */
  419. strategy?: string;
  420. /**
  421. * 指定自定义更新的通知设置。可以是“NO_PUSH”或“PUSH”,默认为“NO_PUSH”。
  422. * Specifies notification setting for the custom update.
  423. */
  424. notification?: string;
  425. }
  426. /**
  427. * 表示 FBInstant.updateAsync 的一项排行榜更新
  428. * Represents a leaderboard update for FBInstant.updateAsync.
  429. */
  430. interface LeaderboardUpdatePayload {
  431. /**
  432. * 对于排行榜更新,此属性应为 “LEADERBOARD”
  433. * For a leaderboard update, this should be 'LEADERBOARD'. text.
  434. */
  435. action: string;
  436. /**
  437. * 更新排行榜的名称。
  438. * The name of the leaderboard to feature in the update.
  439. */
  440. name: string;
  441. /**
  442. * 可选的文本消息
  443. * Optional text message
  444. */
  445. text?: string;
  446. }
  447. interface APIError {
  448. /**
  449. * 错误码
  450. * The relevant error code
  451. */
  452. code: string;
  453. /**
  454. * 错误信息
  455. * A message describing the error
  456. */
  457. message: string;
  458. }
  459. interface Product {
  460. /**
  461. * 产品的名称
  462. * The title of the product
  463. */
  464. title: string,
  465. /**
  466. * 产品的游戏指定id
  467. * The product's game-specified identifier
  468. */
  469. productID: string,
  470. /**
  471. * 产品的描述
  472. * The product description
  473. */
  474. description: string,
  475. /**
  476. * 产品相关图片的链接
  477. * A link to the product's associated image
  478. */
  479. imageURI: string,
  480. /**
  481. * 产品的价格
  482. * The price of the product
  483. */
  484. price: string,
  485. /**
  486. * 产品的货币代码
  487. * The currency code for the product
  488. */
  489. priceCurrencyCode: string,
  490. }
  491. interface PurchaseConfig {
  492. /**
  493. * 产品id
  494. * The identifier of the product to purchase
  495. */
  496. productID: string,
  497. /**
  498. * 可选参数,开发人员指定的内容,将包含在返回的购买签名请求里。
  499. * An optional developer-specified payload, to be included in the returned purchase's signed request.
  500. */
  501. developerPayload: string,
  502. }
  503. interface Purchase {
  504. /**
  505. * 可选参数,开发人员指定的内容,将包含在返回的购买签名请求里。
  506. * A developer-specified string, provided during the purchase of the product
  507. */
  508. developerPayload: string,
  509. /**
  510. * 购买交易的标识符
  511. * The identifier for the purchase transaction
  512. */
  513. paymentID: string,
  514. /**
  515. * 产品id
  516. * The product's game-specified identifier
  517. */
  518. productID: string,
  519. /**
  520. * 发生购买时的Unix时间戳
  521. * Unix timestamp of when the purchase occurred
  522. */
  523. purchaseTime: string,
  524. /**
  525. * 代表可用于消费者购买时的token
  526. * A token representing the purchase that may be used to consume the purchase
  527. */
  528. purchaseToken: string,
  529. /**
  530. * 购买请求的服务器签名编码
  531. * Server-signed encoding of the purchase request
  532. */
  533. signedRequest: string,
  534. }
  535. /**
  536. * 排行榜
  537. * Leaderboard
  538. */
  539. interface Leaderboard {
  540. /**
  541. * 排行榜的名称
  542. * The name of the leaderboard.
  543. */
  544. getName(): string,
  545. /**
  546. * 排行榜的上下文id
  547. * The ID of the context that the leaderboard is associated with
  548. */
  549. getContextID(): string;
  550. /**
  551. * 获取排行榜中玩家总量
  552. * Fetches the total number of player entries in the leaderboard.
  553. */
  554. getEntryCountAsync(): Promise<number>;
  555. /**
  556. * 更新玩家的分数
  557. * Updates the player's score.
  558. */
  559. setScoreAsync(score: number, extraData: string): Promise<LeaderboardEntry>;
  560. /**
  561. * 获取当前玩家游戏榜单的入口点
  562. * Retrieves the leaderboard's entry for the current player, or null if the player has not set one yet.
  563. */
  564. getPlayerEntryAsync(): Promise<LeaderboardEntry>;
  565. /**
  566. * 检索一组排行榜条目, 按排行榜中的评分顺序排序。
  567. * Retrieves a set of leaderboard entries, ordered by score ranking in the leaderboard.
  568. */
  569. getEntriesAsync(count: number, offset: number): Promise<Array<LeaderboardEntry>>;
  570. /**
  571. * 检索与当前玩家分数相邻的玩家(包括当前玩家)的排行榜分录,按照玩家的级别排序。
  572. * Retrieves the leaderboard score entries of the current player's connected players (including the current player), ordered by local rank within the set of connected players.
  573. */
  574. getConnectedPlayerEntriesAsync(count: number, offset: number): Promise<Array<LeaderboardEntry>>;
  575. }
  576. interface LeaderboardEntry {
  577. /**
  578. * 获取与该项关联的分数。
  579. * Gets the score associated with the entry.
  580. */
  581. getScore(): number;
  582. /**
  583. * 获取与该项关联的分数, 格式化为与排行榜关联的评分格式。
  584. * Gets the score associated with the entry, formatted with the score format associated with the leaderboard.
  585. */
  586. getFormattedScore(): string;
  587. /**
  588. * 获取上次更新排行榜条目的时间戳。
  589. * Gets the timestamp of when the leaderboard entry was last updated.
  590. */
  591. getTimestamp(): number;
  592. /**
  593. * 获取排行榜中玩家得分的等级。
  594. * Gets the rank of the player's score in the leaderboard.
  595. */
  596. getRank(): number;
  597. /**
  598. * 获取与分数关联的额外数据,由开发者设定
  599. * Gets the developer-specified payload associated with the score, or null if one was not set.
  600. */
  601. getExtraData(): string;
  602. /**
  603. * 获取有关与该项关联的玩家的信息。
  604. * Gets information about the player associated with the entry.
  605. */
  606. getPlayer(): LeaderboardPlayer
  607. }
  608. interface LeaderboardPlayer {
  609. /**
  610. * 玩家的名字
  611. * Gets the player's localized display name.
  612. */
  613. getName(): string;
  614. /**
  615. * 玩家的头像链接
  616. * Returns a url to the player's public profile photo.
  617. */
  618. getPhoto(): string;
  619. /**
  620. * 玩家的id
  621. * Gets the game's unique identifier for the player.
  622. */
  623. getID(): string;
  624. }
  625. }
  626. type FBContextFilter = "NEW_CONTEXT_ONLY" | "INCLUDE_EXISTING_CHALLENGES";
  627. type FBPlatform = "IOS" | "ANDROID" | "WEB" | "MOBILE_WEB";