MoreGameData.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import Main from "../Main"
  2. import AdM from "../manager/AdM"
  3. import GameM, { AUDIO_TYPE } from "../manager/GameM"
  4. import UiM, { PANEL_NAME } from "../manager/UiM"
  5. import EffectNode from "../ui/EffectNode"
  6. import MoreGameNode from "../ui/MoreGameNode"
  7. import LogUtil from "../utils/LogUtil"
  8. import { Utils } from "../utils/Utils"
  9. import { CFG_SERVER, HTTP_TYPE } from "./CommonData"
  10. /** 彩蛋数据类 */
  11. export default class MoreGameData {
  12. /**推荐游戏信息 */
  13. moreGameList = null
  14. public interProperTotal = 0;
  15. private static instance: MoreGameData = null
  16. private static downLoadTask: Map<string, DownloadTaskClazz> = null
  17. static get Instance(): MoreGameData {
  18. if (!this.instance) {
  19. this.downLoadTask = new Map<string, any>()
  20. this.instance = new MoreGameData()
  21. }
  22. return this.instance
  23. }
  24. private testData = {
  25. "normal": [
  26. {
  27. "nebulaAppId": "wxd2e694c55d457ded",
  28. "title": "爱上切水果",
  29. "introduction": "切水果、切水果、切、切、切!",
  30. "downloadUrl": "https://xcx-box.oss-cn-hangzhou.aliyuncs.com/hotgame/haocheyijia/hutui/CutFruit.apk",
  31. "packageName": "com.aries.asqsg.mz",
  32. "icon": "https://oss.duiweize.com/fission/5vp7CPeadkABlgIe.png",
  33. "banner": "https://oss.duiweize.com/fission/EXrqiYF6BkC1ZsZf.png",
  34. },
  35. {
  36. "nebulaAppId": "wx74fee17e08a29836",
  37. "title": "炮火掠夺",
  38. "introduction": "大炮一响,黄金万两!",
  39. "downloadUrl": "https://xcx-box.oss-cn-hangzhou.aliyuncs.com/hotgame/haocheyijia/hutui/FirePillage.apk",
  40. "packageName": "com.aries.firepillage.mz",
  41. "icon": "https://oss.duiweize.com/fission/BZ9ZUicxgCbWb33D.png",
  42. "banner": "https://oss.duiweize.com/fission/vGPWCE2tuGZwLSJH.png",
  43. }
  44. ],
  45. "banner": [
  46. {
  47. "nebulaAppId": "wx07895e2f78748daf",
  48. "title": "豪车大富翁",
  49. "introduction": "豪车、金钱、美女,你要的这里都有!",
  50. "downloadUrl": "https://xcx-box.oss-cn-hangzhou.aliyuncs.com/hotgame/haocheyijia/hutui/HundredsOfMillions.apk",
  51. "packageName": "org.cocos2d.hundredsofmillion.mk.mz",
  52. "icon": "https://oss.duiweize.com/fission/fviQN9OpUmmqpTjU.png",
  53. "banner": "https://oss.duiweize.com/fission/eWUxr3OySHIohqtB.png",
  54. },
  55. {
  56. "nebulaAppId": "wx22b3f0f8005f0e51",
  57. "title": "航海传奇",
  58. "introduction": " 战舰、短裙、红包,你想要的都有!",
  59. "downloadUrl": "https://xcx-box.oss-cn-hangzhou.aliyuncs.com/hotgame/haocheyijia/hutui/LegendOfNavigation.apk",
  60. "packageName": "com.aries.hhcq.mz",
  61. "icon": "https://oss.duiweize.com/fission/ZqDsgbj9PT94RcG0.png",
  62. "banner": "https://oss.duiweize.com/fission/goS05API8LtqB0QH.png",
  63. }
  64. ]
  65. }
  66. sendRecommendList() {
  67. GameM.httpM.sendDatas(HTTP_TYPE.recommendList, null, MoreGameData.Instance.getRecommendList.bind(this));
  68. }
  69. getRecommendList(data) {
  70. // LogUtil.logV("getRecommendList ", data)
  71. this.moreGameList = data
  72. // //test
  73. // this.moreGameList = this.testData
  74. if (cc.isValid(UiM.Instance.moreGameNode)) {
  75. UiM.Instance.moreGameNode.getComponent(MoreGameNode).initView()
  76. }
  77. if (cc.isValid(UiM.Instance.hallNode)) {
  78. let main = UiM.Instance.hallNode.getComponent(Main)
  79. if (this.moreGameList == null || this.moreGameList == undefined) {
  80. main.moreGameBtn.node.active = false
  81. return
  82. }
  83. if ((this.moreGameList.banner == null || this.moreGameList.banner == undefined || this.moreGameList.banner.length == 0)
  84. && (this.moreGameList.normal == null || this.moreGameList.normal == undefined || this.moreGameList.normal.length == 0)) {
  85. main.moreGameBtn.node.active = false
  86. return
  87. }
  88. if (cc.sys.platform == cc.sys.ANDROID) {
  89. main.setMoreGameSp(this.moreGameList.normal)
  90. }
  91. main.moreGameBtn.node.active = true
  92. }
  93. }
  94. /**上报 */
  95. sendAppDownloadLog(downLoadAppId: string) {
  96. GameM.httpM.sendDatas(HTTP_TYPE.appDownloadLog, { "downloadAppId": downLoadAppId }, MoreGameData.Instance.getAppDownloadLog.bind(this));
  97. }
  98. getAppDownloadLog(data) {
  99. LogUtil.logV("getAppDownloadLog", data)
  100. this.moreGameList = data
  101. }
  102. // nebulaAppId: wx07895e2f78748das
  103. // title: 测试3
  104. // introduction: 阿达大厦大
  105. // downloadUrl: htt://www.baidu.com
  106. // packageName:
  107. // icon: https://oss.duiweize.com/fission/5HVo76AZCBEBjjAz.png
  108. // banner: https://oss.duiweize.com/fission/Q2xrr3pJRcpoRhz3.png
  109. // type: 2
  110. // sort: 2
  111. createNewTask(data): DownloadTaskClazz {
  112. let name = data.nebulaAppId + ".apk"
  113. let url = data.downloadUrl
  114. let packageName = data.packageName
  115. if (packageName != "") {
  116. let isExist = AdM.canStartGame(packageName, true)
  117. if (isExist) {
  118. AdM.onSendEvent('hutuiEventLaunch_' + data.nebulaAppId, "互推启动" + data.title, 'hutuiEventLaunch')
  119. //判断在原生环境下存在就直接启动
  120. return null
  121. }
  122. }
  123. if (jsb.fileUtils.isFileExist(jsb.fileUtils.getWritablePath() + name)) {
  124. AdM.installApk(jsb.fileUtils.getWritablePath() + name)
  125. return null
  126. }
  127. if (MoreGameData.downLoadTask[data.nebulaAppId]) {
  128. EffectNode.instance.PlayTipSimple('正在下载中...')
  129. return null
  130. }
  131. EffectNode.instance.PlayTipSimple('开始下载...')
  132. let downLoadTask = new DownloadTaskClazz(0, 0, data.nebulaAppId, null)
  133. let curTask = Utils.downFile2Local(url, data.nebulaAppId + ".apk", (locaPath) => {
  134. LogUtil.logV("sendAppDownloadLog", "success")
  135. downLoadTask.progress = -2
  136. this.sendAppDownloadLog(data.nebulaAppId)
  137. setTimeout(() => {
  138. AdM.installApk(jsb.fileUtils.getWritablePath() + name)
  139. }, 1)
  140. }, () => {
  141. downLoadTask.progress = -1
  142. MoreGameData.downLoadTask[data.nebulaAppId] = null
  143. EffectNode.instance.PlayTip('下载失败,请稍后再试')
  144. }, (task: jsb.DownloaderTask, bytesReceived: number, totalBytesReceived: number, totalBytesExpected: number) => {
  145. downLoadTask.progress = bytesReceived
  146. downLoadTask.totalBytesReceives = totalBytesReceived
  147. })
  148. downLoadTask.downloadTask = curTask
  149. MoreGameData.downLoadTask[data.nebulaAppId] = downLoadTask
  150. AdM.onSendEvent('hutuiEventDown_' + data.nebulaAppId, "互推下载" + data.title, 'hutuiEventDown')
  151. return downLoadTask
  152. }
  153. getTaskInfo(appId): DownloadTaskClazz {
  154. // for(let entry of t)
  155. return MoreGameData.downLoadTask[appId]
  156. }
  157. /** 随机获取信息流位游戏 展示在十倍领取*/
  158. getRandomSteam() {
  159. if (!this.moreGameList) {
  160. return null
  161. }
  162. let index = Utils.rnd(0, this.moreGameList.stream.length - 1)
  163. let data = this.moreGameList.stream[index]
  164. return data
  165. }
  166. /** 随机获取底部位游戏 展示在更多游戏界面列表小图*/
  167. getRandomNormal() {
  168. if (!this.moreGameList) {
  169. return null
  170. }
  171. let index = Utils.rnd(0, this.moreGameList.normal.length - 1)
  172. let data = this.moreGameList.normal[index]
  173. return data
  174. }
  175. getRandomInter() {
  176. if (!this.moreGameList) {
  177. return null
  178. }
  179. let len = this.moreGameList.plug.length
  180. let proper = this.interProperTotal * Math.random();
  181. console.log('proper ', proper)
  182. let add = 0
  183. let index = 0
  184. for (var j = 0; j < len; j++) {
  185. add += this.moreGameList.plug[j].probability
  186. if (proper <= add) {
  187. index = j
  188. break
  189. }
  190. }
  191. let data = this.moreGameList.plug[index]
  192. return data
  193. }
  194. }
  195. /** 下载任务 */
  196. class DownloadTaskClazz {
  197. private _progress = 0
  198. private _totalBytesReceives = 0
  199. private _nebulaAppId = ""
  200. private _downloadTask = null
  201. constructor(progress, totalBytesReceives, nebulaAppId, downloadTask) {
  202. this._progress = progress
  203. this._totalBytesReceives = totalBytesReceives
  204. this._nebulaAppId = nebulaAppId
  205. this._downloadTask = downloadTask
  206. }
  207. public get progress(): number {
  208. return this._progress
  209. }
  210. public set progress(progress: number) {
  211. this._progress = progress
  212. }
  213. public get totalBytesReceives(): number {
  214. return this._totalBytesReceives
  215. }
  216. public set totalBytesReceives(totalBytesReceives: number) {
  217. this._totalBytesReceives = totalBytesReceives
  218. }
  219. public get downloadTask(): any {
  220. return this._downloadTask
  221. }
  222. public set downloadTask(downloadTask: any) {
  223. this._downloadTask = downloadTask
  224. }
  225. }