GameSDK.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. "use strict";
  2. (function() {
  3. var GameSDK = window.GameSDK = {
  4. // CallBack
  5. callBacks: {},
  6. // Internal -- Call Native
  7. callNative: function(cmd, param) {
  8. param = param == null ? "" : JSON.stringify(param);
  9. var rt = loadRuntime();
  10. rt.callCustomCommand({
  11. success(msg) {
  12. console.log('success:', msg);
  13. },
  14. fail(msg) {
  15. console.log('fail:', msg);
  16. },
  17. }, cmd, param);
  18. },
  19. nativeCallback: function(cmd, param) {
  20. var func = this.callBacks[cmd];
  21. if (func) {
  22. func(JSON.parse(param));
  23. }
  24. },
  25. registerCallback: function(cmd, func) {
  26. this.callBacks[cmd] = func;
  27. },
  28. // 设置回调函数
  29. setOnInitCB: function(func) {
  30. this.registerCallback("onInit", func);
  31. },
  32. setOnRoomInfoCB: function(func) {
  33. this.registerCallback("onRoomInfo", func);
  34. },
  35. setOnReadyCB: function(func) {
  36. this.registerCallback("onReady", func);
  37. },
  38. setOnStartCB: function(func) {
  39. this.registerCallback("onStart", func);
  40. },
  41. setOnMessageCB: function(func) {
  42. this.registerCallback("onMessage", func);
  43. },
  44. setOnFinishCB: function(func) {
  45. this.registerCallback("onFinish", func);
  46. },
  47. setOnAudioCB: function(func) {
  48. this.registerCallback("onAudio", func);
  49. },
  50. setOnResumeCB: function(func) {
  51. this.registerCallback("onResume", func);
  52. },
  53. setOnPauseCB: function(func) {
  54. this.registerCallback("onPause", func);
  55. },
  56. setOnPayCB: function(func) {
  57. this.registerCallback("onPay", func);
  58. },
  59. // 初始化SDK
  60. // 参数:
  61. // gameId: int 游戏ID
  62. init: function(gameId) {
  63. var param = {
  64. "gameId": gameId
  65. };
  66. this.callNative("init", param);
  67. },
  68. // 退出游戏
  69. // 参数:
  70. // reason: int 退出原因: 1 - 正常退出,2-异常退出
  71. quit: function(reason) {
  72. var param = {
  73. "reason": reason
  74. };
  75. this.callNative("quit", param);
  76. },
  77. // 获取游戏房间信息
  78. getRoomInfo: function() {
  79. this.callNative("getRoomInfo");
  80. },
  81. // 游戏终止
  82. // 参数:
  83. // result: int 游戏结果: 1、胜,2、负,3、平
  84. finish: function(result) {
  85. var param = {
  86. "result": result
  87. };
  88. this.callNative("finish", param);
  89. },
  90. // 设置屏幕朝向
  91. // 参数:
  92. // orientation: int 屏幕朝向: 0、横屏,1、竖屏
  93. setOrientation: function(orientation) {
  94. var param = {
  95. "orientation": orientation
  96. };
  97. this.callNative("setOrientation", param);
  98. },
  99. // 设置声音
  100. // 参数:
  101. // enable: int 是否开启: 0、关闭,1、开启
  102. // volume: int 音量
  103. setAudio: function(enable, volume) {
  104. var param = {
  105. "enable": enable,
  106. "volume": volume
  107. };
  108. this.callNative("setAudio", param);
  109. },
  110. // 设置麦克风
  111. // 参数:
  112. // enable: int 是否开启: 0、关闭,1、开启
  113. // volume: int 音量: 0 ~ 100
  114. setMic: function(enable, volume) {
  115. var param = {
  116. "enable": enable,
  117. "volume": volume
  118. };
  119. this.callNative("setMic", param);
  120. },
  121. // 设置游戏加载进度(SDK版本 >= 2)
  122. // 参数:
  123. // progress: int 加载进度: 0 ~ 100
  124. // 说明: 从SDK版本2开始,平台增加了统一的游戏加载进度界面,用于游戏后台加载时显示。
  125. // 游戏需要在初始化后,通过此函数报告游戏加载进度。加载界面将显示“加载中...”
  126. // 当进度>=100%时,加载界面并不会关闭,加载界面将提醒用户“等待对手进入中...”
  127. // 因此,游戏需要在对手都进入房间后,调用hideLoadProgress函数关闭加载界面。
  128. setLoadProgress: function(progress) {
  129. var param = {
  130. "progress": progress
  131. };
  132. this.callNative("setLoadProgress", param);
  133. },
  134. // 隐藏游戏加载进度(SDK版本 >= 2)
  135. // 参数:
  136. // 无
  137. // 说明: 用于关闭加载进度界面。此后玩家才可以和游戏交互。
  138. hideLoadProgress: function() {
  139. var param = {};
  140. this.callNative("hideLoadProgress", param);
  141. },
  142. // 游戏准备
  143. // 参数
  144. // userData: string 用户数据
  145. ready: function(userData) {
  146. var param = {
  147. "userData": userData
  148. }
  149. this.callNative("ready", param);
  150. },
  151. // 游戏消息广播
  152. // 参数
  153. // message: string 广播的消息
  154. // includeMe: int 是否也广播给自己: 0、不包含,1、包含
  155. broadcast: function(message, includeMe) {
  156. if (includeMe == null) {
  157. includeMe = 0;
  158. }
  159. var param = {
  160. "message": message,
  161. "includeMe": includeMe
  162. }
  163. this.callNative("broadcast", param);
  164. },
  165. // 游戏结束
  166. // 参数
  167. // result: int 游戏结果: 1、胜,2、负,3、平
  168. gameOver: function(result) {
  169. var param = {
  170. "result": result,
  171. }
  172. this.callNative("gameOver", param);
  173. },
  174. // 游戏支付
  175. // 参数
  176. // orderId: int 订单号
  177. // goodsName: string 商品名称
  178. // goodsDesc: string 商品描述
  179. // orderAmount: int 订单金额
  180. // extension: string 透传参数
  181. // notifyURL: string 支付付款通知地址
  182. pay: function(orderId, goodsName, goodsDesc, orderAmount, extension, notifyURL) {
  183. var param = {
  184. "orderId": orderId,
  185. "goodsName": goodsName,
  186. "goodsDesc": goodsDesc,
  187. "orderAmount": orderAmount,
  188. "extension": extension,
  189. "notifyURL": notifyURL
  190. }
  191. this.callNative("pay", param);
  192. },
  193. }
  194. })()